From 2bb20bbd064368f552433874c522f19ab48b8dda Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Sun, 6 Sep 2015 18:33:18 +0200 Subject: [PATCH] Allow renaming zones Signed-off-by: Stefan Schantl Signed-off-by: Michael Tremer --- man/network-zone.xml | 20 +++++++++++++++ src/bash-completion/network | 2 +- src/functions/functions.util | 7 +++++ src/functions/functions.zone | 40 +++++++++++++++++++++++++++++ src/network | 50 ++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) diff --git a/man/network-zone.xml b/man/network-zone.xml index 52e3d77f..012bc849 100644 --- a/man/network-zone.xml +++ b/man/network-zone.xml @@ -180,6 +180,26 @@ + + + + ZONE rename NAME + + + + + Renames the zone to NAME. + + + 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. + + + Zones that are marked to be destroyed cannot be renamed. + + + diff --git a/src/bash-completion/network b/src/bash-completion/network index a9579f86..002a1f9a 100644 --- a/src/bash-completion/network +++ b/src/bash-completion/network @@ -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}") ) diff --git a/src/functions/functions.util b/src/functions/functions.util index de2cb1e6..3e5703e5 100644 --- a/src/functions/functions.util +++ b/src/functions/functions.util @@ -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=$@ diff --git a/src/functions/functions.zone b/src/functions/functions.zone index e740a1a7..baa89f04 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -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}" diff --git a/src/network b/src/network index 4d23e504..edeb825d 100644 --- a/src/network +++ b/src/network @@ -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 -- 2.39.2