]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.cli
network: Add help tests for start and stop command.
[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() {
23 if [ -n "${1}" ]; then
24 network_config_set $@
25 else
26 network_config_print
27 fi
28}
29
30function cli_device() {
31 local action=${1}
32 shift
33
34 local device
35 local devices=$@
36
37 if [ -z "${devices}" ]; then
38 devices=$(devices_get_all)
39 fi
40
41 case "${action}" in
42 discover)
43 echo "# XXX need to implement --raw here"
44 for device in ${devices}; do
45 cli_device_discover ${device} $@
46 done
47 ;;
48
49 show|"")
50 for device in ${devices}; do
51 cli_device_print ${device}
52 done
53 ;;
54 *)
55 cli_usage device
56 ;;
57 esac
58}
59
60function cli_device_print() {
61 local device=${1}
62
63 if ! device_exists ${device}; then
64 error "Device '${device}' does not exist."
65 return ${EXIT_ERROR}
66 fi
67
68 echo "${device}"
69 echo " Type: $(device_get_type ${device})"
70 echo " Addr: $(device_get_address ${device})"
71 echo
72}
73
74function cli_device_discover() {
75 local device=${1}
76 shift
77
78 local device_type=$(device_get_type ${device})
79 if [ "${device_type}" != "real" ]; then
80 return ${EXIT_OK}
81 fi
82
83 local raw
84
85 while [ $# -gt 0 ]; do
86 case "${1}" in
87 --raw)
88 raw=1
89 ;;
90 esac
91 shift
92 done
93
94 local up
95 device_is_up ${device} && up=1
96 device_set_up ${device}
97
98 enabled raw || echo "${device}"
99
100 local hook
101 local out
102 local ret
103 for hook in $(hooks_get_all); do
104 out=$(hook_exec ${hook} discover ${device})
105 ret=$?
106
107 [ ${ret} -eq ${DISCOVER_NOT_SUPPORTED} ] && continue
108
109 if enabled raw; then
110 case "${ret}" in
111 ${DISCOVER_OK})
112 echo "${hook}: OK"
113 local line
114 while read line; do
115 echo "${hook}: ${line}"
116 done <<<"${out}"
117 ;;
118
119 ${DISCOVER_ERROR})
120 echo "${hook}: FAILED"
121 ;;
122 esac
123 else
124 case "${ret}" in
125 ${DISCOVER_OK})
126 echo " ${hook} was successful."
127 local line
128 while read line; do
129 echo " ${line}"
130 done <<<"${out}"
131 ;;
132
133 ${DISCOVER_ERROR})
134 echo " ${hook} failed."
135 ;;
136 esac
137 fi
138 done
139
140 echo # New line
141
142 [ "${up}" = "1" ] || device_set_down ${device}
143}
144
145function cli_zone() {
146 local action
147 local zone
148
149 if zone_name_is_valid ${1}; then
150 zone=${1}
151 action=${2}
152 shift 2
153
154 case "${action}" in
155 config|down|edit|port|show|status|up)
156 zone_${action} ${zone} $@
157 ;;
158 esac
159 else
160 action=${1}
161 shift
162
163 case "${action}" in
164 create|remove)
165 zone_${action} $@
166 ;;
167 *)
168 error "Unrecognized argument: '${action}'"
169 ;;
170 esac
171 fi
172}
173
174function cli_start() {
1d7bc4f3
MT
175 if cli_help_requested $@; then
176 cli_usage root-start
177 exit ${EXIT_OK}
178 fi
179
1848564d
MT
180 local zones=$(zones_get $@)
181
182 local zone
183 for zone in ${zones}; do
184 zone_up ${zone}
185 done
186}
187
188function cli_stop() {
1d7bc4f3
MT
189 if cli_help_requested $@; then
190 cli_usage root-stop
191 exit ${EXIT_OK}
192 fi
193
1848564d
MT
194 local zones=$(zones_get $@)
195
196 local zone
197 for zone in ${zones}; do
198 zone_down ${zone}
199 done
200}
201
1d7bc4f3
MT
202function cli_help_requested() {
203 local argument
204 for argument in $@; do
205 if [ "${argument}" = "help" -o "${argument}" = "-h" -o "${argument}" = "--help" ]; then
206 return ${EXIT_OK}
207 fi
208 done
209
210 return ${EXIT_ERROR}
211}
212
1848564d
MT
213function cli_usage() {
214 local what=${1}
215
216 case "${what}" in
217 root)
218 echo "${0}: [command] <options ...>"
219 echo
220 echo " start - ..."
221 echo " stop - ..."
222 echo
223 echo " config - ..."
224 echo
225 echo " device - ..."
226 echo " show - ???"
227 echo " zone - ..."
228 echo
229 ;;
1d7bc4f3
MT
230 root-start|root-stop)
231 echo "${0}: ${what#root-} [--local-only|--remote-only|--all|<zone>...]"
232 echo
233 echo " This commands ${what#root-}s all zones by default."
234 echo " One can pass several parameters to only process a subset of all"
235 echo " available zones:"
236 echo
237 echo -e " ${BOLD}--local-only${NORMAL}"
238 echo " Process all local zones which includes every zone without red."
239 echo
240 echo -e " ${BOLD}--remote-only${NORMAL}"
241 echo " Process all remote zones which means only the red ones."
242 echo
243 echo -e " ${BOLD}--all${NORMAL}"
244 echo " Process all zones. This is the default parameter."
245 echo
246 echo " Additionally, you can pass one or more zone names which will"
247 echo " be processed."
248 echo
249 ;;
1848564d
MT
250 usage)
251 echo
252 echo " Run '${0} help' to get information how to use this tool."
253 echo
254 ;;
255 *)
256 error "No help available for this command '${what}'."
1d7bc4f3 257 echo
1848564d
MT
258 ;;
259 esac
1d7bc4f3
MT
260
261 echo "Network configuration tool. Report all bugs to <http://bugs.ipfire.org>."
1848564d 262}