-
Notifications
You must be signed in to change notification settings - Fork 8
/
deploy_frame.py
379 lines (322 loc) · 19.3 KB
/
deploy_frame.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
import json
import os
import random
import re
import shutil
import string
import subprocess
import tempfile
from packaging import version
import platform
from io import StringIO
from scp import SCPClient
from app import create_app
from app.huey import huey
from app.models import get_apps_from_scenes
from app.models.drivers import drivers_for_device, write_drivers_nim, write_waveshare_driver_nim
from app.models.log import new_log as log
from app.models.frame import Frame, update_frame, get_frame_json, generate_scene_nim_source
from app.utils.ssh_utils import get_ssh_connection, exec_command, remove_ssh_connection, exec_local_command
@huey.task()
def deploy_frame(id: int):
app = create_app()
with app.app_context():
ssh = None
try:
frame = Frame.query.get_or_404(id)
if frame.status == 'deploying':
raise Exception(f"Already deploying, will not deploy again. Request again to force deploy.")
frame.status = 'deploying'
update_frame(frame)
# TODO: add the concept of builds into the backend (track each build in the database)
build_id = ''.join(random.choice(string.ascii_lowercase) for i in range(12))
log(id, "stdout", f"Deploying frame {frame.name} with build id {build_id}")
nim_path = find_nim_v2()
with tempfile.TemporaryDirectory() as temp_dir:
ssh = get_ssh_connection(frame)
log(id, "stdout", f"- Getting target architecture")
uname_output = []
exec_command(frame, ssh, f"uname -m", uname_output)
arch = "".join(uname_output).strip()
if arch == "aarch64" or arch == "armv7l":
cpu = "arm64"
elif arch == "armv6l":
cpu = "arm"
elif arch == "i386":
cpu = "i386"
else:
cpu = "amd64"
drivers = drivers_for_device(frame.device)
# create a build .tar.gz
log(id, "stdout", f"- Copying build folders")
build_dir, source_dir = create_build_folders(temp_dir, build_id)
log(id, "stdout", f"- Applying local modifications")
make_local_modifications(frame, source_dir)
log(id, "stdout", f"- Creating build archive")
archive_path = create_local_build_archive(frame, build_dir, build_id, nim_path, source_dir, temp_dir, cpu)
with SCPClient(ssh.get_transport()) as scp:
# build the release on the server
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii ntp\" || sudo apt -y install ntp")
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii build-essential\" || sudo apt -y install build-essential")
if drivers.get("evdev"):
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii libevdev-dev\" || sudo apt -y install libevdev-dev")
if drivers.get('waveshare'):
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii liblgpio-dev\" || sudo apt -y install liblgpio-dev")
exec_command(frame, ssh, "if [ ! -d /srv/frameos/ ]; then sudo mkdir -p /srv/frameos/ && sudo chown $(whoami):$(whoami) /srv/frameos/; fi")
exec_command(frame, ssh, f"mkdir -p /srv/frameos/build/")
log(id, "stdout", f"> add /srv/frameos/build/build_{build_id}.tar.gz")
scp.put(archive_path, f"/srv/frameos/build/build_{build_id}.tar.gz")
exec_command(frame, ssh, f"cd /srv/frameos/build && tar -xzf build_{build_id}.tar.gz && rm build_{build_id}.tar.gz")
exec_command(frame, ssh, f"cd /srv/frameos/build/build_{build_id} && sh ./compile_frameos.sh")
exec_command(frame, ssh, f"mkdir -p /srv/frameos/releases/release_{build_id}")
exec_command(frame, ssh, f"cp /srv/frameos/build/build_{build_id}/frameos /srv/frameos/releases/release_{build_id}/frameos")
log(id, "stdout", f"> add /srv/frameos/releases/release_{build_id}/frame.json")
scp.putfo(StringIO(json.dumps(get_frame_json(frame), indent=4) + "\n"), f"/srv/frameos/releases/release_{build_id}/frame.json")
# TODO: abstract driver-specific install steps
# TODO: abstract vendor logic
if inkyPython := drivers.get("inkyPython"):
exec_command(frame, ssh, f"cp -r /srv/frameos/build/build_{build_id}/vendor /srv/frameos/releases/release_{build_id}/vendor")
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii python3-pip\" || sudo apt -y install python3-pip")
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii python3-venv\" || sudo apt -y install python3-venv")
exec_command(frame, ssh, f"cd /srv/frameos/releases/release_{build_id}/vendor/{inkyPython.vendor_folder} && python3 -m venv env && env/bin/pip3 install -r requirements.txt")
if inkyHyperPixel2r := drivers.get("inkyHyperPixel2r"):
exec_command(frame, ssh, f"cp -r /srv/frameos/build/build_{build_id}/vendor /srv/frameos/releases/release_{build_id}/vendor")
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii python3-pip\" || sudo apt -y install python3-pip")
exec_command(frame, ssh, "dpkg -l | grep -q \"^ii python3-venv\" || sudo apt -y install python3-venv")
exec_command(frame, ssh, f"cd /srv/frameos/releases/release_{build_id}/vendor/{inkyHyperPixel2r.vendor_folder} && python3 -m venv env && env/bin/pip3 install -r requirements.txt")
# add frameos.service
with open("../frameos/frameos.service", "r") as file:
service_contents = file.read().replace("%I", frame.ssh_user)
with SCPClient(ssh.get_transport()) as scp:
scp.putfo(StringIO(service_contents), f"/srv/frameos/releases/release_{build_id}/frameos.service")
exec_command(frame, ssh, f"sudo cp /srv/frameos/releases/release_{build_id}/frameos.service /etc/systemd/system/frameos.service")
exec_command(frame, ssh, "sudo chown root:root /etc/systemd/system/frameos.service")
exec_command(frame, ssh, "sudo chmod 644 /etc/systemd/system/frameos.service")
# swap out the release
exec_command(frame, ssh, f"rm -rf /srv/frameos/current && ln -s /srv/frameos/releases/release_{build_id} /srv/frameos/current")
# restart
exec_command(frame, ssh, "sudo systemctl daemon-reload")
exec_command(frame, ssh, "sudo systemctl stop frameos.service || true")
exec_command(frame, ssh, "sudo systemctl enable frameos.service")
exec_command(frame, ssh, "sudo systemctl start frameos.service")
exec_command(frame, ssh, "sudo systemctl status frameos.service")
if drivers.get("i2c"):
exec_command(frame, ssh, 'grep -q "^dtparam=i2c_vc=on$" /boot/config.txt || echo "dtparam=i2c_vc=on" | sudo tee -a /boot/config.txt')
exec_command(frame, ssh, 'command -v raspi-config > /dev/null && sudo raspi-config nonint get_i2c | grep -q "1" && { sudo raspi-config nonint do_i2c 0; echo "I2C is now enabled"; }')
if drivers.get("spi"):
exec_command(frame, ssh, 'sudo raspi-config nonint do_spi 0')
frame.status = 'starting'
update_frame(frame)
except Exception as e:
log(id, "stderr", str(e))
frame.status = 'uninitialized'
update_frame(frame)
finally:
if ssh is not None:
ssh.close()
log(frame.id, "stdinfo", "SSH connection closed")
remove_ssh_connection(ssh)
def find_nim_v2():
nim_path = find_nim_executable()
if not nim_path:
raise Exception("Nim executable not found")
nim_version = get_nim_version(nim_path)
if not nim_version or nim_version < version.parse("2.0.0"):
raise Exception("Nim version 2.0.0 or higher is required")
return nim_path
def create_build_folders(temp_dir, build_id):
build_dir = os.path.join(temp_dir, f"build_{build_id}")
source_dir = os.path.join(temp_dir, "frameos")
# 1. copy the frameos folder to the temp folder
os.makedirs(source_dir, exist_ok=True)
shutil.copytree("../frameos", source_dir, dirs_exist_ok=True)
# 2. make a new build folder with the build_id
os.makedirs(build_dir, exist_ok=True)
return build_dir, source_dir
def make_local_modifications(frame: Frame, source_dir: str):
shutil.rmtree(os.path.join(source_dir, "src", "scenes"), ignore_errors=True)
os.makedirs(os.path.join(source_dir, "src", "scenes"), exist_ok=True)
# write all source apps
for node_id, sources in get_apps_from_scenes(frame.scenes).items():
app_id = "nodeapp_" + node_id.replace('-', '_')
app_dir = os.path.join(source_dir, "src", "apps", app_id)
os.makedirs(app_dir, exist_ok=True)
for file, source in sources.items():
with open(os.path.join(app_dir, file), "w") as file:
file.write(source)
# only one scene called "default" for now
for scene in frame.scenes:
scene_source = generate_scene_nim_source(frame, scene)
with open(os.path.join(source_dir, "src", "scenes", f"{scene.get('id')}.nim"), "w") as file:
file.write(scene_source)
drivers = drivers_for_device(frame.device)
with open(os.path.join(source_dir, "src", "drivers", "drivers.nim"), "w") as file:
drivers_nim = write_drivers_nim(drivers)
file.write(drivers_nim)
if drivers.get("waveshare"):
with open(os.path.join(source_dir, "src", "drivers", "waveshare", "driver.nim"), "w") as file:
drivers_nim = write_waveshare_driver_nim(drivers)
file.write(drivers_nim)
def create_local_build_archive(frame: Frame, build_dir: str, build_id: str, nim_path: str, source_dir: str, temp_dir: str, cpu: str):
# TODO: abstract driver-specific vendor steps
drivers = drivers_for_device(frame.device)
if inkyPython := drivers.get('inkyPython'):
os.makedirs(os.path.join(build_dir, "vendor"), exist_ok=True)
shutil.copytree(f"../frameos/vendor/{inkyPython.vendor_folder}/", os.path.join(build_dir, "vendor", inkyPython.vendor_folder), dirs_exist_ok=True)
shutil.rmtree(os.path.join(build_dir, "vendor", inkyPython.vendor_folder, "env"), ignore_errors=True)
shutil.rmtree(os.path.join(build_dir, "vendor", inkyPython.vendor_folder, "__pycache__"), ignore_errors=True)
if inkyHyperPixel2r := drivers.get('inkyHyperPixel2r'):
os.makedirs(os.path.join(build_dir, "vendor"), exist_ok=True)
shutil.copytree(f"../frameos/vendor/{inkyHyperPixel2r.vendor_folder}/", os.path.join(build_dir, "vendor", inkyHyperPixel2r.vendor_folder), dirs_exist_ok=True)
shutil.rmtree(os.path.join(build_dir, "vendor", inkyHyperPixel2r.vendor_folder, "env"), ignore_errors=True)
shutil.rmtree(os.path.join(build_dir, "vendor", inkyHyperPixel2r.vendor_folder, "__pycache__"), ignore_errors=True)
# Tell a white lie
log(frame.id, "stdout", f"- No cross compilation. Generating source code for compilation on frame.")
# run "nim c --os:linux --cpu:arm64 --compileOnly --genScript --nimcache:tmp/build_1 src/frameos.nim"
status, out, err = exec_local_command(
frame,
f"cd {source_dir} && nimble assets -y && {nim_path} compile --os:linux --cpu:{cpu} --compileOnly --genScript --nimcache:{build_dir} src/frameos.nim 2>&1"
)
if status != 0:
last_line = [line for line in err.split("\n") if line != ''][-1]
if match := re.match(r'^(.*\.nim)\((\d+), (\d+)\),*', last_line):
filename = match.group(1)
line = int(match.group(2))
column = int(match.group(3))
source_path = os.path.realpath(source_dir)
final_path = os.path.realpath(os.path.join(source_dir, filename))
if os.path.commonprefix([final_path, source_path]) == source_path:
filename = final_path[len(source_path) + 1:]
with open(final_path, "r") as file:
lines = file.readlines()
log(frame.id, "stdout", f"Error in {filename}:{line}:{column}")
log(frame.id, "stdout", f"Line {line}: {lines[line - 1]}")
log(frame.id, "stdout", f".......{'.' * (column - 1 + len(str(line)))}^")
else:
log(frame.id, "stdout", f"Error in {filename}:{line}:{column}")
raise Exception("Failed to generate frameos sources")
# Copy the file "nimbase.h" to "build_1/nimbase.h"
nimbase_path = find_nimbase_file(nim_path)
if not nimbase_path:
raise Exception("nimbase.h not found")
shutil.copy(nimbase_path, os.path.join(build_dir, "nimbase.h"))
if waveshare := drivers.get('waveshare'):
shutil.copy(os.path.join(source_dir, "src", "drivers", "waveshare", "ePaper", "Debug.h"), os.path.join(build_dir, "Debug.h")) # TODO: name
shutil.copy(os.path.join(source_dir, "src", "drivers", "waveshare", "ePaper", "DEV_Config.c"), os.path.join(build_dir, "DEV_Config.c"))
shutil.copy(os.path.join(source_dir, "src", "drivers", "waveshare", "ePaper", "DEV_Config.h"), os.path.join(build_dir, "DEV_Config.h"))
# TODO: select just one
shutil.copy(os.path.join(source_dir, "src", "drivers", "waveshare", "ePaper", "EPD_7in5_V2.c"), os.path.join(build_dir, "EPD_7in5_V2.c"))
shutil.copy(os.path.join(source_dir, "src", "drivers", "waveshare", "ePaper", "EPD_7in5_V2.h"), os.path.join(build_dir, "EPD_7in5_V2.h"))
shutil.copy(os.path.join(source_dir, "src", "drivers", "waveshare", "ePaper", "EPD_2in13_V3.c"), os.path.join(build_dir, "EPD_2in13_V3.c"))
shutil.copy(os.path.join(source_dir, "src", "drivers", "waveshare", "ePaper", "EPD_2in13_V3.h"), os.path.join(build_dir, "EPD_2in13_V3.h"))
# Update the compilation script for verbose output
script_path = os.path.join(build_dir, "compile_frameos.sh")
log(frame.id, "stdout", f"Cleaning build script at {script_path}")
with open(script_path, "r") as file:
lines = file.readlines()
with open(script_path, "w") as file:
file.write("#!/bin/sh\n")
file.write("set -eu\n")
file.write("mkdir -p ../cache\n") # make sure we have the cache folder
file.write("cached_files_count=0\n") # Initialize cached files counter
for i, line in enumerate(lines):
if line.startswith("gcc -c") and line.strip().endswith(".c"):
source_file = line.split(' ')[-1].strip()
o_file = line.split(' ')[-2].strip()
source_cleaned = '/'.join(source_file.split('@s')[-3:]).replace('@m', './')
# take the md5sum of the source file <source>.c
file.write(f"md5sum=$(md5sum {source_file} | awk '{{print $1}}')\n")
# check if there's a file in the cache folder called <md5sum>.c.o
file.write(f"if [ -f ../cache/${{md5sum}}.{cpu}.c.o ]; then\n")
# if there is, make a symlink to the build folder with the name <source>.o
file.write(f" cached_files_count=$((cached_files_count + 1))\n")
file.write(f" ln -s ../cache/${{md5sum}}.{cpu}.c.o {o_file}\n")
file.write("else\n")
# if not, run the command in "line" and then copy the <source>.c.o into the build folder as <md5sum>.c.o
file.write(f" echo [{i + 1}/{len(lines)}] Compiling on device: {source_cleaned}\n")
file.write(f" {line.strip()}\n")
file.write(f" cp {o_file} ../cache/${{md5sum}}.{cpu}.c.o\n")
file.write("fi\n")
else:
file.write(f"echo [{i + 1}/{len(lines)}] Compiling on device: frameos\n")
file.write(line)
file.write("echo \"Used $cached_files_count cached files\"\n")
# 7. Zip it up "(cd tmp && tar -czf ./build_1.tar.gz build_1)"
archive_path = os.path.join(temp_dir, f"build_{build_id}.tar.gz")
zip_base_path = os.path.join(temp_dir, f"build_{build_id}")
shutil.make_archive(zip_base_path, 'gztar', temp_dir, f"build_{build_id}")
return archive_path
def find_nim_executable():
# Common paths for nim executable based on the operating system
common_paths = {
'Windows': [
'C:\\Program Files\\Nim\\bin\\nim.exe',
'C:\\Nim\\bin\\nim.exe'
],
'Darwin': [ # macOS
'/opt/homebrew/bin/nim',
'/usr/local/bin/nim'
],
'Linux': [
'/usr/bin/nim',
'/usr/local/bin/nim',
'/opt/nim/bin/nim',
]
}
# Check if nim is in the PATH
if is_executable_in_path('nim'):
return 'nim' # nim is in the PATH
# If not in PATH, check common paths based on the OS
os_type = platform.system()
for path in common_paths.get(os_type, []):
if os.path.isfile(path) and os.access(path, os.X_OK):
return path
return None # nim not found
def is_executable_in_path(executable):
try:
subprocess.run([executable, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return True
except FileNotFoundError:
return False
def get_nim_version(executable_path):
try:
result = subprocess.run([executable_path, '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
# Example output: "Nim Compiler Version 1.4.8 [Linux: amd64]"
output = result.stdout.split('\n')[0]
version_str = output.split()[3]
return version.parse(version_str)
except Exception as e:
print(f"An error occurred while getting Nim version: {e}")
return None
def find_nimbase_file(nim_executable: str):
nimbase_paths = []
# Try to get paths from 'nim dump'
try:
nim_dump_output = subprocess.check_output([nim_executable, "dump"], text=True)
# Extract paths that might contain 'nimbase.h'
nimbase_paths.extend(line for line in nim_dump_output.splitlines() if 'lib' in line)
except subprocess.CalledProcessError as e:
print(f"Error running 'nim dump': {e}")
# Add common paths based on the operating system
os_type = platform.system()
if os_type == 'Darwin': # macOS
nimbase_paths.append('/usr/local/lib/nim')
elif os_type == 'Linux':
nimbase_paths.append('/usr/lib/nim')
nimbase_paths.append('/opt/nim/lib')
elif os_type == 'Windows':
nimbase_paths.append('C:\\Nim\\lib')
# Search for nimbase.h in the collected paths
for path in nimbase_paths:
nimbase_file = os.path.join(path, 'nimbase.h')
if os.path.isfile(nimbase_file):
return nimbase_file
if os_type == 'Darwin': # macOS
base_dir = '/opt/homebrew/Cellar/nim/'
if os.path.exists(base_dir):
for version_dir in os.listdir(base_dir):
nimbase_path = os.path.join(base_dir, version_dir, 'nim', 'lib', 'nimbase.h')
if os.path.isfile(nimbase_path):
return nimbase_path
return None # nimbase.h not found