Bash examples - shows use of key=val parameter passing, error checking, functions, etc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#set -x | |
# source common function and variables | |
source "$(readlink -f $0 | xargs dirname)/shared" | |
REPO_DIR="${HOME}/NewDeploy/Repositories/Config_$(date +%Y%m%d%H%M%S)" | |
# check if another instance of this deployment is in progress | |
check_another_instance $(basename -- "$0") | |
start_release() { | |
confirm_start | |
[ ! -d "${REPO_DIR}" ] && { echo -e "${b_red}No repository present, exiting...${clear}"; exit 1; } | |
git -C "${REPO_DIR}" checkout -q "${BRANCH}" | |
[ ! $? -eq 0 ] && { echo -e "${b_red}Error checking out branch ${BRANCH}, exiting${clear}"; exit 1; } | |
br=$(git -C "${REPO_DIR}" branch -v | grep '\*' | cut -d ' ' -f2) | |
[ "${br}" != "${BRANCH}" ] && { echo -e "${b_red}Branch mis-match exiting...${clear}"; exit 1; } | |
IFS=, | |
for TARGET in $MACHINES | |
do | |
TARGET=$(echo "${TARGET}" | xargs echo -n) | |
[ ! -d "${REPO_DIR}/host_configs/${TARGET}" ] && { echo -e "${b_red}[$(date)] no config path $REPO_DIR}/host_configs/${TARGET} exists, skipping...${clear}"; continue ; } | |
# get Env of host if DEPLOY_ENV is not provided | |
if [ -z "${DEPLOY_ENV}" ] | |
then | |
get_target_env "${TARGET}" | |
else | |
CFG_ENV="${DEPLOY_ENV}" | |
fi | |
# go through common files and copy to the right place | |
find ${REPO_DIR}/common/${CFG_ENV}/* -prune -type d | while IFS= read -r d; do | |
folder=${d##*/} | |
rsync -avz --delete $d/* ${TARGET}:./deploy/config_versions/config_${COMMIT}/$folder/ | |
[ ! $? -eq 0 ] && { echo -e "${b_red}Error Rsync commit folder to ${TARGET}, continuing to next host ${clear}"; continue ;} | |
done | |
ssh $TARGET "rm -f config; ln -sfn deploy/config_versions/config_${COMMIT} config;" | |
[ ! $? -eq 0 ] && { echo -e "${b_red}Error removing config on ${TARGET}, continuing to next host ${clear}"; continue ;} | |
echo -e "${green}Deployment on host $TARGET is complete${clear}" | |
done | |
} | |
get_params "${@}" | |
# check # of given params | |
[ "$#" -lt 2 ] && { print_usage; exit 1; } | |
start_message "Config" | |
echo -e "${green}\nRemoving current Config Dir and git cloning Config repo..${clear}\n\n" | |
rm -rf "${REPO_DIR:?}" | |
COMMIT=$(git -C "${REPO_DIR}" rev-parse --short=7 origin/"${BRANCH}") | |
[ ! $? -eq 0 ] && { echo -e "${b_red}Error getting the Commit number from repository, exiting${clear}"; exit 1; } | |
# get list of targets from passed parameters | |
[ -n "${HOSTS}" ] && { MACHINES="${HOSTS}"; } | |
[ -n "${DEPLOY_ENV}" ] && { MACHINES="${hostlist[$DEPLOY_ENV]}"; } | |
if [ -n "${MACHINES}" ]; then start_release; else echo -e "${b_red}No machines provided${clear}"; fi | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment