-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci.py
More file actions
174 lines (125 loc) · 4.38 KB
/
ci.py
File metadata and controls
174 lines (125 loc) · 4.38 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright (c) 2025 Yifeng Wu
# All rights reserved.
# This file is not licensed for use, modification, or distribution without
# explicit written permission from the copyright holder.
# /// script
# requires-python = ">=3.11"
# dependencies = ["click"]
# ///
import json
import subprocess
from base64 import b64encode
from pathlib import Path
import click
ROOT_DIR = Path(__file__).parent
DEPLOY_DIR = ROOT_DIR / "deploy"
MANIFEST = DEPLOY_DIR / "manifest.yaml"
ENGINE_CHART = DEPLOY_DIR / "engine"
CLIENT_CHART = DEPLOY_DIR / "client"
K3S_NAMESPACE = "hiresify"
K3S_URL = "https://get.k3s.io"
K3S_VERSION = "1.33.3+k3s1"
K3S_PREFIX = f"curl -sfL {K3S_URL} | INSTALL_K3S_VERSION=v{K3S_VERSION} sh -s -"
DOCKER_HUB = "registry-1.docker.io"
BITNAMI_OCI = f"oci://{DOCKER_HUB}/bitnamicharts"
HELM_URL = "https://get.helm.sh"
HELM_VERSION = "3.18.6"
HELM_PLATFORM = "linux-amd64"
HELM_ARCHIVE = f"helm-v{HELM_VERSION}-{HELM_PLATFORM}.tar.gz"
@click.group()
def cli() -> None:
"""CLI tool for Hiresify developers."""
@cli.group()
def cluster() -> None:
"""Manage a local cluster for deploying this project."""
@cluster.command("create")
def cluster_create() -> None:
"""Create the node cluster for a local deployment."""
cmd = (
f"{K3S_PREFIX} server --write-kubeconfig-mode=644 && "
"mkdir -p ~/.kube && "
"cp /etc/rancher/k3s/k3s.yaml ~/.kube/config"
)
if subprocess.run(cmd, check=False, shell=True).returncode:
click.secho("Failed to launch the K3s server.", fg="red")
cmd = (
f"curl -sfL -o {HELM_ARCHIVE} {HELM_URL}/{HELM_ARCHIVE} && "
f"tar -zxvf {HELM_ARCHIVE} && "
f"sudo cp {HELM_PLATFORM}/helm /usr/local/bin && "
f"rm -rf {HELM_ARCHIVE} {HELM_PLATFORM}"
)
if subprocess.run(cmd, check=False, shell=True).returncode:
click.secho("Failed to install and configure helm.", fg="red")
@cli.group()
def predeploy() -> None:
"""Perform pre-deployment operations on the local cluster."""
@predeploy.command("config")
@click.option(
"--docker-username",
envvar="DOCKER_USERNAME",
help="The username used to log in Docker Hub",
required=True,
)
@click.option(
"--docker-password",
envvar="DOCKER_PASSWORD",
help="The password used to log in Docker Hub",
required=True,
)
def predeploy_config(docker_username: str, docker_password: str) -> None:
"""Set up the pre-deployment configuration."""
docker_config = _encode_text(
json.dumps(
{
"auths": {
"docker.io": {
"username": docker_username,
"password": docker_password,
"auth": _encode_text(f"{docker_username}:{docker_password}"),
}
}
}
)
)
template = MANIFEST.read_text(encoding="utf-8")
MANIFEST.write_text(template.format(docker_config=docker_config))
cmd = ["kubectl", "apply", "-f", MANIFEST]
try:
if subprocess.run(cmd, check=False).returncode:
click.secho(f"Failed to apply manifest {MANIFEST}.", fg="red")
finally:
MANIFEST.write_text(template)
@cli.group()
def deploy() -> None:
"""Manage a local deployment of this project."""
@deploy.command("up")
def deploy_up() -> None:
"""Spin up the local deployment."""
cmd = " && ".join(
[
_build_helm_command(f"upgrade --install {chart.name} {chart}")
for chart in (ENGINE_CHART, CLIENT_CHART)
]
)
if subprocess.run(cmd, check=False, shell=True).returncode:
click.secho("Failed to spin up the local deployment.", fg="red")
@deploy.command("down")
def deploy_down() -> None:
"""Spin down the local deployment."""
cmd = " && ".join(
[
_build_helm_command(f"uninstall {chart.name}")
for chart in (ENGINE_CHART, CLIENT_CHART)
]
)
if subprocess.run(cmd, check=False, shell=True).returncode:
click.secho("Failed to spin down the local deployment.", fg="red")
# -- helper functions
def _encode_text(text: str) -> str:
"""Encode the given text in base64."""
return b64encode(text.encode()).decode()
def _build_helm_command(cmd: str) -> str:
"""Build the full Helm command with the given subcommand."""
return f"sudo helm {cmd} -n {K3S_NAMESPACE} --kubeconfig ~/.kube/config"
if __name__ == "__main__":
cli()