This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·186 lines (170 loc) · 6.62 KB
/
setup.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env bash
set -euo pipefail
currentScriptDir="$( cd "$( dirname "${BASH_SOURCE[0]:-}" )" >/dev/null 2>&1 && pwd )"
scriptsDir="${currentScriptDir}/scripts"
source "${scriptsDir}/functions.sh"
checkVars() {
if [ -z "${INSTALL_USER:-}" ]; then
prompt INSTALL_USER "Which user will use the installation (e.g. ec2-user)? Leave blank to use the current user." "ec2-user"
else
info "Found INSTALL_USER = '$INSTALL_USER'"
fi
if [ -z "${INSTALL_USER_HOME:-}" ]; then
prompt INSTALL_USER_HOME "Where is your Linux Home Directory? Leave blank to use the current user's Home." "$(getent passwd "$INSTALL_USER" | cut -d: -f6)"
else
info "Found INSTALL_USER_HOME = '$INSTALL_USER_HOME'"
fi
if [ -z "${PLATFORM_ECR_IMAGE:-}" ]; then
prompt PLATFORM_ECR_IMAGE "What is the Platform ECR Image URI?" ""
else
info "Found PLATFORM_ECR_IMAGE = '$PLATFORM_ECR_IMAGE'"
fi
if [ -z "${AWS_ECR_CREDENTIALS:-}" ]; then
prompt AWS_ECR_CREDENTIALS "What are your AWS ECR Credentials? Input in the form: <AWS_ACCESS_KEY_ID>:<AWS_SECRET_ACCESS_KEY>" ""
else
info "Found AWS_ECR_CREDENTIALS!"
fi
}
activateNetIpForward() {
if [ $(sysctl -n net.ipv4.ip_forward) -eq 1 ]; then
info "DEVOP-781: OK -> $(sysctl net.ipv4.ip_forward)"
else
if grep -q "^net.ipv4.ip_forward" /etc/sysctl.conf; then
info "DEVOP-781: REPLACING net.ipv4.ip_forward"
sed -i.bak "s/net.ipv4.ip_forward.*/net.ipv4.ip_forward=1/" /etc/sysctl.conf
rm -f /etc/sysctl.conf.bak
else
info "DEVOP-781: APPENDING net.ipv4.ip_forward"
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf
fi
info "DEVOP-781: restarting network"
systemctl restart network
if [ $(sysctl -n net.ipv4.ip_forward) -eq 1 ]; then
info "DEVOP-781: OK -> $(sysctl net.ipv4.ip_forward)"
else
info "Still broken after network restart -> $(sysctl net.ipv4.ip_forward)"
exit 1
fi
fi
}
dnsAutoConfigure() {
if [ ! -f /etc/init.d/dns-auto-configure ]; then
info "dns-auto-configure - Cannot find dns-auto-configure service. Installing now..."
bash -c "${currentScriptDir}/scripts/dns-auto-configure-scripts/dns-auto-configure-setup"
else
info "dns-auto-configure - found service, no need to install."
fi
}
provisioningSteps() {
local check_only=${1:-}
if ! command -v aws > /dev/null; then
if [ -z "${check_only}" ]; then
info "Installing AWS CLI..."
curl --fail -sSL "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o awscliv2.zip
unzip -u -q awscliv2.zip
./aws/install --bin-dir /usr/bin --update
else
die "Provisioning not performed. Please run script as sudo first."
fi
fi
aws --version
if ! command -v jq > /dev/null; then
if [ -z "${check_only}" ]; then
info "Installing jq..."
yum install -q -y jq
else
die "Provisioning not performed. Please run script as sudo first."
fi
fi
jq --version
if ! command -v docker > /dev/null; then
if [ -z "${check_only}" ]; then
info "Installing Docker..."
amazon-linux-extras install -q -y docker
systemctl enable docker
systemctl start docker
else
die "Provisioning not performed. Please run script as sudo first."
fi
fi
docker --version
if ! command -v psql > /dev/null; then
if [ -z "${check_only}" ]; then
info "Installing Postgres..."
amazon-linux-extras install -q -y postgresql11
else
die "Provisioning not performed. Please run script as sudo first."
fi
fi
psql --version
if ! command -v docker-compose > /dev/null; then
if [ -z "${check_only}" ]; then
info "Installing docker-compose..."
curl --fail -sSL "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose
else
die "Provisioning not performed. Please run script as sudo first."
fi
fi
docker-compose --version
}
dockerPullPlatform() {
local awsKey=$(echo ${AWS_ECR_CREDENTIALS//:*})
local awsSec=$(echo ${AWS_ECR_CREDENTIALS//*:})
local platformEcrImageRegistry=$(echo $PLATFORM_ECR_IMAGE | cut -d/ -f 1)
local platformEcrImageRegion=$(echo $PLATFORM_ECR_IMAGE | cut -d. -f 4)
info "Attempting to login to the ECR repo '${platformEcrImageRegistry}'..."
AWS_ACCESS_KEY_ID=$awsKey AWS_SECRET_ACCESS_KEY=$awsSec \
aws ecr get-login-password --region "${platformEcrImageRegion}" | \
docker login --username AWS --password-stdin "${platformEcrImageRegistry}"
docker pull ${PLATFORM_ECR_IMAGE}
}
setupPlatformScripts() {
docker create --name tmp-platform-init ${PLATFORM_ECR_IMAGE} bash
if ! rm -rf $INSTALL_USER_HOME/docker-compose; then
die "Could not delete the existing '$INSTALL_USER_HOME/docker-compose'. Please run the setup.sh as root to provision the instance and start scripts."
fi
docker cp tmp-platform-init:/docker-compose $INSTALL_USER_HOME/docker-compose
docker rm tmp-platform-init
chown -R $INSTALL_USER $INSTALL_USER_HOME/docker-compose
cd $INSTALL_USER_HOME/docker-compose || exit 1
make
}
# Main
info "Hello from $(whoami) in $(pwd)"
checkVars
# Run as root to setup instance
if [[ $EUID -eq 0 ]]; then
info "Setting up Instance as root user..."
info "Preemptively adding docker group and giving $INSTALL_USER membership..."
[ $(getent group docker) ] || groupadd -r docker
if id "${INSTALL_USER}" | grep -q "(docker)"; then
info "User '${INSTALL_USER}' has the docker group. Doing nothing..."
else
added_docker='true'
usermod -aG docker $INSTALL_USER
fi
activateNetIpForward
dnsAutoConfigure
provisioningSteps
else
provisioningSteps 'true'
info "Pulling Platform without setting up instance..."
fi
info "Setting up Platform..."
dockerPullPlatform
setupPlatformScripts
info "Instance now provisioned. Checking docker group membership..."
if [[ "${added_docker:-}" == 'true' ]]; then
info "WARNING: User '${INSTALL_USER}' has just been added to the docker group."
info "WARNING: Please logout and login as ${INSTALL_USER} before continuing with the steps below."
fi
echo
info "ATTENTION: Login again if required (see above)"
info "To start spotlight, please copy and paste the following:"
echo "{
export AWS_ECR_CREDENTIALS=${AWS_ECR_CREDENTIALS}
cd ${INSTALL_USER_HOME}/docker-compose
make start/spotlight/aws-dev
}"