Add update_site script

We will make use of common functions across the deploy_site,
update_site and redeploy_server script.

This will help to reduce common and repetitive codes across
the scripts.

Note that the Shipyard CLI needs to be installed before we
can use these scripts.

Change-Id: I31e5c8723346666a592951f59856008ccb64128b
This commit is contained in:
Anthony Lin 2018-02-02 08:13:50 +00:00
parent 8d0ebfaeb1
commit 47e49b3b3b
5 changed files with 208 additions and 263 deletions

View File

@ -13,160 +13,15 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
set -x set -ex
# Note that we will need to execute the deckhand_load_yaml # We can excute the script in the following manner:
# script first before the deploy_site script
# Check to ensure that the Shipyard CLI has been installed on
# the Genesis host during the deckhand YAML load phase. Exit
# script if Shipyard CLI is not installed.
command -v shipyard >/dev/null 2>&1 || { echo >&2 "Please install Shipyard CLI before executing the script."; exit 1; }
# Define Namespace
namespace="ucp"
# Initialize Variables with Default Values
# Note that 'query_time' has a default value of 90 seconds
# Note that 'deploy_timeout' has a default value of 60 loops (based on
# 90 seconds back off per cycle, i.e. 60 * 90 = 5400 seconds = 1.5 hrs)
query_time=90
deploy_timeout=60
OS_USER_DOMAIN_NAME="default"
OS_PROJECT_DOMAIN_NAME="default"
OS_PROJECT_NAME="service"
OS_USERNAME="shipyard"
OS_PASSWORD="password"
OS_AUTH_URL="http://keystone.${namespace}:80/v3"
# Override OpenStack Environment Variables, query_time and
# deploy_timeout if need be
# For instance, we can run the script in the following manner:
# #
# $ ./deploy_site.sh -q 110 -t 120 -u SY -p supersecret -d test -D test -n admin -l http://keystone.test:80/v3 # $ ./deploy_site.sh
# #
# This will set the variables to the following values:
#
# - query_time=110
# - deploy_timeout=120
# - OS_USERNAME=SY
# - OS_PASSWORD=supersecret
# - OS_USER_DOMAIN_NAME=test
# - OS_PROJECT_DOMAIN_NAME=test
# - OS_PROJECT_NAME=admin
# - OS_AUTH_URL=http://keystone.test:80/v3
#
while getopts q:t:u:p:d:D:n:l: option
do
case "${option}"
in
q) query_time=${OPTARG};;
t) deploy_timeout=${OPTARG};;
u) OS_USERNAME=${OPTARG};;
p) OS_PASSWORD=${OPTARG};;
d) OS_USER_DOMAIN_NAME=${OPTARG};;
D) OS_PROJECT_DOMAIN_NAME=${OPTARG};;
n) OS_PROJECT_NAME=${OPTARG};;
l) OS_AUTH_URL=${OPTARG};;
esac
done
# Export Environment Variables # Source environment variables
export OS_USER_DOMAIN_NAME=${OS_USER_DOMAIN_NAME} source set_env
export OS_PROJECT_DOMAIN_NAME=${OS_PROJECT_DOMAIN_NAME}
export OS_PROJECT_NAME=${OS_PROJECT_NAME}
export OS_USERNAME=${OS_USERNAME}
export OS_PASSWORD=${OS_PASSWORD}
export OS_AUTH_URL=${OS_AUTH_URL}
# Define Color # Execute shipyard action for deploy_site
NC='\033[0m' bash execute_shipyard_action.sh 'deploy_site'
RED='\033[0;31m'
GREEN='\033[0;32m'
# Execute deploy_site
echo -e "Execute deploy_site Dag...\n"
shipyard create action deploy_site
# The status or lifecycle phase of an action can be
#
# 1) Pending - The action is scheduled or preparing for execution.
# 2) Processing - The action is underway.
# 3) Complete - The action has completed successfully.
# 4) Failed - The action has encountered an error, and has failed.
# 5) Paused - The action has been paused by a user.
# 6) Unknown (*) - Unknown State for corner cases
# 7) null - We will end up with a `null` response from Shipyard if we
# query the status of the task with an expired keystone token.
# Note that this should never happen if we use Shipyard CLI as
# new token is retrieved each time. Description for state 'null'
# is included here for information only.
#
# Print current list of actions in Shipyard
shipyard get actions
# Retrieve the ID of the 'deploy_site' action that is currently being executed
echo -e "Retrieving Action ID...\n"
action_id=`shipyard get actions | grep deploy_site | grep -i Processing | awk '{print $2}'`
echo "The Action ID is" ${action_id}
echo
# Initialize 'action_lifecycle' to 'Pending'
action_lifecycle="Pending"
# Polling for 'deploy_site' action
deploy_counter=1
check_timeout_counter() {
# Check total elapsed time
# The default time out is set to 1.5 hrs
# This value can be changed by setting $2
if [[ $deploy_counter -eq $deploy_timeout ]]; then
echo 'Deploy Site task has timed out.'
break
fi
}
while true;
do
# Get Current State of Action Lifecycle
shipyard describe ${action_id} > /tmp/get_action_status
action_lifecycle=`cat /tmp/get_action_status | grep Lifecycle | awk '{print $2}'`
# Print output of Shipyard CLI
cat /tmp/get_action_status
if [[ $action_lifecycle == "Complete" ]]; then
echo -e '\nSite Successfully Deployed\n'
break
fi
# Check Dag state
if [[ $action_lifecycle == "Failed" ]] || [[ $action_lifecycle == "Paused" ]] || \
[[ $action_lifecycle == "Unknown"* ]] || [[ $action_lifecycle == "null" ]]; then
echo -e "Dag Execution is in" ${RED}$action_lifecycle${NC} "state\n"
break
else
echo -e "Dag Execution is in" ${GREEN}$action_lifecycle${NC} "state\n"
# Back off between each iteration
echo -e "Back Off for $query_time seconds...\n"
sleep $query_time
# Step counter and check if deployment has timed out
((deploy_counter++))
check_timeout_counter
fi
done
# Delete the temporary file
rm /tmp/get_action_status
# Return exit code so that we can use it to determine the final
# state of the workflow
if [[ $action_lifecycle == "Complete" ]]; then
exit 0
else
exit 1
fi

138
tools/execute_shipyard_action.sh Executable file
View File

@ -0,0 +1,138 @@
#!/bin/bash
# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This is a common script that is used by the deploy_site, update_site
# and redeploy_server scripts
set -ex
check_timeout_counter() {
# Check total elapsed time
# The default time out is set to 1.5 hr
if [[ $counter -ge $max_count ]]; then
echo 'Worflow Execution Timed Out!'
break
fi
}
run_action () {
# Define Variables
action=$1
server=$2
# Define Color
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
# Execute action
echo -e "Execute ${action} Dag...\n"
# Note that deploy and update site do not require additional parameter
# to be passed in while redeploy_server requires user to indicate which
# server to redeploy
if ! [[ ${server} ]] && [[ ${action} ]]; then
shipyard create action ${action}
elif [[ ${action} == 'redeploy_server' && ${server} ]]; then
shipyard create action redeploy_server --param="server-name=${server}"
else
echo "Invalid Input!"
exit 1
fi
# The status or lifecycle phase of an action can be
#
# 1) Pending - The action is scheduled or preparing for execution.
# 2) Processing - The action is underway.
# 3) Complete - The action has completed successfully.
# 4) Failed - The action has encountered an error, and has failed.
# 5) Paused - The action has been paused by a user.
# 6) Unknown (*) - Unknown State for corner cases
# 7) null - We will end up with a `null` response from Shipyard if we
# query the status of the task with an expired keystone token.
# Note that this should never happen if we use Shipyard CLI as
# new token is retrieved each time. Description for state 'null'
# is included here for information only.
#
# Print current list of actions in Shipyard
shipyard get actions
# Retrieve the ID of the action that is currently being executed
echo -e "Retrieving Action ID...\n"
action_id=`shipyard get actions | grep ${action} | grep -i Processing | awk '{print $2}'`
if ! [[ ${action_id} ]]; then
echo "Unable to Retrieve Action ID!"
exit 1
else
echo "The Action ID is" ${action_id}
fi
# Initialize 'action_lifecycle' to 'Pending'
action_lifecycle="Pending"
# Initialize counter to 1
counter=1
while true;
do
# Get Current State of Action Lifecycle
describe_action=`shipyard describe ${action_id}`
action_lifecycle=`echo ${describe_action} | awk '{print $6}'`
if [[ $action_lifecycle == "Complete" ]]; then
echo -e '\nSuccessfully performed' ${action}
echo -e '\n'
break
fi
# Check Dag state
if [[ $action_lifecycle == "Failed" ]] || [[ $action_lifecycle == "Paused" ]] || \
[[ $action_lifecycle == "Unknown"* ]] || [[ $action_lifecycle == "null" ]]; then
echo -e "Dag Execution is in" ${RED}$action_lifecycle${NC} "state\n"
break
else
echo -e "Dag Execution is in" ${GREEN}$action_lifecycle${NC} "state\n"
# Back off between each iteration
echo -e "Back Off for $query_time seconds...\n"
sleep $query_time
# Step counter and check the timeout counter
((counter++))
check_timeout_counter
fi
done
# Return exit code so that we can use it to determine the final
# state of the workflow
if [[ $action_lifecycle == "Complete" ]]; then
exit 0
else
exit 1
fi
}
# Note that we will need to execute the deckhand_load_yaml
# script first before the deploy_site script
# Check to ensure that the Shipyard CLI has been installed on
# the Genesis host during the deckhand YAML load phase. Exit
# script if Shipyard CLI is not installed.
command -v shipyard >/dev/null 2>&1 || { echo >&2 "Please install Shipyard CLI before executing the script."; exit 1; }
# Calls 'run_action' function
run_action "${@}"

115
tools/redeploy_server.sh Normal file → Executable file
View File

@ -15,11 +15,6 @@
set -ex set -ex
# Check to ensure that the Shipyard CLI has been installed on
# the Genesis host during the deckhand YAML load phase. Exit
# script if Shipyard CLI is not installed.
command -v shipyard >/dev/null 2>&1 || { echo >&2 "Please install Shipyard CLI before executing the script."; exit 1; }
# We will need to pass the name of the server that we want to redeploy # We will need to pass the name of the server that we want to redeploy
# when we execute the script. It is mandatory to do so and the script # when we execute the script. It is mandatory to do so and the script
# will exit with exception if the server name is missing. For instance, # will exit with exception if the server name is missing. For instance,
@ -34,111 +29,9 @@ fi
# Define Variables # Define Variables
server=$1 server=$1
namespace="ucp"
# Initialize Variables # Source environment variables
# Note that 'query_time' has a default value of 90 seconds source set_env
# Note that 'redeploy_server_max_count' has a default value of 40 loops (based on
# 90 seconds back off per cycle, i.e. 40 * 90 = 3600 seconds = 1 hr)
# Note that user can use a different value for each of the variables by exporting
# the required environment variable prior to running the script
query_time=${query_time:-90}
redeploy_server_max_count=${redeploy_server_max_count:-40}
# Export Environment Variables # Execute shipyard action for redeploy_server
export OS_USER_DOMAIN_NAME="${OS_USER_DOMAIN_NAME:-default}" bash execute_shipyard_action.sh 'redeploy_server' ${server}
export OS_PROJECT_DOMAIN_NAME="${OS_PROJECT_DOMAIN_NAME:-default}"
export OS_PROJECT_NAME="${OS_PROJECT_NAME:-service}"
export OS_USERNAME="${OS_USERNAME:-shipyard}"
export OS_PASSWORD="${OS_PASSWORD:-password}"
export OS_AUTH_URL="${OS_AUTH_URL:-http://keystone.${namespace}:80/v3}"
# Define Color
NC='\033[0m'
RED='\033[0;31m'
GREEN='\033[0;32m'
# Execute redeploy_server dag
echo -e "Execute redeploy_server Dag...\n"
shipyard create action redeploy_server --param="server-name=${server}"
# The status or lifecycle phase of an action can be
#
# 1) Pending - The action is scheduled or preparing for execution.
# 2) Processing - The action is underway.
# 3) Complete - The action has completed successfully.
# 4) Failed - The action has encountered an error, and has failed.
# 5) Paused - The action has been paused by a user.
# 6) Unknown (*) - Unknown State for corner cases
# 7) null - We will end up with a `null` response from Shipyard if we
# query the status of the task with an expired keystone token.
# Note that this should never happen if we use Shipyard CLI as
# new token is retrieved each time. Description for state 'null'
# is included here for information only.
#
# Print current list of actions in Shipyard
shipyard get actions
# Retrieve the ID of the 'redeploy_server' action that is currently being executed
echo -e "Retrieving Action ID...\n"
action_id=`shipyard get actions | grep redeploy_server | grep -i Processing | awk '{print $2}'`
if ! [[ ${action_id} ]]; then
echo "Unable to Retrieve 'redeploy_server' Action ID!"
exit 1
else
echo "The Action ID is" ${action_id}
fi
# Initialize 'action_lifecycle' to 'Pending'
action_lifecycle="Pending"
# Polling for 'redeploy_server' action
redeploy_server_counter=1
check_timeout_counter() {
# Check total elapsed time
# The default time out is set to 1 hr
if [[ $redeploy_server_counter -ge $redeploy_server_max_count ]]; then
echo 'Redeploy server task has timed out.'
break
fi
}
while true;
do
# Get Current State of Action Lifecycle
describe_action=`shipyard describe ${action_id}`
action_lifecycle=`echo ${describe_action} | awk '{print $6}'`
if [[ $action_lifecycle == "Complete" ]]; then
echo -e '\nServer Successfully Redeployed\n'
break
fi
# Check Dag state
if [[ $action_lifecycle == "Failed" ]] || [[ $action_lifecycle == "Paused" ]] || \
[[ $action_lifecycle == "Unknown"* ]] || [[ $action_lifecycle == "null" ]]; then
echo -e "Dag Execution is in" ${RED}$action_lifecycle${NC} "state\n"
break
else
echo -e "Dag Execution is in" ${GREEN}$action_lifecycle${NC} "state\n"
# Back off between each iteration
echo -e "Back Off for $query_time seconds...\n"
sleep $query_time
# Step counter and check the timeout counter
((redeploy_server_counter++))
check_timeout_counter
fi
done
# Return exit code so that we can use it to determine the final
# state of the workflow
if [[ $action_lifecycle == "Complete" ]]; then
exit 0
else
exit 1
fi

32
tools/set_env Executable file
View File

@ -0,0 +1,32 @@
#!/bin/bash
# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Define Variable
# Note that 'query_time' has a default value of 90 seconds
# Note that 'max_count' has a default value of 60 loops (based on
# 90 seconds back off per cycle, i.e. 60 * 90 = 5400 seconds = 1.5 hr)
# Note that user can use a different value for each of the variables by
# exporting the required environment variable prior to running the script
max_count=${max_count:-60}
namespace="${namespace:-ucp}"
query_time=${query_time:-90}
# Export Environment Variables
export OS_AUTH_URL="${OS_AUTH_URL:-http://keystone.${namespace}:80/v3}"
export OS_PASSWORD="${OS_PASSWORD:-password}"
export OS_PROJECT_DOMAIN_NAME="${OS_PROJECT_DOMAIN_NAME:-default}"
export OS_PROJECT_NAME="${OS_PROJECT_NAME:-service}"
export OS_USERNAME="${OS_USERNAME:-shipyard}"
export OS_USER_DOMAIN_NAME="${OS_USER_DOMAIN_NAME:-default}"

27
tools/update_site.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash
# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -ex
# We can excute the script in the following manner:
#
# $ ./update_site.sh
#
# Source environment variables
source set_env
# Execute shipyard action for update_site
bash execute_shipyard_action.sh 'update_site'