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