forked from openenergymonitor/emonpi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemoncmsupdate
More file actions
executable file
·362 lines (302 loc) · 11.5 KB
/
emoncmsupdate
File metadata and controls
executable file
·362 lines (302 loc) · 11.5 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash
echo
echo "================================="
echo "Emoncms update started"
echo "Emoncms update script V1.2.0"
echo
date
echo
#########################################################################################
echo "#############################################################"
echo
# Check emonSD base image is minimum required release date, else don't update
image_version=$(ls /boot | grep emonSD)
echo "emonSD version: $image_version"
echo
if [ "$image_version" == "emonSD-07Nov16" ] || [ "$image_version" == "emonSD-03May16" ] || [ $image_version == "emonSD-26Oct17" ] || [ $image_version == "emonSD-13Jun18" ]; then
echo "emonSD base image check pass...continue update"
else
echo "ERROR: emonSD base image old or undefined...update will not continue"
echo "See latest verson: https://github.com/openenergymonitor/emonpi/wiki/emonSD-pre-built-SD-card-Download-&-Change-Log"
echo "Stopping update"
exit
fi
echo
echo "#############################################################"
uid=`id -u`
echo "EUID: $uid"
if [ "$uid" = "0" ] ; then
# update is being ran mistakenly as root, need to remove all sudo crontab service runner entries
echo "update running as root: remove sudo crontab service runner entries"
crontab -l > mycron
if grep -Fxq " * * * * * /home/pi/emonpi/service-runner >> /var/log/service-runner.log 2>&1" mycron ; then
sed -i "/\s\* \* \* \* \* \/home\/pi\/emonpi\/service-runner >> \/var\/log\/service-runner.log 2>&1/d" ./mycron
fi
if grep -Fxq "* * * * * /home/pi/emonpi/service-runner >> /var/log/service-runner.log 2>&1" mycron ; then
sed -i "/\* \* \* \* \* \/home\/pi\/emonpi\/service-runner >> \/var\/log\/service-runner.log 2>&1/d" ./mycron
fi
crontab mycron
rm mycron
echo "switching to Pi user & restarting script"
echo
echo "**MANUAL SYSTEM REBOOT REQUIRED**"
echo
echo "Please shutdown and reboot emonPi then run Update again"
su -c $0 pi
exit
fi
#########################################################################################
# Add backup module service-runner
echo "Checking cron tab for service runner entry..."
crontab -l > mycron
# Check crontab entry does not already exist, if not add new entry
if ! grep -Fxq "* * * * * /home/pi/emonpi/service-runner >> /var/log/service-runner.log 2>&1" mycron ; then
echo "Add service runner cron entry"
echo "* * * * * /home/pi/emonpi/service-runner >> /var/log/service-runner.log 2>&1" >> mycron
crontab mycron
else
echo "service runner crontab entry already installed"
fi
rm mycron
# Check if double service runner (with space) entry exits, if so remove it
crontab -l > mycron
if grep -Fxq " * * * * * /home/pi/emonpi/service-runner >> /var/log/service-runner.log 2>&1" mycron ; then
echo "Remove duplicate service runner entry"
sed -i "/\s\* \* \* \* \* \/home\/pi\/emonpi\/service-runner >> \/var\/log\/service-runner.log 2>&1/d" ./mycron
crontab mycron
fi
rm mycron
#########################################################################################
# Install sudoers entry to enable emoncms reboot button
if [ ! -f /etc/sudoers.d/emoncms-rebootbutton ]; then
# double check correct sudo syntax
sudo visudo -cf /home/pi/emonpi/emoncms-rebootbutton && \
sudo cp /home/pi/emonpi/emoncms-rebootbutton /etc/sudoers.d/
sudo chmod 0440 /etc/sudoers.d/emoncms-rebootbutton
echo
echo "Install emonPi Emoncms admin reboot button sudoers entry"
fi
#########################################################################################
# Install sudoers entry to enable toggling file-system RO/RW mount via Emoncms admin
if [ ! -f /etc/sudoers.d/emoncms-filesystem ]; then
# double check correct sudo syntax
sudo visudo -cf /home/pi/emonpi/emoncms-filesystem && \
sudo cp /home/pi/emonpi/emoncms-filesystem /etc/sudoers.d/
sudo chmod 0440 /etc/sudoers.d/emoncms-filesystem
echo
echo "emonPi Emoncms admin file-system toggle buttons sudoers entry installed"
fi
#########################################################################################
# Install sudoers entry to enable emonhub reboot button
if [ ! -f /etc/sudoers.d/emonhub-sudoers ]; then
# double check correct sudo syntax
sudo visudo -cf /home/pi/emonpi/emonhub-sudoers && \
sudo cp /home/pi/emonpi/emonhub-sudoers /etc/sudoers.d/
sudo chmod 0440 /etc/sudoers.d/emonhub-sudoers
echo
echo "emonhub service constol sudoers entry installed"
fi
#########################################################################################
# Install sudoers entry for WiFI AP
if [ ! -f /etc/sudoers.d/emoncms-setup-sudoers ]; then
# double check correct sudo syntax
sudo visudo -cf /home/pi/emonpi/emoncms-setup/emoncms-setup-sudoers && \
sudo cp /home/pi/emonpi/emoncms-setup/emoncms-setup-sudoers /etc/sudoers.d/
sudo chmod 0440 /etc/sudoers.d/emoncms-setup-sudoers
echo
echo "Emoncms setup module sudoers entry installed"
fi
#########################################################################################
# Setup user group to enable reading pi GPU temperature
#https://github.com/emoncms/emoncms/pull/869
sudo usermod -G video www-data
#########################################################################################
# Record current state of emoncms settings.php
echo
current_settings_md5="$(/home/pi/emonpi/./md5sum.py /var/www/emoncms/settings.php)"
echo "current settings.php md5: $current_settings_md5"
current_default_settings_md5="$(/home/pi/emonpi/md5sum.py /var/www/emoncms/default.emonpi.settings.php)"
echo "Default settings.php md5: $current_default_settings_md5"
#########################################################################################
# Pulling in latest Emoncms changes
echo
echo "git pull /var/www/emoncms"
cd /var/www/emoncms
git branch
git status
git pull
#########################################################################################
# check to see if user has modifed settings.php and if update is need. Auto apply of possible
echo
new_default_settings_md5="$(/home/pi/emonpi/md5sum.py /var/www/emoncms/default.emonpi.settings.php)"
echo "NEW default settings.php md5: $new_default_settings_md5"
if [ "$current_default_settings_md5" == "$current_settings_md5" ]; then
echo "settings.php has NOT been user modifed"
settings_unmodified=true
else
echo "settings.php HAS been user modified"
settings_unmodified=false
fi
# check to see if there is an update waiting for settings.php
if [ "$new_default_settings_md5" != "$current_default_settings_md5" ]; then
echo "Update required to settings.php..."
if [ $settings_unmodified == true ]; then
sudo cp /var/www/emoncms/default.emonpi.settings.php /var/www/emoncms/settings.php
echo "settings.php autoupdated"
else
echo "**ERROR: unable to autoupdate settings.php since user changes are present, manual review required**"
fi
else
echo "settings.php not updated"
fi
#########################################################################################
echo
if [ -d /var/www/emoncms/Modules/nodes ]; then
echo "git pull /var/www/emoncms/Modules/nodes"
cd /var/www/emoncms/Modules/nodes
git status
git branch
git pull
fi
echo
echo "git pull /var/www/emoncms/Modules/app"
cd /var/www/emoncms/Modules/app
git branch
git status
git pull
git checkout stable
echo
echo "git pull /var/www/emoncms/Modules/config"
cd /var/www/emoncms/Modules/config
git branch
git status
git pull
git checkout stable
echo
echo "git pull /var/www/emoncms/Modules/wifi"
cd /var/www/emoncms/Modules/wifi
git branch
git status
git pull
git checkout stable
echo "git pull /var/www/emoncms/Modules/dashboard"
cd /var/www/emoncms/Modules/dashboard
git status
git pull
git checkout stable
echo
if [ -d /var/www/emoncms/Modules/graph ]; then
echo "git pull /var/www/emoncms/Modules/graph"
cd /var/www/emoncms/Modules/graph
git branch
git status
git pull
git checkout stable
else
echo "Installing Emoncms graph module https://github.com/emoncms/graph"
cd /var/www/emoncms/Modules
git clone https://github.com/emoncms/graph
fi
echo
if [ -d /home/pi/postprocess ]; then
echo "git pull /home/pi/postprocess"
cd /home/pi/postprocess
sudo chmod 777 .git -R
git checkout emonpi
git pull
ln -sf /home/pi/postprocess/postprocess-module /var/www/emoncms/Modules/postprocess
else
echo "git clone https://github.com/emoncms/postprocess"
cd /home/pi
git clone -b emonpi https://github.com/emoncms/postprocess
cd /home/pi/postprocess
git checkout emonpi
fi
if [ -d /home/pi/sync ]; then
echo "git pull /home/pi/sync"
cd /home/pi/sync
git checkout master
git pull
ln -sf /home/pi/sync/sync-module /var/www/emoncms/Modules/sync
else
echo "git clone https://github.com/emoncms/sync"
cd /home/pi
git clone https://github.com/emoncms/sync
cd /home/pi/sync
git checkout master
ln -sf /home/pi/sync/sync-module /var/www/emoncms/Modules/sync
fi
if [ -d /home/pi/backup ]; then
echo "git pull /home/pi/backup"
cd /home/pi/backup
git branch
git status
git pull
git checkout stable
else
echo "Installing Emoncms backup module from https://github.com/emoncms/backup"
cd /home/pi
git clone https://github.com/emoncms/backup
cd /home/pi/backup
git checkout stable
ln -s /home/pi/backup/backup/ /var/www/emoncms/Modules/backup
echo "Restarting apache web-server"
sudo service apache2 restart
fi
# Install emoncms-setup module (symlink from emonpi repo)
if [ -d /home/pi/emonpi/emoncms-setup ] && [ ! -d /var/www/emoncms/Modules/setup ]; then
echo "Installing emoncms/emonPi setup module: symlink from ~/emonpi/emoncms-setup"
ln -s /home/pi/emonpi/emoncms-setup /var/www/emoncms/Modules/setup
else
if [ ! -d /home/pi/emonpi/emoncms-setup ]; then
echo "Cannot find emoncms-setup module, please update ~/emonpi repo"
fi
fi
echo
################################################################################################
if [ -d /etc/systemd ] && [ -f /var/www/emoncms/scripts/mqtt_input.service ]; then
if [ -f /etc/init.d/mqtt_input ]; then
echo "replacing initd mqtt_input with systemd mqtt input"
sudo /etc/init.d/mqtt_input stop
sudo rm /etc/init.d/mqtt_input
sudo cp /var/www/emoncms/scripts/mqtt_input.service /etc/systemd/system/mqtt_input.service
sudo systemctl daemon-reload
sudo systemctl enable mqtt_input.service
sudo systemctl start mqtt_input.service
else
echo "update mqtt_input systemd unit file"
sudo cp /var/www/emoncms/scripts/mqtt_input.service /etc/systemd/system/mqtt_input.service
sudo systemctl daemon-reload
fi
fi
## Don't overwrite settings unless required (un comment if required)
# echo "Copy new default.emonpi.settings.php to settings.php & make backup old.emonpi.settings.php"
# sudo cp /var/www/emoncms/settings.php /var/www/emoncms/old.settings.php
# sudo cp /var/www/emoncms/default.emonpi.settings.php /var/www/emoncms/settings.php
echo
echo "Update Emoncms database"
php /home/pi/emonpi/emoncmsdbupdate.php
echo
echo "Restarting Services..."
sudo /etc/init.d/emonhub restart
sudo /etc/init.d/feedwriter restart
echo
if [ -f /etc/init.d/emoncms-nodes-service ]; then
sudo /etc/init.d/emoncms-nodes-service restart
fi
echo
if [ -f /etc/init.d/openhab ]; then
sudo /etc/init.d/openhab restart
fi
echo
if [ -f /etc/init.d/mqtt_input ]; then
sudo /etc/init.d/mqtt_input start
sleep 1
sudo /etc/init.d/mqtt_input restart
else
sudo systemctl restart mqtt_input.service
fi
echo
echo "set log rotate config owner to root"
sudo chown root:root /etc/logrotate.conf
echo "Restarting Services..."