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