Developing lightweight computation at the DSG edge

qmp_system.sh 4.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/sh
#/etc/rc.common
#    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#    The full GNU General Public License is included in this distribution in
#    the file called "COPYING".
#
# Contributors:
#	Axel Neumann
#	Pau Escrich <p4u@dabax.net>
#	Simó Albert i Beltran
#

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() {

	if qmp_uci_test qmp.node.community_node_id; then
		local community_node_id=$(qmp_get_id)
	else
		local community_node_id=$(qmp_get_id)
		qmp_uci_set node.community_node_id $community_node_id
	fi

	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
	uci set system.@system[0].hostname=${community_id}${community_node_id}
	uci commit system
	echo "${community_id}${community_node_id}" > /proc/sys/kernel/hostname

	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
		
		[ "$s" == "vpn" ] && [ -e /etc/init.d/synctincvpn ] && {
			[ $(qmp_uci_get services.$s) -eq 1 ] && \
			qmp_enable_service synctincvpn || qmp_disable_service synctincvpn
		
		}
		
		[ "$s" == "captive_portal" ] && [ -e /etc/init.d/tinyproxy ] && {
			[ $(qmp_uci_get services.$s) -eq 1 ] && \
			qmp_enable_service tinyproxy || qmp_disable_service tinyproxy
		}
		
		[ "$s" == "altermap" ] && [ -e /etc/init.d/altermap-agent ] && {
			[ $(qmp_uci_get services.$s) -eq 1 ] && \
			qmp_enable_service altermap-agent || qmp_disable_service altermap-agent
		}
		
		[ "$s" == "b6m" ] && [ -e /etc/init.d/b6m-spread ] && {
			[ $(qmp_uci_get services.$s) -eq 1 ] && \
			qmp_enable_service b6m-spread || qmp_disable_service b6m-spread
		}
		
		[ "$s" == "gwck" ] && [ -e /etc/init.d/gwck ] && {
			[ $(qmp_uci_get services.$s) -eq 1 ] && \
			qmp_enable_service gwck || qmp_disable_service gwck
		}
		
		[ "$s" == "auto_upgrade" ] && {
			true
		}
		
		[ "$s" == "mesh_dns" ] && {
			true
		}
		
		[ "$s" == "bwtest" ] && [ -n "$(which netserver)" ] && {
			[ $(qmp_uci_get services.$s) -eq 1 ] && \
			qmp_enable_netserver || qmp_disable_netserver
		}
	done
	uci commit qmp
}