forked from AndyTaylorTweet/Pi-Star_Binaries_sbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpistar-update-git
executable file
·201 lines (179 loc) · 7.39 KB
/
pistar-update-git
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
#!/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. #
# #
# (stripped down to run only the GIT updates) #
# #
###############################################################################
#
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)
gitBranch=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git rev-parse --abbrev-ref HEAD)
# Git check / update function
gitStatusRemote=$(git ls-remote --heads ${gitRemoteURL} | grep master | awk {'print $1'})
gitStatusLocal=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git log --pretty=format:"%H" -1)
# Return the output
if [[ ${gitStatusRemote} != ${gitStatusLocal} ]]; then
echo "1"
else
echo "0"
fi
}
git_update() {
# Set the function variables
gitFolder=${1}
gitBranch=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git rev-parse --abbrev-ref HEAD)
# 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 successful, 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 successful, reverting to Pi-Star original files..."
if [[ ${gitFolder} == "/var/www/dashboard" ]]; then
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git stash push -m "save Pi-Star user config files" -- ${gitFolder}/config/config.php ${gitFolder}/config/ircddblocal.php ${gitFolder}/config/language.php
fi
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git reset --hard origin/master
if [[ ${gitFolder} == "/var/www/dashboard" ]]; then
git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git stash pop
fi
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
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} aprsgateway.service > /dev/null 2>&1
systemctl ${doWhat} dstarrepeater.service > /dev/null 2>&1
systemctl ${doWhat} nextiondriver.service > /dev/null 2>&1
systemctl ${doWhat} m17gateway.service > /dev/null 2>&1
systemctl ${doWhat} mmdvmhost.service > /dev/null 2>&1 && sleep 2 > /dev/null 2>&1
}
update_bin() {
gitFolder=/usr/local/bin
gitUrl=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git config --get remote.origin.url)
gitUrl=${gitUrl/https:\/\/github.com/}
gitUrl=${gitUrl/.git/}
echo -n "Updating DV Binaries ($gitUrl) ... "
git_update /usr/local/bin
echo -e "Done \n"
}
update_sbin() {
gitFolder=/usr/local/sbin
gitUrl=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git config --get remote.origin.url)
gitUrl=${gitUrl/https:\/\/github.com/}
gitUrl=${gitUrl/.git/}
echo -n "Updating Pi-Star Binaries ($gitUrl) ... "
git_update /usr/local/sbin
echo -e "Done \n"
}
update_dash() {
gitFolder=/var/www/dashboard
gitUrl=$(git --work-tree=${gitFolder} --git-dir=${gitFolder}/.git config --get remote.origin.url)
gitUrl=${gitUrl/https:\/\/github.com/}
gitUrl=${gitUrl/.git/}
echo -n "Updating Dashboard ($gitUrl) ... "
git_update /var/www/dashboard
echo -e "Done \n"
}
main_function() {
# Make the disk writable
xro=$(sed -n "s/\/dev\/.* \/ ext4 \(r[ow]\).*/\1/p" /proc/mounts)
if [ "$xro" == "ro" ]; then
sudo mount -o remount,rw /
fi
echo $(date "+%a %D - %l:%M:%S %p %Z") = $(date -u "+%l:%M:%S %p %Z - %a %D") "("$(date "+%z")")"
echo "Stopping Services..."
service_handle stop
echo -e "Done \n"
case ${1:-all} in
bin|all) update_bin;;&
sbin|all) update_sbin;;&
dash|all) update_dash;;
*) ;;
esac
echo "Updating PiStar-Firewall..."
pistar-firewall > /dev/null 2>&1
echo -e "Done \n"
echo "Starting Services..."
service_handle start
echo -e "Done \n"
echo "Updates complete, syncing disk cache before making the disk Read-Only"
# Make the disk read-only
/bin/sync
/bin/sync
/bin/sync
if [ "$xro" == "ro" ]; then
sudo mount -o remount,ro /
fi
# Tell the user we are done
echo "Finished"
}
if [ -t 1 ]; then
# run via terminal, output to screen and log file
main_function ${1} | sudo tee -a /var/log/pi-star/pi-star_update.log
else
# if not run via terminal, log everything into a log file
main_function >> /var/log/pi-star/pi-star_update.log 2>&1
fi
exit 0