Developing lightweight computation at the DSG edge

qmp_system.sh 3.86 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

	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
}

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
}

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

102
		[ "$s" == "vpn" ] && [ -e /etc/init.d/synctincvpn ] && {
Pau Escrich's avatar
Pau Escrich committed
103
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
104
			qmp_enable_service synctincvpn || qmp_disable_service synctincvpn
105

106
		}
107

108
		[ "$s" == "captive_portal" ] && [ -e /etc/init.d/tinyproxy ] && {
Pau Escrich's avatar
Pau Escrich committed
109
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
110
111
			qmp_enable_service tinyproxy || qmp_disable_service tinyproxy
		}
112

113
		[ "$s" == "libremap" ] && [ -e /etc/init.d/libremap ] && {
Pau Escrich's avatar
Pau Escrich committed
114
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
115
			qmp_enable_service libremap || qmp_disable_service libremap
116
		}
117

118
		[ "$s" == "b6m" ] && [ -e /etc/init.d/b6m-spread ] && {
Pau Escrich's avatar
Pau Escrich committed
119
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
120
121
			qmp_enable_service b6m-spread || qmp_disable_service b6m-spread
		}
122

123
		[ "$s" == "gwck" ] && [ -e /etc/init.d/gwck ] && {
124
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
125
126
			qmp_enable_service gwck || qmp_disable_service gwck
		}
127

128
129
130
		[ "$s" == "auto_upgrade" ] && {
			true
		}
131

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

137
		[ "$s" == "bwtest" ] && [ -n "$(which netserver)" ] && {
Pau Escrich's avatar
Pau Escrich committed
138
			[ $(qmp_uci_get services.$s) -eq 1 ] &&
139
140
			qmp_enable_netserver || qmp_disable_netserver
		}
141
142

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

		done
148
149
	uci commit qmp
}