ansible-provision.sh 6.69 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env bash

# Ansible provisioning wrapper script that
# assumes the following parameters set
# as environment variables
6
#
7
# - github_username
8
# - server_type
9 10 11 12 13 14 15 16 17 18 19
# - instance_type
# - region
# - aws_account
# - keypair
# - ami
# - root_ebs_size
# - security_group
# - dns_zone
# - dns_name
# - environment
# - name_tag
John Jarvis committed
20
set -x
Han Su Kim committed
21
env
John Jarvis committed
22
export PYTHONUNBUFFERED=1
23
export BOTO_CONFIG=/var/lib/jenkins/${aws_account}.boto
John Jarvis committed
24

25 26 27 28 29 30
run_ansible() {
  ansible-playbook $@
  ret=$?
  [ $ret -ne 0 ] && exit $ret
}

31
# This DATE_TIME will be used as instance launch time tag
32
TERMINATION_DATE_TIME=`date +"%m-%d-%Y %T" --date="-7 days ago"`
33

34 35 36 37 38 39 40 41 42
if [[ -z $BUILD_USER ]]; then
    BUILD_USER=jenkins
fi

if [[ -z $BUILD_USER_ID ]]; then
    BUILD_USER_ID=edx-sandbox
fi


John Jarvis committed
43 44 45 46
if [[ -z $WORKSPACE ]]; then
    dir=$(dirname $0)
    source "$dir/ascii-convert.sh"
else
John Jarvis committed
47
    source "$WORKSPACE/configuration/util/jenkins/ascii-convert.sh"
John Jarvis committed
48
fi
49

50 51
if [[ -z $static_url_base ]]; then
  static_url_base="/static"
52 53
fi

54
if [[ -z $github_username  ]]; then
John Jarvis committed
55
  github_username=$BUILD_USER_ID
56 57 58 59 60 61 62
fi

if [[ ! -f $BOTO_CONFIG ]]; then
  echo "AWS credentials not found for $aws_account"
  exit 1
fi

63
extra_vars_file="/var/tmp/extra-vars-$$.yml"
64
extra_var_arg="-e@${extra_vars_file}"
65

John Jarvis committed
66
if [[ $edx_internal == "true" ]]; then
John Jarvis committed
67
    # if this is a an edx server include
John Jarvis committed
68
    # the secret var file
69
    extra_var_arg="-e@${WORKSPACE}/configuration-secure/ansible/vars/developer-sandbox.yml -e@${extra_vars_file}"
70 71
fi

72
if [[ -z $region ]]; then
John Jarvis committed
73
  region="us-east-1"
74 75 76
fi

if [[ -z $zone ]]; then
77 78 79 80 81
  zone="us-east-1c"
fi

if [[ -z $vpc_subnet_id ]]; then
  vpc_subnet_id="subnet-cd867aba"
82 83 84
fi

if [[ -z $elb ]]; then
John Jarvis committed
85
  elb="false"
86 87
fi

88
if [[ -z $dns_name ]]; then
89
  dns_name=${github_username}
90 91 92 93 94 95
fi

if [[ -z $name_tag ]]; then
  name_tag=${github_username}-${environment}
fi

96
if [[ -z $ami ]]; then
97
  if [[ $server_type == "full_edx_installation" ]]; then
98
    ami="ami-867d3bee"
99
  elif [[ $server_type == "ubuntu_12.04" || $server_type == "full_edx_installation_from_scratch" ]]; then
Edward Zarecor committed
100
    ami="ami-e2bbff8a"
Han Su Kim committed
101
  elif [[ $server_type == "ubuntu_14.04(experimental)" ]]; then
Edward Zarecor committed
102
    ami="ami-88562de0"
103 104 105 106
  fi
fi

if [[ -z $instance_type ]]; then
107
  instance_type="t2.medium"
108 109
fi

110 111 112 113
if [[ -z $enable_monitoring ]]; then
  enable_monitoring="false"
fi

114 115 116
# Lowercase the dns name to deal with an ansible bug
dns_name="${dns_name,,}"

117
deploy_host="${dns_name}.${dns_zone}"
John Jarvis committed
118
ssh-keygen -f "/var/lib/jenkins/.ssh/known_hosts" -R "$deploy_host"
119

John Jarvis committed
120 121
cd playbooks/edx-east

122
cat << EOF > $extra_vars_file
John Jarvis committed
123 124 125 126
---
ansible_ssh_private_key_file: /var/lib/jenkins/${keypair}.pem
edx_platform_version: $edxapp_version
forum_version: $forum_version
127
notifier_version: $notifier_version
John Jarvis committed
128 129 130 131 132 133 134
xqueue_version: $xqueue_version
xserver_version: $xserver_version
ora_version: $ora_version
ease_version: $ease_version
certs_version: $certs_version
discern_version: $discern_version
EDXAPP_STATIC_URL_BASE: $static_url_base
135 136 137 138 139 140 141
EDXAPP_LMS_NGINX_PORT: 80
EDXAPP_LMS_PREVIEW_NGINX_PORT: 80
EDX_ANSIBLE_DUMP_VARS: true
migrate_db: "yes"
openid_workaround: True
rabbitmq_ip: "127.0.0.1"
rabbitmq_refresh: True
142
COMMON_HOSTNAME: $dns_name
143 144
COMMON_DEPLOYMENT: edx
COMMON_ENVIRONMENT: sandbox
145 146
# User provided extra vars
$extra_vars
John Jarvis committed
147 148
EOF

149 150
if [[ $basic_auth == "true" ]]; then
    # vars specific to provisioning added to $extra-vars
John Jarvis committed
151
    cat << EOF_AUTH >> $extra_vars_file
152
COMMON_ENABLE_BASIC_AUTH: True
153 154
COMMON_HTPASSWD_USER: $auth_user
COMMON_HTPASSWD_PASS: $auth_pass
155 156
XQUEUE_BASIC_AUTH_USER: $auth_user
XQUEUE_BASIC_AUTH_PASSWORD: $auth_pass
157
EOF_AUTH
158 159 160 161 162 163

else
    cat << EOF_AUTH >> $extra_vars_file
COMMON_ENABLE_BASIC_AUTH: False
EOF_AUTH

164 165
fi

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
if [[ $edx_internal == "true" ]]; then
    # if this isn't a public server add the github
    # user and set edx_internal to True so that
    # xserver is installed
    cat << EOF >> $extra_vars_file
EDXAPP_PREVIEW_LMS_BASE: preview.${deploy_host}
EDXAPP_LMS_BASE: ${deploy_host}
EDXAPP_CMS_BASE: studio.${deploy_host}
EDXAPP_SITE_NAME: ${deploy_host}
CERTS_DOWNLOAD_URL: "http://${deploy_host}:18090"
CERTS_VERIFY_URL: "http://${deploy_host}:18090"
edx_internal: True
COMMON_USER_INFO:
  - name: ${github_username}
    github: true
    type: admin
USER_CMD_PROMPT: '[$name_tag] '
John Jarvis committed
183
COMMON_ENABLE_NEWRELIC_APP: $enable_monitoring
184 185 186 187
COMMON_ENABLE_DATADOG: $enable_monitoring
FORUM_NEW_RELIC_ENABLE: $enable_monitoring
EDXAPP_NEWRELIC_LMS_APPNAME: sandbox-${dns_name}-edxapp-lms
EDXAPP_NEWRELIC_CMS_APPNAME: sandbox-${dns_name}-edxapp-cms
188
EDXAPP_NEWRELIC_WORKERS_APPNAME: sandbox-${dns_name}-edxapp-workers
189 190
XQUEUE_NEWRELIC_APPNAME: sandbox-${dns_name}-xqueue
FORUM_NEW_RELIC_APP_NAME: sandbox-${dns_name}-forums
191
SANDBOX_USERNAME: $github_username
192 193 194
EOF
fi

195

John Jarvis committed
196 197
if [[ $recreate == "true" ]]; then
    # vars specific to provisioning added to $extra-vars
198
    cat << EOF >> $extra_vars_file
199 200 201 202 203
dns_name: $dns_name
keypair: $keypair
instance_type: $instance_type
security_group: $security_group
ami: $ami
204
region: $region
205
zone: $zone
206
instance_tags:
207 208 209 210 211
    environment: $environment
    github_username: $github_username
    Name: $name_tag
    source: jenkins
    owner: $BUILD_USER
212
    instance_termination_time: $TERMINATION_DATE_TIME
213
    datadog: monitored
214
root_ebs_size: $root_ebs_size
215
name_tag: $name_tag
216 217 218 219 220 221
dns_zone: $dns_zone
rabbitmq_refresh: True
elb: $elb
EOF


222

223
    # run the tasks to launch an ec2 instance from AMI
224
    cat $extra_vars_file
225
    run_ansible edx_provision.yml -i inventory.ini $extra_var_arg --user ubuntu
John Jarvis committed
226 227

    if [[ $server_type == "full_edx_installation" ]]; then
John Jarvis committed
228 229
        # additional tasks that need to be run if the
        # entire edx stack is brought up from an AMI
230 231
        run_ansible rabbitmq.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
        run_ansible restart_supervisor.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
John Jarvis committed
232
    fi
233
fi
234

235
declare -A deploy
236
roles="edxapp forum notifier xqueue xserver ora discern certs demo testcourses"
John Jarvis committed
237 238 239
for role in $roles; do
    deploy[$role]=${!role}
done
240

John Jarvis committed
241 242
# If reconfigure was selected or if starting from an ubuntu 12.04 AMI
# run non-deploy tasks for all roles
243
if [[ $reconfigure == "true" || $server_type == "full_edx_installation_from_scratch" ]]; then
244
    cat $extra_vars_file
245
    run_ansible edx_continuous_integration.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
246 247
fi

248
if [[ $reconfigure != "true" && $server_type == "full_edx_installation" ]]; then
249 250 251
    # Run deploy tasks for the roles selected
    for i in $roles; do
        if [[ ${deploy[$i]} == "true" ]]; then
252
            cat $extra_vars_file
253
            run_ansible ${i}.yml -i "${deploy_host}," $extra_var_arg --user ubuntu --tags deploy
254 255 256
        fi
    done
fi
257

258
# deploy the edx_ansible role
259
run_ansible edx_ansible.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
260

261
# set the hostname
262
run_ansible set_hostname.yml -i "${deploy_host}," -e hostname_fqdn=${deploy_host} --user ubuntu
263

264
rm -f "$extra_vars_file"