Jim Rigsbee
2020-02-26 a88031495958d4341fb4782e2ba8ababc7303e2f
commit | author | age
ce9ea2 1 #!/bin/bash
GC 2
3 # wapper script to agnostic_ansible_deployer playbooks
4
08b74a 5 if [ $# -lt 2 ]; then
ce9ea2 6     echo "2 args needed" >&2
GC 7     echo
8     echo 'wapper script to agnostic_ansible_deployer playbooks'
9     echo "./$0 RCFILE ACTION"
10     echo 'args'
11     echo '- RCFILE: to be sourced, containing needed env variables (especially envtype_args array)'
ae44e5 12     echo '- ACTION: provision|destroy|stop|start|scaleup'
ce9ea2 13     exit 2
GC 14 fi
15
16 set -xe -o pipefail
03c6d6 17 ORIG=$(cd $(dirname $0); cd ..; pwd)
GC 18 DEPLOYER_REPO_PATH="${ORIG}/ansible"
ce9ea2 19
bc95e8 20 export ANSIBLE_CONFIG=${DEPLOYER_REPO_PATH}/ansible.cfg
GC 21
ce9ea2 22 . "$1"
GC 23
24 if [ -z "${GUID}" ]; then
25     echo "GUID is mandatory"
26     exit 2
27 fi
03c6d6 28
ce9ea2 29
GC 30 REGION=${REGION:-us-east-1}
31 KEYNAME=${KEYNAME:-ocpkey}
32 ENVTYPE=${ENVTYPE:-generic-example}
45add8 33 PROFILE=${PROFILE:-default}
ce9ea2 34 CLOUDPROVIDER=${CLOUDPROVIDER:-ec2}
GC 35 if [ "$CLOUDPROVIDER" = "ec2" ]; then
36     if [ -z "${HOSTZONEID}" ]; then
37         echo "HOSTZONEID vars are mandatory"
38         exit 2
39     fi
03c6d6 40     INVENTORY=${DEPLOYER_REPO_PATH}/inventory/ec2.sh
GC 41 else
42     INVENTORY=${DEPLOYER_REPO_PATH}/inventory/${CLOUDPROVIDER}.py
ce9ea2 43 fi
GC 44
45 INSTALL_IPA_CLIENT=${INSTALL_IPA_CLIENT:-false}
46 REPO_METHOD=${REPO_METHOD:-file}
47 SOFTWARE_TO_DEPLOY=${SOFTWARE_TO_DEPLOY:-none}
48
49 STACK_NAME=${ENVTYPE}-${GUID}
50
51 case $2 in
52     provision)
08b74a 53         shift; shift
ce9ea2 54         ansible-playbook \
GC 55             ${DEPLOYER_REPO_PATH}/main.yml  \
03c6d6 56             -i ${INVENTORY} \
ce9ea2 57             -e "ANSIBLE_REPO_PATH=${DEPLOYER_REPO_PATH}" \
GC 58             -e "guid=${GUID}" \
59             -e "env_type=${ENVTYPE}" \
60             -e "key_name=${KEYNAME}" \
61             -e "cloud_provider=${CLOUDPROVIDER}" \
62             -e "aws_region=${REGION}" \
928f77 63             -e "azure_region=${REGION}" \
ce9ea2 64             -e "HostedZoneId=${HOSTZONEID}" \
GC 65             -e "install_ipa_client=${INSTALL_IPA_CLIENT}" \
66             -e "software_to_deploy=${SOFTWARE_TO_DEPLOY}" \
67             -e "repo_method=${REPO_METHOD}" \
08b74a 68             ${ENVTYPE_ARGS[@]} \
GC 69             "$@"
ce9ea2 70         ;;
ae44e5 71
ce9ea2 72     destroy)
08b74a 73         shift; shift
ce9ea2 74         ansible-playbook \
GC 75             ${DEPLOYER_REPO_PATH}/configs/${ENVTYPE}/destroy_env.yml \
03c6d6 76             -i ${INVENTORY} \
ce9ea2 77             -e "ANSIBLE_REPO_PATH=${DEPLOYER_REPO_PATH}" \
GC 78             -e "guid=${GUID}" \
79             -e "env_type=${ENVTYPE}"  \
80             -e "cloud_provider=${CLOUDPROVIDER}" \
81             -e "aws_region=${REGION}"  \
928f77 82             -e "azure_region=${REGION}"  \
ce9ea2 83             -e "HostedZoneId=${HOSTZONEID}"  \
GC 84             -e "key_name=${KEYNAME}"  \
928f77 85             ${ENVTYPE_ARGS[@]} \
08b74a 86             "$@"
ce9ea2 87         ;;
ae44e5 88
GC 89     scaleup)
90         shift; shift
91         ansible-playbook \
92             ${DEPLOYER_REPO_PATH}/configs/${ENVTYPE}/scaleup.yml \
93             -i ${INVENTORY} \
94             -e "ANSIBLE_REPO_PATH=${DEPLOYER_REPO_PATH}" \
95             -e "guid=${GUID}" \
96             -e "env_type=${ENVTYPE}" \
97             -e "key_name=${KEYNAME}" \
98             -e "cloud_provider=${CLOUDPROVIDER}" \
99             -e "aws_region=${REGION}" \
928f77 100             -e "azure_region=${REGION}"  \
ae44e5 101             -e "HostedZoneId=${HOSTZONEID}" \
GC 102             -e "install_ipa_client=${INSTALL_IPA_CLIENT}" \
103             -e "software_to_deploy=${SOFTWARE_TO_DEPLOY}" \
104             -e "repo_method=${REPO_METHOD}" \
105             ${ENVTYPE_ARGS[@]} \
106             "$@"
107         ;;
108
ce9ea2 109     stop)
45add8 110         aws ec2 stop-instances --profile $PROFILE --region $REGION --instance-ids $(aws ec2 describe-instances --filters "Name=tag:aws:cloudformation:stack-name,Values=${STACK_NAME}" --query Reservations[*].Instances[*].InstanceId --profile $PROFILE --region $REGION --output text)
ce9ea2 111         ;;
GC 112     start)
45add8 113         aws ec2 start-instances --profile $PROFILE --region $REGION --instance-ids `aws ec2 describe-instances --filters "Name=tag:aws:cloudformation:stack-name,Values=${STACK_NAME}" --query Reservations[*].Instances[*].InstanceId --profile $PROFILE --region $REGION --output text`
ce9ea2 114         ;;
GC 115 esac