forked from PhotonVision/photon-image-modifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·373 lines (322 loc) · 10.3 KB
/
install.sh
File metadata and controls
executable file
·373 lines (322 loc) · 10.3 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
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
#!/bin/bash
# Exit on errors
set -e
needs_arg() {
if [ -z "$OPTARG" ]; then
die "Argument is required for --$OPT option" \
"See './install.sh -h' for more information."
fi;
}
die() {
for arg in "$@"; do
echo "$arg" 1>&2
done
exit 1
}
debug() {
if [ -z "$QUIET" ] ; then
for arg in "$@"; do
echo "$TEST$arg"
done
fi
}
package_is_installed(){
dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -q "ok installed"
}
install_if_missing() {
if package_is_installed "$1" ; then
debug "Found existing $1. Skipping..."
# Always mark our upstream apt deps as held back, which will prevent the package
# from being automatically installed, upgraded or removed
if [[ -z $TEST ]]; then
apt-mark manual "$1"
fi
return
fi
debug "Installing $1..."
if [[ -z $TEST ]]; then
apt-get install --yes "$1"
# Always mark our upstream apt deps as held back, which will prevent the package
# from being automatically installed, upgraded or removed
apt-mark manual "$1"
fi
debug "$1 installation complete."
}
get_versions() {
PHOTON_VISION_RELEASES="$(wget -qO- https://api.github.com/repos/photonvision/photonvision/releases?per_page=$1)"
PHOTON_VISION_VERSIONS=$(get_photonvision_releases | \
sed -En 's/\"tag_name\": \"(.+)\",/\1/p' | \
sed 's/^[[:space:]]*//'
)
echo "$PHOTON_VISION_VERSIONS"
}
is_chroot() {
if systemd-detect-virt -r; then
return 0
else
return 1
fi
}
help() {
cat << EOF
This script installs Photonvision.
It must be run as root.
Syntax: sudo ./install.sh [options]
options:
-h, --help
Display this help message.
-l [count], --list-versions=[count]
Lists the most recent versions of PhotonVision.
Count: Number of recent versions to show, max value is 100.
Default: 30
-v <version>, --version=<version>
Specifies which version of PhotonVision to install.
If not specified, the latest stable release is installed.
-a <arch>, --arch=<arch>
Install PhotonVision for the specified architecture.
Supported values: aarch64, x86_64
-m [option], --install-nm=[option]
Controls NetworkManager installation (Ubuntu only).
Options: "yes", "no", "ask".
Default: "ask" (unless -q or --quiet is specified, then "no").
"ask" prompts for installation. Ignored on other distros.
-n, --no-networking
Disable networking. This will also prevent installation of
NetworkManager, overriding -m,--install-nm.
-q, --quiet
Silent install, automatically accepts all defaults. For
non-interactive use. Makes -m,--install-nm default to "no".
-t, --test
Run in test mode. All actions that make chnages to the system
are suppressed.
EOF
}
# Exit with message if attempting to run on SystemCore
if grep -iq "systemcore" /etc/os-release; then
die "This install script does not work on Systemcore."
fi
INSTALL_NETWORK_MANAGER="ask"
VERSION="latest"
while getopts "hlv:a:mnqt-:" OPT; do
if [ "$OPT" = "-" ]; then
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
else
nextopt=${!OPTIND} # check for an optional argument followinng a short option
if [[ -n $nextopt && $nextopt != -* ]]; then
OPTIND=$((OPTIND + 1))
OPTARG=$nextopt
fi
fi
case "$OPT" in
h | help)
help
exit 0
;;
l | list-versions)
COUNT=${OPTARG:-30}
get_versions "$COUNT"
exit 0
;;
v | version)
needs_arg
VERSION=${OPTARG}
;;
a | arch) needs_arg; ARCH=$OPTARG
;;
m | install-nm)
INSTALL_NETWORK_MANAGER="$(echo "${OPTARG:-yes}" | tr '[:upper:]' '[:lower:]')"
case "$INSTALL_NETWORK_MANAGER" in
yes)
;;
no)
;;
ask)
;;
* )
die "Valid options for -m, --install-nm are: 'yes', 'no', and 'ask'"
;;
esac
;;
n | no-networking) DISABLE_NETWORKING="true"
;;
q | quiet) QUIET="true"
;;
t | test) TEST="[TEST]:"
;;
\?) # Handle invalid short options
die "Error: Invalid option -$OPTARG" \
"See './install.sh -h' for more information."
;;
* ) # Handle invalid long options
die "Error: Invalid option --$OPT" \
"See './install.sh -h' for more information."
;;
esac
done
debug "This is the installation script for PhotonVision."
if [[ "$(id -u)" != "0" && -z $TEST ]]; then
die "This script must be run as root"
fi
if is_chroot ; then
debug "Running in chroot. Arch should be specified."
fi
if [[ -z "$ARCH" ]]; then
debug "Arch was not specified. Inferring..."
ARCH=$(uname -m)
debug "Arch was inferred to be $ARCH"
fi
ARCH_NAME=""
if [ "$ARCH" = "aarch64" ]; then
ARCH_NAME="linuxarm64"
elif [ "$ARCH" = "armv7l" ]; then
die "ARM32 is not supported by PhotonVision. Exiting."
elif [ "$ARCH" = "x86_64" ]; then
ARCH_NAME="linuxx64"
else
die "Unsupported or unknown architecture: '$ARCH'." \
"Please specify your architecture using: ./install.sh -a <arch> " \
"Run './install.sh -h' for more information."
fi
debug "Installing for platform $ARCH"
# make sure that we are downloading a valid version
if [ "$VERSION" = "latest" ] ; then
RELEASE_URL="https://api.github.com/repos/photonvision/photonvision/releases/latest"
else
RELEASE_URL="https://api.github.com/repos/photonvision/photonvision/releases/tags/$VERSION"
fi
DOWNLOAD_URL=$(curl -sk "$RELEASE_URL" |
grep "browser_download_url.*$ARCH_NAME.jar" |
cut -d : -f 2,3 |
tr -d '"'
)
if [[ -z $DOWNLOAD_URL ]] ; then
die "PhotonVision '$VERSION' is not available for $ARCH_NAME!" \
"See ./install --list-versions for a list of available versions."
fi
DISTRO=$(lsb_release -is)
# Only ask if it makes sense to do so.
# i.e. the distro is Ubuntu, you haven't requested disabling networking,
# and you have requested a quiet install.
if [[ "$INSTALL_NETWORK_MANAGER" == "ask" ]]; then
if [[ "$DISTRO" != "Ubuntu" || -n "$DISABLE_NETWORKING" || -n "$QUIET" ]] ; then
INSTALL_NETWORK_MANAGER="no"
fi
fi
if [[ "$INSTALL_NETWORK_MANAGER" == "ask" ]]; then
debug ""
debug "Photonvision uses NetworkManager to control networking on your device."
debug "This could possibly mess up the network configuration in Ubuntu."
read -p "Do you want this script to install and configure NetworkManager? [y/N]: " response
if [[ $response == [yY] || $response == [yY][eE][sS] ]]; then
INSTALL_NETWORK_MANAGER="yes"
fi
fi
debug "Updating package list..."
if [[ -z $TEST ]]; then
apt-get update
fi
debug "Updated package list."
install_if_missing curl
install_if_missing avahi-daemon
install_if_missing cpufrequtils
install_if_missing libatomic1
install_if_missing v4l-utils
install_if_missing sqlite3
install_if_missing openjdk-17-jre-headless
debug "Setting cpufrequtils to performance mode"
if [[ -z $TEST ]]; then
if [ -f /etc/default/cpufrequtils ]; then
sed -i -e 's/^#\?GOVERNOR=.*$/GOVERNOR=performance/' /etc/default/cpufrequtils
else
echo 'GOVERNOR=performance' > /etc/default/cpufrequtils
fi
fi
if [[ "$INSTALL_NETWORK_MANAGER" == "yes" ]]; then
debug "NetworkManager installation requested. Installing components..."
install_if_missing network-manager
install_if_missing net-tools
debug "Configuring..."
if [[ -z $TEST ]]; then
systemctl disable systemd-networkd-wait-online.service
if [[ -d /etc/netplan/ ]]; then
cat > /etc/netplan/00-default-nm-renderer.yaml <<EOF
network:
renderer: NetworkManager
EOF
fi
debug "network-manager installation complete."
fi
fi
debug ""
debug "Installing additional math packages"
if [[ "$DISTRO" = "Ubuntu" && -z $(apt-cache search libcholmod3) ]]; then
debug "Adding jammy to list of apt sources"
if [[ -z $TEST ]]; then
if [[ "$ARCH" = "x86_64" ]]; then
add-apt-repository -y -S 'deb http://security.ubuntu.com/ubuntu jammy main universe'
else
add-apt-repository -y -S 'deb http://ports.ubuntu.com/ubuntu-ports jammy main universe'
fi
fi
fi
install_if_missing libcholmod3
install_if_missing liblapack3
install_if_missing libsuitesparseconfig5
debug ""
debug "Downloading PhotonVision '$VERSION'..."
if [[ -z $TEST ]]; then
mkdir -p /opt/photonvision
cd /opt/photonvision || die "Tried to enter /opt/photonvision, but it was not created."
curl -sk "$RELEASE_URL" |
grep "browser_download_url.*$ARCH_NAME.jar" |
cut -d : -f 2,3 |
tr -d '"' |
wget -qi - -O photonvision.jar
fi
debug "Downloaded PhotonVision."
debug "Creating the PhotonVision systemd service..."
if [[ -z $TEST ]]; then
# service --status-all doesn't list photonvision on OrangePi use systemctl instead:
if [[ $(systemctl --quiet is-active photonvision) = "active" ]]; then
debug "PhotonVision is already running. Stopping service."
systemctl stop photonvision
systemctl disable photonvision
rm /lib/systemd/system/photonvision.service
rm /etc/systemd/system/photonvision.service
systemctl daemon-reload
systemctl reset-failed
fi
cat > /lib/systemd/system/photonvision.service <<EOF
[Unit]
Description=Service that runs PhotonVision
[Service]
WorkingDirectory=/opt/photonvision
# Run photonvision at "nice" -10, which is higher priority than standard
Nice=-10
# for non-uniform CPUs, like big.LITTLE, you want to select the big cores
# look up the right values for your CPU
# AllowedCPUs=4-7
ExecStart=/usr/bin/java -Xmx512m -jar /opt/photonvision/photonvision.jar
ExecStop=/bin/systemctl kill photonvision
Type=simple
Restart=on-failure
RestartSec=1
[Install]
WantedBy=multi-user.target
EOF
if [ "$DISABLE_NETWORKING" = "true" ]; then
sed -i "s/photonvision.jar/photonvision.jar -n/" /lib/systemd/system/photonvision.service
fi
if grep -q "RK3588" /proc/cpuinfo; then
debug "This has a Rockchip RK3588, enabling big cores"
sed -i 's/# AllowedCPUs=4-7/AllowedCPUs=4-7/g' /lib/systemd/system/photonvision.service
fi
cp /lib/systemd/system/photonvision.service /etc/systemd/system/photonvision.service
chmod 644 /etc/systemd/system/photonvision.service
systemctl daemon-reload
systemctl enable photonvision.service
fi
debug "Created PhotonVision systemd service."
debug "PhotonVision installation successful."