-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathemoncmsupdate
executable file
·394 lines (351 loc) · 13.8 KB
/
emoncmsupdate
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/bash
echo
echo "==========================================="
echo "Emoncms update started"
echo "Emoncms update script V1.2.1 (6th Feb 2019)"
date
echo "==========================================="
echo
username="pi"
userpi=0
env_pi=1
echo "username: $username"
echo
echo "Checking environment:"
# Check that specified user directory exists
if [ -d /home/$username ]; then
echo "- User directory /home/$username found"
if [ "$username" = "pi" ]; then
userpi=1
fi
else
echo "- User directory /home/$username not found (please ammend username)"
exit 0
fi
if ! [ "$userpi" = "1" ]; then
echo "- No pi user directory, unlikely a pi"
env_pi=0
fi
# Check emonSD version
image_version=$(ls /boot | grep emonSD)
if [ "$image_version" = "" ]; then
echo "- Could not find emonSD version file"
image_version=0
env_pi=0
else
echo "- emonSD version: $image_version"
fi
if [ "$env_pi" = "0" ]; then
echo "- Assuming non pi install"
fi
echo
uid=`id -u`
echo "EUID: $uid"
if [ "$uid" = "0" ] ; then
# update is being ran mistakenly as root, switch to user
echo "update running as root: switching to $username user & restarting script"
echo
echo "**MANUAL SYSTEM REBOOT REQUIRED**"
echo
echo "Please reboot and run update again"
su -c $0 $username
exit
fi
#########################################################################################
if [ "$env_pi" = "1" ]; then
# 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/$username/emonpi/emoncms-rebootbutton && \
sudo cp /home/$username/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/$username/emonpi/emoncms-filesystem && \
sudo cp /home/$username/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
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/$username/emonpi/emonhub-sudoers && \
sudo cp /home/$username/emonpi/emonhub-sudoers /etc/sudoers.d/
sudo chmod 0440 /etc/sudoers.d/emonhub-sudoers
echo
echo "emonhub service control 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/$username/emonpi/emoncms-setup/emoncms-setup-sudoers && \
sudo cp /home/$username/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 GPU temperature (pi only)
#https://github.com/emoncms/emoncms/pull/869
if [ "$env_pi" = "1" ]; then
sudo usermod -G video www-data
fi
#########################################################################################
# Record current state of emoncms settings.php
echo
current_settings_md5="$(/home/$username/emonpi/./md5sum.py /var/www/emoncms/settings.php)"
echo "current settings.php md5: $current_settings_md5"
current_default_settings_md5="$(/home/$username/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 "Checking status of /var/www/emoncms git repository"
cd /var/www/emoncms
branch=$(git branch | grep \* | cut -d ' ' -f2)
echo "- git branch: $branch"
changes=$(git diff-index --quiet HEAD --)
if $changes; then
echo "- no local changes"
echo "- running: git pull origin $branch"
echo
git pull origin $branch
else
echo "- changes"
fi
#########################################################################################
# check to see if user has modifed settings.php and if update is need. Auto apply of possible
echo
new_default_settings_md5="$(/home/$username/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
#########################################################################################
echo "=========================================="
echo "UPDATING EMONCMS MODULES"
echo "=========================================="
echo
# Update modules installed directly in the Modules folder
for module in "nodes" "app" "config" "wifi" "dashboard" "graph" "device"; do
echo "------------------------------------------"
echo "Updating $module module"
echo "------------------------------------------"
if [ -d /var/www/emoncms/Modules/$module ]; then
cd /var/www/emoncms/Modules/$module
branch=$(git branch | grep \* | cut -d ' ' -f2)
echo "- git branch: $branch"
changes=$(git diff-index --quiet HEAD --)
if $changes; then
echo "- no local changes"
echo "- running: git pull origin $branch"
echo
git pull origin $branch
else
echo "- changes"
fi
else
echo "$module module does not exist"
fi
echo
done
# Update modules installed in the home/user folder
for module in "postprocess" "sync" "backup"; do
echo "------------------------------------------"
echo "Updating $module module"
echo "------------------------------------------"
if [ -d /home/$username/$module ]; then
cd /home/$username/$module
branch=$(git branch | grep \* | cut -d ' ' -f2)
echo "- git branch: $branch"
changes=$(git diff-index --quiet HEAD --)
if $changes; then
echo "- no local changes"
echo "- running: git pull origin $branch"
echo
git pull origin $branch
# ln -sf /home/$username/$module/$module-module /var/www/emoncms/Modules/$module
else
echo "- changes"
fi
else
echo "$module module does not exist"
fi
echo
done
#########################################################################################
# Automatic installation of new modules if they dont already exist
echo "=========================================="
echo "AUTO INSTALL EMONCMS MODULES"
echo "=========================================="
# Direct installation in Modules folder
for module in "graph" "device"; do
if ! [ -d /var/www/emoncms/Modules/$module ]; then
echo "- Installing Emoncms $module module https://github.com/emoncms/$module"
cd /var/www/emoncms/Modules
git clone https://github.com/emoncms/$module
cd /var/www/emoncms/Modules/$module
if [ `git branch --list stable` ]; then
echo "-- git checkout stable"
git checkout stable
fi
else
echo "- $module module already installed"
fi
done
# Direct installation in Modules folder
for module in "postprocess" "sync" "backup"; do
if ! [ -d /home/$username/$module ]; then
echo
echo "- Installing Emoncms $module module https://github.com/emoncms/$module"
cd /home/$username
git clone https://github.com/emoncms/$module
ln -s /home/$username/$module/$module-module /var/www/emoncms/Modules
cd /home/$username/$module/$module-module
if [ `git branch --list stable` ]; then
echo "-- git checkout stable"
git checkout stable
fi
echo
else
echo "- $module module already installed"
fi
done
echo
#########################################################################################
sudo service apache2 restart
if [ "$env_pi" = "1" ]; then
# Install emoncms-setup module (symlink from emonpi repo)
if [ -d /home/$username/emonpi/emoncms-setup ] && [ ! -d /var/www/emoncms/Modules/setup ]; then
echo "Installing emoncms/emonPi setup module: symlink from ~/emonpi/emoncms-setup"
ln -s /home/$username/emonpi/emoncms-setup /var/www/emoncms/Modules/setup
else
if [ ! -d /home/$username/emonpi/emoncms-setup ]; then
echo "Cannot find emoncms-setup module, please update ~/emonpi repo"
fi
fi
echo
fi
################################################################################################
# Removal of services
################################################################################################
echo "=========================================="
echo "SERVICES"
echo "=========================================="
# 6th Feb 2019: mqtt_input renamed to emoncms_mqtt
for service in "mqtt_input"; do
if [ -f /etc/init.d/$service ]; then
echo "removing initd $service service"
sudo /etc/init.d/$service stop
sudo rm /etc/init.d/$service
fi
if [ -L /lib/systemd/system/$service.service ] || [ -f /lib/systemd/system/$service.service ]; then
echo "Removing $service.service"
sudo systemctl stop $service.service
sudo systemctl disable $service.service
sudo rm /lib/systemd/system/$service.service
sudo systemctl daemon-reload
fi
# if the service still exists in /etc then remove it
if [ -L /etc/systemd/system/$service.service ] || [ -f /etc/systemd/system/$service.service ]; then
echo "Removing $service.service from /etc/systemd/system"
sudo rm /etc/systemd/system/$service.service
fi
done
################################################################################################
# Installation or correction of services
################################################################################################
for service in "emoncms_mqtt" "feedwriter" "service-runner"; do
if [ -d /etc/systemd ] && [ -f /var/www/emoncms/scripts/services/$service/$service.service ]; then
if [ -f /etc/init.d/$service ]; then
echo "removing initd $service service"
sudo /etc/init.d/$service stop
sudo rm /etc/init.d/$service
fi
# service will be symlinked to /etc/systemd/system by "systemctl enable"
if [ -f /etc/systemd/system/$service.service ]; then
if ! [ -L /etc/systemd/system/$service.service ]; then
echo "Removing hard copy of $service.service from /etc/systemd/system (should be symlink)"
sudo systemctl stop $service.service
sudo systemctl disable $service.service
sudo rm /etc/systemd/system/$service.service
sudo systemctl daemon-reload
fi
fi
if [ -f /lib/systemd/system/$service.service ]; then
if ! [ -L /lib/systemd/system/$service.service ]; then
echo "Removing hard copy of $service.service in /lib/systemd/system (should be symlink)"
sudo systemctl stop $service.service
sudo systemctl disable $service.service
sudo rm /lib/systemd/system/$service.service
sudo systemctl daemon-reload
fi
fi
if [ ! -f /lib/systemd/system/$service.service ]; then
echo "Installing $service.service in /lib/systemd/system (creating symlink)"
sudo ln -s /var/www/emoncms/scripts/services/$service/$service.service /lib/systemd/system
sudo systemctl enable $service.service
sudo systemctl start $service.service
else
echo "$service.service already installed"
fi
fi
done
echo "------------------------------------------"
################################################################################################
## 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
if [ "$env_pi" = "1" ]; then
echo "Update Emoncms database"
php /home/$username/emonpi/emoncmsdbupdate.php
echo
fi
echo "Restarting Services..."
for service in "feedwriter" "mqtt_input" "emoncms_mqtt" "emoncms-nodes-service" "emonhub" "openhab"; do
if [ -f /lib/systemd/system/$service.service ]; then
echo "- sudo systemctl restart $service.service"
sudo systemctl restart $service.service
state=$(systemctl show $service | grep ActiveState)
echo "--- $state ---"
elif [ -f /etc/init.d/$service ]; then
echo "- sudo /etc/init.d/$service restart"
sudo /etc/init.d/$service restart
fi
done
echo
if [ "$env_pi" = "1" ]; then
echo "set log rotate config owner to root"
sudo chown root:root /etc/logrotate.conf
fi
echo
echo "------------------------------------------"
echo "Emoncms update script complete"
echo "------------------------------------------"