Developing lightweight computation at the DSG edge

qmp_system.sh 4.91 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
21
	fi

	[ $(echo -n $device_id | wc -c) -lt 4 ] && {
		qmp_log "Warning, device_id not defined properly, using failsafe 0000"
		device_id=0000
22
		qmp_uci_set node.device_id $device_id
23
24
25
26
27
28
	}

	if [ -n "$(qmp_uci_get node.mesh_name)" ]; then
		local mesh_name=$(qmp_uci_get node.mesh_name)
	else
		local mesh_name=""
29
	fi
30

31
32
	local device_name="$(qmp_uci_get node.device_name)"
	[ -z "$device_name" ] && device_name="qMp" && qmp_uci_set node.device_name $device_name
33
34

	# set hostname
35
36
37
38
39
40
41
42
43
44
45
46
	local append_id=0
	if [ -n "$(qmp_uci_get node.append_id)" ]; then
		append_id="$(uci get qmp.node.append_id)"
	fi

	local prepend_cnm=0
	if [ -n "$(qmp_uci_get node.prepend_cnm)" ]; then
		prepend_cnm="$(uci get qmp.node.prepend_cnm)"
	fi

	if [ $append_id -eq 1 ]; then
		device_name="${device_name}-${device_id}"
47
48
	fi

49
50
51
52
53
54
55
56
	if [ $prepend_cnm -eq 1 ] && [ -n "${mesh_name}" ]; then
		device_name="${mesh_name}-${device_name}"
	fi

	uci set system.@system[0].hostname="${device_name}"
	echo "${device_name}" > /proc/sys/kernel/hostname

	uci commit system
57
58
59
60
61
62
63
64

	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
65
66
67

	# configure maps
	qmp_set_maps
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
}

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
}

88
89
90
91
qmp_set_maps() {
	[ $(qmp_uci_get services.libremap) -eq 1 ] && {
		qmp_uci_get_raw libremap.location && {
			local lat="$(qmp_uci_get node.latitude)"
92
			local lon="$(qmp_uci_get node.longitude)"
93
94
95
96
			local elev="$(qmp_uci_get node.elevation)"
			[ -n "$lat" ] && [ -n "$lon" ] && {
				elev=${elev:-0}
				qmp_uci_set_raw libremap.location.latitude="$lat"
97
				qmp_uci_set_raw libremap.location.longitude="$lon"
98
				qmp_uci_set_raw libremap.location.elev="$elev"
99
100
101
102
103
				
				uci set libremap.qmp.enabled=1
				uci set libremap.bmx6.enabled=1
				uci set libremap.wireless.enabled=1
				uci commit
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
# -----------------------------------
#          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
151

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

156
		}
157

158
		[ "$s" == "captive_portal" ] && [ -e /etc/init.d/tinyproxy ] && {
Pau Escrich's avatar
Pau Escrich committed
159
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
160
161
			qmp_enable_service tinyproxy || qmp_disable_service tinyproxy
		}
162

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

168
		[ "$s" == "b6m" ] && [ -e /etc/init.d/b6m-spread ] && {
Pau Escrich's avatar
Pau Escrich committed
169
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
170
171
			qmp_enable_service b6m-spread || qmp_disable_service b6m-spread
		}
172

173
		[ "$s" == "gwck" ] && [ -e /etc/init.d/gwck ] && {
174
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
175
176
			qmp_enable_service gwck || qmp_disable_service gwck
		}
177

178
179
180
		[ "$s" == "auto_upgrade" ] && {
			true
		}
181

182
		[ "$s" == "mesh_dns" ] && [ -e /etc/init.d/mdns ] && {
Pau Escrich's avatar
Pau Escrich committed
183
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
184
			qmp_enable_service mdns || qmp_disable_service mdns
185
		}
186

187
		[ "$s" == "bwtest" ] && [ -n "$(which netserver)" ] && {
Pau Escrich's avatar
Pau Escrich committed
188
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
189
190
			qmp_enable_netserver || qmp_disable_netserver
		}
191
192

		[ "$s" == "munin" ] && [ -e /etc/init.d/munin ] && {
Pau Escrich's avatar
Pau Escrich committed
193
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
194
195
196
197
			qmp_enable_service munin || qmp_disable_service munin
		}

		done
198
199
	uci commit qmp
}