This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
deploy-aws.sh
executable file
·164 lines (137 loc) · 3.95 KB
/
deploy-aws.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
#
# Deploy the librecores site to AWS
#
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Install dependencies required to run this script
function install_deps
{
echo Installing Python dependencies
pip3 install --user -r $SCRIPT_DIR/ansible/requirements.txt
}
function usage
{
cat << EOF
Usage: $0 ENVIRONMENT ACTION
Executes the deployment action ACTION in the environment ENVIRONMENT on
Amazon Web Services (AWS).
Available ENVIRONMENTs:
staging
Staging setup: stage.librecores.org
production
Production setup: librecores.org
Available ACTIONs:
provision
Run the initial setup of the AWS account to create all necessary
instances and firewall settings.
deploy
Deploy current code
resetdata
DANGEROUS: Resets all data in the database.
initdata
Initialize the site with the data fixtures from Git.
Environment variables:
ANSIBLE_VAULT_PASSWORD_FILE
Path to a password file used to decrypt the Ansible Vault.
EOF
}
# Check if all required AWS credentials are set in the environment
function ensure_aws_creds
{
test -f aws-secrets.include && . aws-secrets.include
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
echo Error: No AWS secrets were found in the environment. >&2
echo >&2
echo Fix option 1: >&2
echo cp aws-secrets.include.dist aws-secrets.include >&2
echo and update the file with your credentials. >&2
echo >&2
echo Fix option 2:>&2
echo Manually set the environment variables AWS_ACCESS_KEY_ID and >&2
echo AWS_SECRET_ACCESS_KEY. >&2
echo >&2
echo Then run this script again. >&2
exit 1
fi
}
# Check if the SSH keys for the environment $argv[1] are available
function ensure_ssh_keys
{
environment=$1
if [ ! -f $HOME/.ssh/librecores-$environment ] ||
[ ! -f $HOME/.ssh/librecores-$environment.pub ]; then
echo "Installing SSH keys for $environment. You may be asked for the "
echo "$environment vault password."
echo
ansible-playbook $ANSIBLE_EXTRA_ARGS \
ansible/$environment-aws-configure-local-ssh.yml
fi
if [ ! -f $HOME/.ssh/librecores-$environment ] ||
[ ! -f $HOME/.ssh/librecores-$environment.pub ]; then
echo Unable to install SSH keys for $environment environment. >&2
exit 1
fi
}
# check (and possibly install) dependencies
ansible_missing=$(which ansible >/dev/null 2>&1; echo $?)
boto_missing=$(python -c "import boto" >/dev/null 2>&1; echo $?)
if [ $ansible_missing -eq 1 ] || [ $boto_missing -eq 1 ]; then
install_deps
fi
if [ -z "$ANSIBLE_VAULT_PASSWORD_FILE" ]; then
ANSIBLE_EXTRA_ARGS="--ask-vault-pass $ANSIBLE_EXTRA_ARGS"
else
ANSIBLE_EXTRA_ARGS="--vault-password-file=$ANSIBLE_VAULT_PASSWORD_FILE $ANSIBLE_EXTRA_ARGS"
fi
# Disable host key checking in Ansible's SSH
export ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_CONFIG=$SCRIPT_DIR/ansible/ansible.cfg
environment=$1
action=$2
case $environment in
staging|production)
echo Running steps in $environment environment.
;;
*)
echo ERROR: Unknown environment '$environment'. >&2
echo
usage
exit 1
esac
case $action in
show-inventory)
ensure_aws_creds
ansible-inventory \
-i $SCRIPT_DIR/ansible/aws_ec2.yml \
--playbook-dir $SCRIPT_DIR/ansible \
--list
;;
provision)
ensure_aws_creds
ensure_ssh_keys $environment
ansible-playbook \
--private-key $HOME/.ssh/librecores-$environment \
-i $SCRIPT_DIR/ansible/aws_ec2.yml \
$ANSIBLE_EXTRA_ARGS \
ansible/$environment-aws-provision.yml
;;
deploy|resetdata|initdata)
ensure_ssh_keys $environment
ansible-playbook \
--private-key $HOME/.ssh/librecores-$environment \
-i $SCRIPT_DIR/aws-static-inventory \
$ANSIBLE_EXTRA_ARGS \
ansible/$environment-aws-$action.yml
;;
"")
echo ERROR: No action given. >&2
echo
usage
exit 1
;;
*)
echo "ERROR: Unknown action '$action'." >&2
echo
usage
exit 1
esac