Project

General

Profile

Statistics
| Branch: | Revision:

gdp-if / mqtt-gateway / setup-uhk-gateway.sh @ master

History | View | Annotate | Download (5.59 KB)

1
#!/bin/sh
2

    
3
#
4
#  Set up the U Michigan gateway code on a Swarm box (BBB or Ubuntu)
5
#
6
#	Right now this _must_ be run in ~debian (the home directory
7
#		for user "debian".  The start-up scripts assume the
8
#		code is there.  No, this is not best practice.
9
#	Does _not_ include the MQTT-GDP gateway code, which will
10
#		often run on another host.
11
#
12

    
13
. `dirname $0`/setup-common.sh
14

    
15
# commit of Michigan gateway code to run: avoid bleeding edge bits
16
# (may be null to use latest commit)
17
#gw_commit=71f9a6fc3d192364182473538121d430d4f4bd79
18

    
19
node_setup=true
20
if [ "x$1" = "x-n" ]
21
then
22
	node_setup=false
23
	shift
24
fi
25

    
26
nodevers=6
27

    
28
# heuristic to see if we are running on a beaglebone
29
beaglebone=false
30
test `uname -m` = "arm7l" && beaglebone=true
31
test `uname -m` = "armv7l" && beaglebone=true
32

    
33
instdir=$1
34
if [ ! -z "$instdir" ]
35
then
36
	# don't need to do anything yet
37
	:
38
elif [ `whoami` = "debian" ]
39
then
40
	cd ~debian
41
	instdir=`pwd`/gateway/software
42
elif $beaglebone
43
then
44
	fatal "$0 must be run as debian on beaglebone"
45
else
46
	warn "You may need to adjust paths in startup scripts"
47
	instdir=/usr/lib/uhk
48
fi
49

    
50
root=`pwd`
51
info "Installing Urban Heartbeat Kit from $root into $instdir"
52

    
53
if $beaglebone && [ -z "$gw_commit" ]
54
then
55
	# beaglebone, not much disk space
56
	gitdepth="--depth 1"
57
else
58
	gitdepth=""
59
fi
60

    
61
if ! grep -q "^mosquitto:" /etc/passwd
62
then
63
	info "Creating user mosquitto"
64
	sudo adduser --system mosquitto
65
fi
66

    
67
echo ""
68
info "Determining OS version"
69

    
70
case $OS-$OSVER in
71
	ubuntu-1404*)
72
		pkgadd="libmosquitto0-dev mosquitto-clients"
73
		;;
74

    
75
	debian-*|ubuntu-1604*)
76
		pkgadd="libmosquitto-dev mosquitto-clients"
77
		;;
78

    
79
	*)
80
		warn "Unknown OS or Version $OS-$OSVER; guessing"
81
		pkgadd="libmosquitto-dev mosquitto-clients"
82
		;;
83
esac
84

    
85
echo ""
86
info "Installing Debian packages"
87
test ! -d /var/lib/bluetooth &&
88
	mkdir /var/lib/bluetooth &&
89
	chmod 700 /var/lib/bluetooth
90
sudo apt-get install -y \
91
	avahi-daemon \
92
	bluetooth \
93
	bluez \
94
	curl \
95
	g++ \
96
	gcc \
97
	git \
98
	libavahi-compat-libdnssd-dev \
99
	libbluetooth-dev \
100
	libudev-dev \
101
	locales \
102
	make \
103
	psmisc \
104
	mosquitto \
105
	$pkgadd
106

    
107
info "Turning off excessive mosquitto logging"
108
sudo sed -i.ORIG -e '/#log_type error/i log_type none' \
109
	/etc/mosquitto/mosquitto.conf
110
if [ -f /etc/mosquitto/conf.d/swarmgateway.conf ]
111
then
112
	sudo sed -i.ORIG \
113
		-e '/log_type error/s/error/none/' \
114
		/etc/mosquitto/conf.d/swarmgateway.conf
115

    
116
	info "Disabling unnecessary incoming sockets"
117
	sudo sed -i \
118
		-e '/listener 9001/s/^/#/' \
119
		-e '/protocol websockets/s/^/#/' \
120
		/etc/mosquitto/conf.d/swarmgateway.conf
121
fi
122

    
123
info "Enabling bluetooth daemon"
124
sudo update-rc.d bluetooth defaults
125

    
126
# check out the git tree from UMich
127
echo ""
128
info "Checking out Gateway source tree from Michigan"
129
cd $root
130
rm -rf gateway
131
git clone $gitdepth https://github.com/lab11/gateway.git
132
cd gateway
133

    
134
if [ ! -z "$gw_commit" ]
135
then
136
	echo ""
137
	info "Getting lab11/gateway commit $gw_commit"
138
	git checkout $gw_commit
139
fi
140

    
141
# verify that we have checked things out
142
if [ ! -d software -o ! -d systemd ]
143
then
144
	fatal "$0 must be run from root of gateway git tree" 1>&2
145
fi
146

    
147
if $node_setup
148
then
149
	echo ""
150
	info "Set up node.js"
151
	info ">>> NOTE WELL: this may give several warnings about xpc-connection."
152
	info ">>> These should be ignored."
153
	curl -sL https://deb.nodesource.com/setup_$nodevers.x | sudo -E bash -
154
	sudo apt-get install -y nodejs
155

    
156
	# get the names of the packages that might run
157
	cd systemd
158
	pkgs=`ls -d *.service | sed 's/\.service//'`
159

    
160
	# initialize remaining dependencies for each service
161
	#	We also install the packages themselves even though they are
162
	#	run out of the source tree; doing npm install in each source
163
	#	directory causes duplicate dependencies, and our disks are
164
	#	just too small.
165
	cd $root/gateway/software
166
	umask 022
167
	if [ `pwd` != "$instdir" ]
168
	then
169
		if [ ! -d "$instdir" ]
170
		then
171
			info "Creating $instdir"
172
			sudo mkdir -p $instdir && sudo chown `whoami` $instdir
173
		fi
174
		cp -rp [a-z]* $instdir
175
		cd $instdir
176
		mkdir node_modules
177
	fi
178
	for i in $pkgs
179
	do
180
		echo ""
181
		info "Initializing for package $i"
182
		npm install $i --prefix $instdir
183
	done
184

    
185
	echo ""
186
	info "Clearing NPM cache"
187
	npm cache clean
188
fi
189

    
190
# install system startup scripts
191
if [ "$INITSYS" != "systemd" ]
192
then
193
	fatal "Cannot initialize system startup scripts: only systemd supported"
194
fi
195

    
196
echo ""
197
info "Installing system startup scripts"
198
info "  ... mosquitto.service"
199
sudo cp $root/mosquitto.service /etc/systemd/system
200
cd $root/gateway/systemd
201
for i in *.service
202
do
203
	info "  ... $i"
204
	sed "s;/home/debian/gateway/software/;$instdir/;" $i |
205
		sudo dd of=/etc/systemd/system/$i
206
done
207

    
208
echo ""
209
info "Selectively enabling system startup scripts"
210

    
211
enable() {
212
	info "Enabling service $1"
213
	sudo systemctl enable $1
214
}
215

    
216
disable() {
217
	if [ -z "$2" ]
218
	then
219
		info "Disabling service $1"
220
	else
221
		info "Disabling service $1: $2"
222
	fi
223
	sudo systemctl disable $1
224
	sudo systemctl stop $1
225
}
226

    
227
skip() {
228
	if [ -z "$2" ]
229
	then
230
		info "Skipping service $1"
231
	else
232
		info "Skipping service $1: $2"
233
	fi
234
}
235

    
236
enable	mosquitto
237
enable	adv-gateway-ip
238
disable	app-runner		"throws errors as of 01/13/2019"
239
enable	ble-address-sniffer-mqtt
240
enable	ble-gateway-mqtt
241
disable	ble-nearby
242
disable	gateway-mqtt-emoncms	"not in use at Berkeley"
243
disable	gateway-mqtt-gatd	"not in use at Berkeley"
244
disable	gateway-mqtt-log	"not in use at Berkeley"
245
enable	gateway-mqtt-topics
246
disable	gateway-publish
247
disable	gateway-server		"throws errors as of 01/13/2019"
248
disable	gateway-ssdp
249
disable	gateway-watchdog-email
250
disable	ieee802154-monjolo-gateway
251
disable node-red.service	"security risk"
252
disable node-red.socket		"security risk"
253
disable cloud9.service		"security concerns"
254
disable cloud9.socket		"security concerns"
255
disable bonescript.service	"security concerns"
256
disable bonescript.socket	"security concerns"