-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·109 lines (91 loc) · 2.54 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
#!/bin/sh
set -e
Main(){
PrintLogo
desired_state="$1" && { shift || true; }
case $desired_state in
up) ClusterUp $@
;;
stop) ClusterStop $@
;;
down) ClusterDown $@
;;
show-kubeconfig) ShowKubeconfig
;;
*) PrintUsage && exit 1
;;
esac
}
PrintLogo(){
cat <<EOF
========================================
PiwPiiwn - k8s Cluster State Manager
========================================
EOF
}
PrintUsage(){
cat <<EOF
Usage: $0 [up|stop|down|show-kubeconfig]
---
up - Create the cluster
stop - Stop destroy workers
down - Destroy cluster
show-kubeconfig - Displays kubeconfig file
EOF
}
TerraformInit(){
# https://github.com/hashicorp/terraform/issues/13022#issuecomment-294262392
terraform init \
-backend-config "bucket=${AWS_TFBACKEND_S3_BUCKET:-piwpiiwn-tfstate}" \
-backend-config "key=scaleway-k8s-${CLUSTER_NAME:-piwpiiwn}.tfstate"
}
ClusterUp(){
TerraformInit
terraform apply ${@:---var-file=/app/cluster-start.tfvars --auto-approve}
}
ClusterStop(){
TerraformInit
terraform apply ${@:---var-file=/app/cluster-stop.tfvars --auto-approve}
}
ClusterDown(){
TerraformInit
terraform destroy ${@:---var-file=/app/cluster-start.tfvars --auto-approve}
}
ShowKubeconfig(){
TerraformInit
echo '=== KUBECONFIG ==='
terraform output kubeconfig
echo '=================='
}
Setup(){
# Create scaleway config file from environment vars
[ -f ~/.config/scw/config.yaml ] && { return; } || { mkdir -p ~/.config/scw; }
cat > ~/.config/scw/config.yaml <<EOF
access_key: ${SCW_ACCESS_KEY:-VARNOTSET}
secret_key: ${SCW_SECRET_KEY:-VARNOTSET}
default_organization_id: ${SCW_DEFAULT_ORGANIZATION_ID:-VARNOTSET}
default_project_id: ${SCW_DEFAULT_PROJECT_ID:-VARNOTSET}
default_region: ${SCW_DEFAULT_REGION:-VARNOTSET}
default_zone: ${SCW_DEFAULT_ZONE:-VARNOTSET}
EOF
sed -i '/.*VARNOTSET$/d' ~/.config/scw/config.yaml
# Update tfvars based on env vars
awk '/^variable /{gsub("\"","");print $2}' 99_vars.tf \
| while read var_name
do
local env_var_name=$( echo "$var_name" | awk '{print toupper($0)}')
local value="$(eval echo "\$${env_var_name}")"
if [ ! -z "$value" ]
then
for file in $(ls /app/*.tfvars)
do
grep -q '^'${var_name}'\s*=' $file \
&& sed -i 's/^'${var_name}'\s*=.*/'${var_name}' = "'${value}'"/' $file \
|| echo "${var_name} = \"${value}\"" >> $file
done
fi
done
}
# Setup if I am inside docker container
awk -F/ '$2 == "docker"' /proc/self/cgroup > /dev/null 2>&1 && Setup
Main $@