ansible-provision.sh 6.29 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 31 32 33
if [[ -z $BUILD_USER ]]; then
    BUILD_USER=jenkins
fi

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


John Jarvis committed
34 35 36 37
if [[ -z $WORKSPACE ]]; then
    dir=$(dirname $0)
    source "$dir/ascii-convert.sh"
else
John Jarvis committed
38
    source "$WORKSPACE/configuration/util/jenkins/ascii-convert.sh"
John Jarvis committed
39
fi
40

41 42
if [[ -z $static_url_base ]]; then
  static_url_base="/static"
43 44
fi

45
if [[ -z $github_username  ]]; then
John Jarvis committed
46
  github_username=$BUILD_USER_ID
47 48 49 50 51 52 53
fi

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

54
extra_vars_file="/var/tmp/extra-vars-$$.yml"
55
extra_var_arg="-e@${extra_vars_file}"
56

John Jarvis committed
57
if [[ $edx_internal == "true" ]]; then
John Jarvis committed
58
    # if this is a an edx server include
John Jarvis committed
59
    # the secret var file
John Jarvis committed
60
    extra_var_arg="-e@${extra_vars_file} -e@${WORKSPACE}/configuration-secure/ansible/vars/developer-sandbox.yml"
61 62
fi

63
if [[ -z $region ]]; then
John Jarvis committed
64
  region="us-east-1"
65 66 67
fi

if [[ -z $zone ]]; then
68 69 70 71 72
  zone="us-east-1c"
fi

if [[ -z $vpc_subnet_id ]]; then
  vpc_subnet_id="subnet-cd867aba"
73 74 75
fi

if [[ -z $elb ]]; then
John Jarvis committed
76
  elb="false"
77 78
fi

79 80 81 82 83 84 85 86
if [[ -z $dns_name ]]; then
  dns_name=$github_username
fi

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

87
if [[ -z $ami ]]; then
88
  if [[ $server_type == "full_edx_installation" ]]; then
89
    ami="ami-f213ba9a"
90
  elif [[ $server_type == "ubuntu_12.04" || $server_type == "full_edx_installation_from_scratch" ]]; then
91
    ami="ami-a217b2ca"
Han Su Kim committed
92
  elif [[ $server_type == "ubuntu_14.04(experimental)" ]]; then
93
    ami="ami-10389d78"
94 95 96 97
  fi
fi

if [[ -z $instance_type ]]; then
98
  instance_type="t2.medium"
99 100
fi

101 102 103 104
if [[ -z $enable_monitoring ]]; then
  enable_monitoring="false"
fi

105
deploy_host="${dns_name}.${dns_zone}"
John Jarvis committed
106
ssh-keygen -f "/var/lib/jenkins/.ssh/known_hosts" -R "$deploy_host"
107

John Jarvis committed
108 109
cd playbooks/edx-east

110
cat << EOF > $extra_vars_file
John Jarvis committed
111 112 113 114 115 116 117 118 119 120 121
---
ansible_ssh_private_key_file: /var/lib/jenkins/${keypair}.pem
edx_platform_version: $edxapp_version
forum_version: $forum_version
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
122 123 124 125 126 127 128
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
129
COMMON_HOSTNAME: $dns_name
130 131
COMMON_DEPLOYMENT: edx
COMMON_ENVIRONMENT: sandbox
132 133
# User provided extra vars
$extra_vars
John Jarvis committed
134 135
EOF

136 137
if [[ $basic_auth == "true" ]]; then
    # vars specific to provisioning added to $extra-vars
John Jarvis committed
138
    cat << EOF_AUTH >> $extra_vars_file
139
COMMON_ENABLE_BASIC_AUTH: True
140 141
COMMON_HTPASSWD_USER: $auth_user
COMMON_HTPASSWD_PASS: $auth_pass
142 143
XQUEUE_BASIC_AUTH_USER: $auth_user
XQUEUE_BASIC_AUTH_PASSWORD: $auth_pass
144
EOF_AUTH
145 146 147 148 149 150

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

151 152
fi

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
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
170
COMMON_ENABLE_NEWRELIC_APP: $enable_monitoring
171 172 173 174 175 176
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
XQUEUE_NEWRELIC_APPNAME: sandbox-${dns_name}-xqueue
FORUM_NEW_RELIC_APP_NAME: sandbox-${dns_name}-forums
177
SANDBOX_USERNAME: $github_username
178 179 180
EOF
fi

181

John Jarvis committed
182 183
if [[ $recreate == "true" ]]; then
    # vars specific to provisioning added to $extra-vars
184
    cat << EOF >> $extra_vars_file
185 186 187 188 189
dns_name: $dns_name
keypair: $keypair
instance_type: $instance_type
security_group: $security_group
ami: $ami
190
region: $region
191
zone: $zone
192
instance_tags:
193 194 195 196 197
    environment: $environment
    github_username: $github_username
    Name: $name_tag
    source: jenkins
    owner: $BUILD_USER
198
    datadog: monitored
199
root_ebs_size: $root_ebs_size
200
name_tag: $name_tag
201 202 203 204 205 206
dns_zone: $dns_zone
rabbitmq_refresh: True
elb: $elb
EOF


207

208
    # run the tasks to launch an ec2 instance from AMI
209
    cat $extra_vars_file
210
    ansible-playbook edx_provision.yml -i inventory.ini $extra_var_arg --user ubuntu
John Jarvis committed
211 212

    if [[ $server_type == "full_edx_installation" ]]; then
John Jarvis committed
213 214
        # additional tasks that need to be run if the
        # entire edx stack is brought up from an AMI
Han Su Kim committed
215 216
        ansible-playbook rabbitmq.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
        ansible-playbook restart_supervisor.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
John Jarvis committed
217
    fi
218
fi
219

220
declare -A deploy
John Jarvis committed
221
roles="edxapp forum xqueue xserver ora discern certs demo testcourses"
John Jarvis committed
222 223 224
for role in $roles; do
    deploy[$role]=${!role}
done
225

John Jarvis committed
226 227
# If reconfigure was selected or if starting from an ubuntu 12.04 AMI
# run non-deploy tasks for all roles
228
if [[ $reconfigure == "true" || $server_type == "full_edx_installation_from_scratch" ]]; then
229
    cat $extra_vars_file
Han Su Kim committed
230
    ansible-playbook edx_continuous_integration.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
231 232
fi

233
if [[ $reconfigure != "true" && $server_type == "full_edx_installation" ]]; then
234 235 236
    # Run deploy tasks for the roles selected
    for i in $roles; do
        if [[ ${deploy[$i]} == "true" ]]; then
237
            cat $extra_vars_file
Han Su Kim committed
238
            ansible-playbook ${i}.yml -i "${deploy_host}," $extra_var_arg --user ubuntu --tags deploy
239 240 241
        fi
    done
fi
242

243
# deploy the edx_ansible role
Han Su Kim committed
244
ansible-playbook edx_ansible.yml -i "${deploy_host}," $extra_var_arg --user ubuntu
245

246
# set the hostname
Han Su Kim committed
247
ansible-playbook set_hostname.yml -i "${deploy_host}," -e hostname_fqdn=${deploy_host} --user ubuntu
248

249
rm -f "$extra_vars_file"