forked from bitfocus/companion-pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sh
executable file
·92 lines (74 loc) · 2.64 KB
/
update.sh
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
#!/usr/bin/env bash
set -e
# this is the bulk of the update script
# It is a separate file, so that the freshly cloned copy is invoked, not the old copy
# imitiate the fnm setup done in .bashrc
export FNM_DIR=/opt/fnm
export PATH=/opt/fnm:$PATH
eval "`fnm env`"
# update companion soruce
cd /usr/local/src/companion
git fetch --all
# The version can be the first argument, or we can prompt for it
SELECTED_REF=$1
if [ -z "$SELECTED_REF" ]; then
# Run interactive version picker
yarn --cwd "/usr/local/src/companionpi/update-prompt" install
node "/usr/local/src/companionpi/update-prompt/main.js"
# Get result
if [ -f /tmp/companion-version-selection ]; then
SELECTED_REF=$(cat /tmp/companion-version-selection)
rm /tmp/companion-version-selection 2&>/dev/null || true
fi
fi
if [ -n "$SELECTED_REF" ]; then
# companion is not safe to be started
touch /usr/local/src/companion/UPDATE_IN_PROGRESS
echo "Switching to $SELECTED_REF"
# switch to the new ref
git checkout $SELECTED_REF
GIT_BRANCH=$(git branch --show-current)
if [[ "$GIT_BRANCH" != "" ]]; then
# only do a pull if on a branch
git pull
fi
# update the node version
fnm use --install-if-missing
fnm default $(fnm current)
npm --unsafe-perm install -g yarn
# make sure there is a swap file in case there is not enough memory
SWAPFILE="/swapfile-upgrade"
if [ ! -f "$SWAPFILE" ]; then
fallocate -l 2G $SWAPFILE
chmod 600 $SWAPFILE
mkswap $SWAPFILE
fi
swapon $SWAPFILE || true
# install dependencies
yarn config set network-timeout 100000 -g
export NODE_OPTIONS=--max-old-space-size=8192 # some pi's run out of memory
yarn update
# swap is no longer needed
swapoff $SWAPFILE || true
# companion is safe to be started
rm /usr/local/src/companion/UPDATE_IN_PROGRESS || true
else
echo "Skipping update"
fi
# update some tooling
cd /usr/local/src/companionpi
cp 50-companion.rules /etc/udev/rules.d/
udevadm control --reload-rules
cp 090-companion_sudo /etc/sudoers.d/
adduser -q companion gpio
adduser -q companion dialout
# update startup script
cp companion.service /etc/systemd/system
systemctl daemon-reload
# install some scripts
ln -s -f /usr/local/src/companionpi/companion-license /usr/local/bin/companion-license
ln -s -f /usr/local/src/companionpi/companion-help /usr/local/bin/companion-help
ln -s -f /usr/local/src/companionpi/companion-update /usr/local/sbin/companion-update
ln -s -f /usr/local/src/companionpi/companion-reset /usr/local/sbin/companion-reset
# install the motd
ln -s -f /usr/local/src/companionpi/motd /etc/motd