From afd8e93f09426a6a5fb864baaf26a883e59b9848 Mon Sep 17 00:00:00 2001 From: Antoni Segura Puimedon Date: Thu, 8 Jun 2017 12:45:27 +0200 Subject: [PATCH] devstack-heat: refactor and improve In order to add more options, let's do a refactor and improve the code a bit. One of the improvements is that it now waits for the stack not to be in progress anymore before proceeding to show the stack outputs. Change-Id: I68b64260d0a4b368d0427c5b231c44ea9ec55079 Signed-off-by: Antoni Segura Puimedon --- contrib/devstack-heat/devstack-heat | 79 +++++----------------- contrib/devstack-heat/lib/devstack-heat | 88 +++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 63 deletions(-) create mode 100644 contrib/devstack-heat/lib/devstack-heat diff --git a/contrib/devstack-heat/devstack-heat b/contrib/devstack-heat/devstack-heat index 1ee26014d..8e37e8b1c 100755 --- a/contrib/devstack-heat/devstack-heat +++ b/contrib/devstack-heat/devstack-heat @@ -1,39 +1,17 @@ #!/bin/bash -function sub_stack() { - local latest_commit - local deployment=${1:-master_} - local tmpdir +CURR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - if [[ "${deployment}" == "master_" ]]; then - if [[ "$DEVSTACK_HEAT_GH_TOKEN" == "" ]]; then - set -e - echo "Didn't find a Github token in ENV var DEVSTACK_HEAT_GH_TOKEN. Falling back to cloning repo..." - tmpdir=$(mktemp -d) - git clone https://github.com/openstack/kuryr-kubernetes "${tmpdir}/kuryr-kubernetes" - pushd "${tmpdir}/kuryr-kubernetes" - latest_commit=$(git rev-parse HEAD) - popd - rm -fr "${tmpdir}" - set +e - else - latest_commit=$(curl -s -H "Authorization: token $DEVSTACK_HEAT_GH_TOKEN" https://api.github.com/repos/openstack/kuryr-kubernetes/commits/master | jq -r '.sha') - fi - if [[ "$latest_commit" == "null" ]]; then - echo "Couldn't get a valid master commit" - exit 1 - fi - deployment="${deployment}${latest_commit}" - else - deployment="gerrit_${deployment}" - fi +# shellcheck disable=SC1091 +# shellcheck source=lib/devstack-heat +source "${CURR_DIR}/lib/devstack-heat" + +function sub_stack() { + local deployment=${1:-master_} + deployment=$(_generate_deployment_name "${deployment}") # create stack - read -p "Deploying the stack ${deployment}[y/N]?" -n 1 -r - echo - if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then - exit 1 - fi + _confirm_or_exit "Deploying the stack ${deployment}" echo "Starting..." openstack stack create -e hot/parameters.yml -t hot/devstack_heat_template.yml "$deployment" @@ -46,59 +24,34 @@ function sub_unstack() local deployment deployment=${1:-master_} if [[ "${deployment}" == "master_" ]]; then - echo "You must put the whole stack name for unstacking" + (>&2 echo "You must put the whole stack name for unstacking") exit 1 fi - openstack stack delete "$deployment" + _confirm_or_exit "Deleting the stack ${deployment}" + openstack stack delete "$deployment" } function sub_show() { local deployment - deployment=${1:-master_} if [[ "${deployment}" == "master_" ]]; then - echo "You must put the whole stack name for showing the stack resources" + (>&2 echo "You must put the whole stack name for showing the stack") exit 1 fi + + _wait_for_after_in_progress "${1:-master_}" echo "VM subnet: $(openstack stack output show "${deployment}" vm_subnet -f json | jq -r '.output_value')" echo "Nodes FIPs: $(openstack stack output show "${deployment}" node_fips -f json | jq -r '.output_value' | jq -r '.[]?' | xargs echo)" printf "\n" } -function sub_getkey() { - local deployment - deployment=${1:-master_} - if [[ "${deployment}" == "master_" ]]; then - echo "You must put the whole stack name for getting the key" - exit 1 - fi - - openstack stack output show "${deployment}" master_key_priv -f json | jq -r '.output_value' -} - -function sub_ssh() { - local deployment - local key - local fip - - deployment=${1:-master_} - if [[ "${deployment}" == "master_" ]]; then - echo "You must put the whole stack name for getting the key" - exit 1 - fi - key="${deployment}.pem" - fip=$(openstack stack output show "${deployment}" node_fips -f json | jq -r '.output_value' | jq -r '.[]?' | xargs echo) - sub_getkey "${deployment}" > "${key}" - chmod 0600 "${key}" - ssh -i "${deployment}.pem" -o "StrictHostKeyChecking no" "stack@${fip}" - exit $? -} function sub_help() { local myname myname=$(basename "$0") + printf "Usage: %s [options]\n" "$myname" printf "Subcommands:\n" printf " stack gerrit_change_number Create Heat stack\n" diff --git a/contrib/devstack-heat/lib/devstack-heat b/contrib/devstack-heat/lib/devstack-heat new file mode 100644 index 000000000..6dcc2e09e --- /dev/null +++ b/contrib/devstack-heat/lib/devstack-heat @@ -0,0 +1,88 @@ +#!/bin/bash + +function sub_getkey() { + local deployment + deployment=${1:-master_} + if [[ "${deployment}" == "master_" ]]; then + (>&2 echo "You must put the whole stack name for getting the key") + exit 1 + fi + + openstack stack output show "${deployment}" master_key_priv -f json | jq -r '.output_value' +} + +function _wait_for_after_in_progress() { + local deployment + local status + deployment="$1" + + while true; do + status=$(openstack stack show "${deployment}" -c stack_status -f value) + if [[ ! "$status" =~ IN_PROGRESS$ ]]; then + break + fi + done +} + + +function sub_ssh() { + local deployment + local key + local fip + + deployment=${1:-master_} + if [[ "${deployment}" == "master_" ]]; then + (>&2 echo "You must put the whole stack name for logging into the node") + exit 1 + fi + key="${deployment}.pem" + fip=$(openstack stack output show "${deployment}" node_fips -f json | jq -r '.output_value' | jq -r '.[]?' | xargs echo) + sub_getkey "${deployment}" > "${key}" + chmod 0600 "${key}" + + # shellcheck disable=SC2029 + ssh -i "${deployment}.pem" -o "StrictHostKeyChecking no" "stack@${fip}" + exit $? +} + +function _generate_deployment_name() { + local latest_commit + local deployment=${1:-master_} + local tmpdir + + if [[ "${deployment}" == "master_" ]]; then + if [[ "$DEVSTACK_HEAT_GH_TOKEN" == "" ]]; then + set -e + (>&2 echo "Didn't find a Github token in ENV var DEVSTACK_HEAT_GH_TOKEN. Falling back to cloning repo...") + tmpdir=$(mktemp -d) + git clone https://git.openstack.org/openstack/kuryr-kubernetes "${tmpdir}/kuryr-kubernetes" > /dev/null + pushd "${tmpdir}/kuryr-kubernetes" > /dev/null + latest_commit=$(git rev-parse HEAD) + popd > /dev/null + rm -fr "${tmpdir}" + set +e + else + latest_commit=$(curl -s -H "Authorization: token $DEVSTACK_HEAT_GH_TOKEN" https://api.github.com/repos/openstack/kuryr-kubernetes/commits/master | jq -r '.sha') + fi + if [[ "$latest_commit" == "null" ]]; then + (>&2 echo "Couldn't get a valid master commit") + exit 1 + fi + deployment="${deployment}${latest_commit}" + else + deployment="gerrit_${deployment}" + fi + + echo "${deployment}" +} + +function _confirm_or_exit() { + local question + question="$1" + + read -p "${question}[y/N]?" -n 1 -r + echo + if [[ ! "$REPLY" =~ ^[Yy]$ ]]; then + exit 1 + fi +}