forked from ocadotechnology/aimmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaimmo_setup.py
329 lines (270 loc) · 10.4 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
from __future__ import print_function
import platform
import subprocess
import traceback
from subprocess import PIPE, CalledProcessError
# First we find and store the OS we are currently on, 0 if we didn't figure out which one
hostOS = 0
OStypes = {"mac": 1, "windows": 2, "linux": 3}
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
def _cmd(command):
"""
:param command: command/subprocess to be run, as a string.
Takes in a command/subprocess, and runs it as if you would
inside a terminal. DO NOT USE outside of the aimmo-setup script, and DO NOT INCLUDE
in any release build, as this function is able to run bash scripts, and can run commands
with sudo if specified.
"""
p = subprocess.Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True)
(_, _) = p.communicate()
if p.returncode != 0:
raise CalledProcessError
def install_yarn(operatingSystem):
"""
:param operatingSystem: values from the OStypes dict. (Should be updated to enum once python 3 is available)
OS dependant, so it must be passed to this function in order to run correctly.
"""
print("Installing Yarn...")
if operatingSystem == OStypes["mac"]:
_cmd("brew install yarn")
elif operatingSystem == OStypes["linux"]:
_cmd("sudo apt-get install yarn")
elif operatingSystem == OStypes["windows"]:
pass
else:
raise Exception
def install_pipenv(operatingSystem):
"""
:param operatingSystem: values from the OStypes dict. (Should be updated to enum once python 3 is available)
OS dependant, so it must be passed to this function in order to run correctly.
"""
print("Installing pipenv...")
if operatingSystem == OStypes["mac"]:
_cmd("brew install pipenv")
elif operatingSystem == OStypes["linux"]:
_cmd("pip install pipenv")
elif operatingSystem == OStypes["windows"]:
pass
else:
raise Exception
def install_docker(operatingSystem):
"""
:param operatingSystem: values from the OStypes dict. (Should be updated to enum once python 3 is available)
OS dependant, so it must be passed to this function in order to run correctly.
"""
print("Installing Docker...")
if operatingSystem == OStypes["mac"]:
_cmd("brew cask install docker")
elif operatingSystem == OStypes["linux"]:
_cmd("sudo apt-get install docker-ce")
elif operatingSystem == OStypes["windows"]:
pass
else:
raise Exception
def install_minikube(operatingSystem):
"""
:param operatingSystem: values from the OStypes dict. (Should be updated to enum once python 3 is available)
OS dependant, so it must be passed to this function in order to run correctly.
"""
print(
"Installing minikube..."
) # If minikube version changes this will need updating
if operatingSystem == OStypes["mac"]:
_cmd(
"curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.25.2/minikube-darwin-amd64"
)
_cmd("chmod +x minikube")
_cmd("sudo mv minikube /usr/local/bin/")
elif operatingSystem == OStypes["linux"]:
_cmd(
"curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.25.2/minikube-linux-amd64"
)
_cmd("chmod +x minikube")
_cmd("sudo mv minikube /usr/local/bin/")
elif operatingSystem == OStypes["windows"]:
pass
else:
raise Exception
def install_kurbernetes(operatingSystem):
"""
:param operatingSystem: values from the OStypes dict. (Should be updated to enum once python 3 is available)
OS dependant, so it must be passed to this function in order to run correctly.
"""
print(
"Installing Kubernetes..."
) # If kubernetes version changes this will need updating
if operatingSystem == OStypes["mac"]:
_cmd(
"curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/v1.9.4/bin/darwin/amd64/kubectl"
)
_cmd("chmod +x kubectl")
_cmd("sudo mv kubectl /usr/local/bin/")
elif operatingSystem == OStypes["linux"]:
_cmd("sudo snap install kubectl --classic")
elif operatingSystem == OStypes["windows"]:
pass
else:
raise Exception
def install_pip(): # Linux only
print("Installing pip...")
_cmd("sudo apt-get install python-pip")
def install_snap(): # Linux only
print("Installing snap...")
_cmd("sudo apt install snapd")
def install_nodejs(): # Linux only
print("Installing Nodejs...")
_cmd("sudo apt-get install -y nodejs")
def run_pipenv_install(): # OS independent
print('Running "pipenv install"...')
_cmd("pipenv install --dev")
def set_up_frontend_dependencies(): # Mac & Linux only
print("Setting up frontend dependencies...")
_cmd("cd ./game_frontend | sudo yarn")
def check_homebrew(): # Mac only
_cmd("brew -v")
print("Homebrew Found...")
def check_for_cmdtest(): # Linux/Ubuntu only
"""
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.
"""
p = subprocess.Popen(
"dpkg-query -W -f='${status}' cmdtest", shell=True, stdout=PIPE
)
(stdout, _) = p.communicate()
if "unknown" not in stdout:
print(
"Looks like cmdtest is installed on your machine, this can cause issues when installing Yarn."
)
answer = False
answered = False
while not answered:
choice = raw_input("Is it okay if I remove cmdtest? [y/n]").lower()
if choice in valid:
answer = valid[choice]
answered = True
else:
print("Please answer 'yes' or 'no' ('y' or 'n').")
if answer:
print("Removing cmdtest...")
_cmd("apt-get remove cmdtest")
else:
print("Continuing without removing cmdtest...")
def update_apt_get(): # Linux only
print("Updating apt-get...")
_cmd("sudo apt-get update")
def get_nodejs(): # Linux only
print("Getting Nodejs...")
_cmd("curl python-software-properties | sudo apt-get install")
_cmd("curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -")
def add_aimmo_to_hosts_file(): # Mac & Linux only
with open("/etc/hosts", "r") as hostfile:
data = hostfile.read().replace("\n", "")
if "192.168.99.100 local.aimmo.codeforlife.education" not in data:
print("Adding Kurono to /etc/hosts...")
_cmd(
"sudo sh -c 'echo 192.168.99.100 local.aimmo.codeforlife.education >> /etc/hosts'"
)
else:
print("Kurono already present in /etc/hosts...")
def add_parcel_bundler(): # Mac & Linux only
print("Adding parcel-bundler globally...")
_cmd("yarn global add parcel-bundler")
def configure_yarn_repo(): # Linux only
print("Configuring Yarn repository...")
_cmd("curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -")
_cmd(
'echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list'
)
def mac_setup(hostOS):
"""
Runs the list of commands, sequentially, needed in order to set up Kurono for a Mac.
After this has been run the user needs to open Docker to finalise its install,
and get the unity package for Kurono from aimmo-unity.
"""
try:
check_homebrew()
install_yarn(hostOS)
add_parcel_bundler()
set_up_frontend_dependencies()
install_pipenv(hostOS)
run_pipenv_install()
install_docker(hostOS)
install_minikube(hostOS)
install_kurbernetes(hostOS)
except CalledProcessError as e:
print("A command has return an exit code != 0, so something has gone wrong.")
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 Exception as e:
print(
"Something went very wrong, maybe I couldn't read hosts? Otherwise I have no idea what it was D:"
)
traceback.print_exc()
def windows_setup(hostOS):
pass
def linux_setup(hostOS):
try:
update_apt_get()
get_nodejs()
install_nodejs()
check_for_cmdtest()
configure_yarn_repo()
install_yarn(hostOS)
add_parcel_bundler()
install_pip()
install_pipenv(hostOS)
set_up_frontend_dependencies()
install_kurbernetes(hostOS)
install_docker(hostOS)
add_aimmo_to_hosts_file()
except CalledProcessError as e:
print("Command returned an exit code != 0, so something has gone wrong.")
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 Exception as e:
print(
"Something went very wrong, maybe I couldn't read hosts? otherwise I have no idea what it was D:"
)
traceback.print_exc()
print(
"---------------------------------------------------------------------------------------------------"
)
print(
"| Welcome to Kurono! This script should make your life a little easier, |"
)
print(
"| just be kind if it doesn't work. |"
)
print(
"| You may be asked to enter your password during this setup. |"
)
print(
"---------------------------------------------------------------------------------------------------"
)
if platform.system() == "Darwin":
hostOS = OStypes["mac"]
print("MAC found!")
mac_setup(hostOS)
elif platform.system() == "Windows":
hostOS = OStypes["windows"]
print("WINDOWS found!")
windows_setup(hostOS)
elif platform.system() == "Linux":
hostOS = OStypes["linux"]
print("LINUX found!")
linux_setup(hostOS)
else:
print("Could not detect operating system. Maybe you're using")
print("something other than Windows, Mac, or Linux?")