This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
aimmo_setup.py
530 lines (425 loc) · 15.7 KB
/
aimmo_setup.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
from __future__ import print_function
from enum import Enum
import re
import sys
import platform
import subprocess
import traceback
import inspect
from subprocess import PIPE, CalledProcessError
# python2 support
try:
input = raw_input
except NameError:
pass
MINIKUBE_VERSION = "latest"
KUBECTL_VERSION = "latest"
class OSType(Enum):
MAC = 1
LINUX = 2
WINDOWS = 3
class ArchType(Enum):
AMD64 = 1
ARM64 = 2
def main():
print(
"+----------------------------------------------------------------------------------------------------------+\n"
"| Welcome to Kurono! |\n"
"| This script should make your life a little easier, |\n"
"| just be kind if it doesn't work. |\n"
"| You may be asked to enter your password during this setup. |\n"
"| |\n"
"| **This setup script is currently for Mac and Linux only.** |\n"
"+----------------------------------------------------------------------------------------------------------+\n"
)
try:
os_type = get_os_type()
arch_type = get_arch_type()
setup = setup_factory(os_type, arch_type)
try:
print("Starting setup for OS: %s\n" % os_type.name)
setup(os_type, arch_type)
print("\nFinished setup.")
except CalledProcessError as e:
print("Something has gone wrong.")
print("Command '%s' returned exit code '%s'" % (e.cmd, e.returncode))
traceback.print_exc()
except OSError as e:
print("Tried to execute a command that didn't exist.")
traceback.print_exc()
except ValueError as e:
print("Tried to execute a command with invalid arguments.")
traceback.print_exc()
except KeyError as e:
print("Setup encountered an error: %s" % e.args[0])
except:
print("An unexpected error has occured:\n")
raise
def get_os_type():
"""
Return the OS type if one can be determined
Returns:
OSType: OS type
"""
system = platform.system()
system_os_type_map = {
"Darwin": OSType.MAC,
"Linux": OSType.LINUX,
"Windows": OSType.WINDOWS,
}
try:
return system_os_type_map[system]
except KeyError:
raise KeyError("'%s' system is not supported" % system)
def get_arch_type():
"""
Return the architecture type
Returns:
ArchType: architecture type
"""
arch = platform.machine()
arch_type_map = {
"amd64": ArchType.AMD64,
"x86_64": ArchType.AMD64,
"arm64": ArchType.ARM64,
}
try:
return arch_type_map[arch]
except KeyError:
raise KeyError("'%s' architecture is not supported" % arch)
def setup_factory(os_type, arch_type):
"""
Return the setup function which matches supplied host type
Args:
os_type (OSType): the type of host to setup
arch_type (ArchType): host architecture type
Returns:
Callable: setup function
"""
if os_type == OSType.MAC:
return mac_setup
elif os_type == OSType.LINUX:
return linux_setup
elif os_type == OSType.WINDOWS:
return windows_setup
raise RuntimeError("could not find setup function for supplied host type")
def mac_setup(os_type, arch_type):
"""
Runs the commands needed in order to set up Kurono for MAC
Args:
os_type (OSType): host OS type
arch_type (ArchType): host architecture type
"""
tasks = [
ensure_homebrew_installed,
install_sqlite3,
install_nodejs,
install_yarn,
set_up_frontend_dependencies,
install_pipenv,
build_pipenv_virtualenv,
install_docker,
install_minikube,
install_kubectl,
install_helm,
helm_add_agones_repo,
minikube_start_profile,
helm_install_aimmo,
]
_create_sudo_timestamp()
for task in tasks:
task(os_type, arch_type)
def windows_setup(os_type, arch_type):
raise NotImplementedError
def linux_setup(os_type, arch_type):
"""
Runs the commands needed in order to set up Kurono for LINUX
Args:
os_type (OSType): host OS type
arch_type (ArchType): host architecture type
"""
tasks = [
update_apt_packages,
install_nodejs,
check_for_cmdtest,
configure_yarn_repo,
install_yarn,
install_pip,
install_pipenv,
build_pipenv_virtualenv,
set_up_frontend_dependencies,
install_docker,
install_minikube,
install_kubectl,
install_helm,
helm_add_agones_repo,
minikube_start_profile,
helm_install_aimmo,
]
_create_sudo_timestamp()
for task in tasks:
task(os_type, arch_type)
def _create_sudo_timestamp():
"""
Request sudo access to create timestamp file for duration of setup
"""
print("\033[1mrequesting_sudo_access\033[0m... ")
# Request sudo password before task
subprocess.Popen("sudo true", stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True).communicate()
print("\033[1mrequesting_sudo_access\033[0m... [ \033[92mOK\033[0m ]")
def _cmd(command, comment=None):
"""
Run command inside a terminal
Args:
command (str): command to be run
comment (str): optional comment
Returns:
Tuple[int, List[str]]: return code, stdout lines output
"""
stdout_lines = []
if not comment:
# Set comment to calling function name
comment = inspect.currentframe().f_back.f_code.co_name
if comment:
print(" " * 110, end="\r")
print("\033[1m%s\033[0m...\n" % comment, end="\r")
p = subprocess.Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
for line in iter(p.stdout.readline, b""):
stdout_lines.append(line.decode("utf-8"))
sys.stdout.write("%s\r" % line.decode("utf-8")[:-1].rstrip())
sys.stdout.flush()
# Delete line
sys.stdout.write("\x1b[2K")
sys.stdout.write("\x1b[1A")
p.communicate()
if p.returncode != 0:
if comment:
sys.stdout.write("\033[1m%s\033[0m... [ \033[93mFAILED\033[0m ]\n" % comment)
for line in stdout_lines:
sys.stdout.write(f"{line}\n")
raise CalledProcessError(p.returncode, command)
if comment:
sys.stdout.write("\033[1m%s\033[0m... [ \033[92mOK\033[0m ]\n" % comment)
return (p.returncode, stdout_lines)
def ensure_homebrew_installed(os_type, arch_type):
if os_type == OSType.MAC:
_cmd("brew -v")
def install_sqlite3(os_type, arch_type):
if os_type == OSType.MAC:
try:
if _cmd("sqlite3 -version", "check_sqlite3")[0] == 0:
return
except CalledProcessError:
pass
_cmd("brew install sqlite3")
def install_yarn(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
try:
if _cmd("yarn --version ", "check_yarn")[0] == 0:
return
except CalledProcessError:
pass
if os_type == OSType.MAC:
_cmd("npm install --global yarn", "install yarn")
elif os_type == OSType.LINUX:
_cmd("sudo npm install --global yarn", "install yarn")
def set_up_frontend_dependencies(os_type, arch_type):
if os_type == OSType.MAC:
_cmd("cd ./game_frontend && yarn")
elif os_type == OSType.LINUX:
_cmd("cd ./game_frontend && sudo yarn")
def install_pipenv(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
try:
if _cmd("pipenv --version", "check_pipenv")[0] == 0:
return
except CalledProcessError:
pass
if os_type == OSType.MAC:
_cmd("brew install pipenv")
elif os_type == OSType.LINUX:
_cmd("pip install pipenv")
def build_pipenv_virtualenv(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
_cmd("pipenv install --dev")
def install_docker(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
try:
if _cmd("docker -v", "check_docker")[0] == 0:
return
except CalledProcessError:
pass
if os_type == OSType.MAC:
_cmd("brew install --cask docker")
elif os_type == OSType.LINUX:
# First time install needs to setup a repository
# Update the package and install them
# Add Docker's GPG key
# The following command is used to setup the stable repository
# Install docker
docker_install = """sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce
sudo apt-get install -y docker-ce-cli
sudo apt-get install -y containerd.io
"""
try:
_cmd(docker_install)
except CalledProcessError:
print("\nInstalation failed, trying again..\n")
_cmd(docker_install)
def install_minikube(os_type, arch_type, version=MINIKUBE_VERSION):
comment = "install_minikube"
if version == "latest":
rc, lines = _cmd("curl https://api.github.com/repos/kubernetes/minikube/releases/latest | grep tag_name")
match = re.search(r"(v[0-9\.]+)", lines[0])
if rc == 0 and match:
version = match.group(1)
if os_type in [OSType.MAC, OSType.LINUX]:
try:
_, lines = _cmd("minikube version", "check_minikube")
if version in lines[0]:
return
except CalledProcessError:
pass
if os_type == OSType.MAC:
_cmd(
"curl -Lo minikube https://storage.googleapis.com/minikube/releases/%s/minikube-darwin-%s"
% (version, arch_type.name.lower()),
comment + ": download",
)
elif os_type == OSType.LINUX:
_cmd(
"curl -Lo minikube https://storage.googleapis.com/minikube/releases/%s/minikube-linux-%s"
% (version, arch_type.name.lower()),
comment + ": download",
)
if os_type in [OSType.MAC, OSType.LINUX]:
_cmd("chmod +x minikube", comment + ": set permissions")
_cmd("sudo mv minikube /usr/local/bin/", comment + ": copy binary")
def install_kubectl(os_type, arch_type, version=KUBECTL_VERSION):
comment = "install_kubectl"
if version == "latest":
rc, lines = _cmd("curl -L -s https://dl.k8s.io/release/stable.txt")
if rc == 0:
version = lines[0]
if os_type in [OSType.MAC, OSType.LINUX]:
try:
_, lines = _cmd("kubectl version --client --short", "check_kubectl")
if version in lines[0]:
return
except CalledProcessError:
pass
if os_type == OSType.MAC:
_cmd(
"curl -Lo kubectl https://dl.k8s.io/release/%s/bin/darwin/%s/kubectl"
% (
version,
(arch_type.name).lower(),
),
comment + ": download",
)
if os_type == OSType.LINUX:
_cmd(
"curl -Lo kubectl https://dl.k8s.io/release/%s/bin/linux/%s/kubectl"
% (
version,
(arch_type.name).lower(),
),
comment + ": download",
)
if os_type in [OSType.MAC, OSType.LINUX]:
_cmd("chmod +x kubectl", comment + ": set permissions")
_cmd("sudo mv kubectl /usr/local/bin/", comment + ": copy binary")
def install_helm(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
try:
rc, _ = _cmd("helm version > /dev/null", "check_helm")
if rc == 0:
return
except CalledProcessError:
pass
_cmd("curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash")
def helm_add_agones_repo(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
_cmd("helm repo add agones https://agones.dev/chart/stable && " "helm repo update")
def minikube_start_profile(os_type, arch_type):
if os_type == OSType.MAC:
_cmd("minikube start -p agones --driver=hyperkit")
if os_type == OSType.LINUX:
_cmd("minikube start -p agones")
def helm_install_aimmo(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
try:
if _cmd("helm status -n agones-system aimmo > /dev/null", "check_helm_aimmo")[0] == 0:
return
except CalledProcessError:
pass
_cmd(
"minikube profile agones && "
"helm install aimmo --namespace agones-system --create-namespace agones/agones"
)
def install_pip(os_type, arch_type):
if os_type == OSType.LINUX:
try:
if _cmd("pip --version", "check_pip")[0] == 0:
return
except CalledProcessError:
pass
_cmd("sudo apt-get install python3-pip", "install_pip")
def install_nodejs(os_type, arch_type):
if os_type in [OSType.MAC, OSType.LINUX]:
try:
if _cmd("node --version", "check_nodejs")[0] == 0:
return
except CalledProcessError:
pass
if os_type == OSType.MAC:
_cmd("brew install node@14")
if os_type == OSType.LINUX:
_cmd("curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -" "sudo apt-get install -y nodejs")
def check_for_cmdtest(os_type, arch_type):
"""
This function is for use within the Linux setup section of the script. It checks if
the cmdtest package is installed, if it is we ask the user if we can remove it, if yes
we remove the package, if not the process continues without removing it.
"""
if os_type == OSType.LINUX:
try:
_cmd("dpkg-query -W -f='{status}' cmdtest")
except CalledProcessError:
return
while True:
choice = input(
"Looks like cmdtest is installed on your machine. "
"cmdtest clashes with yarn so we recommend to remove it. "
"Is it okay to remove cmdtest? [y/n]"
).lower()
if choice in ["y", "yes"]:
_cmd("sudo apt-get remove -y cmdtest", "remove_cmdtest")
break
if choice in ["n", "no"]:
print("Continuing without removing cmdtest...")
break
print("Please answer 'yes' or 'no' ('y' or 'n').")
def update_apt_packages(os_type, arch_type):
if os_type == OSType.LINUX:
_cmd("sudo apt-get update")
def configure_yarn_repo(os_type, arch_type):
comment = "configure_yarn"
if os_type == OSType.LINUX:
_cmd(
"curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -",
comment + ": add key",
)
_cmd(
'echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list',
comment + ": add repo",
)
if __name__ == "__main__":
main()