-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-k3s.py
More file actions
141 lines (118 loc) · 4.69 KB
/
Copy pathdeploy-k3s.py
File metadata and controls
141 lines (118 loc) · 4.69 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# SPDX-FileCopyrightText: 2026 Kristofer Älvring <kristofer@mainly.ai>
# SPDX-FileCopyrightText: 2026 Leah Lundqvist <leah@mainly.ai>
#
# SPDX-License-Identifier: GPL-2.0-only
import json
import os
import subprocess
import time
reg = os.environ.get("REGISTRY", "registry.production.mainly.ai")
should_build = os.environ.get("ONLY_BUILD", "ws,vault,webservices,webstatic").split(",")
def try_os_system(cmd, errmsg):
print(f"$ {cmd}")
res = os.system(cmd)
if res != 0 and "IGNORE_DOCKER_BUILD_ERRORS" not in os.environ:
print(f"!! {errmsg} !!")
exit()
def load_config():
config_keys = [
"VITE_DEPLOYMENT_BASE",
"VITE_COPILOT_URL",
"VITE_STRIPE_PK",
"VITE_TURNSTILE_SITEKEY",
"VITE_NODE_ENV",
"VITE_ASKAI2_URL",
"VITE_ASKAI2_DIFF_FIX_URL",
"VITE_TEMPLATE_REPO_ID",
]
config = {}
if os.path.exists("deploy_config.json"):
with open("deploy_config.json") as f:
config = json.load(f)
changed = False
for key in config_keys:
if key not in config:
config[key] = input(f"{key}=")
changed = True
if changed:
with open("deploy_config.json", "w") as f:
json.dump(config, f)
return config
def build_images():
config = load_config()
envstr = " ".join([f"{k}={v}" for k, v in config.items()])
if "webstatic" in should_build:
try_os_system(
f'{envstr} NODE_OPTIONS="--max-old-space-size=4096" npm run build',
"Failed to build webstatic",
)
if "webservices" in should_build:
try_os_system(
"rm -rf apps/webservice/dist/", "Failed to remove webservice dist directory"
)
try_os_system(
"mkdir -p apps/webservice/dist/",
"Failed to create webservice dist directory",
)
try_os_system(
"cp -r ../mainlyai/dist/*.whl apps/webservice/dist/",
"Failed to copy webservice wheel",
)
if "ws" in should_build:
try_os_system(
"cd apps/ws-gateway && cargo build --release", "Failed to build ws-gateway"
)
if "vault" in should_build:
try_os_system("cd apps/vault && cargo build --release", "Failed to build vault")
# Generate unique build hash for service worker cache invalidation
build_hash = hex(int(time.time() * 1000))[2:] # Millisecond timestamp as hex
if "ws" in should_build:
try_os_system(
f"docker build --platform linux/amd64 -t {reg}/ws-gateway -f apps/ws-gateway/Dockerfile apps/ws-gateway",
"Failed to build ws-gateway image",
)
if "vault" in should_build:
try_os_system(
f"docker build --platform linux/amd64 -t {reg}/vault -f apps/vault/Dockerfile apps/vault",
"Failed to build vault image",
)
if "webservices" in should_build:
try_os_system(
f"docker build --platform linux/amd64 -t {reg}/webservices -f apps/webservice/Dockerfile apps/webservice",
"Failed to build webservice image",
)
if "webstatic" in should_build:
print(f"Building webstatic with build hash: {build_hash}")
try_os_system(
f"docker build --platform linux/amd64 --build-arg BUILD_HASH={build_hash} -t {reg}/webstatic -f apps/Dockerfile apps",
"Failed to build webstatic image",
)
if "ws" in should_build:
try_os_system(
f"docker push {reg}/ws-gateway ", "Failed to push ws-gateway image"
)
if "vault" in should_build:
try_os_system(f"docker push {reg}/vault ", "Failed to push vault image")
if "webservices" in should_build:
try_os_system(
f"docker push {reg}/webservices", "Failed to push webservice image"
)
if "webstatic" in should_build:
try_os_system(f"docker push {reg}/webstatic", "Failed to push webstatic image")
def deploy():
print(
"! kubeconfig is currently uncopied, ssh into the host to restart the deployments"
)
# try_os_system('kubectl apply -f webservices.yml', 'Failed to create namespace')
# try_os_system('kubectl rollout restart deployment ws-gateway', 'Failed to restart ws-gateway deployment')
# try_os_system('kubectl rollout restart deployment vault', 'Failed to restart vault deployment')
# try_os_system('kubectl rollout restart deployment webservices', 'Failed to restart webservices deployment')
# try_os_system('kubectl rollout restart deployment webstatic', 'Failed to restart webstatic deployment')
if __name__ == "__main__":
build_images()
print("Do you want to deploy? (y/n) ")
res = input()
if res != "y":
print("Aborting")
exit()
deploy()