-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathda_install.py
More file actions
executable file
·121 lines (104 loc) · 3.92 KB
/
da_install.py
File metadata and controls
executable file
·121 lines (104 loc) · 3.92 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
#!/usr/bin/env python3
import requests
import os
import time
import zipfile
from io import BytesIO
def zip_current_dir():
zip_bytes = BytesIO()
with zipfile.ZipFile(zip_bytes, 'w', zipfile.ZIP_DEFLATED) as zip_handle:
for root, dirs, files in os.walk(".", topdown=True):
# Modifying dirs in place will skip the .git directory
dirs[:] = [d for d in dirs if d != '.git' and d != '.mypy_cache']
for file in files:
zip_handle.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(".", '..')))
zip_bytes.seek(0, 0)
return zip_bytes.getvalue()
def make_playground_payload():
user_id = os.getenv('USER_ID')
project = os.getenv('PROJECT_NAME')
restart = os.getenv('RESTART')
data = {}
if user_id:
data["user_id"] = user_id
if project:
data["project"] = project
if restart:
data["restart"] = restart
files = {'file': ('deploy.zip', zip_current_dir())}
print(f"Installing using {list(data.keys())} and zip")
return {'data': data or None, 'files': files}
def make_server_payload():
# optional env vars
github_url = os.getenv('GITHUB_URL')
github_branch = os.getenv('GITHUB_BRANCH')
pypi_package = os.getenv('PYPI_PACKAGE')
payload = {}
files = {}
if pypi_package:
payload["pip"] = pypi_package
elif github_url:
payload["github_url"] = github_url
if github_branch:
payload['branch'] = github_branch
else:
files["zip"] = ('deploy.zip', zip_current_dir())
print(f"Installing using {list(payload.keys())}, {list(files.keys())}")
return {'data': payload or None, 'files': files or None}
def install_to_server(install_url, headers, payload, polling_url):
resp = requests.post(install_url, headers=headers, data=payload['data'], files=payload['files'])
if not resp.ok:
print(f"Not able to install {payload['data']} at {install_url}: {resp.text}")
return 1
if resp.status_code == 204:
print("Success! DA server did not need to restart")
return 0
# Just loop a bunch of times until we are sure that it installed.
task_id = resp.json()["task_id"]
sleep_count = 0
MAX_SLEEP_COUNT = 15
while sleep_count < MAX_SLEEP_COUNT:
updated_resp = requests.get(polling_url, params={"task_id": task_id}, headers=headers)
if not updated_resp.ok:
print(f"Not able to determine if {payload['data']} finished installing: {updated_resp.status_code}: {updated_resp.text}")
if updated_resp.status_code == 504:
print("Waiting for 20 seconds to see if the server comes back up")
time.sleep(20)
continue
else:
return 2
body = updated_resp.json()
if body['status'] == 'working':
print(f"waiting (for {(MAX_SLEEP_COUNT - sleep_count) * 10} more seconds)")
time.sleep(10)
sleep_count += 1
if body['status'] == 'completed':
if body.get('ok', True):
print("Success!")
return 0
else:
print(f"Not successful installing {payload['data']}: {body['error_message']}")
return 3
if body['status'] == 'unknown':
print(f"task_id unknown?: {body}")
return 4
print(f"Timed out waiting to determine if {payload['data']} finished installing. Check the server, it might have still!")
return 5
def main():
print("Starting install to docassemble server")
server_url = os.environ['SERVER_URL']
headers = {"X-API-KEY": os.environ['DOCASSEMBLE_DEVELOPER_API_KEY']}
if os.environ['INSTALL_TYPE'] == 'playground':
install_url = f"{server_url}/api/playground_install"
polling_url = f"{server_url}/api/restart_status"
payload = make_playground_payload()
else:
install_url = f"{server_url}/api/package"
polling_url = f"{server_url}/api/package_update_status"
payload = make_server_payload()
return install_to_server(install_url, headers, payload, polling_url)
if __name__ == "__main__":
retval = main()
exit(retval)