-
Notifications
You must be signed in to change notification settings - Fork 5
/
up.sh
executable file
·93 lines (73 loc) · 3.74 KB
/
up.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
#!/usr/bin/env bash
trap "exit" INT
. config.sh
mkdir -p tmp
echo "========================================================================="
echo "k8s-hello: Creating EKS cluster"
echo "========================================================================="
if eksctl get cluster ${CLUSTER_NAME} >/dev/null 2>&1; then
echo "EKS cluster already exists. Skipping ...."
else
cat eksctl/cluster-definition.yaml | envsubst > tmp/cluster-definition.yaml
eksctl create cluster -f tmp/cluster-definition.yaml
rm tmp/cluster-definition.yaml
fi
echo "========================================================================="
echo "k8s-hello: Create Deploy Role and grant Kubernetes Access"
echo "========================================================================="
if aws cloudformation describe-stack-resources --stack-name=${DEPLOYER_ROLE_STACK_NAME} > /dev/null 2>&1; then
echo "Role stack already exists. Updating it ...."
aws cloudformation update-stack \
--stack-name=${DEPLOYER_ROLE_STACK_NAME} \
--template-body file://cloudformation/create-deployer-role.yml \
--parameters ParameterKey=DeployerRoleName,ParameterValue=${DEPLOYER_ROLE_NAME} \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
> /dev/null 2>&1
else
aws cloudformation create-stack \
--stack-name=${DEPLOYER_ROLE_STACK_NAME} \
--template-body file://cloudformation/create-deployer-role.yml \
--parameters ParameterKey=DeployerRoleName,ParameterValue=${DEPLOYER_ROLE_NAME} \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
> /dev/null 2>&1
aws cloudformation wait stack-create-complete --stack-name ${DEPLOYER_ROLE_STACK_NAME}
fi
aws eks --region eu-west-1 update-kubeconfig --name ${CLUSTER_NAME} >/dev/null 2>&1
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
ROLE=" - rolearn: arn:aws:iam::$ACCOUNT_ID:role/${DEPLOYER_ROLE_NAME}\n username: build\n groups:\n - system:masters"
kubectl get -n kube-system configmap/aws-auth -o yaml | awk "/mapRoles: \|/{print;print \"$ROLE\";next}1" > tmp/aws-auth-patch.yml
kubectl patch configmap/aws-auth -n kube-system --patch "$(cat tmp/aws-auth-patch.yml)"
kubectl patch configmap/aws-auth -n kube-system --patch "$(cat tmp/aws-auth-patch.yml)" >/dev/null 2>&1
rm tmp/aws-auth-patch.yml
echo "========================================================================="
echo "k8s-hello: Creating Project Pipeline"
echo "========================================================================="
cat cloudformation/parameters.json | envsubst > tmp/parameters.json
if aws cloudformation describe-stack-resources --stack-name=${PIPELINE_STACK_NAME} > /dev/null 2>&1; then
echo "Pipeline stack already exists. Updating it ...."
aws cloudformation update-stack \
--stack-name=${PIPELINE_STACK_NAME} \
--template-body file://cloudformation/code-pipeline.yml \
--parameters file://tmp/parameters.json \
--capabilities CAPABILITY_IAM \
> /dev/null 2>&1
if [ "$?" == "0" ]; then
aws cloudformation wait stack-update-complete \
--stack-name=${PIPELINE_STACK_NAME}
fi
rm tmp/parameters.json
else
aws cloudformation create-stack \
--stack-name=${PIPELINE_STACK_NAME} \
--template-body file://cloudformation/code-pipeline.yml \
--parameters file://tmp/parameters.json \
--capabilities CAPABILITY_IAM \
> /dev/null 2>&1
rm tmp/parameters.json
aws cloudformation wait stack-create-complete \
--stack-name=${PIPELINE_STACK_NAME}
fi
echo "========================================================================="
echo "k8s-hello: FINISHED"
echo "========================================================================="
rmdir tmp