Developing lightweight computation at the DSG edge

qmp_system.sh 4.59 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#!/bin/sh
QMP_PATH="/etc/qmp"
SOURCE_SYS=1

. $QMP_PATH/qmp_common.sh
[ -z "$SOURCE_GW" ] && . $QMP_PATH/qmp_gw.sh
[ -z "$SOURCE_NET" ] && . $QMP_PATH/qmp_network.sh

qmp_configure_system() {

11
12
13
	if [ -n "$(qmp_uci_get node.device_id)" ]; then
		local device_id=$(qmp_uci_get node.device_id)
		device_id="$(echo -n $device_id | tr -cd 'ABCDEFabcdef0123456789' | tail -c 10)"
14
	else
15
16
		local device_id=$(qmp_get_id_hostname)
		device_id="$(echo -n $device_id | tr -cd 'ABCDEFabcdef0123456789' | tail -c 4)"
17
18
19
20
		[ $(echo -n $device_id | wc -c) -lt 4 ] && {
			qmp_log "Warning, device_id not defined properly, using failsafe 0000"
			device_id=0000
		}
21
		qmp_uci_set node.device_id $device_id
22
	fi
23

24
25
	local device_name="$(qmp_uci_get node.device_name)"
	[ -z "$device_name" ] && device_name="qMp" && qmp_uci_set node.device_name $device_name
26
27

	# set hostname
28
	local append_id="$(uci get qmp.node.append_id)"
29
	
30
	if [ $append_id -eq 0 ]; then
31
32
33
34
35
36
		uci set system.@system[0].hostname="${device_name}"
		echo "${device_name}" > /proc/sys/kernel/hostname
	else
		uci set system.@system[0].hostname="${device_name}-${device_id}"
		echo "${device_name}-${device_id}" > /proc/sys/kernel/hostname
	fi
37
	uci commit system
38

39
40
41
42
43
44
45
46

	uci set uhttpd.main.listen_http="80"
	uci set uhttpd.main.listen_https="443"
	uci commit uhttpd
	/etc/init.d/uhttpd restart

	# configuring hosts
	qmp_set_hosts
47
48
49

	# configure maps
	qmp_set_maps
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
}

qmp_set_hosts() {
	qmp_log "Configuring /etc/hosts file with qmpadmin entry"

	local ip=$(uci get bmx6.general.tun4Address | cut -d'/' -f1)
	local hn=$(uci get system.@system[0].hostname)

	if [ -z "$ip" -o -z "$hn" ]; then
		echo "Cannot get IP or HostName"
		return
	fi

	if [ $(cat /etc/hosts | grep -c "^$ip.*qmpadmin") -eq 0 ]; then
		cat /etc/hosts | grep -v qmpadmin > /tmp/hosts.tmp
		echo "$ip $hn admin.qmp qmpadmin" >> /tmp/hosts.tmp
		cp /tmp/hosts.tmp /etc/hosts
	fi
}

70
71
72
73
qmp_set_maps() {
	[ $(qmp_uci_get services.libremap) -eq 1 ] && {
		qmp_uci_get_raw libremap.location && {
			local lat="$(qmp_uci_get node.latitude)"
74
			local lon="$(qmp_uci_get node.longitude)"
75
76
77
78
			local elev="$(qmp_uci_get node.elevation)"
			[ -n "$lat" ] && [ -n "$lon" ] && {
				elev=${elev:-0}
				qmp_uci_set_raw libremap.location.latitude="$lat"
79
				qmp_uci_set_raw libremap.location.longitude="$lon"
80
				qmp_uci_set_raw libremap.location.elev="$elev"
81
82
83
84
85
				
				uci set libremap.qmp.enabled=1
				uci set libremap.bmx6.enabled=1
				uci set libremap.wireless.enabled=1
				uci commit
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
# -----------------------------------
#          Services section
# -----------------------------------

qmp_enable_netserver() {
	qmp_log Enabling service netserver
	qmp_disable_service netserver
	killall -9 netserver 2>/dev/null
	netserver -6 -p 12865
	return 0
}

qmp_disable_netserver() {
	qmp_log Disabling service netserver
	qmp_disable_service netserver
	killall -9 netserver 2>/dev/null
	return 0
}

qmp_enable_service() {
	qmp_log Enabling service $1
	/etc/init.d/$1 start
	/etc/init.d/$1 enable
	return 0
}

qmp_disable_service() {
	qmp_log Disabling service $1
	/etc/init.d/$1 stop 2>/dev/null
	/etc/init.d/$1 disable
	return 0
}

qmp_list_services() {
	echo "$(uci show qmp.services | cut -d. -f3 | cut -d= -f1 | grep .)"
}

qmp_set_services() {
	local s
	for s in $(qmp_list_services); do
133

134
		[ "$s" == "vpn" ] && [ -e /etc/init.d/synctincvpn ] && {
Pau Escrich's avatar
Pau Escrich committed
135
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
136
			qmp_enable_service synctincvpn || qmp_disable_service synctincvpn
137

138
		}
139

140
		[ "$s" == "captive_portal" ] && [ -e /etc/init.d/tinyproxy ] && {
Pau Escrich's avatar
Pau Escrich committed
141
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
142
143
			qmp_enable_service tinyproxy || qmp_disable_service tinyproxy
		}
144

145
		[ "$s" == "libremap" ] && [ -e /etc/init.d/libremap ] && {
Pau Escrich's avatar
Pau Escrich committed
146
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
147
			qmp_enable_service libremap || qmp_disable_service libremap
148
		}
149

150
		[ "$s" == "b6m" ] && [ -e /etc/init.d/b6m-spread ] && {
Pau Escrich's avatar
Pau Escrich committed
151
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
152
153
			qmp_enable_service b6m-spread || qmp_disable_service b6m-spread
		}
154

155
		[ "$s" == "gwck" ] && [ -e /etc/init.d/gwck ] && {
156
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
157
158
			qmp_enable_service gwck || qmp_disable_service gwck
		}
159

160
161
162
		[ "$s" == "auto_upgrade" ] && {
			true
		}
163

164
		[ "$s" == "mesh_dns" ] && [ -e /etc/init.d/mdns ] && {
Pau Escrich's avatar
Pau Escrich committed
165
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
166
			qmp_enable_service mdns || qmp_disable_service mdns
167
		}
168

169
		[ "$s" == "bwtest" ] && [ -n "$(which netserver)" ] && {
Pau Escrich's avatar
Pau Escrich committed
170
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
171
172
			qmp_enable_netserver || qmp_disable_netserver
		}
173
174

		[ "$s" == "munin" ] && [ -e /etc/init.d/munin ] && {
Pau Escrich's avatar
Pau Escrich committed
175
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
176
177
178
179
			qmp_enable_service munin || qmp_disable_service munin
		}

		done
180
181
	uci commit qmp
}