-
Notifications
You must be signed in to change notification settings - Fork 2
/
knitpi-install
executable file
·184 lines (162 loc) · 6.42 KB
/
knitpi-install
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
#!/usr/bin/python3
# knitpi - Raspberry Pi interface for Brother KH-930 knitting machine
# Copyright (C) 2018-2018 Johannes Bauer
#
# This file is part of knitpi.
#
# knitpi is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; this program is ONLY licensed under
# version 3 of the License, later versions are explicitly excluded.
#
# knitpi is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with knitpi; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Johannes Bauer <[email protected]>
import os
import sys
import pwd
import time
import subprocess
import shutil
need_reboot = False
config = {
"self_name": "knitpi-install",
"username": "knitpi",
"gitremote": "https://github.com/johndoe31415/knitpi",
"apt-pkgs": [ "git", "python3", "python3-pip", "python3-mako", "python3-flask", "python3-gevent", "libpng-dev", "libgpiod-dev" ],
}
config["homedir"] = "/home/%s" % (config["username"])
config["gitdir"] = config["homedir"] + "/knitpi"
if os.getuid() != 0:
print("You must run this script as root.", file = sys.stderr)
sys.exit(1)
# First stop services to be able to update them. This may fail if they don't yet exist.
try:
subprocess.check_call([ "systemctl", "stop", "knitpi-ui" ])
except subprocess.CalledProcessError:
pass
try:
subprocess.check_call([ "systemctl", "stop", "knitpi-core" ])
except subprocess.CalledProcessError:
pass
# Install all packages that are required
subprocess.check_call([ "apt-get", "update" ])
subprocess.check_call([ "apt-get", "install", "--yes" ] + config["apt-pkgs"])
# Check if knitpi user exists. Create it if it doesn't.
try:
pwdinfo = pwd.getpwnam(config["username"])
except KeyError:
homedir = "/home/%s" % (config["username"])
subprocess.check_call([ "useradd", "-c", "KnitPi User", "-d", homedir, "-G", "gpio,spi,video", "-s", "/bin/bash", "knitpi" ])
pwdinfo = pwd.getpwnam(config["username"])
if not os.path.isdir(config["homedir"]):
os.mkdir(config["homedir"])
os.chown(config["homedir"], pwdinfo.pw_uid, pwdinfo.pw_gid)
os.chmod(config["homedir"], 0o700)
# Do the local setup as user in forked process
pid = os.fork()
if pid == 0:
# We're the child process. Drop privileges.
os.setgroups([ ])
os.setgid(pwdinfo.pw_gid)
os.setuid(pwdinfo.pw_uid)
chg_env = dict(os.environ)
chg_env["HOME"] = config["homedir"]
chg_env["USER"] = config["username"]
chg_env["LOGNAME"] = config["username"]
if not os.path.isdir(config["gitdir"]):
# Git not yet cloned.
os.chdir(config["homedir"])
subprocess.check_call([ "git", "clone", config["gitremote"] ], env = chg_env)
# Reset to latest master
os.chdir(config["gitdir"])
subprocess.check_call([ "git", "clean", "-dfx" ], env = chg_env)
subprocess.check_call([ "git", "reset", "--hard", "origin/master" ], env = chg_env)
subprocess.check_call([ "git", "checkout", "master" ], env = chg_env)
subprocess.check_call([ "git", "pull" ], env = chg_env)
# Compile firmware
os.chdir(config["gitdir"] + "/firmware")
subprocess.check_call([ "make" ], env = chg_env)
# Download uwsgi
subprocess.check_call([ "pip3", "install", "uwsgi" ], env = chg_env)
sys.exit(0)
else:
# Wait for child to finish
os.waitpid(pid, 0)
# Then create the systemd services for the core
with open("/etc/systemd/system/knitpi-core.service", "w") as f:
print("[Unit]", file = f)
print("Description=KnitPi core", file = f)
print(file = f)
print("[Service]", file = f)
print("Type=simple", file = f)
print("ExecStart=%s/firmware/knitserver -v -f %s/socket" % (config["gitdir"], config["homedir"]), file = f)
print("User=%s" % (config["username"]), file = f)
print("Nice=-15", file = f)
print(file = f)
print("[Install]", file = f)
print("WantedBy=multi-user.target", file = f)
os.chmod("/etc/systemd/system/knitpi-core.service", 0o644)
subprocess.check_call([ "systemctl", "enable", "knitpi-core" ])
subprocess.check_call([ "systemctl", "start", "knitpi-core" ])
# And also for the UI
with open("/etc/systemd/system/knitpi-ui.service", "w") as f:
print("[Unit]", file = f)
print("Description=KnitPi user interface", file = f)
print("After=network.target", file = f)
print(file = f)
print("[Service]", file = f)
print("Type=simple", file = f)
print("ExecStart=%s/.local/bin/uwsgi knitui_prod.ini" % (config["homedir"]), file = f)
print("User=%s" % (config["username"]), file = f)
print("WorkingDirectory=%s/knitpi/ui" % (config["homedir"]), file = f)
print("AmbientCapabilities=CAP_NET_BIND_SERVICE", file = f)
print(file = f)
print("[Install]", file = f)
print("WantedBy=multi-user.target", file = f)
os.chmod("/etc/systemd/system/knitpi-ui.service", 0o644)
subprocess.check_call([ "systemctl", "enable", "knitpi-ui" ])
subprocess.check_call([ "systemctl", "start", "knitpi-ui" ])
# Add a systemd service that can self-update the firmware
shutil.copy(config["gitdir"] + "/" + config["self_name"], "/usr/local/bin/knitpi-install")
with open("/etc/systemd/system/knitpi-update.service", "w") as f:
print("[Unit]", file = f)
print("Description=KnitPi software update", file = f)
print("After=network.target", file = f)
print(file = f)
print("[Service]", file = f)
print("Type=simple", file = f)
print("Restart=no", file = f)
print("ExecStart=/usr/local/bin/knitpi-install", file = f)
os.chmod("/etc/systemd/system/knitpi-update.service", 0o644)
os.chmod("/usr/local/bin/knitpi-install", 0o755)
# Add sudoers entry if not already present
add_line = "knitpi ALL=NOPASSWD: /bin/systemctl start knitpi-update"
with open("/etc/sudoers") as f:
lines = f.read().split("\n")
if add_line not in lines:
with open("/etc/sudoers", "a") as f:
print(file = f)
print("# Allow knitpi user to upgrade the knitpi firmware", file = f)
print(add_line, file = f)
# Enable SPI in configuration
add_line = "dtparam=spi=on"
with open("/boot/config.txt") as f:
lines = f.read().split("\n")
if add_line not in lines:
need_reboot = True
with open("/boot/config.txt", "a") as f:
print(file = f)
print("# SPI is needed for KnitPi", file = f)
print(add_line, file = f)
if not need_reboot:
print("Installation finished.")
else:
print("Installation finished. Changes require you to reboot your Raspberry Pi.")