-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy
183 lines (158 loc) · 6.23 KB
/
deploy
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
#!/bin/bash
# Install (or reinstall to update!) the controller on a fresh raspbian install
#
# If you want to prevent fs resize on first raspbian boot,
# remove "init=/usr/lib/raspi-config/init_resize.sh" from /boot/cmdline.txt
#
# Then ,creating image from SD card and shrink it:
#
# # install pishrink.sh from https://github.com/Drewsif/PiShrink
# wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
# chmod +x pishrink.sh
# sudo mv pishrink.sh /usr/local/bin
# # copy SD to disk
# sudo dd if=/dev/disk2 | pv | dd bs=4m of=Downloads/crazyschool-raspberrypi-<date>.img
# sudo pishrink.sh !$
# scp !$ mien.ch:/var/www/mien.ch/static/crazyschool/.
###
### Script configuration
###
BASEDIR="/home/pi/crazyschool/activity-controller"
echo
echo -e "\033[1m\e[35m* Crazyschool activity-controller software installer for Raspberry Pi !\e[0m"
if [ "$1" == "replace-ini" ]
then
echo
echo -e "\033[33m* File crazyschool.ini will be replaced with version from repository (option: -s replace-ini)\e[0m"
REPLACE_INI=1
else
echo
echo -e "\033[33m* File crazyschool.ini be kept to its local version (to replace with version from repository, use option: -s replace-ini)\e[0m"
REPLACE_INI=0
fi
###
### Bootstrap: updating operating system first
###
# FIXME: on Raspbian 11 Bullseye, the update breaks vlc
# echo
# echo -e "\033[1m* Updating operating system...\e[0m"
# sudo apt-get update && sudo apt-get -y upgrade
# clone git repository, saving the existing in case of failure (eg. invalid credentials)
if ! git --version &> /dev/null
then
sudo apt-get install -y --no-install-recommends git
fi
###
### Installing core software
###
echo -e "\033[1m* Installing crazyschool software, please enter your github credentials below:\e[0m"
TMPDIR=$BASEDIR-tmp
if git clone git@github.com:crazyschool/activity-controller.git $TMPDIR
then
mv $BASEDIR/venv $TMPDIR/venv &> /dev/null # reuse venv if possible
BACKUP=/tmp/crazyschool-deploy-backup-environment
mkdir -p $BACKUP
mv $BASEDIR/environment/activity/* $BACKUP/.
sudo rm -rf $BASEDIR # FIXME: some file belong to root in $BASEDIR, it shouldn't: rm: cannot remove '/home/pi/crazyschool/activity-controller/environment/service/__pycache__/__main__.cpython-37.pyc': Permission denied
mv $TMPDIR $BASEDIR
else
echo -e "\091[m* Failed to clone github repository, exiting.\e[0m"
rm -rf $TMPDIR
exit 1
fi
cd $BASEDIR
# FIXME: explicit master branch checkout
# for update on installations that were using branch dev
git checkout master
# UPGRADE TO 0.0.50: overwrite chrome script
mkdir -p /tmp/crazyschool-environment-activity-backup
cp -r $BASEDIR/environment/activity/. /tmp/crazyschool-environment-activity-backup/.
#
cp -r $BACKUP/* $BASEDIR/environment/activity/.
rm -rf $BACKUP
# UPGRADE TO 0.0.50
cp /tmp/crazyschool-environment-activity-backup/qcm/chrome $BASEDIR/environment/activity/qcm/chrome
###
### System infrastructure
###
echo
echo -e "\033[1m* Installing dependencies... \e[0m "
PKGS=() # collecting packages names and installiing them at once speeds up the process
sudo touch /boot/ssh
PKGS+=(python3 python3-pip python3-venv python3-dev gcc) # python infrastructure
PKGS+=(vim screen) # nice to have
# per activity dependencies
PKGS+=(xserver-xorg xserver-xorg-legacy x11-xserver-utils xinit openbox chromium-browser sed) # activity: qcm
PKGS+=(mpg123) # activity: bell
PKGS+=(vlc) # activity: videoplay
# install all packages at once
sudo apt-get install -y --no-install-recommends ${PKGS[*]}
sudo apt-get autoremove -y
#sudo apt-get clean && sudo rm -r /var/lib/apt/lists/* # cleaning
###
### Dependencies configuration installation
###
# activity: QCM
# install GUI (X and chrome browser)
# from https://die-antwort.eu/techblog/2017-12-setup-raspberry-pi-for-kiosk-mode/
# allow starting x as user: https://gist.github.com/alepez/6273dc5220c1c5ec5f3f126e739d58bf
#sudo usermod -a -G tty pi # not needed
sudo cp /etc/X11/Xwrapper.config /etc/X11/Xwrapper.config.orig
sudo sed -i 's/allowed_users=console/allowed_users=anybody/g' /etc/X11/Xwrapper.config
# TODO: fix locale
# TODO in /boot/config.txt:
# - force HDMI video output
# - force JACK audio output
# - raspi memory split ?
###
### Activity-controller software deployment and system setup
###
# download activity-controller software: done in Boostrap section above
# make venv and install requirements.txt
if [ ! -d "$BASEDIR/venv" ]
then
echo "Creating python venv at $BASEDIR/venv"
python3 -m venv $BASEDIR/venv
fi
echo "Updating python venv"
. $BASEDIR/venv/bin/activate && pip3 install -r requirements.txt
deactivate
# create crazyschool configuration file on /boot partition
if [[ ! -f "/boot/crazyschool.ini" || $REPLACE_INI == 1 ]]
then
echo "Creating default /boot/crazyschool.ini"
sudo cp $BASEDIR/environment/crazyschool.ini.example /boot/crazyschool.ini
fi
# TODO: set static IP for first access
TIMEZONE="Europe/Paris"
echo
echo -e "\033[1m* Setting timezone to $TIMEZONE\e[0m "
sudo timedatectl set-timezone $TIMEZONE
# setup systemd services
echo -e "\033[1m* Setting up systemd services...\e[0m "
echo "Updating services definitions"
# sudo rm -rf /etc/systemd/system/crazyschool* # Note: this erases `crazyschool.device@historifrise`
sudo cp $BASEDIR/environment/systemd/services/* /etc/systemd/system/.
sudo systemctl daemon-reload
echo "Configuring journalctl for logs persistence and file size limit"
sudo mkdir -p /etc/systemd/journald.conf.d
sudo su -c 'echo "[Journal]
Storage=persistent
SystemMaxFileSize=500M" > /etc/systemd/journald.conf.d/crazyschool.conf'
# enable crazyschool services manager
echo "Running crazyschool.service to enable services defined in crazyschool.ini"
sudo systemctl start crazyschool.service
sleep 2
echo "Restarting services"
sudo systemctl restart crazyschool.* systemd-journald.service
# all good
echo
echo -e "\033[1m* Done.\e[0m "
echo "Open http://$(hostname -I | cut -d ' ' -f 1):8000 to access this activity-controller web interface"
echo "Edit /boot/crazyschool.ini to customize configuration"
echo "View running services by typing: systemctl list-units crazyschool.*"
echo "View logs by typing: journalctl -n100 -f -u crazyschool.*"
echo
# Write before login message
# TODO: add to /etc/issue ?
# https://superuser.com/questions/290294/how-to-display-a-message-before-login