Project

General

Profile

Statistics
| Branch: | Revision:

gdp-if / mqtt-gateway / gdp-provision-sensor.sh @ master

History | View | Annotate | Download (3.6 KB)

1 eb78623e Eric Allman
#!/bin/sh
2
{ test -r /usr/local/etc/gdp.conf.sh && . /usr/local/etc/gdp.conf.sh; } ||
3
	{ test -r /etc/gdp.conf.sh && . /etc/gdp.conf.sh; }
4
5
#
6
#  Create logs and adjust configuration files for new sensors.
7
#
8
#    Usage: $0 mqtt-gateway-config sensor ...
9
#	Verifies that each sensor is not already in the config file.
10
#	If not, creates the log file if it does not already exist
11
#	The configuration file must already exist, even if it has
12
#	no sensors listed.
13
#
14
15
# configure defaults
16 8d59f16d Eric Allman
: ${GDP_ROOT:=/usr}
17
if [ "$GDP_ROOT" = "/usr" ]
18 eb78623e Eric Allman
then
19
	: ${GDP_ETC:=/etc/gdp}
20
else
21 8d59f16d Eric Allman
	: ${GDP_ETC:=$GDP_ROOT/etc}
22 eb78623e Eric Allman
fi
23
: ${GDP_VAR:=/var/swarm/gdp}
24
: ${GDP_KEYS_DIR:=$GDP_ETC/keys}
25
: ${GDP_LOG_DIR:=/var/log/gdp}
26
: ${GDP_USER:=gdp}
27
: ${MQTT_GATEWAY_ARGS:=-D*=2}
28
: ${MQTT_GATEWAY_LOG:=$GDP_LOG_DIR/mqtt-gdp-gateway.log}
29
: ${LLOGGER:=llogger}
30
31
EX_USAGE=64
32
EX_OSFILE=72
33
EX_CONFIG=78
34
35
# if we are running as root, start over as gdp
36
test `whoami` = "root" && exec sudo -u $GDP_USER $0 "$@"
37
if [ `whoami` != "$GDP_USER" ]
38
then
39
	echo "[FATAL] Must be run as $GDP_USER or root"
40
	exit $EX_USAGE
41
fi
42
43
if [ $# -lt 2 ]
44
then
45
	echo "[FATAL] Usage: $0 mqtt-gateway-config sensor ..."
46
	exit $EX_USAGE
47
fi
48
49 8d59f16d Eric Allman
echo "[INFO] Running $0 with GDP_ROOT=$GDP_ROOT GDP_ETC=$GDP_ETC"
50 eb78623e Eric Allman
51
# get the short host name
52
shorthost=`basename "$1" ".conf" | sed -e 's/^mqtt-gateway.//' -e 's/\..*//'`
53
shift
54
55
# verify the configuration file
56
if [ ! -e $GDP_ETC/mqtt-gateway.$shorthost.conf ]
57
then
58
	echo "[FATAL] No configuration $GDP_ETC/mqtt-gateway.$shorthost.conf"
59
	exit $EX_CONFIG
60
fi
61
62
# save the names of the devices we want to provision
63
devices=$@
64
65
# get the existing configuration file and take the first line for the log name
66
configfile=$GDP_ETC/mqtt-gateway.$shorthost.conf
67
config=`sed -e 's/#.*//' $configfile`
68
set -- $config
69 3ca57859 Eric Allman
log_root=$1
70 eb78623e Eric Allman
shift
71
72
# save the clean configuration (just existing devices, white space removed)
73
config="$@"
74
75
# now we can go back to devices in our arguments
76
set -- $devices
77
78
# if there is no keys directory, be sure to create it
79
if [ ! -d $GDP_KEYS_DIR ]
80
then
81
	if ! mkdir -p $GDP_KEYS_DIR
82
	then
83
		echo "[FATAL] Cannot create keys directory $GDP_KEYS_DIR"
84
		exit $EX_OSFILE
85
	fi
86
	chmod 755 $GDP_KEYS_DIR
87
fi
88
89
announce_keys_dir=false
90
for i
91
do
92
	echo "[INFO] Provisioning for sensor $i on gateway $shorthost"
93
94
	sensor=`echo $i | tr '[A-F]' '[a-f]' | sed -e 's/[^0-9a-f]//g'`
95
	if [ "$sensor" != "$i" ]
96
	then
97
		echo "[INFO] Modified $i to $sensor"
98
	fi
99
100
	# check for duplicates --- if so, just pass
101
	addconfig=true
102
	if echo $config | grep -q $sensor
103
	then
104
		addconfig=false
105
		echo "[WARN] Sensor $sensor already provisioned on $shorthost"
106
	else
107
		# check to see if it already exists in another configuration
108
		dups=`grep -l "^[ 	]*$sensor" $GDP_ETC/mqtt-gateway.*.conf`
109
		if [ ! -z "$dups" ]
110
		then
111
			echo "[ERROR] Sensor $sensor already provisioned in file"
112
			echo "[ERROR]    $dups"
113
			echo "[ERROR] Please edit this configuration and try again."
114
			continue
115
		fi
116
	fi
117
118
	# see if the log already exists --- if not, create
119 8d59f16d Eric Allman
	if ! $GDP_ROOT/bin/log-exists $log_root.device.$sensor
120 eb78623e Eric Allman
	then
121 3ca57859 Eric Allman
		echo "[INFO] Creating log $log_root.device.$sensor"
122 8d59f16d Eric Allman
		if $GDP_ROOT/bin/log-create -K$GDP_KEYS_DIR -e none \
123 3ca57859 Eric Allman
			$log_root.device.$sensor
124 eb78623e Eric Allman
		then
125 3ca57859 Eric Allman
			echo "[INFO] Created log $log_root.device.$sensor"
126 eb78623e Eric Allman
			announce_keys_dir=true
127
		else
128
			echo "[ERROR] Could not create log; skipping $sensor"
129
		fi
130
	else
131 3ca57859 Eric Allman
		echo "[WARN] $log_root.device.$sensor already exists;" \
132 eb78623e Eric Allman
				"continuing anyway"
133
	fi
134
135
	if $addconfig
136
	then
137
		# append device name to configuration file
138
		echo "[INFO] Adding configuration for $sensor to $configfile"
139
		echo $sensor >> $configfile
140
	fi
141
done
142
143
if $announce_keys_dir
144
then
145
	echo "[INFO] New private key(s) are in $GDP_KEYS_DIR"
146
fi