forked from bitovi/bitops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·263 lines (232 loc) · 7.82 KB
/
entrypoint.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env bash
set -xe
export PATH=/root/.local/bin:$PATH
export TEMPDIR="/tmp/bitops_deployment"
export SECRETS_MGR=""
export IMG_REPO=""
export ENVROOT="$TEMPDIR/$ENVIRONMENT"
export KUBE_CONFIG_FILE="$TEMPDIR/.kube/config"
export HELM_RELEASE_NAME=""
export HELM_DEBUG_COMMAND=""
export HELM_DEPLOY=${HELM_CHARTS:=false}
export BITOPS_DIR="/opt/bitops"
export SCRIPTS_DIR="$BITOPS_DIR/scripts"
export ERROR='\033[0;31m'
export SUCCESS='\033[0;32m'
export WARN='\033[1;33m'
export NC='\033[0m'
export CREATE_KUBECONFIG_BASE64="false"
export TERRAFORM_APPLIED="false"
# ops repo paths
ROOT_DIR="/opt/bitops_deployment"
ENVROOT="$ROOT_DIR/$ENVIRONMENT"
CREATE_CLUSTER=false
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
printf "${ERROR}environment variable (AWS_ACCESS_KEY_ID) not set ${NC}"
exit 1
fi
if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
printf "${ERROR}environment variable (AWS_SECRET_ACCESS_KEY) not set ${NC}"
exit 1
fi
if [ -z "$AWS_DEFAULT_REGION" ]; then
printf "${ERROR}environment variable (AWS_DEFAULT_REGION) not set ${NC}"
exit 1
fi
if [ -z "$CLUSTER_NAME" ]; then
echo "environment variable (CLUSTER_NAME) not set "
CREATE_CLUSTER=true
fi
if [ -z "$ENVIRONMENT" ]; then
printf "${ERROR}environment variable (ENVIRONMENT) not set ${NC}"
exit 1
fi
if [ -z "$DEBUG" ]; then
echo "environment variable (DEBUG) not set"
export DEBUG=0
fi
if [ -z "$KUBECONFIG_BASE64" ]; then
echo "environment variable (KUBECONFIG_BASE64) not set"
fi
if [ -z "$NAMESPACE" ]; then
echo "environment variable (NAMESPACE) not set"
export NAMESPACE="default"
fi
if [ -z "$TIMEOUT" ]; then
echo "environment variable (TIMEOUT) not set"
export TIMEOUT="500s"
fi
rm -rf /tmp/bitops_deployment
if [ -e /opt/bitops_deployment ];
then
echo "Creating temp directory: $TEMPDIR"
if ! mkdir -p /tmp/bitops_deployment/.kube
then
echo "failed to create: $TEMPDIR"
else
echo "Successfully created $TEMPDIR "
fi
if ! cp -rf /opt/bitops_deployment/* /tmp/bitops_deployment/
then
echo "failed to copy repo to: $TEMPDIR"
else
echo "Successfully Copied repo to $TEMPDIR "
fi
else
echo "running locally"
if ! mkdir -p /tmp/bitops_deployment/.kube;
then
echo "failed to create: $TEMPDIR"
else
echo "Successfully Created $TEMPDIR "
fi
if ! cp -rf /opt/deploy/* /tmp/bitops_deployment/
then
echo "failed to copy repo to: $TEMPDIR"
else
echo "Successfully Copied repo to $TEMPDIR "
fi
fi
function create_aws_profile() {
#!/usr/bin/env bash
echo "#!/usr/bin/env bash" > ~/.bashrc
echo "" >> ~/.bashrc
echo "PATH=/root/.local/bin:$PATH" >> ~/.bashrc
mkdir -p /root/.aws /root/.kube
cat <<EOF > /root/.aws/credentials
[default]
aws_access_key_id = "${AWS_ACCESS_KEY_ID}"
aws_secret_access_key = "${AWS_SECRET_ACCESS_KEY}"
EOF
cat <<EOF > /root/.aws/config
[default]
region = "$AWS_DEFAULT_REGION"
output = json
EOF
echo "#kubeconfig" > "$TEMPDIR"/.kube/config
export KUBE_CONFIG_FILE="$TEMPDIR"/.kube/config
get_context
}
function create_config_map() {
echo "Creating config map."
curl -o aws-auth-cm.yaml https://amazon-eks.s3-us-west-2.amazonaws.com/cloudformation/2019-02-11/aws-auth-cm.yaml
TMP_WORKER_ROLE=$(shyaml get-value role < $TEMPDIR/opscruise-test/terraform/bitops.config.yaml)
AWS_ROLE_PREFIX=$(echo $TMP_WORKER_ROLE | awk -F\/ {'print $1'})
ROLE_NAME=$(echo $TMP_WORKER_ROLE | awk -F\/ {'print $2'})
WORKER_ROLE=$AWS_ROLE_PREFIX'\/'$ROLE_NAME
cat aws-auth-cm.yaml | sed 's/ARN of instance role (not instance profile)//g' | sed 's/[<]/'"$ROLE"'/g' | sed 's/[>]//g' > aws-auth-cm.yaml-tmp
rm -rf aws-auth-cm.yaml
mv aws-auth-cm.yaml-tmp aws-auth-cm.yaml
kubectl apply --kubeconfig="$KUBE_CONFIG_FILE" -f aws-auth-cm.yaml
}
function get_context() {
printf "${ERROR}HELM_DEPLOY: $HELM_DEPLOY...... ${NC}"
if [ -n "$CLUSTER_NAME" ]; then
echo "Using $CLUSTER_NAME cluster..."
else
CLUSTER_NAME=$(shyaml get-value cluster < "$TEMPDIR/$ENVIRONMENT"/terraform/bitops.config.yaml || true)
CLUSTER_NAME=$(echo $CLUSTER_NAME | sed 's/true//g')
if [ -z "$CLUSTER_NAME" ]; then
printf "${ERROR} Please set the CLUSTER_NAME environment variable. If you do not have a cluster set the TERRAFORM_APPLY to true .${NC} "
return 1
fi
fi
if [ -z "$KUBECONFIG_BASE64" ]
then
echo "${WARN}KUBECONFIG is empty${NC}"
if [ "$TERRAFORM_APPLY" == "true" ]; then
echo "Unable to find KUBECONFIG_BASE64. Attempting to retrieve KUBECONFIG from Terraform..."
echo "This will create an EKS Cluster in AWS. Charges may apply."
CREATE_KUBECONFIG_BASE64=true
bash $SCRIPTS_DIR/terraform/terraform_apply.sh
export KUBECONFIG_BASE64=$(cat "$TEMPDIR"/.kube/config | base64)
fi
if [ "${TERRAFORM_PLAN_ALTERNATE_COMMAND}" == "true" ]; then
printf "${WARN}Running Alternate Terraform command.${NC}"
bash $SCRIPTS_DIR/terraform/terraform_plan.sh
fi
if [ "${TERRAFORM_APPLY_ALTERNATE_COMMAND}" == "true" ]; then
printf "${WARN}Running Alternate Terraform command.${NC}"
bash $SCRIPTS_DIR/terraform/terraform_apply.sh
fi
if [ -z "$TERRAFORM_APPLY" ]; then
printf "${WARN}TERRAFORM_APPLY and KUBECONFIG is empty...
Either supply KUBECONFIG_BASE64 or set TERRAFORM_APPLY to true...${NC}"
fi
else
if [[ "${TERRAFORM_APPLY}" == "true" ]]; then
#create config file
HELM_DEPLOY=true
create_kubeconfig
bash $SCRIPTS_DIR/terraform/terraform_apply.sh
else
#create config file
HELM_DEPLOY=true
create_kubeconfig
fi
fi
if [ -z "$KUBECONFIG_BASE64" ] && [[ ${TERRAFORM_APPLY} == "false" ]] && [[ ${TEST} == "false" ]]; then
printf "${ERROR} You did not supply KUBECONFIG_BASE64 and you have chosen not to create a cluster.\n
To create a cluster, set the environment variable TERRAFORM_APPLY to true.${NC} "
return 1
fi
}
function create_kubeconfig() {
echo "Creating kubeconfig file"
mkdir -p "$TMPDIR"/.kube
echo "${KUBECONFIG_BASE64}" | base64 -d > config
mv config "$TEMPDIR"/.kube/config
export KUBE_CONFIG_FILE="$TEMPDIR"/.kube/config
}
function clean_workspace() {
echo "Running cleanup..."
rm -rf "$TEMPDIR"
}
echo "Running deployments"
if [ -z "${AWS_ACCESS_KEY_ID}" ] || [ -z "${AWS_SECRET_ACCESS_KEY}" ]
then
printf "${ERROR}Your AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY is not set."
return 1
else
# Run Tests
if [ -n "$TEST" ]; then
printf "${SUCCESS} all arguments parsed successfully. Exiting... ${NC}"
# Todo: Add more tests.
exit 0
fi
echo "Creating AWS Profile"
create_aws_profile
fi
if [[ ${TERRAFORM_PLAN} == "true" ]];then
echo "Running Terraform Plan"
bash $SCRIPTS_DIR/terraform/terraform_plan.sh
fi
if [[ ${TERRAFORM_DESTROY} == "true" ]];then
echo "Destroying Cluster"
bash $SCRIPTS_DIR/terraform/terraform_destroy.sh
fi
if [[ ${HELM_DEPLOY} == "true" ]];then
echo "Installing Helm Charts"
/bin/bash $SCRIPTS_DIR/helm/helm_install_charts.sh
fi
if [ -z "$EXTERNAL_HELM_CHARTS" ]
then
echo "EXTERNAL_HELM_CHARTS directory not set."
else
echo "Running External Helm Charts."
bash -x $SCRIPTS_DIR/helm/helm_install_external_charts.sh
fi
if [ -z "$HELM_CHARTS_S3" ]
then
echo "HELM_CHARTS_S3 not set."
else
echo "Adding S3 Helm Repo."
bash -x $SCRIPTS_DIR/helm/helm_install_charts_from_s3.sh
fi
if [[ ${ANSIBLE_PLAYBOOKS} == "true" ]];then
echo "Running Ansible Playbooks"
bash -x $SCRIPTS_DIR/ansible/ansible_install_playbooks.sh
fi
# Cleanup Workspace
clean_workspace
printf "${SUCCESS}BitOps Completed Successfully.${NC}"