-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-iot-stuff
executable file
·364 lines (329 loc) · 14.7 KB
/
install-iot-stuff
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
#!/bin/bash
#
# Nothing to see here, yet just snippets
#
# Test equipment: the goal is to test through all relevant Debian/Ubuntu variants
# (and prepare supporting newer distros by taking care of their conventions)
#
# Potential test candidates:
#
# - Armbian (Debian Jessie) running on H3 with legacy kernel (32-bit)
# - Armbian (Ubuntu Xenial) running on ODROID-C2 (64-bit)
# - Raspbian (Debian Jessie) running on OPi Zero (32-bit, ARMv6)
# - Debian Stretch in 64-bit: https://github.com/bamarni/pi64 ?
#
# No decision yet
Main() {
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# we need at least one argument
[ $# -eq 0 ] && DisplayUsage
# let's parse the command line arguments
while getopts 'ha:p:' c ; do
case ${c} in
h)
# display short help
DisplayUsage
;;
a)
# read name of admin account
adminname=${OPTARG}
;;
p)
# read passwd of admin account
adminpass=${OPTARG}
;;
esac
done
shift "$((OPTIND - 1))"
[ "X${adminname}" = "X" ] && adminname="admin"
export adminname adminpass
Packages="$*"
exit 0
PreRequisits
} # Main
DisplayUsage() {
# check if stdout is a terminal...
if test -t 1; then
# see if it supports colors...
ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then
BOLD="$(tput bold)"
NC='\033[0m' # No Color
LGREEN='\033[1;32m'
fi
fi
echo -e "Usage: ${BOLD}${0##*/} [-h] [-a adm] [-p pwd] [pkg] [pkg] [pkg] ...${NC}\n"
echo -e "${BOLD}${0##*/}${NC} can be used to install the packages listed below conveniently."
echo -e "Just use 1 to many as command line arguments in any order. To install for example"
echo -e "Webmin and NodeJS you would do an ${BOLD}${0##*/} webmin nodejs${NC}\n"
echo -e "Please note that some packages require providing the name of an admin account"
echo -e "and a password using ${BOLD}-a${NC} and ${BOLD}-p${NC} respectively. These are ${BOLD}apache${NC} (phpliteadmin),"
echo -e "${BOLD}nodered${NC} and ${BOLD}mosquitto${NC}. In case you want to install these please use ${BOLD}-p${NC} to"
echo -e "provide an administrative password for authorization and also ${BOLD}-a${NC} if you're"
echo -e "not satisfied with \"admin\" being used as default account name.\n\nThe following packages are currently supported:\n"
# display capabilities dynamically, we parse ourself for installer functions (their
# name has to start with 'Install') and use the comment in function section to explain
# what will be installed
Whitespace=' '
awk -F'(' '/^Install/ {print $1}' "$0" | sort | while read ; do
PackageName="$(echo "${REPLY}" | tr '[:upper:]' '[:lower:]' | sed 's/^install//')"
Description="$(grep "^${REPLY}" "$0" | cut -f2 -d#)"
printf "${BOLD}%s %s ${NC}${Description}\n" ${PackageName} "${Whitespace:${#PackageName}}"
done
echo -e "\nExamples:\n'${BOLD}${0##*/} -p secret apache${NC}' will use 'secret' as password for"
echo -e "phpliteadmin while '${BOLD}${0##*/} -a boss -p strange mosquitto${NC}' will set"
echo -e "up Mosquitto with logon credentials boss:strange. If you want to use different"
echo -e "logins for different services you would need to call this installer more than"
echo -e "once sind otherwise same credentials will be used for all services (recommended).\n"
exit 0
} # DisplayUsage
PreRequisits() {
# TODO: If ${SUDO_USER} is not set then check whether installation
# happens from within image creation
Username="${SUDO_USER}"
UserGID="${SUDO_GID}"
UserHome="$(awk -F":" "/^${Username}:/ {print \$6}" < /etc/passwd)"
if [ ! -d "${UserHome}" ]; then
echo -e "There's something wrong. Please step back, create an unprivileged" >&2
echo -e "user account and then call \"sudo ${0##*/} $@\" again. Exiting" >&2
exit 1
fi
# create a safe temporary dir
MyTempDir=$(mktemp -d /tmp/${0##*/}.XXXXXX)
if [ ! -d "${MyTempDir}" ]; then
MyTempDir=/tmp/${0##*/}.$RANDOM.$RANDOM.$RANDOM.$$
(umask 066 && mkdir ${MyTempDir}) || (echo "Failed to create temp dir. Aborting" >&2 ; exit 1)
fi
chmod 711 "${MyTempDir}"
# trap "rm -rf \"${MyTempDir}\" ; exit 0" 0 1 2 3 15
# Install missing dependencies
which curl >/dev/null 2>&1 || apt-get -f -qq -y install curl
# log current installed packages and other stuff
dpkg -l >"${MyTempDir}/packages-now.txt" &
echo -e "DistroArchitecture=$(dpkg --print-architecture)" >>"${MyTempDir}/environment.txt"
echo -e "CPUArchitecture=$(uname -m)" >>"${MyTempDir}/environment.txt"
# get some package names
apt-cache search php | grep sqlite | head -n1 | cut -f1 -d' ' >"${MyTempDir}/php-sqlite.pkg" &
# export some internal functions
export -f GrabPhpLiteAdmin UnprivilegedInstallNodeRED
# check installation
AdjustGroups ${Username} &
} # PreRequisits
GrabPhpLiteAdmin() {
phpLiteAdmin_URL=$(curl -sL 'https://www.phpliteadmin.org/download/' | awk -F'"' '/bitbucket.org\/phpliteadmin\/public\/downloads\/phpLiteAdmin/ {print $2}') &
curl -sL -O https://bitbucket.org/phpliteadmin/public/downloads/phpliteadmin_themes_2013-12-26.zip
curl -sL -O ${phpLiteAdmin_URL}
} # GrabPhpLiteAdmin
InstallApache() { # Apache2, SQLite, PHP and dependencies
echo -e "\nInstalling Apache2, SQLite, PHP and friends now (this can take some time)... \c"
# use meta package names so we get the right version on every distro
apt-get -f -qq -y install apache2 libapache2-mod-php sqlite3 $(cat "${MyTempDir}/php-sqlite.pkg") || \
(echo -e "\nSomething went wrong. Apache2 not installed." >&2 ; return)
# continue with phpliteadmin
[ -d /var/www/html/phpliteadmin ] || mkdir -m 777 /var/www/html/phpliteadmin
cd /var/www/html/phpliteadmin/
su ${Username} -c "bash -c GrabPhpLiteAdmin"
[ -d themes ] || mkdir -m 775 themes
mv *themes*.zip themes/
unzip *.zip && rm *.zip || (echo -e "\nSomething went wrong. PhpLiteAdmin not installed." >&2 ; return)
mv phpliteadmin.php index.php
mv phpliteadmin.config.sample.php phpliteadmin.config.php
(cd themes/ && unzip phpliteadmin_themes_2013-12-26.zip && rm phpliteadmin_themes_2013-12-26.zip) &
sed -i -e "s#^\$directory\(.*\)#\$directory = '${UserHome}/dbs/';#" \
-e "s#^\$password\(.*\)#\$password = '${adminpass}/';#" \
-e "s#^\$subdirectories\(.*\)#\$subdirectories = true;#" /var/www/html/phpliteadmin/phpliteadmin.config.php
chmod 775 /var/www/html/phpliteadmin
chown -R www-data:www-data /var/www/html &
# create SQLite database
[ -d "${UserHome}/dbs" ] || mkdir -m755 "${UserHome}/dbs"
[ -f ${UserHome}/dbs/iot.db" ] && rm ${UserHome}/dbs/iot.db"
sqlite3 "${UserHome}/dbs/iot.db" << EOF
CREATE TABLE IF NOT EXISTS \`pinDescription\` (
\`pinID\` INTEGER PRIMARY KEY NOT NULL,
\`pinNumber\` varchar(2) NOT NULL,
\`pinDescription\` varchar(255) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`pinDirection\` (
\`pinID\` INTEGER PRIMARY KEY NOT NULL,
\`pinNumber\` varchar(2) NOT NULL,
\`pinDirection\` varchar(3) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`pinStatus\` (
\`pinID\` INTEGER PRIMARY KEY NOT NULL,
\`pinNumber\` varchar(2) NOT NULL,
\`pinStatus\` varchar(1) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`users\` (
\`userID\` INTEGER PRIMARY KEY NOT NULL,
\`username\` varchar(28) NOT NULL,
\`password\` varchar(64) NOT NULL,
\`salt\` varchar(8) NOT NULL
);
CREATE TABLE IF NOT EXISTS \`device_list\` (
\`device_name\` varchar(80) NOT NULL DEFAULT '',
\`device_description\` varchar(80) DEFAULT NULL,
\`device_attribute\` varchar(80) DEFAULT NULL,
\`logins\` int(11) DEFAULT NULL,
\`creation_date\` datetime DEFAULT NULL,
\`last_update\` datetime DEFAULT NULL,
PRIMARY KEY (\`device_name\`)
);
CREATE TABLE IF NOT EXISTS \`readings\` (
\`recnum\` INTEGER PRIMARY KEY,
\`location\` varchar(20),
\`value\` int(11) NOT NULL,
\`logged\` timestamp not NULL DEFAULT CURRENT_TIMESTAMP ,
\`device_name\` varchar(40) not null,
\`topic\` varchar(40) not null
);
CREATE TABLE IF NOT EXISTS \`pins\` (
\`gpio0\` int(11) NOT NULL DEFAULT '0',
\`gpio1\` int(11) NOT NULL DEFAULT '0',
\`gpio2\` int(11) NOT NULL DEFAULT '0',
\`gpio3\` int(11) NOT NULL DEFAULT '0'
);
INSERT INTO PINS VALUES(0,0,0,0);
CREATE TABLE IF NOT EXISTS \`temperature_record\` (
\`device_name\` varchar(64) NOT NULL,
\`rec_num\` INTEGER PRIMARY KEY,
\`temperature\` float NOT NULL,
\`date_time\` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS \`Device\` (
\`DeviceID\` INTEGER PRIMARY KEY,
\`DeviceName\` TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS \`DeviceData\` (
\`DataID\` INTEGER PRIMARY KEY,
DeviceID INTEGER,
\`DataName\` TEXT, FOREIGN KEY(DeviceID ) REFERENCES Device(DeviceID)
);
CREATE TABLE IF NOT EXISTS \`Data\` (
SequenceID INTEGER PRIMARY KEY,
\`DeviceID\` INTEGER NOT NULL,
\`DataID\` INTEGER NOT NULL,
\`DataValue\` NUMERIC NOT NULL,
\`epoch\` NUMERIC NOT NULL,
\`timestamp\` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , FOREIGN KEY(DataID, DeviceID ) REFERENCES DeviceData(DAtaID, DeviceID )
);
EOF
chown -R ${Username}:${UserGID} "${UserHome}/dbs" &
systemctl daemon-reload
systemctl restart apache2 &
} # InstallApache
InstallNodeJS() { # NodeJS
echo -e "\nInstalling NodeJS now (this can take some time)... \c"
ARMv6Version="node-v6.10.1-linux-armv6l"
. "${MyTempDir}/environment.txt"
unset ARCHSUFFIX
cd "${MyTempDir}/"
case ${CPUArchitecture} in
armv6*)
# Raspbian/DietPi running on BCM2835, we need ARMv6 binaries
curl -sL -O https://nodejs.org/dist/latest-v6.x/${ARMv6Version}.tar.xz || \
(echo -e "\nSomething went wrong. Can't Install NodeJS. Aborting" >&2 ; exit 1)
pv ${ARMv6Version}.tar.xz | tar -xJf -
cd ${ARMv6Version}
cp -R * /usr/local/
hash -r &
;;
*)
curl -sL -O https://deb.nodesource.com/setup_6.x
bash setup_6.x
case ${DistroArchitecture} in
arm64)
# install armhf packages on arm64
dpkg --add-architecture armhf
ARCHSUFFIX=':armhf'
sed -i -e 's/ https/ [arch=armhf] https/' /etc/apt/sources.list.d/nodesource.list
;;
esac
apt-get -f -qq -y --no-install-recommends install nodejs${ARCHSUFFIX} || \
(echo -e "\nSomething went wrong. Can't Install NodeJS. Aborting" >&2 ; exit 1)
;;
esac
} # InstallNodeJS
InstallNodeRED() { # Node-RED and nodes
which node >/dev/null 2>&1 || (echo -e "\nSomething went wrong. Can't find NodeJS. Aborting" >&2 ; exit 1)
echo -e "\nInstalling Node-RED now (this can take some time)... \c"
# check available RAM
AvailMem=$(( $(free | awk -F" " '/^Mem:/ {print $2}') / 3072 ))
[[ ${AvailMem} -lt 128 ]] && NodeRedMem=128 || NodeRedMem=${AvailMem}
# install only if settings.js has not been altered -- why?
if [ $(grep -c public "${UserHome}/.node-red/settings.js") -ne 1 ]; then
npm install -g --unsafe-perm node-red # why unsafe-perm?
curl -sL "https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/nodered.service" >/lib/systemd/system/nodered.service
curl -sL "https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-start" >/usr/bin/node-red-start
curl -sL "https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/node-red-stop" >/usr/bin/node-red-stop
sed -i -e "s#^User\(.*\)#User=${Username}#" -e "s#^Group\(.*\)#Group=${UserGID}#" \
-e "s#max-old-space-size\(.*\)#max-old-space-size=${NodeRedMem}\"#" \
-e "s#^WorkingDirectory\(.*\)#WorkingDirectory=${UserHome}#" /lib/systemd/system/nodered.service
chmod 755 /usr/bin/node-red-st* # why in /usr/bin and not /usr/local/bin?
systemctl daemon-reload
fi
mkdir -m775 "${UserHome}/.node-red"
cd "${UserHome}/.node-red"
chown ${Username}:${UserGID} "${UserHome}/.node-red"
chown -R ${Username}:${UserGID} "${UserHome}/node_modules"
echo -e "\nInstalling Nodes now (this can take some time)... \c"
# checking for Raspbian specific bits first
/bin/bash -n /etc/os-release && . /etc/os-release || \
(echo -e "\nSomething went wrong. Can't read /etc/os-release. Aborting" >&2 ; exit 1)
case ${ID} in
raspbian)
export Additional_Packages="node-red-contrib-gpio raspi-io"
sudo sed -i -e 's#exit 0#chmod 777 /dev/ttyAMA0\nexit 0#g' /etc/rc.local # better with udev?
apt-get -f -qq -y --no-install-recommends install python{,3}-rpi.gpio &
;;
esac
su ${Username} -c "bash -c UnprivilegedInstallNodeRED"
npm install bcryptjs
# sun is shining, will continue later
} # InstallNodeRED
UnprivilegedInstallNodeRED() {
npm install moment node-red-contrib-config node-red-contrib-grove node-red-contrib-bigtimer \
node-red-contrib-esplogin node-red-contrib-timeout node-red-node-openweathermap \
node-red-node-google node-red-node-sqlite node-red-node-emoncms node-red-node-geofence \
node-red-contrib-ivona node-red-contrib-moment node-red-contrib-particle \
node-red-contrib-web-worldmap node-red-contrib-graphs node-red-contrib-isonline \
node-red-node-ping node-red-node-random node-red-node-smooth node-red-contrib-npm \
node-red-contrib-file-function node-red-contrib-boolean-logic node-red-node-arduino \
node-red-contrib-blynk-websockets node-red-dashboard node-red-node-darksky \
node-red-node-serialport node-red-contrib-owntracks node-red-contrib-chatbot \
${Additional_Packages}
} # UnprivilegedInstallNodeRED
InstallMosquitto() { # Mosquitto with web sockets
/bin/bash -n /etc/os-release && . /etc/os-release || \
(echo -e "\nSomething went wrong. Can't read /etc/os-release. Aborting" >&2 ; exit 1)
echo -e "\nInstalling Mosquitto now (this can take some time)... \c"
case ${VERSION_ID} in
9|16.*)
# Ubuntu Xenial or Debian Stretch -- already in repositories so do nothing
:
;;
*)
# Jessie and others
curl -sL "http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key" -O - | apt-key add -
echo "deb http://repo.mosquitto.org/debian jessie main" > /etc/apt/sources.list.d/mosquitto-jessie.list
apt-get -f -qq -y update
;;
esac
apt-get -f -qq -y --no-install-recommends install mosquitto mosquitto-clients || \
(echo -e "\nSomething went wrong. Can't Install Mosquitto. Aborting" >&2 ; return)
echo -e "listener 9001\nprotocol websockets\nlistener 1883\nallow_anonymous false\npassword_file /etc/mosquitto/passwords" >/etc/mosquitto/conf.d/websockets.conf
touch /etc/mosquitto/passwords # why?
mosquitto_passwd -b /etc/mosquitto/passwords ${adminname} ${adminpass}
} # InstallMosquitto
InstallWebmin() { # Webmin
echo -e "\nInstalling Webmin now (this can take some time)... \c"
curl -sL http://www.webmin.com/jcameron-key.asc | apt-key add -
echo "deb http://download.webmin.com/download/repository sarge contrib" >/etc/apt/sources.list.d/webmin.list
apt-get -f -qq -y update
apt-get -f -qq -y --no-install-recommends install webmin
} # InstallWebmin
AdjustGroups() {
for additionalgroup in adm gpio input sudo netdev audio video dialout plugdev bluetooth ; do
usermod -aG ${additionalgroup} ${Username} 2>/dev/null
done
} # AdjustGroups
Main "$@"