-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrpi_update.py
executable file
·394 lines (326 loc) · 16 KB
/
rpi_update.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
import os
import glob
from pathlib import Path
from time import sleep
from conf_wizard_rh import conf_rh
from modules import clear_the_screen, Bcolors, triangle_image_show, internet_check, load_ota_sys_markers, \
write_ota_sys_markers, load_config, server_start
def check_preferred_rh_version(config):
with open("version.txt", "r") as file:
lines = file.readlines()
line_number = 0
for line in lines:
line_number += 1
if line_number == 1:
stable_name_line = line.strip()
if line_number == 3:
beta_name_line = line.strip()
break
no_dots_preferred_rh_version = stable_name_line.split(".")[0].strip()
converted_rh_version_name = \
no_dots_preferred_rh_version[0] + "." + no_dots_preferred_rh_version[1] + "." + no_dots_preferred_rh_version[2:]
stable_release_name = str(
"v" + converted_rh_version_name) # stable rh target is being loaded from the version.txt file
beta_release_name = str("v" + beta_name_line)
if config.rh_version == 'stable':
server_version = stable_release_name
elif config.rh_version == 'beta':
server_version = beta_release_name
elif config.rh_version == 'master' or config.rh_version == 'main':
server_version = 'main'
else: # in case of 'custom' version selected in wizard
server_version = config.rh_version
return server_version, no_dots_preferred_rh_version
# TODO I would like to move th tags out of being hard-coded here.
# Maybe get a list of tags and ask user to select from list
# or automatically figure out the newest non-beta tag?
def get_rotorhazard_server_version(config):
server_py = Path(f"/home/{config.user}/RotorHazard/src/server/server.py")
server_installed_flag = False
if server_py.exists():
with open(server_py, 'r') as open_file:
for line in open_file:
if line.startswith('RELEASE_VERSION'):
# RELEASE_VERSION = "2.2.0 (dev 1)" # Public release version code
server_version_name = line.strip().split('=')[1].strip()
server_version_name = server_version_name.split('#')[0].replace('"', '').strip()
server_installed_flag = True
break
else:
server_version_name = '0' # string so string operations like .split can be perfomed
server_installed_flag = False
return server_installed_flag, server_version_name
def rh_update_check(config):
update_prompt = f"{Bcolors.RED}-> pending stable update{Bcolors.ENDC}"
# above is showed only when stable version is newer than current
raw_installed_rh_server = get_rotorhazard_server_version(config)[1] # 3.0.0-dev2
installed_rh_server = raw_installed_rh_server.split("-")[0] # 3.0.0
installed_rh_server_number = int(installed_rh_server.replace(".", "")) # 300
server_installed_flag = get_rotorhazard_server_version(config)[0] # check if RH is installed
newest_possible_rh_version = int(check_preferred_rh_version(config)[1]) # derived from OTA name 232.25.3h -> 232
if installed_rh_server_number < newest_possible_rh_version and server_installed_flag is True:
rh_update_available_flag = True
else:
rh_update_available_flag = False
if rh_update_available_flag:
return update_prompt
else:
return ''
def check_rotorhazard_config_status(config):
if os.path.exists(f"/home/{config.user}/RotorHazard/src/server/config.json"):
config_soft = f"{Bcolors.GREEN}configured{Bcolors.ENDC} 👍"
config_flag = True
else:
config_soft = f"{Bcolors.YELLOW}{Bcolors.UNDERLINE}not configured{Bcolors.ENDC} 👎👎👎"
config_flag = False
return config_soft, config_flag
def show_update_completed():
update_completed = """\n\n
#################################################
## ##
##{bold}{green}Update completed! {thumbs}{endc}##
## ##
#################################################
""".format(thumbs="👍👍👍 ", bold=Bcolors.BOLD_S,
endc=Bcolors.ENDC_S, green=Bcolors.GREEN_S)
return update_completed
def end_update(config, server_configured_flag, server_installed_flag):
if not server_configured_flag and server_installed_flag:
configure = f"{Bcolors.GREEN}c - Configure RotorHazard now{Bcolors.ENDC}"
else:
configure = "c - Reconfigure RotorHazard server"
while True:
print(show_update_completed())
clearing_color = ''
old_installations_were_found = False
old_rh_directories_found = glob.glob('.././RotorHazard_*')
if old_rh_directories_found:
clearing_color = Bcolors.GREEN
old_installations_were_found = True
print(f"""
{configure}
r - Reboot - recommended, not a must
s - Start RH server now {clearing_color}
o - Clear old RotorHazard installations{Bcolors.YELLOW}
e - Exit now{Bcolors.ENDC}""")
selection = input()
if selection == 'r':
os.system("sudo reboot")
elif selection == 's':
os.chdir(f"/home/{config.user}/RH_Install-Manager")
server_start()
elif selection == 'o':
os.system("rm -rf ~/RotorHazard_*")
if old_installations_were_found:
print("\n\t\t -- old RH installations cleaned --")
else:
print("\n\t\t -- no more old RH installations --")
sleep(2)
clear_the_screen()
elif selection == 'c':
conf_rh()
elif selection == 'e':
return
def end_installation(config):
while True:
print(f"""
{Bcolors.GREEN}
c - Configure RH server now - recommended {Bcolors.ENDC}
r - Reboot - STRONGLY recommended after configuring
e - Exit now{Bcolors.ENDC}""")
selection = input()
if selection == 'r':
os.system("sudo reboot")
elif selection == 'e':
return
elif selection == 'c':
conf_rh()
break
def installation(conf_allowed, config):
ota_config = load_ota_sys_markers(config.user)
os.system("sudo systemctl stop rotorhazard >/dev/null 2>&1 &") if not config.debug_mode else None
clear_the_screen()
internet_flag = internet_check()
if not internet_flag:
print(f"\n\t{Bcolors.RED}Looks like you don't have internet connection. Installation canceled.{Bcolors.ENDC}")
sleep(2)
else:
print(f"\n\t\t\t{Bcolors.GREEN}Internet connection - OK{Bcolors.ENDC}")
sleep(2)
clear_the_screen()
print(f"\n\n\t{Bcolors.BOLD}Installation process has been started - please wait...{Bcolors.ENDC}\n\n")
installation_completed = """
######################################################
## ##
##{bold}{green}Installation completed {thumbs}{endc}##
## ##
######################################################
After rebooting you can confirm by typing 'sudo raspi-config'
if I2C, SPI and SSH protocols are active.
""".format(thumbs="👍👍👍 ", bold=Bcolors.BOLD_S,
endc=Bcolors.ENDC_S, green=Bcolors.GREEN_S)
if conf_allowed:
if not config.debug_mode:
os.system("./scripts/sys_conf.sh all")
else:
os.system("./scripts/sys_conf.sh ssh")
print("\n\nsimulation mode - SPI, I2C and UART won't be configured\n\n\n")
sleep(3)
ota_config.sys_config_done, ota_config.uart_support_added = True, True
# UART enabling added here so user won't have to reboot Pi again after doing it in Features Menu
write_ota_sys_markers(ota_config, config.user)
os.system(f"./scripts/install_rh.sh {config.user} {check_preferred_rh_version(config)[0]}")
input("\n\n\npress Enter to continue")
clear_the_screen()
print(installation_completed)
os.system("sudo chmod 777 -R ~/RotorHazard")
end_installation(config.user)
def update(config):
os.system("sudo systemctl stop rotorhazard >/dev/null 2>&1 &") if not config.debug_mode else None
internet_flag = internet_check()
if not internet_flag:
print(f"\n\t{Bcolors.RED}Looks like you don't have internet connection. Update canceled.{Bcolors.ENDC}")
sleep(2)
else:
print(f"\n\t\t\t{Bcolors.GREEN}Internet connection - OK{Bcolors.ENDC}")
sleep(2)
clear_the_screen()
if not os.path.exists(f"/home/{config.user}/RotorHazard"):
print(f"""{Bcolors.BOLD}
Looks like you don't have RotorHazard server software installed for now.
If so please install your server software first.{Bcolors.ENDC}{Bcolors.GREEN}
i - Install the software - recommended{Bcolors.ENDC}
a - Abort {Bcolors.ENDC}
""")
selection = input()
if selection == 'i':
conf_allowed = True
installation(conf_allowed, config)
elif selection == 'a':
clear_the_screen()
return
else:
return
else:
clear_the_screen()
print(f"\n\n\t{Bcolors.BOLD}Updating existing installation - please wait...{Bcolors.ENDC}\n\n")
os.system(f"./scripts/update_rh.sh {config.user} {check_preferred_rh_version(config)[0]}")
config_flag, config_soft = check_rotorhazard_config_status(config)
server_installed_flag, server_version_name = get_rotorhazard_server_version(config)
os.system("sudo chmod -R 777 ~/RotorHazard")
end_update(config, config_flag, server_installed_flag)
def main_window(config):
while True:
rh_config_text, rh_config_flag = check_rotorhazard_config_status(config)
clear_the_screen()
server_installed_flag, server_version_name = get_rotorhazard_server_version(config)
if server_installed_flag:
colored_server_version_name = f"{Bcolors.GREEN}{server_version_name}{Bcolors.ENDC}"
else:
colored_server_version_name = f'{Bcolors.YELLOW}{Bcolors.UNDERLINE}installation not found{Bcolors.ENDC}'
update_prompt = rh_update_check(config)
ota_config = load_ota_sys_markers(config.user)
sys_configured_flag = ota_config.sys_config_done
configured_server_target = check_preferred_rh_version(config)[0]
sleep(0.1)
welcome_text = """
\n\n{red} {bold}
AUTOMATIC UPDATE AND INSTALLATION OF ROTORHAZARD RACING TIMER SOFTWARE
{endc}{bold}
You can automatically install and update RotorHazard timing software.
Additional dependencies and libraries also will be installed or updated.
Current database, configs and custom bitmaps will stay on their place.
Source of the software is set to {underline}{blue}{server_version}{endc}{bold} version from the official
RotorHazard repository.
Please update this (OTA) software, before updating RotorHazard server.
Also make sure that you are logged as user {underline}{blue}{user}{endc}{bold} and that you don't have
other terminal windows opened - especially in RotorHazard directory.
You can change those in configuration wizard in Main Menu.
Server version currently installed: {server} {bold}{update_prompt} {bold}
RotorHazard configuration state: {config_soft}
""".format(bold=Bcolors.BOLD, underline=Bcolors.UNDERLINE, endc=Bcolors.ENDC, blue=Bcolors.BLUE,
yellow=Bcolors.YELLOW, red=Bcolors.RED, orange=Bcolors.ORANGE,
server_version=configured_server_target, user=config.user, config_soft=rh_config_text,
server=colored_server_version_name, update_prompt=update_prompt)
print(welcome_text)
if not rh_config_flag and server_installed_flag:
configure = f"{Bcolors.GREEN}c - Configure RotorHazard server{Bcolors.ENDC}"
elif not rh_config_flag and not server_installed_flag:
configure = "c - Reconfigure RotorHazard server"
else:
configure = "c - Configure RotorHazard server"
if not server_installed_flag:
install = f"{Bcolors.GREEN}i - Install software from scratch{Bcolors.ENDC}"
else:
install = "i - Install software from scratch"
print("""
{install}
{configure}
u - Update existing installation
s - Start RotorHazard server now{yellow}
e - Exit to Main Menu{endc}
""".format(yellow=Bcolors.YELLOW, endc=Bcolors.ENDC, configure=configure, install=install))
selection = input()
if selection == 'c':
if server_installed_flag:
conf_rh()
else:
print("Please install RH server before configuring.")
sleep(2)
elif selection == 's':
if server_installed_flag:
server_start()
else:
print("Please install the RotorHazard server first")
sleep(2)
elif selection == 'i':
# rh_found_flag = os.path.exists(f"/home/{config.user}/RotorHazard")
if sys_configured_flag:
clear_the_screen()
already_installed_prompt = """{bold}
Looks like you already have your system configured.{endc}{bold}
If so, please perform installation without sys. config.
{green}i - Force installation without sys. config. {endc}{bold}
c - Force installation and system config. {yellow}
a - Abort both {endc}
""".format(bold=Bcolors.BOLD, endc=Bcolors.ENDC, underline=Bcolors.UNDERLINE,
yellow=Bcolors.YELLOW, green=Bcolors.GREEN_S)
print(already_installed_prompt)
selection = input()
if selection == 'i':
conf_allowed = False
installation(conf_allowed, config)
elif selection == 'c':
confirm_valid_options = ['y', 'yes', 'n', 'no', 'abort', 'a']
while True:
confirm = input("\n\t\tAre you sure? [yes/abort]\t").strip()
if confirm in confirm_valid_options:
break
else:
print("\ntoo big fingers :( wrong command. try again! :)")
if confirm == 'y' or confirm == 'yes':
conf_allowed = True
installation(conf_allowed, config)
elif confirm in ['n', 'no', 'abort', 'a']:
pass
elif selection == 'a':
clear_the_screen()
triangle_image_show()
sleep(0.5)
break
else:
conf_allowed = True
installation(conf_allowed, config)
elif selection == 'u':
update(config)
elif selection == 'e':
clear_the_screen()
os.chdir(f"/home/{config.user}/RH_Install-Manager")
triangle_image_show()
sleep(0.3)
break
def main():
config = load_config()
main_window(config)
if __name__ == "__main__":
main()