-
Notifications
You must be signed in to change notification settings - Fork 1
/
deployer.py
executable file
·205 lines (180 loc) · 6 KB
/
deployer.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import argparse
import json
import os
import subprocess
import tempfile
from contextlib import ExitStack, contextmanager
from pathlib import Path
from ruamel.yaml import YAML
from ruamel.yaml.scanner import ScannerError
yaml = YAML(typ="safe", pure=True)
@contextmanager
def auth(
key_path,
cluster="openorganelle",
location="us-west2-a",
project="segmentation-challenge",
):
orig_file = os.environ.get("CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE")
orig_kubeconfig = os.environ.get("KUBECONFIG")
try:
with (
tempfile.NamedTemporaryFile() as kubeconfig,
get_decrypted_file(key_path) as decrypted_file,
):
os.environ["KUBECONFIG"] = kubeconfig.name
os.environ["CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE"] = (
decrypted_file
)
subprocess.check_call(
[
"gcloud",
"container",
"clusters",
f"--zone={location}",
f"--project={project}",
"get-credentials",
cluster,
]
)
yield
finally:
# restore modified environment variables to its previous state
if orig_kubeconfig is not None:
os.environ["KUBECONFIG"] = orig_kubeconfig
else:
os.environ.pop("KUBECONFIG")
if orig_file is not None:
os.environ["CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE"] = orig_file
else:
os.environ.pop("CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE")
@contextmanager
def get_decrypted_file(original_filepath):
"""
Copied from 2i2c/infrastructure
"""
if not os.path.isfile(original_filepath):
raise FileNotFoundError(
f"""
File Not Found at following location! Have you checked it's the
correct path? {original_filepath}
"""
)
filename = os.path.basename(original_filepath)
_, ext = os.path.splitext(filename)
if "secret" in filename:
with open(original_filepath) as f:
if ext.endswith("json"):
loader_func = json.load
else:
loader_func = yaml.load
try:
content = loader_func(f)
except ScannerError:
raise ScannerError(
"We expect encrypted files to be valid JSON or YAML files."
)
if "sops" not in content:
raise KeyError(
"""
Expecting to find the `sops` key in this encrypted file - but
it wasn't found! Please regenerate the secret in case it has
been checked into version control and leaked!
"""
)
# If file has a `sops` key, we assume it's sops encrypted
with tempfile.NamedTemporaryFile() as f:
subprocess.check_call(
["sops", "--output", f.name, "--decrypt", original_filepath]
)
yield f.name
else:
# The file does not have "secret" in its name, therefore does not need
# to be decrypted. Yield the original filepath unchanged.
yield original_filepath
@contextmanager
def get_decrypted_files(files):
"""
Copied from 2i2c/infrastructure
"""
with ExitStack() as stack:
yield [stack.enter_context(get_decrypted_file(f)) for f in files]
def deploy(type, namespace, values_files, debug, dry_run):
if type == "support":
helm_chart = Path(__file__).parent.joinpath("helm-charts/support")
elif type == "app":
helm_chart = Path(__file__).parent.joinpath("helm-charts/app")
else:
raise ValueError(f"Unknown type: {type}")
cmd = [
"helm",
"upgrade",
"--install",
"--create-namespace",
"--wait",
f"--namespace={namespace}",
namespace,
helm_chart,
]
if dry_run:
cmd.append("--dry-run")
if debug:
cmd.append("--debug")
if values_files:
with get_decrypted_files(values_files) as val_files:
val_files_str = [str(file) for file in val_files]
for val_file in val_files_str:
cmd.append(f"--values={val_file}")
print(f"Running {' '.join([str(c) for c in cmd])}")
subprocess.check_call(cmd)
else:
print(f"Running {' '.join([str(c) for c in cmd])}")
subprocess.check_call(cmd)
def main():
parser = argparse.ArgumentParser(description="Deploy script")
parser.add_argument(
"type", type=str, help="Type of deployment. Choose from support/app!"
)
parser.add_argument(
"--namespace",
type=str,
help="Namespace to deploy to. Choose from support/staging/prod",
)
parser.add_argument(
"--dry-run", action="store_true", help="Perform a dry run"
)
parser.add_argument(
"--debug", action="store_true", help="Enable debug mode"
)
args = parser.parse_args()
key_path = Path(__file__).parent.joinpath(
"helm-charts/enc-deploy-credentials.secret.json"
)
if args.type == "support":
helm_chart = Path(__file__).parent.joinpath("helm-charts/support")
values_files = [
helm_chart.joinpath(f"{args.namespace}.values.yaml"),
]
elif args.type == "app":
helm_chart = Path(__file__).parent.joinpath("helm-charts/app")
values_files = [
helm_chart.joinpath("common.values.yaml"),
helm_chart.joinpath(
f"{args.namespace}/{args.namespace}.values.yaml"
),
helm_chart.joinpath(
f"{args.namespace}/enc-{args.namespace}.secret.values.yaml"
),
]
else:
raise ValueError(f"Unknown type: {args.type}")
with auth(key_path):
deploy(
args.type,
args.namespace,
values_files,
args.debug,
args.dry_run,
)
if __name__ == "__main__":
main()