Developing lightweight computation at the DSG edge

qmp_system.sh 4.46 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
17
		local device_id=$(qmp_get_id_hostname)
		device_id="$(echo -n $device_id | tr -cd 'ABCDEFabcdef0123456789' | tail -c 4)"
		qmp_uci_set node.device_id $device_id
18
	fi
19

20
21
22
	[ $(echo -n $device_id | wc -c) -lt 4 ] && {
		qmp_log "Warning, device_id not defined properly, using failsafe 0000"
		device_id=0000
23
	}
24

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

	# set hostname
29
30
31
32
33
34
35
36
37
	local ig_roaming="$(uci get qmp.roaming.ignore)"
	
	if [ $ig_roaming -eq 1 ]; then
		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
38
	uci commit system
39

40
41
42
43
44
45
46
47

	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
48
49
50

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

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
}

71
72
73
74
qmp_set_maps() {
	[ $(qmp_uci_get services.libremap) -eq 1 ] && {
		qmp_uci_get_raw libremap.location && {
			local lat="$(qmp_uci_get node.latitude)"
75
			local lon="$(qmp_uci_get node.longitude)"
76
77
78
79
			local elev="$(qmp_uci_get node.elevation)"
			[ -n "$lat" ] && [ -n "$lon" ] && {
				elev=${elev:-0}
				qmp_uci_set_raw libremap.location.latitude="$lat"
80
				qmp_uci_set_raw libremap.location.longitude="$lon"
81
82
83
84
85
86
87
88
				qmp_uci_set_raw libremap.location.elev="$elev"
			}
		}
	}


}

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
# -----------------------------------
#          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
129

130
		[ "$s" == "vpn" ] && [ -e /etc/init.d/synctincvpn ] && {
Pau Escrich's avatar
Pau Escrich committed
131
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
132
			qmp_enable_service synctincvpn || qmp_disable_service synctincvpn
133

134
		}
135

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

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

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

151
		[ "$s" == "gwck" ] && [ -e /etc/init.d/gwck ] && {
152
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
153
154
			qmp_enable_service gwck || qmp_disable_service gwck
		}
155

156
157
158
		[ "$s" == "auto_upgrade" ] && {
			true
		}
159

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

165
		[ "$s" == "bwtest" ] && [ -n "$(which netserver)" ] && {
Pau Escrich's avatar
Pau Escrich committed
166
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
167
168
			qmp_enable_netserver || qmp_disable_netserver
		}
169
170

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

		done
176
177
	uci commit qmp
}