]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.cli
network: Make restart timeout configureable.
[people/arne_f/network.git] / functions.cli
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22function cli_config() {
fe688aa4
MT
23 if cli_help_requested $@; then
24 cli_usage root-config
25 exit ${EXIT_OK}
26 fi
27
1848564d
MT
28 if [ -n "${1}" ]; then
29 network_config_set $@
30 else
31 network_config_print
32 fi
33}
34
35function cli_device() {
36 local action=${1}
37 shift
38
39 local device
40 local devices=$@
41
42 if [ -z "${devices}" ]; then
43 devices=$(devices_get_all)
44 fi
45
46 case "${action}" in
47 discover)
48 echo "# XXX need to implement --raw here"
49 for device in ${devices}; do
50 cli_device_discover ${device} $@
51 done
52 ;;
53
54 show|"")
55 for device in ${devices}; do
56 cli_device_print ${device}
57 done
58 ;;
59 *)
60 cli_usage device
61 ;;
62 esac
63}
64
65function cli_device_print() {
66 local device=${1}
67
68 if ! device_exists ${device}; then
69 error "Device '${device}' does not exist."
70 return ${EXIT_ERROR}
71 fi
72
73 echo "${device}"
74 echo " Type: $(device_get_type ${device})"
75 echo " Addr: $(device_get_address ${device})"
76 echo
77}
78
79function cli_device_discover() {
80 local device=${1}
81 shift
82
83 local device_type=$(device_get_type ${device})
84 if [ "${device_type}" != "real" ]; then
85 return ${EXIT_OK}
86 fi
87
88 local raw
89
90 while [ $# -gt 0 ]; do
91 case "${1}" in
92 --raw)
93 raw=1
94 ;;
95 esac
96 shift
97 done
98
99 local up
100 device_is_up ${device} && up=1
101 device_set_up ${device}
102
103 enabled raw || echo "${device}"
104
105 local hook
106 local out
107 local ret
108 for hook in $(hooks_get_all); do
109 out=$(hook_exec ${hook} discover ${device})
110 ret=$?
111
112 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
113
114 if enabled raw; then
115 case "${ret}" in
116 ${DISCOVER_OK})
117 echo "${hook}: OK"
118 local line
119 while read line; do
120 echo "${hook}: ${line}"
121 done <<<"${out}"
122 ;;
123
124 ${DISCOVER_ERROR})
125 echo "${hook}: FAILED"
126 ;;
127 esac
128 else
129 case "${ret}" in
130 ${DISCOVER_OK})
131 echo " ${hook} was successful."
132 local line
133 while read line; do
134 echo " ${line}"
135 done <<<"${out}"
136 ;;
137
138 ${DISCOVER_ERROR})
139 echo " ${hook} failed."
140 ;;
141 esac
142 fi
143 done
144
145 echo # New line
146
147 [ "${up}" = "1" ] || device_set_down ${device}
148}
149
150function cli_zone() {
151 local action
152 local zone
153
154 if zone_name_is_valid ${1}; then
155 zone=${1}
156 action=${2}
157 shift 2
158
159 case "${action}" in
160 config|down|edit|port|show|status|up)
161 zone_${action} ${zone} $@
162 ;;
163 esac
164 else
165 action=${1}
166 shift
167
168 case "${action}" in
169 create|remove)
170 zone_${action} $@
171 ;;
172 *)
173 error "Unrecognized argument: '${action}'"
174 ;;
175 esac
176 fi
177}
178
179function cli_start() {
1d7bc4f3
MT
180 if cli_help_requested $@; then
181 cli_usage root-start
182 exit ${EXIT_OK}
183 fi
184
1848564d
MT
185 local zones=$(zones_get $@)
186
187 local zone
188 for zone in ${zones}; do
189 zone_up ${zone}
190 done
191}
192
193function cli_stop() {
1d7bc4f3
MT
194 if cli_help_requested $@; then
195 cli_usage root-stop
196 exit ${EXIT_OK}
197 fi
198
1848564d
MT
199 local zones=$(zones_get $@)
200
201 local zone
202 for zone in ${zones}; do
203 zone_down ${zone}
204 done
205}
206
bcef495d
MT
207function cli_restart() {
208 if cli_help_requested $@; then
209 cli_usage root-restart
210 exit ${EXIT_OK}
211 fi
212
213 cli_stop $@
214
215 # Give the system some time to calm down
2ae4f579 216 sleep ${TIMEOUT_RESTART}
bcef495d
MT
217
218 cli_start $@
219}
220
1d7bc4f3
MT
221function cli_help_requested() {
222 local argument
223 for argument in $@; do
224 if [ "${argument}" = "help" -o "${argument}" = "-h" -o "${argument}" = "--help" ]; then
225 return ${EXIT_OK}
226 fi
227 done
228
229 return ${EXIT_ERROR}
230}
231
1848564d
MT
232function cli_usage() {
233 local what=${1}
234
235 case "${what}" in
236 root)
237 echo "${0}: [command] <options ...>"
238 echo
239 echo " start - ..."
240 echo " stop - ..."
bcef495d 241 echo " restart - ..."
1848564d
MT
242 echo
243 echo " config - ..."
244 echo
245 echo " device - ..."
246 echo " show - ???"
247 echo " zone - ..."
248 echo
249 ;;
fe688aa4
MT
250 root-config)
251 echo "${0}: ${what#root-} [KEY=VAL, ...]"
252 echo
253 echo " This command allows setting of global configuration parameters."
254 echo
255 echo " If no additional arguments are passed it will list the current configuration."
256 echo
257 echo " You can overwrite the settings like the following:"
258 echo
259 echo " ${0} ${what#root-} DEBUG=1 ..."
260 echo
261 ;;
bcef495d 262 root-start|root-stop|root-restart)
1d7bc4f3
MT
263 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
264 echo
265 echo " This commands ${what#root-}s all zones by default."
266 echo " One can pass several parameters to only process a subset of all"
267 echo " available zones:"
268 echo
269 echo -e " ${BOLD}--local-only${NORMAL}"
270 echo " Process all local zones which includes every zone without red."
271 echo
272 echo -e " ${BOLD}--remote-only${NORMAL}"
273 echo " Process all remote zones which means only the red ones."
274 echo
275 echo -e " ${BOLD}--all${NORMAL}"
276 echo " Process all zones. This is the default parameter."
277 echo
278 echo " Additionally, you can pass one or more zone names which will"
279 echo " be processed."
280 echo
281 ;;
1848564d
MT
282 usage)
283 echo
284 echo " Run '${0} help' to get information how to use this tool."
285 echo
286 ;;
287 *)
288 error "No help available for this command '${what}'."
1d7bc4f3 289 echo
1848564d
MT
290 ;;
291 esac
1d7bc4f3
MT
292
293 echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
1848564d 294}