-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathupload_team_config.py
More file actions
126 lines (111 loc) · 4.99 KB
/
Copy pathupload_team_config.py
File metadata and controls
126 lines (111 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sys
import os
import time
import requests
HTTP_TIMEOUT = 120 # seconds per request
MAX_RETRIES = 5
RETRYABLE_STATUS = {408, 429, 500, 502, 503, 504}
def request_with_retry(method, url, **kwargs):
"""Wrap requests with timeout + retry/backoff for transient backend cold starts."""
kwargs.setdefault("timeout", HTTP_TIMEOUT)
last_exc = None
for attempt in range(1, MAX_RETRIES + 1):
try:
response = requests.request(method, url, **kwargs)
if response.status_code in RETRYABLE_STATUS and attempt < MAX_RETRIES:
wait = min(2 ** attempt, 30)
print(f" [retry {attempt}/{MAX_RETRIES}] {method} {url} -> {response.status_code}; sleeping {wait}s")
time.sleep(wait)
continue
return response
except (requests.ConnectionError, requests.Timeout) as e:
last_exc = e
if attempt < MAX_RETRIES:
wait = min(2 ** attempt, 30)
print(f" [retry {attempt}/{MAX_RETRIES}] {method} {url} -> {type(e).__name__}: {e}; sleeping {wait}s")
time.sleep(wait)
continue
raise
if last_exc:
raise last_exc
return response
if len(sys.argv) < 3:
print("Usage: python upload_team_config.py <backend_endpoint> <directory_path> [<user_principal_id>] [<team_id_from_arg>]")
sys.exit(1)
backend_url = sys.argv[1]
directory_path = sys.argv[2]
user_principal_id = sys.argv[3] if len(sys.argv) > 3 and sys.argv[3].strip() != "" else "00000000-0000-0000-0000-000000000000"
team_id_from_arg = sys.argv[4] if len(sys.argv) > 4 else "00000000-0000-0000-0000-000000000001"
# Convert to absolute path if provided as relative
directory_path = os.path.abspath(directory_path)
print(f"Scanning directory: {directory_path}")
files_to_process = [
("hr.json", "00000000-0000-0000-0000-000000000001"),
("marketing.json", "00000000-0000-0000-0000-000000000002"),
("retail.json", "00000000-0000-0000-0000-000000000003"),
("rfp_analysis_team.json", "00000000-0000-0000-0000-000000000004"),
("contract_compliance_team.json", "00000000-0000-0000-0000-000000000005"),
("ad_copy_team.json", "00000000-0000-0000-0000-000000000006"),
("content_gen.json", "00000000-0000-0000-0000-000000000007"),
]
# Build lookup maps so we can resolve filename<->team_id either direction.
filename_to_team_id = {f: tid for f, tid in files_to_process}
team_id_to_filename = {tid: f for f, tid in files_to_process}
# Resolve which JSON file(s) inside `directory_path` to upload. Prefer the
# explicit team_id passed on the CLI; fall back to whatever *.json files exist
# in the content-pack directory (each pack ships one).
candidate_files = []
expected_name = team_id_to_filename.get(team_id_from_arg)
if expected_name and os.path.isfile(os.path.join(directory_path, expected_name)):
candidate_files.append((expected_name, team_id_from_arg))
elif os.path.isdir(directory_path):
for entry in sorted(os.listdir(directory_path)):
if entry.lower().endswith(".json"):
tid = filename_to_team_id.get(entry, team_id_from_arg)
candidate_files.append((entry, tid))
if not candidate_files:
print(f"No team configuration JSON files found in {directory_path}")
sys.exit(1)
upload_endpoint = backend_url.rstrip('/') + '/api/v4/upload_team_config'
# Process each JSON file in the directory
uploaded_count = 0
for filename, team_id in candidate_files:
file_path = os.path.join(directory_path, filename)
print(f"Uploading file: {filename}")
try:
with open(file_path, 'rb') as file_data:
files = {
'file': (filename, file_data, 'application/json')
}
headers = {
'x-ms-client-principal-id': user_principal_id
}
params = {
'team_id': team_id
}
response = request_with_retry(
"POST",
upload_endpoint,
files=files,
headers=headers,
params=params,
)
if response.status_code == 200:
try:
resp_json = response.json()
if resp_json.get("status") == "success":
print(f"Successfully uploaded team configuration: {resp_json.get('name')} (team_id: {resp_json.get('team_id')})")
uploaded_count += 1
else:
print(f"Upload failed for {filename}. Response: {resp_json}")
sys.exit(1)
except Exception as e:
print(f"Error parsing response for {filename}: {str(e)}")
sys.exit(1)
else:
print(f"Failed to upload {filename}. Status code: {response.status_code}, Response: {response.text}")
sys.exit(1)
except Exception as e:
print(f"Error processing {filename}: {str(e)}")
sys.exit(1)
print(f"Completed uploading team configurations")