Project

General

Profile

Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5.59 KB)

1 298b728e Eric Allman
#!/bin/sh
2
3 97679b97 Eric Allman
#
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 96c8a5c8 Eric Allman
. `dirname $0`/setup-common.sh
14 56574801 Eric Allman
15 0c03fa29 Eric Allman
# commit of Michigan gateway code to run: avoid bleeding edge bits
16
# (may be null to use latest commit)
17 f12b301f Eric Allman
#gw_commit=71f9a6fc3d192364182473538121d430d4f4bd79
18 0c03fa29 Eric Allman
19 4cb52315 Eric Allman
node_setup=true
20
if [ "x$1" = "x-n" ]
21
then
22
	node_setup=false
23
	shift
24
fi
25
26
nodevers=6
27 298b728e Eric Allman
28 97679b97 Eric Allman
# heuristic to see if we are running on a beaglebone
29
beaglebone=false
30
test `uname -m` = "arm7l" && beaglebone=true
31 c4c98d0e Eric Allman
test `uname -m` = "armv7l" && beaglebone=true
32 546ba73d Eric Allman
33 4cb52315 Eric Allman
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 0c03fa29 Eric Allman
if $beaglebone && [ -z "$gw_commit" ]
54 1f2b4389 Eric Allman
then
55
	# beaglebone, not much disk space
56
	gitdepth="--depth 1"
57
else
58
	gitdepth=""
59
fi
60
61 4cb52315 Eric Allman
if ! grep -q "^mosquitto:" /etc/passwd
62
then
63
	info "Creating user mosquitto"
64 c4c98d0e Eric Allman
	sudo adduser --system mosquitto
65 4cb52315 Eric Allman
fi
66
67 97679b97 Eric Allman
echo ""
68
info "Determining OS version"
69
70
case $OS-$OSVER in
71 4cb52315 Eric Allman
	ubuntu-1404*)
72 97679b97 Eric Allman
		pkgadd="libmosquitto0-dev mosquitto-clients"
73
		;;
74
75 4cb52315 Eric Allman
	debian-*|ubuntu-1604*)
76 97679b97 Eric Allman
		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 4cb52315 Eric Allman
echo ""
86 97679b97 Eric Allman
info "Installing Debian packages"
87
test ! -d /var/lib/bluetooth &&
88
	mkdir /var/lib/bluetooth &&
89
	chmod 700 /var/lib/bluetooth
90 c0feda3c Eric Allman
sudo apt-get install -y \
91 298b728e Eric Allman
	avahi-daemon \
92 97679b97 Eric Allman
	bluetooth \
93
	bluez \
94 c0feda3c Eric Allman
	curl \
95
	g++ \
96
	gcc \
97 298b728e Eric Allman
	git \
98
	libavahi-compat-libdnssd-dev \
99 97679b97 Eric Allman
	libbluetooth-dev \
100
	libudev-dev \
101 c0feda3c Eric Allman
	locales \
102
	make \
103 2e7bd0cc Eric Allman
	psmisc \
104 4cb52315 Eric Allman
	mosquitto \
105 97679b97 Eric Allman
	$pkgadd
106 298b728e Eric Allman
107 f1fc03a6 Eric Allman
info "Turning off excessive mosquitto logging"
108 f169f025 Eric Allman
sudo sed -i.ORIG -e '/#log_type error/i log_type none' \
109
	/etc/mosquitto/mosquitto.conf
110 1270dc91 Eric Allman
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 f1fc03a6 Eric Allman
123 c0feda3c Eric Allman
info "Enabling bluetooth daemon"
124
sudo update-rc.d bluetooth defaults
125
126 298b728e Eric Allman
# check out the git tree from UMich
127
echo ""
128 97679b97 Eric Allman
info "Checking out Gateway source tree from Michigan"
129 c0feda3c Eric Allman
cd $root
130 2e7bd0cc Eric Allman
rm -rf gateway
131 1f2b4389 Eric Allman
git clone $gitdepth https://github.com/lab11/gateway.git
132 c0feda3c Eric Allman
cd gateway
133 298b728e Eric Allman
134 0c03fa29 Eric Allman
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 298b728e Eric Allman
# verify that we have checked things out
142
if [ ! -d software -o ! -d systemd ]
143
then
144 546ba73d Eric Allman
	fatal "$0 must be run from root of gateway git tree" 1>&2
145 298b728e Eric Allman
fi
146
147 4cb52315 Eric Allman
if $node_setup
148
then
149 298b728e Eric Allman
	echo ""
150 4cb52315 Eric Allman
	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 298b728e Eric Allman
185 4cb52315 Eric Allman
	echo ""
186
	info "Clearing NPM cache"
187
	npm cache clean
188
fi
189 1f2b4389 Eric Allman
190 298b728e Eric Allman
# install system startup scripts
191 97679b97 Eric Allman
if [ "$INITSYS" != "systemd" ]
192
then
193
	fatal "Cannot initialize system startup scripts: only systemd supported"
194
fi
195
196 298b728e Eric Allman
echo ""
197 97679b97 Eric Allman
info "Installing system startup scripts"
198 4cb52315 Eric Allman
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 298b728e Eric Allman
208
echo ""
209 97679b97 Eric Allman
info "Selectively enabling system startup scripts"
210 298b728e Eric Allman
211
enable() {
212 546ba73d Eric Allman
	info "Enabling service $1"
213 c0feda3c Eric Allman
	sudo systemctl enable $1
214 298b728e Eric Allman
}
215
216 f7400865 Eric Allman
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 298b728e Eric Allman
skip() {
228
	if [ -z "$2" ]
229
	then
230 546ba73d Eric Allman
		info "Skipping service $1"
231 298b728e Eric Allman
	else
232 546ba73d Eric Allman
		info "Skipping service $1: $2"
233 298b728e Eric Allman
	fi
234
}
235
236 4cb52315 Eric Allman
enable	mosquitto
237 298b728e Eric Allman
enable	adv-gateway-ip
238 f7400865 Eric Allman
disable	app-runner		"throws errors as of 01/13/2019"
239 298b728e Eric Allman
enable	ble-address-sniffer-mqtt
240 c0feda3c Eric Allman
enable	ble-gateway-mqtt
241 f7400865 Eric Allman
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 298b728e Eric Allman
enable	gateway-mqtt-topics
246 f7400865 Eric Allman
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 1270dc91 Eric Allman
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"