Developing lightweight computation at the DSG edge

qmp_system.sh 4.36 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
	if [ -n "$(qmp_uci_get node.community_node_id)" ]; then
		local community_node_id=$(qmp_uci_get node.community_node_id)
13
	else
14
		local community_node_id=$(qmp_get_id_hostname)
15
16
		qmp_uci_set node.community_node_id $community_node_id
	fi
17

18
19
	# check if community_node_id is hexadecimal and get last 4 characters
	community_node_id="$(echo -n $community_node_id | tr -cd 'ABCDEFabcdef0123456789' | tail -c 4)"
20

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

26
27
28
29
	local community_id="$(qmp_uci_get node.community_id)"
	[ -z "$community_id" ] && community_id="qmp" && qmp_uci_set node.community_id $community_id

	# set hostname
30
	uci set system.@system[0].hostname="${community_id}-${community_node_id}"
31
	uci commit system
32
	echo "${community_id}-${community_node_id}" > /proc/sys/kernel/hostname
33
34
35
36
37
38
39
40

	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
41
42
43

	# configure maps
	qmp_set_maps
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
}

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
}

64
65
66
67
qmp_set_maps() {
	[ $(qmp_uci_get services.libremap) -eq 1 ] && {
		qmp_uci_get_raw libremap.location && {
			local lat="$(qmp_uci_get node.latitude)"
68
			local lon="$(qmp_uci_get node.longitude)"
69
70
71
72
			local elev="$(qmp_uci_get node.elevation)"
			[ -n "$lat" ] && [ -n "$lon" ] && {
				elev=${elev:-0}
				qmp_uci_set_raw libremap.location.latitude="$lat"
73
				qmp_uci_set_raw libremap.location.longitude="$lon"
74
75
76
77
78
79
80
81
				qmp_uci_set_raw libremap.location.elev="$elev"
			}
		}
	}


}

82
83
84
85
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
# -----------------------------------
#          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
122

123
		[ "$s" == "vpn" ] && [ -e /etc/init.d/synctincvpn ] && {
Pau Escrich's avatar
Pau Escrich committed
124
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
125
			qmp_enable_service synctincvpn || qmp_disable_service synctincvpn
126

127
		}
128

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

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

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

144
		[ "$s" == "gwck" ] && [ -e /etc/init.d/gwck ] && {
145
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
146
147
			qmp_enable_service gwck || qmp_disable_service gwck
		}
148

149
150
151
		[ "$s" == "auto_upgrade" ] && {
			true
		}
152

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

158
		[ "$s" == "bwtest" ] && [ -n "$(which netserver)" ] && {
Pau Escrich's avatar
Pau Escrich committed
159
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
160
161
			qmp_enable_netserver || qmp_disable_netserver
		}
162
163

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

		done
169
170
	uci commit qmp
}