forked from rcrum003/HiveControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devupgrade.sh
executable file
·308 lines (267 loc) · 10.3 KB
/
devupgrade.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
#Upgrade script HiveControl updates
# Used when going from version to version
# Includes methods to upgrade databases, as well as pull new files
# Gets the current version installed and brings up to the latest version when running the script
#Move all of this code into a script that checks for new code once a day.
# If new code is available, trigger an alert in the UI. Clicking gives instructions on how to upgrade.
#Get the latest upgrade script
function show_help {
echo "--------------------"
echo "devupgrade help"
echo "--------------------"
echo " Usage: ./devupgrade.sh branch"
echo " Example: ./devupgrade.sh develop"
echo ""
echo "WARNING: DO NOT RUN UNLESS YOU KNOW WHAT THIS SCRIPT DOES"
echo ""
}
branch=$1
if [[ -z "$branch" ]]; then
#echo "you are running from the command line"
#-----------------------------
#Get Variables from input
#----------------------------
#Shows Help if nothing is selected
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
if [ $# -eq 0 ]; then
show_help
exit 1
fi
fi
echo "branch is $branch"
Upgrade_ver="55"
source /home/HiveControl/scripts/hiveconfig.inc
source /home/HiveControl/scripts/data/logger.inc
#Check to see if the update.sh is at the latest version available
DATE=$(TZ=":$TIMEZONE" date '+%F %T')
#Check to see if we are latest version
Installed_Ver=$(cat /home/HiveControl/VERSION)
#Latest_Ver=$(curl -s https://raw.githubusercontent.com/rcrum003/HiveControl/master/VERSION)
#if [[ $(echo "$Installed_Ver == $Latest_Ver" | bc) -eq 1 ]]; then
# echo "Nothing to do, you are at the latest version"
# loglocal "$DATE" UPGRADE WARNING "Upgrade attempted, but nothing to upgrade. Installed is latest"
# echo "Error: Nothing to Upgrade"
# exit
#fi
#Back everything up, just in case (mainly the database)
echo "Backing up Database to hive-data.bckup"
cp /home/HiveControl/data/hive-data.db /home/HiveControl/data/hive-data.bckup
echo "============================================="
# Get the latest code from github into a temporary repository
echo "Getting Branch $branch"
#Remove any remnants of past code upgrades
#rm -rf /home/HiveControl/upgrade
#Make us a fresh directory
#mkdir /home/HiveControl/upgrade
#Start in our directory
cd /home/HiveControl/upgrade/HiveControl
#Get the code
git checkout $branch
exit
#Set some variables
WWWTempRepo="/home/HiveControl/upgrade/HiveControl/www/public_html"
DestWWWRepo="/home/HiveControl/www/public_html"
DestDB="/home/HiveControl/data/hive-data.db"
scriptsource="/home/HiveControl/upgrade/HiveControl/scripts"
scriptDest="/home/HiveControl/scripts"
#Remove some initial installation files from repository for upgrade
#Remove the offending file, since we don't want to upgrade these
rm -rf $WWWTempRepo/include/db-connect.php
rm -rf $WWWTempRepo/data/*
echo "....... Storing it in /home/HiveControl/upgrade"
echo "============================================="
#Upgrade www
echo "Upgrading WWW pages"
cp -Rp $WWWTempRepo/pages/* $DestWWWRepo/pages/
cp -Rp $WWWTempRepo/admin/* $DestWWWRepo/admin/
cp -Rp $WWWTempRepo/include/* $DestWWWRepo/include/
cp -Rp $WWWTempRepo/errors/* $DestWWWRepo/errors/
echo "============================================="
#Upgrade our code
echo "Upgrading our shell scripts"
#cp -R /home/HiveControl/scripts/
rm -rf $scriptsource/hiveconfig.inc
cp -Rp $scriptsource/* $scriptDest/
cd $scriptDest
find . -name '*.sh' -exec chmod u+x {} +
echo "============================================="
#Upgrade our DB
#Get DBVersion
#Get the latest upgrade script
DB_ver=$(cat /home/HiveControl/data/DBVERSION)
DBPatches="/home/HiveControl/upgrade/HiveControl/patches/database"
#Get the version available
DB_latest_ver=$(curl -s https://raw.githubusercontent.com/rcrum003/HiveControl/master/data/DBVERSION)
if [[ $( echo "$DB_ver < $DB_latest_ver" | bc) -eq 1 ]]; then
echo "Upgrading DBs"
if [[ $DB_ver -eq "0" ]]; then
#Upgarding to version 1
echo "Applying DB Ver1 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_6
#Set DB Ver to the next
let DB_ver="1"
fi
if [[ $DB_ver -eq "1" ]]; then
#Upgarding to version 2
echo "Applying DB Ver2 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_7
#Set DB Ver to the next
let DB_ver="2"
fi
if [[ $DB_ver -eq "2" ]]; then
#Upgarding to version 2
echo "Applying DB Ver3 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_8
#Set DB Ver to the next
let DB_ver="3"
fi
if [[ $DB_ver -eq "3" ]]; then
#Upgarding to version 2
echo "Applying DB Ver4 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_9
#Set DB Ver to the next
let DB_ver="4"
fi
if [[ $DB_ver -eq "4" ]]; then
#Upgarding to version 2
echo "Applying DB Ver5 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_10
#Set DB Ver to the next
let DB_ver="5"
fi
if [[ $DB_ver -eq "5" ]]; then
#Upgarding to next version
echo "Applying DB Ver6 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_11
sqlite3 $DestDB "UPDATE allhivedata SET IN_COUNT=0 WHERE OUT_COUNT is null"
sqlite3 $DestDB "UPDATE allhivedata SET OUT_COUNT=0 WHERE OUT_COUNT is null"
#Set DB Ver to the next
let DB_ver="6"
fi
if [[ $DB_ver -eq "6" ]]; then
#Upgarding to next version
echo "Applying DB Ver7 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_12
sqlite3 $DestDB "UPDATE allhivedata SET IN_COUNT=0 WHERE OUT_COUNT is null"
sqlite3 $DestDB "UPDATE allhivedata SET OUT_COUNT=0 WHERE OUT_COUNT is null"
#Set DB Ver to the next
let DB_ver="7"
fi
if [[ $DB_ver -eq "7" ]]; then
#Upgarding to next version
echo "Applying DB Ver8 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_13
#Set DB Ver to the next
#Upgrade Cron for Message Queue and Pollen Counter
#Get Crontab as it is
sudo crontab -l > /home/HiveControl/install/cron/cron1.orig
#Echo our new content into a new crontab file with the old
sudo cat /home/HiveControl/upgrade/HiveControl/patches/cron/CRON_PATCH_1 >> /home/HiveControl/install/cron/cron1.orig
sudo crontab /home/HiveControl/install/cron/cron1.orig
#Copy new images related to this feature set
sudo cp /home/HiveControl/upgrade/HiveControl/www/public_html/images/* /home/HiveControl/www/public_html/images/
let DB_ver="8"
fi
if [[ $DB_ver -eq "8" ]]; then
#Upgarding to next version
echo "Applying DB Ver9 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_14
#Set DB Ver to the next
let DB_ver="9"
fi
if [[ $DB_ver -eq "9" ]]; then
#Upgarding to next version
echo "Applying DB Ver10 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_15
sqlite3 $DestDB < $DBPatches/DB_PATCH_16
#Set DB Ver to the next
let DB_ver="10"
fi
if [[ $DB_ver -eq "10" ]]; then
echo "Ok, so not a DB upgrade, but only needs to be once and this was the best place for it"
sudo echo "www-data ALL=(ALL) NOPASSWD: /usr/local/bin/hx711" >> /etc/sudoers
let DB_ver="11"
fi
if [[ $DB_ver -eq "11" ]]; then
echo "Applying DB Ver12 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_17
let DB_ver="12"
fi
if [[ $DB_ver -eq "12" ]]; then
echo "Applying DB Ver13 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_18
sudo crontab -l > /home/HiveControl/install/cron/cron2.orig
#Echo our new content into a new crontab file with the old
sudo cat /home/HiveControl/upgrade/HiveControl/patches/cron/CRON_PATCH_2 >> /home/HiveControl/install/cron/cron2.orig
sudo crontab /home/HiveControl/install/cron/cron2.orig
#Copy new images related to this feature set
sudo cp /home/HiveControl/upgrade/HiveControl/www/public_html/images/* /home/HiveControl/www/public_html/images/
let DB_ver="13"
fi
if [[ $DB_ver -eq "13" ]]; then
echo "Applying DB Ver14 Upgrades"
sqlite3 $DestDB < $DBPatches/DB_PATCH_19
sudo sqlite3 $DestDB "UPDATE hiveconfig SET RUN=\"yes\";"
sudo cp /home/HiveControl/upgrade/HiveControl/www/public_html/images/* /home/HiveControl/www/public_html/images/
let DB_ver="14"
fi
else
echo "Skipping DB, no new database upgrades available"
fi
sudo echo $DB_ver > /home/HiveControl/data/DBVERSION
#Update install stuff
sudo cp -Rp /home/HiveControl/upgrade/HiveControl/install/* /home/HiveControl/install/
if [[ "$Installed_Ver" < "1.62" ]]; then
#Only run this if we haven't got the latest code
mkdir /home/HiveControl/install/init.d
sudo cp -Rp /home/HiveControl/upgrade/HiveControl/install/init.d/* /home/HiveControl/install/init.d/
sudo mkdir /home/HiveControl/software/beecamcounter
sudo cp -Rp /home/HiveControl/upgrade/HiveControl/software/beecamcounter/* /home/HiveControl/software/beecamcounter/
fi
if [[ "$Installed_Ver" < "1.68" ]]; then
#Only run this if we haven't got the latest code
#Install PIGPIO
####################################################################################
# GPIO Library
####################################################################################
echo "Installing PIGPIO library for DHT and HX711 Sensors"
#Kill pigpiod just in case it is already running
sudo killall pigpiod
#Get code
cd /home/HiveControl/software
sudo wget https://github.com/joan2937/pigpio/archive/master.zip
sudo unzip master.zip
cd pigpio-master
make -j4
sudo make install
sudo apt-get install python-pigpio python3-pigpio -y
#Update SUDOERs
sudo cp /etc/sudoers /home/HiveControl/install/sudoers.org
sudo cp /home/HiveControl/upgrade/HiveControl/install/sudoers.d/hivecontrol.sudoers /etc/sudoers
CHECKSUDO=$(visudo -c -f /etc/sudoers |grep "/etc/sudoers:" |awk '{print $3}')
if [[ $CHECKSUDO == "OK" ]]; then
#Copy over SUDOERs file
echo "SUCCESS"
else
echo "Something went wrong with our SUDOERS file, so I didn't change anything"
sudo cp /home/HiveControl/install/sudoers.org /etc/sudoers
fi
#Installing DHTXX Code
cd /home/HiveControl/software
sudo mkdir DHTXXD
cd DHTXXD
sudo wget http://abyz.co.uk/rpi/pigpio/code/DHTXXD.zip
unzip DHTXXD.zip
sudo gcc -Wall -pthread -o DHTXXD test_DHTXXD.c DHTXXD.c -lpigpiod_if2
sudo cp DHTXXD /usr/local/bin/
fi
echo "============================================="
echo "success"
#Cleanup and set the flag in the DB
loglocal "$DATE" UPGRADE SUCCESS "Upgraded to HiveControl ver $branch"
sqlite3 $DestDB "UPDATE hiveconfig SET upgrade_available=\"no\" WHERE id=1"
sqlite3 $DestDB "UPDATE hiveconfig SET HCVersion='$branch' WHERE id=1"
#Move the VERSION
cp /home/HiveControl/upgrade/HiveControl/VERSION /home/HiveControl/