-
Notifications
You must be signed in to change notification settings - Fork 0
/
pistar-update
287 lines (256 loc) · 11.1 KB
/
pistar-update
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
#!/bin/bash
#
###############################################################################
# #
# Pi-Star Auto Update Tool #
# #
# Version 3.5, Code, Design and Development by Andy Taylor (MW0MWZ). #
# #
# Make it simple to update the OS. #
# #
###############################################################################
#
if [ "$(id -u)" != "0" ]; then
echo -e "You need to be root to run this command...\n"
exit 1
fi
exec 200>/var/lock/pistar-update.lock || exit 1
if ! flock -n 200 ; then
echo -e "Another instance is already running...\n"
exit 1
fi
git_checkUpdateRequired() {
# Set the function variables
gitFolder=${1}
gitRemoteURL=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git config --get remote.origin.url)
# Git check / update function
gitStatusRemote=$(git ls-remote --heads ${gitRemoteURL} | grep master | cut -c 1-7)
gitStatusLocal=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git log --pretty=format:"%h" -1 | cut -c 1-7)
# Return the output
if [[ ${gitStatusRemote} != ${gitStatusLocal} ]]; then
echo "1"
else
echo "0"
fi
}
git_update() {
# Set the function variables
gitFolder=${1}
# Handle the special case for /usr/loca/sbin
if [[ ${gitFolder} == "/usr/local/sbin" ]]; then
# Assume unchanged for pistar-upnp.service
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git update-index --assume-unchanged pistar-upnp.service
fi
if [[ $(git_checkUpdateRequired ${gitFolder}) -gt 0 ]]; then
echo "Updating ${gitFolder}..."
# If this script is updated, re-run the update with the new version.
if [[ ${gitFolder} == "/usr/local/sbin" ]]; then
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git fetch
if [ "$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git diff --name-only origin/master 2>/dev/null | grep pistar-update 2>/dev/null)" = "pistar-update" ]; then
echo "Found a new version of pistar-update..."
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git pull origin master
if [[ $(git_checkUpdateRequired ${gitFolder}) -gt 0 ]]; then
echo "Update to new version of pistar-update was not successfull, forcing update..."
rm -rf ${gitFolder}/pistar-upnp.service
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git reset --hard origin/master
fi
echo "Restarting update process with the new version..."
exec "$0" "$@"
exit 1
fi
fi
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git pull origin master
# Re-check that the updates are now good
if [[ $(git_checkUpdateRequired ${gitFolder}) -gt 0 ]]; then
if [[ ${gitFolder} == "/usr/local/sbin" ]]; then
rm -rf ${gitFolder}/pistar-upnp.service
fi
echo "Updates were not successfull, reverting to Pi-Star original files..."
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git reset --hard origin/master
fi
else
echo "No updates for ${gitFolder} available"
fi
}
service_handle() {
# What do we want do to?
doWhat=${1}
systemctl ${doWhat} pistar-watchdog.service > /dev/null 2>&1
systemctl ${doWhat} pistar-remote.service > /dev/null 2>&1
systemctl ${doWhat} dmrgateway.service > /dev/null 2>&1
systemctl ${doWhat} dapnetgateway.service > /dev/null 2>&1
systemctl ${doWhat} ircddbgateway.service > /dev/null 2>&1
systemctl ${doWhat} timeserver.service > /dev/null 2>&1
systemctl ${doWhat} ysfgateway.service > /dev/null 2>&1
systemctl ${doWhat} ysf2dmr.service > /dev/null 2>&1
systemctl ${doWhat} ysf2nxdn.service > /dev/null 2>&1
systemctl ${doWhat} ysf2p25.service > /dev/null 2>&1
systemctl ${doWhat} ysfparrot.service > /dev/null 2>&1
systemctl ${doWhat} dmr2ysf.service > /dev/null 2>&1
systemctl ${doWhat} dmr2nxdn.service > /dev/null 2>&1
if [ -f /lib/systemd/system/m17gateway.service ]; then
systemctl ${doWhat} m17gateway.service > /dev/null 2>&1
fi
systemctl ${doWhat} p25gateway.service > /dev/null 2>&1
systemctl ${doWhat} p25parrot.service > /dev/null 2>&1
systemctl ${doWhat} nxdngateway.service > /dev/null 2>&1
systemctl ${doWhat} nxdn2dmr.service > /dev/null 2>&1
systemctl ${doWhat} nxdnparrot.service > /dev/null 2>&1
systemctl ${doWhat} dstarrepeater.service > /dev/null 2>&1
systemctl ${doWhat} mmdvmhost.service > /dev/null 2>&1 && sleep 2 > /dev/null 2>&1
}
main_function() {
# Make the disk writable
mount -o remount,rw /
if [ -d /boot/firmware ]; then
mount -o remount,ro /boot/firmware
else
mount -o remount,ro /boot
fi
if [ -t 1 ]; then
# This is running from a terminal, so it should be safe to update the OS
echo -e "Updating OS...\n"
apt-get update -y --allow-releaseinfo-change
apt-get upgrade --fix-missing --fix-broken -y
apt-get clean
echo "Done"
echo "Checking nginx config"
if ! [ -f /etc/systemd/system/nginx.service.d/override.conf ]; then
if ! [ $(cat /lib/systemd/system/nginx.service | grep -o "mkdir") ]; then
sed -i '\/PIDFile=\/run\/nginx.pid/a ExecStartPre=\/bin\/mkdir -p \/var\/log\/nginx' /lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl restart nginx.service
echo "nginx config has been repaired - re-running pistar-update"
exec "$0" "$@"
exit 1
fi
fi
fi
echo "Stopping Services..."
service_handle stop
echo "Done"
echo "Updating DV Binaries..."
git_update /usr/local/bin
echo "Done"
# echo "Updating Pi-Star Binaries..."
# git_update /usr/local/sbin
# echo "Done"
mount -o remount,rw /
if [ -d /boot/firmware ]; then
mount -o remount,ro /boot/firmware
else
mount -o remount,ro /boot
fi
echo "Downloading modified HostFilesUpdate.sh..."
# Get the hardware type, this may be important later (RPi | NanoPi | OdroidXU4)
pistarHardware=$(awk -F "= " '/Hardware/ {print $2}' /etc/pistar-release)
if [ "${pistarHardware}" == "NanoPi" ]; then
curl --fail -o /usr/local/sbin/HostFilesUpdate.sh -s https://s3.qra-team.online/PiStar/HostFilesUpdate-nano
else
curl --fail -o /usr/local/sbin/HostFilesUpdate.sh -s https://s3.qra-team.online/PiStar/HostFilesUpdate-rpi
fi
echo "------------"
/usr/local/sbin/HostFilesUpdate.sh
echo "Done"
# Pre-Fix some config in MMDVMHost for update purposes.
mmdvmHostVer=`MMDVMHost -v | awk '{print $3}' | cut -c 1-8`
needsUpdate=`grep -c Gwy /etc/mmdvmhost`
if [ ${mmdvmHostVer} \> 20171031 ] && [ ${needsUpdate} \> 0 ]; then
# Config needs to be updated, add in the changes here
sed -i "/GwyAddress=/c\\GatewayAddress=127.0.0.1" /etc/mmdvmhost
sed -i "/GwyPort=/c\\GatewayPort=4200" /etc/mmdvmhost
fi
needsTypeLine=$(sed -n '/^\[DMR Network\]/,/^\[/p' /etc/mmdvmhost | grep "^Type=" | wc -l)
dmrAddress=$(sed -n '/^\[DMR Network\]/,/^\[/p' /etc/mmdvmhost | grep "^Address=" | awk -F "=" '/Address=/ {print $2}')
if [ ${needsTypeLine} \< 1 ]; then
if [[ "${dmrAddress}" == "127.0.0.1" ]]; then
sed -i 's/\[DMR Network\]/\[DMR Network\]\nType=Gateway/g' /etc/mmdvmhost
else
sed -i 's/\[DMR Network\]/\[DMR Network\]\nType=Direct/g' /etc/mmdvmhost
fi
else
if [[ "${dmrAddress}" == "127.0.0.1" ]]; then
sed -i "/Type=Dire/c\\Type=Gateway" /etc/mmdvmhost
else
sed -i "/Type=Gate/c\\Type=Direct" /etc/mmdvmhost
fi
fi
# Fix up new P25Gateway Config Hostfile setup
if [[ $(/usr/local/bin/P25Gateway --version | awk '{print $3}' | cut -c -8) -gt "20180108" ]]; then
sed -i 's/Hosts=\/usr\/local\/etc\/P25Hosts.txt/HostsFile1=\/usr\/local\/etc\/P25Hosts.txt\nHostsFile2=\/usr\/local\/etc\/P25HostsLocal.txt/g' /etc/p25gateway
fi
if [ ! -f /root/P25Hosts.txt ]; then
touch /root/P25Hosts.txt
fi
# Fix up new NXDNGateway Config Hostfile setup
if [[ $(/usr/local/bin/NXDNGateway --version | awk '{print $3}' | cut -c -8) -gt "20180801" ]]; then
sed -i 's/HostsFile=\/usr\/local\/etc\/NXDNHosts.txt/HostsFile1=\/usr\/local\/etc\/NXDNHosts.txt\nHostsFile2=\/usr\/local\/etc\/NXDNHostsLocal.txt/g' /etc/nxdngateway
fi
if [ ! -f /root/NXDNHosts.txt ]; then
touch /root/NXDNHosts.txt
fi
if [ ! -f /usr/local/etc/NXDNHostsLocal.txt ]; then
touch /usr/local/etc/NXDNHostsLocal.txt
fi
# If we are ready to use the new DMRGateway
if [[ $(/usr/local/bin/DMRGateway --version | awk '{print $3}' | cut -c -8) -gt "20170924" ]] && [[ $(grep -c "\[DMR Network 3\]" /etc/dmrgateway) -eq "1" ]] && [[ ! -f /usr/local/etc/DMR_Audio/no_NO.indx ]]; then
curl --fail -o /usr/local/etc/DMR_Audio/en_US.ambe -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/en_US.ambe
curl --fail -o /usr/local/etc/DMR_Audio/en_US.indx -s https://raw.githubusercontent.com/g4klx/DMRGateway/master/Audio/en_US.indx
fi
# I previously had the wrong URL for the P25 Audio files; remove the broken downloads.
if [[ -f /usr/local/etc/P25_Audio/en_GB.imbe ]]; then
testFileIMBE=`file -i /usr/local/etc/P25_Audio/en_GB.imbe | cut -d " " -f2`
if [[ $testFileIMBE == text* ]]; then
rm -rf /usr/local/etc/P25_Audio
fi
fi
# Download the correct P25 Audio Files
if [[ ! -d /usr/local/etc/P25_Audio ]]; then
echo "Downloading P25 Voice Files..."
mkdir /usr/local/etc/P25_Audio
curl --fail -o /usr/local/etc/P25_Audio/en_US.imbe -s https://raw.githubusercontent.com/g4klx/P25Clients/master/P25Gateway/Audio/en_US.imbe
curl --fail -o /usr/local/etc/P25_Audio/en_US.indx -s https://raw.githubusercontent.com/g4klx/P25Clients/master/P25Gateway/Audio/en_US.indx
echo "Done"
fi
# Download the correct M17 Audio Files
if [[ ! -d /usr/local/etc/M17_Audio ]]; then
echo "Downloading M17 Voice Files..."
mkdir /usr/local/etc/M17_Audio
curl --fail -o /usr/local/etc/M17_Audio/en_US.indx -s https://raw.githubusercontent.com/g4klx/M17Gateway/main/Audio/en_US.indx
curl --fail -o /usr/local/etc/M17_Audio/en_US.m17 -s hhttps://raw.githubusercontent.com/g4klx/M17Gateway/main/Audio/en_US.m17
echo "Done"
fi
if [[ $(grep "\[Voice\]" /etc/p25gateway | wc -l) -eq 0 ]]; then
echo "Updating P25Gateway config..."
echo "" >> /etc/p25gateway
sed -i '$a[Voice]\nEnabled=1\nLanguage=en_GB\nDirectory=/usr/local/etc/P25_Audio\n' /etc/p25gateway
echo "Done"
fi
if [[ $(cat /etc/dstarrepeater | grep -o "mmdvmRXInvert" | wc -l) -eq 0 ]]; then
echo "Adding better MMDVM Config to /etc/dstarrepeater"
echo "mmdvmRXInvert=0" >> /etc/dstarrepeater
echo "mmdvmTXInvert=0" >> /etc/dstarrepeater
echo "mmdvmPTTInvert=0" >> /etc/dstarrepeater
echo "mmdvmTXDelay=50" >> /etc/dstarrepeater
echo "mmdvmRXLevel=100" >> /etc/dstarrepeater
echo "mmdvmTXLevel=100" >> /etc/dstarrepeater
fi
echo "Starting Services..."
service_handle start
echo "Done"
echo "Updates complete, syncing disk cache before making the disk Read-Only"
# Make the disk read-only
/bin/sync
/bin/sync
/bin/sync
mount -o remount,ro /
if [ -d /boot/firmware ]; then
mount -o remount,ro /boot/firmware
else
mount -o remount,ro /boot
fi
# Tell the user we are done
echo "Finished"
}
main_function >> /var/log/pi-star/pi-star_update.log 2>&1
exit 0