-
Notifications
You must be signed in to change notification settings - Fork 0
/
mothpi.sh
executable file
·154 lines (126 loc) · 3.89 KB
/
mothpi.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
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
#!/bin/bash
# 2021, Ludwig Kürzinger, Technische Universität München
# exit on error
set -e
BASEDIR=$(dirname "$0")
BASEDIR=$(realpath "$BASEDIR")
CONFIGPATH=${HOME}/.mothpi
SYSTEMD_ENV_PATH=$(realpath $HOME)"/.config/environment.d/mothpi.conf"
help() {
PROGRAM=$(basename "$0")
cat >&2 <<-_EOF
DESCRIPTION
Automated MothPi manager
USAGE
USAGE
${PROGRAM} <mode>
mode Select script functionality
Modes
serve Run Mothpi as a service
stop Stop all services
run Run Mothpi standalone
selfcheck Perform an installation check
update Download mothpi updates
sync Upload all moth pictures
tunnel Open reverse SSH tunnel
init Install mothpi-programm
Only run this the first time
uninstall Remove all service links
_EOF
exit 0
}
init(){
# copy systemd unit files
echo "Copy systemd unit files"
mkdir -p ~/.config/systemd/user/
cp -uv ${BASEDIR}/systemd/*.service ~/.config/systemd/user/
cp -uv ${BASEDIR}/systemd/*.timer ~/.config/systemd/user/
echo "Save configuration."
touch ~/.mothpi
mkdir -p ~/.config/environment.d/
cat <<EOF > ${SYSTEMD_ENV_PATH}
MOTHPI_BASEDIR=${BASEDIR}
MOTHPI_CONFIG=${CONFIGPATH}
MOTHPI_PICTURES_DIR=/home/pi/pics/
MOTHPI_REMOTE_PORT=10022
MOTHPI_SERVER_PORT=58022
MOTHPI_SERVER_STORAGE_DIR=/storage/remote/dl.visammod/mothpi/
EOF
# syncing is done with:
# rsync -r --exclude="/*/" -vz -e "ssh -p ${MOTHPI_SERVER_PORT}" --remove-source-files --include='*.jpg' ${MOTHPI_PICTURES_DIR} ${MOTHPI_SERVER_ADRESS}:${MOTHPI_SERVER_STORAGE_DIR}
echo "Enable service"
systemctl --user enable mothpi.service
systemctl --user enable sshtunnel.service
systemctl --user enable uploader.timer
echo "reboot for changes to take effect"
}
selfcheck(){
# list directories
PROGRAM=$(basename "$0")
echo "Starting self check of ${PROGRAM}"
echo "Running from: ${BASEDIR}"
# check systemd services and timers
echo "Systemd config:"
cat ${SYSTEMD_ENV_PATH}
echo "Mothpi service status: "
systemctl --user status mothpi.service || true
systemctl --user status sshtunnel.service || true
systemctl --user status uploader.timer || true
echo "active timers:"
systemctl list-timers --all --user
}
update(){
cd ~/mothpi
echo "-- Update repository (and overwrite all changes!)"
git fetch origin master
git reset --hard origin/master
# soft: git pull origin master
echo "-- Stop systemd services"
systemctl --user stop mothpi.service
echo "-- Update systemd files"
cp -uv ${BASEDIR}/systemd/*.service ~/.config/systemd/user/
cp -uv ${BASEDIR}/systemd/*.timer ~/.config/systemd/user/
echo "-- Restart systemd services, reboot or run:"
echo "systemctl --user start mothpi.service"
systemctl --user daemon-reload
}
sync(){
echo "To upload pictures, run:"
echo "systemctl --user start uploader.timer"
}
run(){
# run mothpi
python3 ${BASEDIR}/mothpi/main.py
}
serve(){
echo "To run mothpi as a service: "
echo "systemctl --user start mothpi.service"
echo "to stop: "
echo "systemctl --user stop mothpi.service"
echo "to obtain logs:"
echo "journalctl --user -u mothpi.service"
}
tunnel(){
echo "To create the reverse ssh tunnel, run:"
echo "systemctl --user start sshtunnel.service"
}
## Application menu
if [[ $1 == "init" ]]; then
init
elif [[ $1 == "selfcheck" ]]; then
selfcheck
elif [[ $1 == "update" ]]; then
update
elif [[ $1 == "sync" ]]; then
sync
elif [[ $1 == "run" ]]; then
run
elif [[ $1 == "serve" ]]; then
serve
elif [[ $1 == "tunnel" ]]; then
tunnel
else
help
fi
echo "$(basename "$0") done."