]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | function zone_dir() { | |
23 | local zone=${1} | |
24 | ||
d2a21d01 | 25 | echo "${NETWORK_ZONE_DIR}/zones/${zone}" |
1848564d MT |
26 | } |
27 | ||
28 | function zone_exists() { | |
29 | local zone=${1} | |
711ffac1 MT |
30 | assert isset zone |
31 | ||
1848564d MT |
32 | [ -d "$(zone_dir ${zone})" ] |
33 | } | |
34 | ||
35 | function zone_match() { | |
36 | local match | |
37 | ||
38 | local i | |
39 | for i in ${VALID_ZONES}; do | |
40 | match="${match}|${i}[0-9]{1,5}" | |
41 | done | |
42 | ||
43 | echo "${match:1:${#match}}" | |
44 | } | |
45 | ||
46 | function zone_name_is_valid() { | |
47 | local zone=${1} | |
4fedddef MT |
48 | |
49 | # Don't accept empty strings. | |
50 | [ -z "${zone}" ] && return ${EXIT_FALSE} | |
711ffac1 | 51 | |
1848564d MT |
52 | [[ ${zone} =~ $(zone_match) ]] |
53 | } | |
54 | ||
55 | function zone_is_local() { | |
56 | local zone=${1} | |
57 | ||
7de0637a | 58 | [[ "${zone:0:${#ZONE_LOCAL}}" = "${ZONE_LOCAL}" ]] |
5e42d659 MT |
59 | } |
60 | ||
61 | function zone_is_nonlocal() { | |
62 | local zone=${1} | |
63 | ||
7de0637a | 64 | [[ "${zone:0:${#ZONE_NONLOCAL}}" = "${ZONE_NONLOCAL}" ]] |
1848564d MT |
65 | } |
66 | ||
67 | function zone_get_hook() { | |
68 | local zone=${1} | |
711ffac1 MT |
69 | assert isset zone |
70 | ||
1848564d MT |
71 | config_get_hook $(zone_dir ${zone})/settings |
72 | } | |
73 | ||
5bb2429a MT |
74 | function zone_start() { |
75 | # This function will bring up the zone | |
76 | # 'asynchronously' with help of systemd. | |
77 | ||
78 | local zone=${1} | |
79 | assert zone_exists ${zone} | |
80 | ||
1d08b9b3 | 81 | service_start "network@${zone}.service" |
5bb2429a MT |
82 | } |
83 | ||
84 | function zone_stop() { | |
85 | # This function will bring down the zone | |
86 | # 'asynchronously' with help of systemd. | |
87 | ||
88 | local zone=${1} | |
89 | assert zone_exists ${zone} | |
90 | ||
1d08b9b3 | 91 | service_stop "network@${zone}.service" |
5bb2429a MT |
92 | } |
93 | ||
e6fd23fd MT |
94 | function zone_reload() { |
95 | local zone="${1}" | |
96 | assert zone_exists "${zone}" | |
97 | ||
98 | service_reload "network@${zone}.service" | |
99 | } | |
100 | ||
fb8c7c92 MT |
101 | function zone_hotplug_event() { |
102 | local zone="${1}" | |
103 | assert zone_exists "${zone}" | |
104 | ||
105 | # If the zone has already been started, we | |
106 | # will reload it so the current configuration | |
107 | # is re-applied. | |
108 | if zone_is_active "${zone}"; then | |
109 | zone_reload "${zone}" | |
110 | return ${?} | |
111 | ||
112 | # If the zone is still down, but in auto-start mode, | |
113 | # we will start it. | |
114 | elif zone_is_enabled "${zone}"; then | |
115 | zone_start "${zone}" | |
116 | return ${?} | |
117 | fi | |
118 | ||
119 | # Otherwise, nothing will be done. | |
120 | return ${EXIT_OK} | |
121 | } | |
122 | ||
5c5b8e36 SS |
123 | function zone_enable() { |
124 | # This function will enable the zone | |
125 | # with help of systemd. | |
126 | ||
127 | local zone="${1}" | |
128 | assert zone_exists "${zone}" | |
129 | ||
130 | # Enable service for the zone | |
131 | service_enable "network@${zone}.service" | |
132 | local ret=$? | |
133 | ||
134 | if [ ${ret} -eq ${EXIT_OK} ]; then | |
135 | log INFO "Auto-start enabled for zone ${zone}" | |
136 | return ${EXIT_OK} | |
137 | fi | |
138 | ||
139 | log ERROR "Could not enable zone ${zone}: ${ret}" | |
140 | return ${ret} | |
141 | } | |
142 | ||
143 | function zone_disable() { | |
144 | # This function will disable the zone | |
145 | # with help of systemd. | |
146 | ||
147 | local zone="${1}" | |
148 | assert zone_exists "${zone}" | |
149 | ||
150 | # Disable service for the zone | |
151 | service_disable "network@${zone}.service" | |
152 | local ret=$? | |
153 | ||
154 | if [ ${ret} -eq ${EXIT_OK} ]; then | |
155 | log INFO "Auto-start disabled for zone ${zone}" | |
156 | return ${EXIT_OK} | |
157 | fi | |
158 | ||
159 | log ERROR "Could not disable zone ${zone}: ${ret}" | |
160 | return ${ret} | |
161 | } | |
162 | ||
163 | function zone_is_enabled() { | |
164 | local zone="${1}" | |
165 | assert isset zone | |
166 | ||
167 | # Ask systemd if the zone is enabled. | |
168 | if service_is_enabled "network@${zone}.service"; then | |
169 | return ${EXIT_TRUE} | |
170 | fi | |
171 | ||
172 | return ${EXIT_FALSE} | |
173 | } | |
174 | ||
e6fd23fd MT |
175 | function zone_is_active() { |
176 | local zone="${1}" | |
177 | assert isset zone | |
178 | ||
179 | if service_is_active "network@${zone}.service"; then | |
180 | return ${EXIT_TRUE} | |
181 | fi | |
182 | ||
183 | return ${EXIT_FALSE} | |
184 | } | |
185 | ||
1848564d MT |
186 | function zone_create() { |
187 | local zone=${1} | |
188 | local hook=${2} | |
189 | shift 2 | |
190 | ||
191 | if ! zone_name_is_valid ${zone}; then | |
192 | error "Zone name '${zone}' is not valid." | |
193 | return ${EXIT_ERROR} | |
194 | fi | |
195 | ||
196 | if zone_exists ${zone}; then | |
197 | error "Zone '${zone}' does already exist." | |
198 | return ${EXIT_ERROR} | |
199 | fi | |
200 | ||
d61a01d4 | 201 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
202 | error "Hook '${hook}' does not exist." |
203 | return ${EXIT_ERROR} | |
204 | fi | |
205 | ||
206 | mkdir -p $(zone_dir ${zone}) | |
207 | ||
a5ebb169 MT |
208 | # Create directories for configs and ports |
209 | mkdir -p $(zone_dir ${zone})/{configs,ports} | |
943e3f7e | 210 | |
d61a01d4 | 211 | hook_zone_exec ${hook} create ${zone} $@ |
1848564d MT |
212 | local ret=$? |
213 | ||
214 | # Maybe the zone create hook did not exit correctly. | |
215 | # If this is the case we remove the created zone immediately. | |
216 | if [ "${ret}" = "${EXIT_ERROR}" ]; then | |
69ace22b | 217 | zone_remove_now ${zone} |
5c5b8e36 | 218 | return ${EXIT_ERROR} |
1848564d | 219 | fi |
5c5b8e36 SS |
220 | |
221 | # Automatically enable zone. | |
222 | zone_enable "${zone}" | |
1848564d MT |
223 | } |
224 | ||
225 | function zone_edit() { | |
226 | local zone=${1} | |
227 | shift | |
228 | ||
229 | if ! zone_exists ${zone}; then | |
230 | error "Zone '${zone}' does not exist." | |
231 | return ${EXIT_ERROR} | |
232 | fi | |
233 | ||
69ace22b MT |
234 | # Check if the zone is tagged for removal. |
235 | if zone_has_remove_tag ${zone}; then | |
236 | error "You cannot edit a zone that is tagged for removal." | |
237 | return ${EXIT_ERROR} | |
238 | fi | |
239 | ||
2472e0ea | 240 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
241 | if [ -z "${hook}" ]; then |
242 | error "Config file did not provide any hook." | |
243 | return ${EXIT_ERROR} | |
244 | fi | |
245 | ||
d61a01d4 | 246 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
247 | error "Hook '${hook}' does not exist." |
248 | return ${EXIT_ERROR} | |
249 | fi | |
250 | ||
d61a01d4 | 251 | hook_zone_exec ${hook} edit ${zone} $@ |
1848564d MT |
252 | } |
253 | ||
69ace22b | 254 | |
1848564d MT |
255 | function zone_remove() { |
256 | local zone=${1} | |
69ace22b | 257 | assert zone_exists ${zone} |
1848564d | 258 | |
69ace22b MT |
259 | # Make the zone for removal. |
260 | touch $(zone_dir ${zone})/.remove | |
261 | ||
262 | log INFO "Zone '${zone}' has been tagged for removal." | |
263 | } | |
264 | ||
265 | function zone_has_remove_tag() { | |
266 | local zone=${1} | |
267 | assert zone_exists ${zone} | |
268 | ||
269 | [ -e "$(zone_dir ${zone})/.remove" ] | |
270 | } | |
271 | ||
272 | # This function will remove the given zone | |
273 | # RIGHT NOW. Use zone_remove to remove it | |
274 | # at the next status change. | |
275 | function zone_remove_now() { | |
276 | local zone=${1} | |
277 | assert zone_exists ${zone} | |
278 | ||
279 | log INFO "Removing zone '${zone}' right now." | |
1848564d | 280 | |
69ace22b MT |
281 | # Force the zone down. |
282 | zone_is_up ${zone} && zone_set_down ${zone} | |
1848564d | 283 | |
5c5b8e36 SS |
284 | # Disable zone. |
285 | zone_disable "${zone}" | |
286 | ||
1848564d MT |
287 | rm -rf $(zone_dir ${zone}) |
288 | } | |
289 | ||
290 | function zone_up() { | |
291 | local zone=${1} | |
292 | shift | |
293 | ||
294 | if ! zone_exists ${zone}; then | |
295 | error "Zone '${zone}' does not exist." | |
296 | return ${EXIT_ERROR} | |
297 | fi | |
298 | ||
69ace22b MT |
299 | # Check if a zone has got the remove tag. |
300 | if zone_has_remove_tag ${zone}; then | |
301 | error "Cannot bring up any zone which is to be removed." | |
302 | return ${EXIT_ERROR} | |
303 | fi | |
304 | ||
2472e0ea | 305 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
306 | if [ -z "${hook}" ]; then |
307 | error "Config file did not provide any hook." | |
308 | return ${EXIT_ERROR} | |
309 | fi | |
310 | ||
d61a01d4 | 311 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
312 | error "Hook '${hook}' does not exist." |
313 | return ${EXIT_ERROR} | |
314 | fi | |
315 | ||
059469a8 MT |
316 | zone_db ${zone} starting |
317 | ||
d61a01d4 MT |
318 | hook_zone_exec ${hook} up ${zone} $@ |
319 | ||
059469a8 | 320 | zone_db ${zone} started |
1848564d MT |
321 | } |
322 | ||
323 | function zone_down() { | |
324 | local zone=${1} | |
325 | shift | |
326 | ||
327 | if ! zone_exists ${zone}; then | |
328 | error "Zone '${zone}' does not exist." | |
329 | return ${EXIT_ERROR} | |
330 | fi | |
331 | ||
2472e0ea | 332 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
333 | if [ -z "${hook}" ]; then |
334 | error "Config file did not provide any hook." | |
335 | return ${EXIT_ERROR} | |
336 | fi | |
337 | ||
d61a01d4 | 338 | if ! hook_zone_exists ${hook}; then |
1848564d MT |
339 | error "Hook '${hook}' does not exist." |
340 | return ${EXIT_ERROR} | |
341 | fi | |
342 | ||
059469a8 MT |
343 | zone_db ${zone} stopping |
344 | ||
d61a01d4 | 345 | hook_zone_exec ${hook} down ${zone} $@ |
059469a8 MT |
346 | |
347 | zone_db ${zone} stopped | |
69ace22b MT |
348 | |
349 | # Remove the zone, if it has got a remove tag. | |
350 | if zone_has_remove_tag ${zone}; then | |
351 | zone_remove_now ${zone} | |
352 | fi | |
1848564d MT |
353 | } |
354 | ||
355 | function zone_status() { | |
2472e0ea MT |
356 | local zone="${1}" |
357 | assert isset zone | |
1848564d MT |
358 | shift |
359 | ||
2472e0ea | 360 | if ! zone_exists "${zone}"; then |
1848564d MT |
361 | error "Zone '${zone}' does not exist." |
362 | return ${EXIT_ERROR} | |
363 | fi | |
364 | ||
2472e0ea | 365 | local hook="$(zone_get_hook "${zone}")" |
1848564d MT |
366 | if [ -z "${hook}" ]; then |
367 | error "Config file did not provide any hook." | |
368 | return ${EXIT_ERROR} | |
369 | fi | |
370 | ||
2472e0ea | 371 | if ! hook_zone_exists "${hook}"; then |
1848564d MT |
372 | error "Hook '${hook}' does not exist." |
373 | return ${EXIT_ERROR} | |
374 | fi | |
375 | ||
2472e0ea | 376 | hook_zone_exec "${hook}" "status" "${zone}" "$@" |
69ace22b MT |
377 | |
378 | # Show that the zone it to be removed soon. | |
379 | if zone_has_remove_tag ${zone}; then | |
380 | warning "This zone is tagged for removal." | |
381 | fi | |
1848564d MT |
382 | } |
383 | ||
711ffac1 MT |
384 | function zone_port() { |
385 | local zone=${1} | |
386 | local action=${2} | |
387 | shift 2 | |
388 | ||
389 | assert isset zone | |
390 | assert isset action | |
391 | assert zone_exists ${zone} | |
392 | ||
393 | case "${action}" in | |
828eb94a | 394 | add|edit|remove) |
943e3f7e MT |
395 | zone_port_${action} ${zone} $@ |
396 | ;; | |
397 | *) | |
398 | error "Unrecognized argument: ${action}" | |
399 | cli_usage root-zone-port-subcommands | |
400 | exit ${EXIT_ERROR} | |
401 | ;; | |
402 | esac | |
711ffac1 MT |
403 | } |
404 | ||
405 | function zone_port_add() { | |
828eb94a | 406 | local zone="${1}" |
711ffac1 MT |
407 | assert isset zone |
408 | ||
828eb94a MT |
409 | local port="${2}" |
410 | assert isset port | |
411 | ||
412 | shift 2 | |
711ffac1 | 413 | |
828eb94a MT |
414 | # Check if the port actually exists. |
415 | if ! port_exists "${port}"; then | |
416 | error "Cannot add port '${port}' which does not exist" | |
417 | return ${EXIT_ERROR} | |
418 | fi | |
419 | ||
420 | # Check if the port is already connected to this or any other zone. | |
421 | local z | |
422 | for z in $(zones_get_all); do | |
423 | if zone_has_port "${z}" "${port}"; then | |
424 | error "Port '${port}' is already assigned to zone '${z}'" | |
425 | return ${EXIT_ERROR} | |
426 | fi | |
427 | done | |
428 | ||
429 | local hook=$(zone_get_hook "${zone}") | |
711ffac1 MT |
430 | assert isset hook |
431 | ||
828eb94a | 432 | hook_zone_exec "${hook}" "port_add" "${zone}" "${port}" "$@" |
711ffac1 MT |
433 | } |
434 | ||
435 | function zone_port_edit() { | |
828eb94a MT |
436 | local zone="${1}" |
437 | assert isset zone | |
711ffac1 | 438 | |
828eb94a MT |
439 | local port="${2}" |
440 | assert isset port | |
711ffac1 | 441 | |
828eb94a | 442 | shift 2 |
711ffac1 | 443 | |
828eb94a MT |
444 | # Check if the port actually exists. |
445 | if ! port_exists "${port}"; then | |
446 | error "Port '${port}' does not exist" | |
447 | return ${EXIT_ERROR} | |
448 | fi | |
711ffac1 | 449 | |
828eb94a MT |
450 | # Check if the zone actually has this port. |
451 | if ! zone_has_port "${zone}" "${port}"; then | |
452 | error "Port '${port}' is not attached to zone '${zone}'" | |
453 | return ${EXIT_ERROR} | |
454 | fi | |
711ffac1 | 455 | |
828eb94a MT |
456 | local hook=$(zone_get_hook "${zone}") |
457 | assert isset hook | |
711ffac1 | 458 | |
828eb94a | 459 | hook_zone_exec "${hook}" "port_edit" "${zone}" "${port}" "$@" |
711ffac1 MT |
460 | } |
461 | ||
828eb94a MT |
462 | function zone_port_remove() { |
463 | local zone="${1}" | |
464 | assert isset zone | |
465 | ||
466 | local port="${2}" | |
467 | assert isset port | |
468 | ||
469 | shift 2 | |
470 | ||
471 | # Check if the zone actually has this port. | |
472 | if ! zone_has_port "${zone}" "${port}"; then | |
473 | error "Port '${port}' is not attached to zone '${zone}'" | |
474 | return ${EXIT_ERROR} | |
475 | fi | |
476 | ||
477 | local hook=$(zone_get_hook "${zone}") | |
478 | assert isset hook | |
711ffac1 | 479 | |
828eb94a | 480 | hook_zone_exec "${hook}" "port_remove" "${zone}" "${port}" "$@" |
711ffac1 MT |
481 | } |
482 | ||
483 | function zone_get_ports() { | |
484 | local zone=${1} | |
485 | ||
486 | assert isset zone | |
487 | ||
488 | local port | |
943e3f7e | 489 | for port in $(zone_dir ${zone})/ports/*; do |
711ffac1 | 490 | port=$(basename ${port}) |
711ffac1 MT |
491 | |
492 | if port_exists ${port}; then | |
493 | echo "${port}" | |
494 | fi | |
495 | done | |
496 | } | |
497 | ||
529141df MT |
498 | function zone_get_ports_num() { |
499 | local zone="${1}" | |
500 | assert isset zone | |
501 | ||
502 | local counter=0 | |
503 | local port | |
504 | for port in $(zone_dir "${zone}")/ports/*; do | |
505 | port="$(basename "${port}")" | |
506 | ||
507 | if port_exists "${port}"; then | |
508 | counter=$(( ${counter} + 1 )) | |
509 | fi | |
510 | done | |
511 | ||
512 | echo "${counter}" | |
513 | return ${EXIT_OK} | |
514 | } | |
515 | ||
3a7fef62 MT |
516 | function zone_has_port() { |
517 | # Check, if the given port is configured | |
518 | # in this zone. | |
519 | ||
520 | local zone=${1} | |
521 | local port=${2} | |
522 | shift 2 | |
523 | ||
524 | assert isset zone | |
525 | assert isset port | |
526 | ||
527 | [ -e "$(zone_dir ${zone})/ports/${port}" ] | |
528 | } | |
529 | ||
1848564d | 530 | function zone_config() { |
ea699552 MT |
531 | local zone="${1}" |
532 | local cmd="${2}" | |
a5ebb169 MT |
533 | shift 2 |
534 | ||
535 | assert isset zone | |
ea699552 MT |
536 | assert isset cmd |
537 | assert zone_exists "${zone}" | |
a5ebb169 | 538 | |
ea699552 MT |
539 | case "${cmd}" in |
540 | create) | |
541 | zone_config_create "${zone}" "$@" | |
a5ebb169 | 542 | ;; |
ea699552 MT |
543 | edit) |
544 | zone_config_edit "${zone}" "$@" | |
545 | ;; | |
546 | remove) | |
547 | zone_config_remove "${zone}" "$@" | |
a5ebb169 MT |
548 | ;; |
549 | *) | |
ea699552 | 550 | error "Unrecognized argument: ${cmd}" |
a5ebb169 MT |
551 | cli_usage root-zone-config-subcommands |
552 | exit ${EXIT_ERROR} | |
553 | ;; | |
554 | esac | |
555 | } | |
556 | ||
ea699552 MT |
557 | function zone_config_cmd() { |
558 | assert [ $# -gt 2 ] | |
a5ebb169 | 559 | |
ea699552 MT |
560 | local cmd="${1}" |
561 | local zone="${2}" | |
562 | shift 2 | |
563 | ||
564 | local hook="$(zone_get_hook "${zone}")" | |
a5ebb169 MT |
565 | assert isset hook |
566 | ||
ea699552 MT |
567 | hook_zone_exec "${hook}" "config_${cmd}" "${zone}" "$@" |
568 | } | |
569 | ||
570 | function zone_config_create() { | |
571 | zone_config_cmd "create" "$@" | |
572 | } | |
573 | ||
574 | function zone_config_edit() { | |
575 | zone_config_cmd "edit" "$@" | |
576 | } | |
577 | ||
578 | function zone_config_remove() { | |
579 | zone_config_cmd "remove" "$@" | |
580 | } | |
581 | ||
582 | function zone_config_show() { | |
583 | zone_config_cmd "show" "$@" | |
a5ebb169 MT |
584 | } |
585 | ||
1848564d MT |
586 | function zone_show() { |
587 | local zone=${1} | |
588 | ||
589 | echo "${zone}" | |
590 | echo " Type: $(zone_get_hook ${zone})" | |
591 | echo | |
592 | } | |
593 | ||
594 | function zones_show() { | |
595 | local zone | |
596 | ||
597 | for zone in $(zones_get $@); do | |
598 | zone_show ${zone} | |
599 | done | |
600 | } | |
601 | ||
602 | function zones_get_all() { | |
603 | local zone | |
d61a01d4 | 604 | for zone in $(zone_dir)/*; do |
1848564d MT |
605 | zone=$(basename ${zone}) |
606 | zone_exists ${zone} || continue | |
607 | ||
608 | echo "${zone}" | |
03170817 | 609 | done |
1848564d MT |
610 | } |
611 | ||
612 | function zones_get_local() { | |
613 | local zone | |
614 | for zone in $(zones_get_all); do | |
615 | zone_is_local ${zone} && echo "${zone}" | |
616 | done | |
617 | } | |
618 | ||
619 | function zones_get_nonlocal() { | |
620 | local zone | |
621 | for zone in $(zones_get_all); do | |
5e42d659 | 622 | zone_is_nonlocal ${zone} && echo "${zone}" |
1848564d MT |
623 | done |
624 | } | |
625 | ||
626 | function zones_get() { | |
627 | local local=1 | |
628 | local remote=1 | |
629 | ||
630 | local zones | |
631 | ||
632 | while [ $# -gt 0 ]; do | |
633 | case "${1}" in | |
634 | --local-only) | |
635 | local=1 | |
636 | remote=0 | |
637 | ;; | |
638 | --remote-only) | |
639 | local=0 | |
640 | remote=1 | |
641 | ;; | |
642 | --all) | |
643 | local=1 | |
644 | remote=1 | |
645 | ;; | |
646 | *) | |
647 | if zone_name_is_valid ${1}; then | |
648 | zones="${zones} ${1}" | |
649 | else | |
650 | warning "Unrecognized argument '${1}'" | |
651 | fi | |
652 | ;; | |
653 | esac | |
654 | shift | |
655 | done | |
656 | ||
657 | if [ -n "${zones}" ]; then | |
658 | local zone | |
659 | for zone in ${zones}; do | |
660 | zone_exists ${zone} && echo "${zone}" | |
661 | done | |
662 | exit ${EXIT_OK} | |
663 | fi | |
664 | ||
665 | if [ ${local} -eq 1 ] && [ ${remote} -eq 1 ]; then | |
666 | zones_get_all | |
667 | elif [ ${local} -eq 1 ]; then | |
668 | zones_get_local | |
669 | elif [ ${remote} -eq 1 ]; then | |
670 | zones_get_nonlocal | |
671 | fi | |
672 | } | |
673 | ||
674 | function zone_ports_list() { | |
675 | local zone=${1} | |
676 | ||
677 | local port | |
a5ebb169 | 678 | for port in $(zone_dir ${zone})/ports/*; do |
1848564d MT |
679 | [ -e "${port}" ] || continue |
680 | ||
681 | echo $(basename ${port}) | |
03170817 | 682 | done |
1848564d MT |
683 | } |
684 | ||
685 | function zone_ports_cmd() { | |
686 | local cmd=${1} | |
687 | local zone=${2} | |
688 | shift 2 | |
689 | ||
711ffac1 MT |
690 | assert isset cmd |
691 | assert isset zone | |
1848564d | 692 | |
711ffac1 | 693 | assert zone_exists ${zone} |
1848564d | 694 | |
711ffac1 MT |
695 | local hook=$(zone_get_hook ${zone}) |
696 | ||
697 | local port | |
698 | for port in $(zone_get_ports ${zone}); do | |
711ffac1 | 699 | hook_zone_exec ${hook} ${cmd} ${zone} ${port} $@ |
1848564d MT |
700 | done |
701 | } | |
702 | ||
703 | function zone_ports_up() { | |
711ffac1 | 704 | zone_ports_cmd port_up $@ |
1848564d MT |
705 | } |
706 | ||
707 | function zone_ports_down() { | |
711ffac1 MT |
708 | zone_ports_cmd port_down $@ |
709 | } | |
710 | ||
711 | function zone_ports_status() { | |
712 | zone_ports_cmd port_status $@ | |
1848564d MT |
713 | } |
714 | ||
1848564d | 715 | function zone_configs_cmd() { |
2472e0ea | 716 | assert [ $# -ge 2 ] |
ea699552 MT |
717 | |
718 | local cmd="${1}" | |
719 | local zone="${2}" | |
1848564d MT |
720 | shift 2 |
721 | ||
ea699552 | 722 | assert zone_exists "${zone}" |
1848564d | 723 | |
1848564d | 724 | local config |
2472e0ea MT |
725 | for config in $(zone_configs_list "${zone}"); do |
726 | local config_hook="$(zone_config_get_hook "${zone}" "${config}")" | |
727 | assert isset config_hook | |
728 | ||
729 | hook_config_exec "${config_hook}" "${cmd}" "${zone}" "${config}" $@ | |
1848564d MT |
730 | done |
731 | } | |
732 | ||
733 | function zone_configs_up() { | |
ea699552 | 734 | zone_configs_cmd "up" $@ |
1848564d MT |
735 | } |
736 | ||
737 | function zone_configs_down() { | |
ea699552 | 738 | zone_configs_cmd "down" $@ |
1848564d MT |
739 | } |
740 | ||
a5ebb169 | 741 | function zone_configs_status() { |
ea699552 MT |
742 | zone_configs_cmd "status" $@ |
743 | } | |
744 | ||
745 | function zone_configs_list() { | |
746 | local zone=${1} | |
747 | ||
748 | local config | |
749 | for config in $(zone_dir ${zone})/configs/*; do | |
750 | [ -e "${config}" ] || continue | |
751 | ||
752 | basename ${config} | |
753 | done | |
a5ebb169 MT |
754 | } |
755 | ||
2472e0ea MT |
756 | function zone_config_get_hook() { |
757 | assert [ $# -eq 2 ] | |
758 | ||
759 | local zone="${1}" | |
760 | assert isset zone | |
761 | ||
762 | local config="${2}" | |
763 | assert isset config | |
764 | ||
765 | local HOOK | |
766 | zone_config_settings_read "${zone}" "${config}" \ | |
767 | --ignore-superfluous-settings HOOK | |
768 | ||
769 | print "${HOOK}" | |
770 | } | |
771 | ||
38f61548 MT |
772 | function zone_has_ip() { |
773 | device_has_ip $@ | |
4231f419 MT |
774 | } |
775 | ||
059469a8 MT |
776 | function zone_db() { |
777 | local zone=${1} | |
778 | local action=${2} | |
779 | shift 2 | |
780 | ||
781 | case "${action}" in | |
782 | starting|started|stopping|stopped) | |
783 | db_connection_update ${zone} ${action} | |
784 | ;; | |
785 | esac | |
786 | } | |
5e42d659 MT |
787 | |
788 | function zone_is_up() { | |
789 | local zone=${1} | |
790 | ||
791 | device_is_up ${zone} | |
792 | } | |
793 | ||
794 | function zone_is_down() { | |
795 | ! zone_is_up $@ | |
796 | } | |
711ffac1 | 797 | |
a5ebb169 | 798 | function zone_get_supported_port_hooks() { |
711ffac1 MT |
799 | local zone=${1} |
800 | ||
801 | local hook=$(zone_get_hook ${zone}) | |
802 | ||
803 | hook_zone_ports_get_all ${hook} | |
804 | } | |
805 | ||
a5ebb169 | 806 | function zone_get_supported_config_hooks() { |
ea699552 | 807 | hook_config_get_all |
a5ebb169 MT |
808 | } |
809 | ||
711ffac1 MT |
810 | function zone_file() { |
811 | local zone=${1} | |
812 | ||
813 | assert isset zone | |
814 | ||
815 | echo "$(zone_dir ${zone})/settings" | |
816 | } | |
817 | ||
e9df08ad | 818 | function zone_settings_read() { |
711ffac1 MT |
819 | local zone=${1} |
820 | ||
821 | assert isset zone | |
822 | ||
bfd6c282 MT |
823 | # Save the HOOK variable. |
824 | local hook="${HOOK}" | |
825 | ||
e9df08ad | 826 | settings_read $(zone_file ${zone}) |
bfd6c282 MT |
827 | |
828 | # Restore hook. | |
829 | HOOK="${hook}" | |
711ffac1 MT |
830 | } |
831 | ||
e9df08ad | 832 | function zone_settings_write() { |
711ffac1 MT |
833 | local zone=${1} |
834 | ||
835 | assert isset zone | |
836 | ||
e9df08ad | 837 | settings_write $(zone_file ${zone}) ${HOOK_SETTINGS} |
711ffac1 MT |
838 | } |
839 | ||
e9df08ad | 840 | function zone_settings_set() { |
711ffac1 MT |
841 | local zone=${1} |
842 | shift | |
843 | local args="$@" | |
844 | ||
845 | assert isset zone | |
846 | ||
847 | ( | |
e9df08ad | 848 | zone_settings_read ${zone} |
711ffac1 MT |
849 | |
850 | for arg in ${args}; do | |
851 | eval "${arg}" | |
852 | done | |
853 | ||
e9df08ad | 854 | zone_settings_write ${zone} |
711ffac1 MT |
855 | ) |
856 | } | |
6b3f9c85 | 857 | |
e9df08ad | 858 | function zone_settings_get() { |
6b3f9c85 MT |
859 | local zone=${1} |
860 | local key=${2} | |
861 | ||
862 | assert isset zone | |
863 | assert isset key | |
864 | ||
865 | ( | |
e9df08ad | 866 | zone_settings_read ${zone} |
6b3f9c85 MT |
867 | |
868 | echo "${!key}" | |
869 | ) | |
870 | } | |
e9df08ad MT |
871 | |
872 | function zone_config_settings_read() { | |
873 | assert [ $# -gt 2 ] | |
874 | ||
875 | local zone="${1}" | |
876 | local config="${2}" | |
877 | shift 2 | |
878 | ||
879 | local path="$(zone_dir "${zone}")/configs/${config}" | |
880 | settings_read "${path}" "$@" | |
881 | } | |
882 | ||
883 | function zone_config_settings_write() { | |
884 | assert [ $# -gt 2 ] | |
885 | ||
886 | local zone="${1}" | |
887 | local config="${2}" | |
888 | shift 2 | |
889 | ||
890 | local path="$(zone_dir "${zone}")/configs/${config}" | |
891 | settings_write "${path}" "$@" | |
892 | } | |
893 | ||
894 | function zone_port_settings_read() { | |
895 | assert [ $# -gt 2 ] | |
896 | ||
897 | local zone="${1}" | |
898 | local port="${2}" | |
899 | shift 2 | |
900 | ||
901 | local path="$(zone_dir "${zone}")/ports/${port}" | |
902 | settings_read "${path}" "$@" | |
903 | } | |
904 | ||
905 | function zone_port_settings_write() { | |
906 | assert [ $# -gt 2 ] | |
907 | ||
908 | local zone="${1}" | |
909 | local port="${2}" | |
910 | shift 2 | |
911 | ||
912 | local path="$(zone_dir "${zone}")/ports/${port}" | |
913 | settings_write "${path}" "$@" | |
914 | } | |
915 | ||
916 | function zone_port_settings_remove() { | |
917 | assert [ $# -eq 2 ] | |
918 | ||
919 | local zone="${1}" | |
920 | local port="${2}" | |
921 | ||
922 | local path="$(zone_dir "${zone}")/ports/${port}" | |
923 | settings_remove "${path}" | |
924 | } |