forked from crosscloudci/cross-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovision.sh
executable file
·175 lines (162 loc) · 8.22 KB
/
provision.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
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
_retry() {
[ -z "${2}" ] && return 1
echo -n ${1}
until printf "." && "${@:2}" &>/dev/null; do sleep 5.2; done; echo "✓"
}
set -e
RED='\033[0;31m'
NC='\033[0m' # No Color
export TF_VAR_name="$2"
export TF_VAR_internal_tld=${TF_VAR_name}.cncf.demo
export TF_VAR_data_dir=$(pwd)/data/${TF_VAR_name}
export TF_VAR_aws_key_name=${TF_VAR_name}
export TF_VAR_packet_api_key=${PACKET_AUTH_TOKEN}
if [ ! -e $KUBERNETES_IMAGE ] ; then
export TF_VAR_kubelet_image_url=$KUBERNETES_IMAGE
fi
if [ ! -e $KUBERNETES_TAG ] ; then
export TF_VAR_kubelet_image_tag=$KUBERNETES_TAG
fi
# tfstate, sslcerts, and ssh keys are currently stored in TF_VAR_data_dir
mkdir -p $TF_VAR_data_dir
# Run CMD
if [ "$1" = "aws-deploy" ] ; then
cd ${DIR}/aws
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=aws-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
# ensure kubeconfig is written to disk on infrastructure refresh
terraform taint -module=kubeconfig null_resource.kubeconfig || true
time terraform apply ${DIR}/aws
ELB=$(terraform output external_elb)
export KUBECONFIG=${TF_VAR_data_dir}/kubeconfig
echo "❤ Polling for cluster life - this could take a minute or more"
_retry "❤ Waiting for DNS to resolve for ${ELB}" ping -c1 "${ELB}"
_retry "❤ Curling apiserver external elb" curl --insecure --silent "https://${ELB}"
_retry "❤ Trying to connect to cluster with kubectl" kubectl cluster-info
kubectl cluster-info
elif [ "$1" = "aws-destroy" ] ; then
cd ${DIR}/aws
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=aws-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
time terraform destroy -force ${DIR}/aws
elif [ "$1" = "azure-deploy" ] ; then
# There are some dependency issues around cert,sshkey,k8s_cloud_config, and dns
# since they use files on disk that are created on the fly
# should probably move these to data resources
cd ${DIR}/azure
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
terraform apply -target null_resource.ssl_ssh_cloud_gen ${DIR}/azure && \
terraform apply -target module.dns.null_resource.dns_gen ${DIR}/azure && \
time terraform apply ${DIR}/azure
elif [ "$1" = "azure-destroy" ] ; then
cd ${DIR}/azure
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
terraform destroy -force -target null_resource.ssl_ssh_cloud_gen ${DIR}/azure && \
terraform destroy -force -target module.dns.null_resource.dns_gen ${DIR}/azure && \
terraform apply -target null_resource.ssl_ssh_cloud_gen ${DIR}/azure && \
terraform apply -target module.dns.null_resource.dns_gen ${DIR}/azure && \
time terraform destroy -force ${DIR}/azure || true
elif [ "$1" = "packet-deploy" ] ; then
cd ${DIR}/packet
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=packet-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
# ensure kubeconfig is written to disk on infrastructure refresh
terraform taint -module=kubeconfig null_resource.kubeconfig || true
time terraform apply ${DIR}/packet
ELB=$(terraform output endpoint)
export KUBECONFIG=${TF_VAR_data_dir}/kubeconfig
echo "❤ Polling for cluster life - this could take a minute or more"
_retry "❤ Waiting for DNS to resolve for ${ELB}" ping -c1 "${ELB}"
_retry "❤ Curling apiserver external elb" curl --insecure --silent "https://${ELB}"
_retry "❤ Trying to connect to cluster with kubectl" kubectl cluster-info
kubectl cluster-info
elif [ "$1" = "packet-destroy" ] ; then
cd ${DIR}/packet
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=packet-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
time terraform destroy -force ${DIR}/packet
elif [ "$1" = "gce-deploy" ] ; then
cd ${DIR}/gce
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=gce-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
# ensure kubeconfig is written to disk on infrastructure refresh
terraform taint -module=kubeconfig null_resource.kubeconfig || true
time terraform apply -target module.vpc.google_compute_subnetwork.cncf ${DIR}/gce
time terraform apply ${DIR}/gce
ELB=$(terraform output external_lb)
export KUBECONFIG=${TF_VAR_data_dir}/kubeconfig
echo "❤ Polling for cluster life - this could take a minute or more"
_retry "❤ Waiting for DNS to resolve for ${ELB}" ping -c1 "${ELB}"
_retry "❤ Curling apiserver external elb" curl --insecure --silent "https://${ELB}"
_retry "❤ Trying to connect to cluster with kubectl" kubectl cluster-info
kubectl cluster-info
elif [ "$1" = "gce-destroy" ] ; then
cd ${DIR}/gce
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=gce-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
time terraform destroy -force ${DIR}/gce || true # Allow to Fail and clean up network on next step
time terraform destroy -force -target module.vpc.google_compute_subnetwork.cncf ${DIR}/gce
elif [ "$1" = "gke-deploy" ] ; then
cd ${DIR}/gke
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=gke-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
# ensure kubeconfig is written to disk on infrastructure refresh
terraform taint -module=kubeconfig null_resource.kubeconfig || true
time terraform apply -target module.vpc ${DIR}/gke && \
time terraform apply ${DIR}/gke
ELB=$(terraform output endpoint)
export KUBECONFIG=${TF_VAR_data_dir}/kubeconfig
echo "❤ Polling for cluster life - this could take a minute or more"
_retry "❤ Waiting for DNS to resolve for ${ELB}" ping -c1 "${ELB}"
_retry "❤ Curling apiserver external elb" curl --insecure --silent "https://${ELB}"
_retry "❤ Trying to connect to cluster with kubectl" kubectl cluster-info
kubectl cluster-info
elif [ "$1" = "gke-destroy" ] ; then
cd ${DIR}/gke
terraform init \
-backend-config 'bucket=aws65972563' \
-backend-config "key=gke-${TF_VAR_name}" \
-backend-config 'region=ap-southeast-2'
time terraform destroy -force -target module.cluster.google_container_cluster.cncf ${DIR}/gke || true
echo "sleep" && sleep 10 && \
time terraform destroy -force -target module.vpc.google_compute_network.cncf ${DIR}/gke || true
time terraform destroy -force ${DIR}/gke || true
elif [ "$1" = "cross-cloud-deploy" ] ; then
terraform get ${DIR}/cross-cloud && \
terraform apply -target module.aws.null_resource.ssl_gen ${DIR}/cross-cloud && \
terraform apply -target module.gce.null_resource.ssl_gen ${DIR}/cross-cloud && \
terraform apply -target module.gce.module.etcd.null_resource.discovery_gen ${DIR}/cross-cloud && \
terraform apply -target module.azure.null_resource.ssl_ssh_cloud_gen ${DIR}/cross-cloud && \
terraform apply -target module.azure.module.dns.null_resource.dns_gen ${DIR}/cross-cloud && \
terraform apply -target module.packet.null_resource.ssl_ssh_gen ${DIR}/cross-cloud && \
terraform apply -target module.packet.module.etcd.null_resource.discovery_gen ${DIR}/cross-cloud && \
time terraform apply ${DIR}/cross-cloud && \
printf "${RED}\n#Commands to Configue Kubectl \n\n" && \
printf 'sudo chown -R $(whoami):$(whoami) $(pwd)/data/${name} \n\n' && \
printf 'export KUBECONFIG=$(pwd)/data/${name}/kubeconfig \n\n'${NC}
# terraform apply -target module.azure.azurerm_resource_group.cncf ${DIR}/cross-cloud && \
elif [ "$1" = "cross-cloud-destroy" ] ; then
time terraform destroy -force ${DIR}/cross-cloud
fi