]> git.ipfire.org Git - people/ms/network.git/commitdiff
Allow renaming zones
authorStefan Schantl <stefan.schantl@ipfire.org>
Sun, 6 Sep 2015 16:33:18 +0000 (18:33 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Sun, 6 Sep 2015 16:33:18 +0000 (18:33 +0200)
Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
man/network-zone.xml
src/bash-completion/network
src/functions/functions.util
src/functions/functions.zone
src/network

index 52e3d77f883ca11edaa69d7c4ca9525f5f9345c9..012bc8490fc14407bcf50d3b1810a171f644537e 100644 (file)
                                        </para>
                                </listitem>
                        </varlistentry>
+
+                       <varlistentry>
+                               <term>
+                                       <command><replaceable>ZONE</replaceable> rename <replaceable>NAME</replaceable></command>
+                               </term>
+
+                               <listitem>
+                                       <para>
+                                               Renames the zone to <replaceable>NAME</replaceable>.
+                                       </para>
+                                       <para>
+                                               The command will shut down the zone if it is up and
+                                               start it again with the new name. If the zone is not
+                                               up it won't be started.
+                                       </para>
+                                       <para>
+                                               Zones that are marked to be destroyed cannot be renamed.
+                                       </para>
+                               </listitem>
+                       </varlistentry>
                </variablelist>
        </refsect1>
 
index a9579f8630668c38cd9a6fb19398dbbe4e22de75..002a1f9a93adc0778c39ba0991ea02327c020867 100644 (file)
@@ -322,7 +322,7 @@ _network_zone_subcommand() {
 
        local words=( $@ )
 
-       local commands="config disable down edit enable port status up"
+       local commands="config disable down edit enable port rename status up"
        local cmd="$(_network_find_on_cmdline "${commands}")"
        if [[ -z "${cmd}" ]]; then
                COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
index de2cb1e69fa8ee7d931866f8bf151a8bba55e861..3e5703e5198707b66f394570b1d708148e5bcdca 100644 (file)
@@ -310,6 +310,13 @@ assert_check_retval() {
        return ${ret}
 }
 
+# This function executes the given command and inverses the return code
+not() {
+       local command="$@"
+
+       ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE}
+}
+
 exec_cmd() {
        local cmd=$@
 
index e740a1a749a3713352f084162b41ea958e5d6c79..baa89f04bde320f569ad929e8c827f88be4068f7 100644 (file)
@@ -284,6 +284,46 @@ zone_edit() {
        hook_zone_exec ${hook} edit ${zone} $@
 }
 
+zone_rename() {
+       assert [ $# -eq 2 ]
+
+       local zone="${1}"
+       local name="${2}"
+
+       assert zone_exists "${zone}"
+       assert not zone_has_destroy_tag "${zone}"
+       assert not zone_exists "${name}"
+
+       # The zone must be shut down before, is then renamed and
+       # potentially brought up again
+
+       # Save if the zone is running right now
+       zone_is_active "${zone}"
+       local zone_was_active="${?}"
+
+       # Save if the zone is enabled (i.e. auto-start)
+       zone_is_enabled "${zone}"
+       local zone_was_enabled="${?}"
+
+       # Stop the zone
+       zone_stop "${zone}"
+
+       # Disable the zone
+       zone_disable "${zone}"
+
+       # Rename the configuration files
+       mv -f "$(zone_dir "${zone}")" "$(zone_dir "${name}")"
+
+       # Enable the zone if it was enabled before
+       [ ${zone_was_enabled} -eq ${EXIT_TRUE} ] && zone_enable "${name}"
+
+       # Start the zone if it was up before
+       [ ${zone_was_active} -eq ${EXIT_TRUE} ] && zone_start "${name}"
+
+       log INFO "Zone ${zone} was renamed to ${name}"
+       return ${EXIT_OK}
+}
+
 
 zone_destroy() {
        local zone="${1}"
index 4d23e5041b51e0707deefae72b1280f9cdd274ef..edeb825d9116f2b40eaee53cc5d511ee7a7df7dd 100644 (file)
@@ -588,6 +588,9 @@ cli_zone() {
                        port)
                                cli_zone_port "${zone}" $@
                                ;;
+                       rename)
+                               cli_zone_rename "${zone}" $@
+                               ;;
                        config|disable|down|edit|enable|status|up)
                                zone_${action} ${zone} $@
                                ;;
@@ -700,6 +703,53 @@ cli_zone_port() {
        exit ${EXIT_OK}
 }
 
+cli_zone_rename() {
+       if cli_help_requested $@; then
+               cli_show_man network-zone
+               exit ${EXIT_OK}
+       fi
+
+       local zone="${1}"
+       local name="${2}"
+       shift 2
+
+       if ! isset name; then
+               error "You need to pass a new name"
+               exit ${EXIT_ERROR}
+       fi
+
+       if ! zone_name_is_valid "${name}"; then
+               error "Invalid new zone name: ${name}"
+               exit ${EXIT_ERROR}
+       fi
+
+       # Check if the zone exists
+       if ! zone_exists "${zone}"; then
+               error "Zone ${zone} does not exist"
+               exit ${EXIT_ERROR}
+       fi
+
+       # Destroyed zones cannot be renamed
+       if zone_has_destroy_tag "${zone}"; then
+               error "Zone ${zone} is about to be destroyed and cannot be renamed"
+               exit ${EXIT_ERROR}
+       fi
+
+       # Check if a zone with the new name already exists
+       if zone_exists "${name}"; then
+               error "Zone ${name} already exists"
+               exit ${EXIT_ERROR}
+       fi
+
+       # Rename
+       if ! zone_rename "${zone}" "${name}"; then
+               error "Could not rename zone ${zone} to ${name}"
+               exit ${EXIT_ERROR}
+       fi
+
+       exit ${EXIT_OK}
+}
+
 cli_list_hooks() {
        local type=${1}
        shift