Developing lightweight computation at the DSG edge

qmp_control.sh 5.04 KB
Newer Older
Pau Escrich's avatar
Pau Escrich committed
1
#!/bin/sh
2
3
4
5
6
7
8
9
QMP_PATH="/etc/qmp"

. $QMP_PATH/qmp_common.sh
. $QMP_PATH/qmp_functions.sh
. $QMP_PATH/qmp_gw.sh
. $QMP_PATH/qmp_wireless.sh
. $QMP_PATH/qmp_network.sh
. $QMP_PATH/qmp_update.sh
10
. $QMP_PATH/qmp_system.sh
11
12

offer_default_gw() {
13
	qmp_gw_default offer $1
14
15
16
17
	qmp_gw_apply
}

search_default_gw() {
18
	qmp_gw_default search $1
19
20
21
22
	qmp_gw_apply
}

disable_default_gw() {
23
	qmp_gw_default disable $1
24
	qmp_gw_apply
25
26
}

27
28
29
30
31
reset_wifi() {
	qmp_reset_wifi
	configure_wifi
}

32
configure_wifi() {
33
	# Configure WiFi on qMp config file
34
	qmp_configure_wifi_initial
35
	# Configure WiFi on OpenWrt config file
36
	qmp_configure_wifi
37
	# Configure the whole network to apply wifi settings
38
	configure_network
39
40
}

41
42
43
44
configure_gw() {
	qmp_gw_apply
}

45
46
apply_services() {
	qmp_set_services
47
48
49
50
}

configure_network() {
	qmp_configure
51
	qmp_bmx6_reload
52
	/etc/init.d/network reload
53
	/etc/init.d/network restart
54
	sleep 5 # Let WiFi devices start up
55
56
57
58
	if /etc/init.d/gwck enabled
	then
		/etc/init.d/gwck restart
	fi
59
	/etc/init.d/dnsmasq restart
p4u's avatar
p4u committed
60
	qmp_restart_firewall
61
62
63
64
}

configure_system() {
	qmp_configure_system
65
	apply_services
66
	qmp_bmx6_reload
67
68
69
	/etc/init.d/uhttpd restart
}

70
enable_ns_xm_ppt() {
71
72
73
74
75
	echo 8 > /sys/class/gpio/export
	echo out > /sys/class/gpio/gpio8/direction
	echo 1 > /sys/class/gpio/gpio8/value
}

76
77
78
79
80
81
enable_ns_xw_ppt() {
	echo 2 > /sys/class/gpio/export
	echo out > /sys/class/gpio/gpio2/direction
	echo 1 > /sys/class/gpio/gpio2/value
}

82
83
84
85
86
87
88
89
90
91
92
publish_hna() {
	[ -z "$1" ] && help
	qmp_publish_hna_bmx6 $1 $2
}

unpublish_hna() {
	[ -z "$1" ] && help
	qmp_unpublish_hna_bmx6 $1
}

upgrade() {
p4u's avatar
p4u committed
93
	qmp_update_upgrade_system $1 && hard_reboot
94
95
96
97
98
}

hard_reboot() {
	echo "System is gonna be rebooted now!"
	echo 1 > /proc/sys/kernel/sysrq
99
	echo b > /proc/sysrq-trigger
100
101
}

102
103
104
configure_all() {
	configure_system
	configure_wifi
105
106
	# configure_wifi just called configure_network immediately before
	# configure_network
107
108
}

p4u's avatar
p4u committed
109
110
safe_apply() {
	[ -e "/tmp/qmp.save.tar.gz" ] && echo "Found saved state at /tmp/qmp.save.tar.gz. Make sure you want to use it!" \
111
	|| {
112
113
114
		echo "Cannot found saved state, saving it..."
		save_state
	}
115

116
	touch /tmp/qmp.safe.test
p4u's avatar
p4u committed
117
	echo "------------------------------------------------------------------------------------"
118
	echo "File /tmp/qmp.safe.test has been created, after configuring the system you will have
p4u's avatar
p4u committed
119
120
	180 seconds to remove it or the previous state will be recovered"
	echo "------------------------------------------------------------------------------------"
121

122
123
	read -p'Do you agree?[y,N] ' a
	[ "$a" != "y" ] && return
124

125
	configure_all
126

p4u's avatar
p4u committed
127
	( sleep 180
128
		[ -e "/tmp/qmp.safe.test" ] && {
p4u's avatar
p4u committed
129
			cp -f /etc/config/qmp /tmp/qmp.wrong
130
			recover_state
p4u's avatar
p4u committed
131
			cp -f /tmp/qmp.wrong /etc/config/qmp.wrong
132
133
134
135
136
137
138
			hard_reboot
		} || rm -f "/tmp/qmp.safe.test"
	) &
}

save_state() {
	echo "Saving state at $(date) in file /tmp/qmp.save.tar.gz"
p4u's avatar
p4u committed
139
140
141
	rm -f /tmp/qmp.save.tar.gz 2>/dev/null
	(cd /overlay/etc && tar czf /tmp/qmp.save.tar.gz .)
	[ ! -e /tmp/qmp.save.tar.gz ] && echo "ERROR: cannot save state, exiting..." && exit 1
142
143
144
145
}

recover_state() {
	echo "Recovering state at $(date) from /tmp/qmp.save.tar.gz"
p4u's avatar
p4u committed
146
147
	[ -e "/tmp/qmp.save.tar.gz" ] && tar xvzf /tmp/qmp.save.tar.gz -C /overlay/etc/ \
	|| echo "Cannot recover state because it has not been saved before"
148
149
}

150
151
help() {
	echo "Use: $0 <function> [params]"
152

153
	echo ""
154
	echo "Configuration:"
p4u's avatar
p4u committed
155
156
157
158
159
	echo " configure_all			: Configure and apply all settings"
	echo " configure_network		: Configure and apply network settings"
	echo " configure_system 		: Configure and apply system settings (qmp.node section and so on)"
	echo " configure_wifi			: Configure all WiFi devices"
	echo " reset_wifi			: Reset, rescan and configure all the WiFi devices"
160
	echo " configure_gw			: Configure and apply gateways settings"
161

162
	echo ""
p4u's avatar
p4u committed
163
164
165
	echo "Safe configuration:"
	echo " save_state			: Saves current state of configuration files"
	echo " recover_state			: Recovers previous saved state"
166
	echo " safe_apply			: Performs a safe configure_all. If something goes wrong, it comes back to the previous state"
167

168
169
	echo ""
	echo "Gateways:"
p4u's avatar
p4u committed
170
171
172
173
174
	echo " offer_default_gw [ipv4|ipv6]	: Offers default gw to the network IPv4 or IPv6, both versions if no value"
	echo " search_default_gw [ipv4|ipv6]	: Search for a default gw in the network IPv4 or IPv6, both versions if no value"
	echo " disable_default_gw [ipv4|ipv6]	: Disables the search/offer of default GW IPv4 and/or IPv6"
	echo " publish_hna			: Publish an IP range (v4 or v6): publish_hna <IP/NETMASK> [ID]"
	echo " unpublish_hna			: Unpublish a current HNA: unpublish_hna <ID>"
175
176
177

	echo ""
	echo "Other:"
178
	echo " apply_services			: Start/stop services depending on qmp configuration"
179
180
	echo " enable_ns_xm_ppt		: Enable PoE passtrough from NanoStation XM M2/M5 devices. Be careful with this option!"
	echo " enable_ns_xw_ppt		: Enable PoE passtrough from NanoStation XW M2/M5 devices. Be careful with this option!"
p4u's avatar
p4u committed
181
	echo " upgrade [URL]			: Upgrade system. By default to the last version, but image url can be provided to force"
182
	echo " hard_reboot			: Performs a hard reboot (using kernel sysrq)"
183

184
	echo ""
185
	exit 0
186
187
188
189
190
}


[ -z "$1" ] && help

191
echo "executing function $1..."
192

193
$@ # 2>/dev/null || echo " Command not found.\n" && help