ansible-provision.sh 6.23 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
Han Su Kim committed
20
env
John Jarvis committed
21
export PYTHONUNBUFFERED=1
22
export BOTO_CONFIG=/var/lib/jenkins/${aws_account}.boto
John Jarvis committed
23

24 25 26 27 28 29 30 31 32
if [[ -z $BUILD_USER ]]; then
    BUILD_USER=jenkins
fi

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

John Jarvis committed
107 108
cd playbooks/edx-east

109
cat << EOF > $extra_vars_file
John Jarvis committed
110 111 112 113 114 115 116 117 118 119 120
---
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
121 122 123 124 125 126 127
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
128
COMMON_HOSTNAME: $dns_name
129 130
COMMON_DEPLOYMENT: edx
COMMON_ENVIRONMENT: sandbox
131 132
# User provided extra vars
$extra_vars
John Jarvis committed
133 134
EOF

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

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

150 151
fi

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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] '
COMMON_ENABLE_NEWRELIC: $enable_monitoring
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
EOF
fi

179

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


205

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

    if [[ $server_type == "full_edx_installation" ]]; then
John Jarvis committed
211 212
        # additional tasks that need to be run if the
        # entire edx stack is brought up from an AMI
Han Su Kim committed
213 214
        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
215
    fi
216
fi
217

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

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

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

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

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

247
rm -f "$extra_vars_file"