-
-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathupdate-service
More file actions
executable file
·81 lines (63 loc) · 2.52 KB
/
update-service
File metadata and controls
executable file
·81 lines (63 loc) · 2.52 KB
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
#!/usr/bin/env python3
# Standalone executable for running the TinyPilot updater and saving the result
# to a file. This executable is meant to be run as a systemd one-shot service,
# launched manually when the user requests an update.
# Ignore pylint complaint that this filename has a dash instead of an
# underscore.
# pylint:disable=invalid-name
import argparse
import logging
import os
import subprocess
import sys
# We’re importing the log package first because it needs to overwrite the
# app-wide logger class before any other module loads it.
import log
import update.launcher
import update.result
import update.result_store
import utc
logger = logging.getLogger(__name__)
_UPDATE_TIMEOUT_SECONDS = 60 * 10
def perform_update():
result = run_update_script()
update.result_store.write(result)
def run_update_script():
logger.info('Launching update script: %s',
update.launcher.UPDATE_SCRIPT_PATH)
try:
# The command arguments are trusted because they aren't based on user
# input.
subprocess.run( # noqa: S603
['/usr/bin/sudo', update.launcher.UPDATE_SCRIPT_PATH],
check=True,
timeout=_UPDATE_TIMEOUT_SECONDS)
except subprocess.TimeoutExpired:
logger.error('Update process timed out')
return make_update_result(update_error='The update timed out')
except subprocess.CalledProcessError as e:
logger.error('Update process terminated with failing exit code: %s',
str(e))
return make_update_result(update_error=f'The update failed: {e}')
logger.info('Update completed successfully')
return make_update_result(update_error=None)
def make_update_result(update_error):
return update.result.Result(error=update_error, timestamp=utc.now())
def main(_):
root_logger = log.create_root_logger(logging.StreamHandler())
root_logger.setLevel(logging.INFO)
logger.info('Starting TinyPilot update')
perform_update()
if __name__ == '__main__':
# Ensure that the script doesn't have unnecessary privileges.
if os.geteuid() == 0:
print("This script doesn't require root privileges.", file=sys.stderr)
print('Please re-run as tinypilot:', file=sys.stderr)
print(' runuser tinypilot --command',
f"'{' '.join(sys.argv)}'",
file=sys.stderr)
sys.exit(1)
parser = argparse.ArgumentParser(
prog='TinyPilot Update Service',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
main(parser.parse_args())