-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform.sh
79 lines (59 loc) · 2.07 KB
/
terraform.sh
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
#!/bin/bash
# (C) Stephen "TheCodeAssassin" Hoogendijk 2015
# Terraform run script for Hatchup
# create a strong and random password using openssl
MYSQL_PASSWORD=$(openssl rand -hex 16)
CURPWD=$(pwd)
OPERATION=$1
KEY_NAME=$2
KEY_PATH=$3
echo
change_config_entry() {
SEARCH=$(printf "%q" "$1" )
REPLACE=$(printf "%q" "$2" )
`sed -i'' -e "s/$SEARCH/$REPLACE/g" ${CONFIG_PATH}/config.ini`
}
cd terraform
CONFIG_PATH="../application/app/config"
if [[ ! ${OPERATION} = "apply" ]] && [[ ! ${OPERATION} = "destroy" ]] ; then
echo "=> Only apply and destroy are valid operation"
echo
exit 1
fi
if [[ $1 = "" ]] || [[ $2 = "" ]] ; then
echo "=> Please use this script as ./run.sh <apply|destroy> <keyname> <public_key_path>"
echo
exit 1
fi
if [ ! -e ${CONFIG_PATH}/config.ini ] ; then
echo
echo "=> Creating config.ini file for the application"
cp ${CONFIG_PATH}/config.ini.template ${CONFIG_PATH}/config.ini
# set the mysql root user password
change_config_entry "MYSQL_PASSWORD" "${MYSQL_PASSWORD}"
else
# use the mysql password from the configuration file
MYSQL_PASSWORD=$(sed -n 's/mysql_pass = "\(.*\)"/\1/p' ${CONFIG_PATH}/config.ini | xargs)
fi
echo "=> Running terraform..."
terraform ${OPERATION} -var "key_name=${KEY_NAME}" -var "public_key_path=${KEY_PATH}" -var "mysql_root_password=${MYSQL_PASSWORD}"
if [ -e terraform.tfstate ] && [ $? = 0 ] && [[ ${OPERATION} = "apply" ]]; then
echo
echo "=> Writing configuration values..."
echo
MYSQL_HOST=$(terraform output mysql_address)
MYSQL_PORT=$(terraform output mysql_port)
ES_HOST=$(terraform output es_address)
WEB_HOST=$(terraform output web_address)
change_config_entry "MYSQL_HOST" "${MYSQL_HOST}"
change_config_entry "MYSQL_PORT" "${MYSQL_PORT}"
change_config_entry "ELASTICSEARCH_HOST" "${ES_HOST}"
change_config_entry "ELASTICSEARCH_PORT" "9200"
echo
echo "=> Application available at http://${WEB_HOST}"
echo
elif [ -e terraform.tfstate ] && [ ! $? = 0 ]; then
echo "=> Terraform failed to ${OPERATION}"
fi
cd ${CURPWD}
exit 0