-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-stack
executable file
·134 lines (116 loc) · 4.48 KB
/
create-stack
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
#!/usr/bin/env bash
set -e
script_dir=$(cd "$(dirname "$0")" ; pwd -P)
source ${script_dir}/fetch-github-registration-token.sh
repo_name=$(fetch-repo-name)
create-update() {
pushd "${script_dir}" > /dev/null
project_name="${1}"
module_name="${2}"
aws_region="${3}"
github_username="${4}"
for i in project_name module_name aws_region github_username; do
if [ -z "!{i}" ]; then
echo "${i} not set. Usage <func: create-update> PROJECT_NAME MODULE_NAME AWS_REGION GITHUB_USERNAME"
exit 1
fi
done
response=$(fetch-github-registration-token ${github_username} ${repo_name})
token=$(echo $response | jq -r .token)
if [ "${token}" == null ]; then
echo "Token is NULL. Are you sure you entered your Personal Access Token correctly?"
echo $response
exit 1
fi
create-update-ssm-parameter "${project_name}" "${module_name}" "${aws_region}" "${token}"
create-update-stack "${project_name}" "${module_name}" "${aws_region}"
popd > /dev/null
}
create-update-ssm-parameter() {
project_name="${1}"
module_name="${2}"
aws_region="${3}"
token="${4}"
for i in project_name module_name token; do
if [ -z "!{i}" ]; then
echo "${i} not set. Usage <func: create-update-ssm-parameter> PROJECT_NAME MODULE_NAME AWS_REGION TOKEN"
exit 1
fi
done
parameter="/${project_name}/${module_name}/github-runner-reg-token"
if [[ ! $(aws ssm get-parameter --name "${parameter}" --region ${aws_region}) ]]; then
echo "Parameter (${parameter}) does not exist. Creating..."
aws ssm put-parameter \
--name "${parameter}" \
--value "${token}" \
--type SecureString \
--region "${aws_region}"
else
echo "Parameter (${parameter}) exists. Updating..."
aws ssm put-parameter \
--name "${parameter}" \
--value "${token}" \
--overwrite \
--region "${aws_region}"
fi
}
create-update-stack() {
project_name="${1}"
module_name="${2}"
aws_region="${3}"
pushd "${script_dir}" > /dev/null
stack_name="${project_name}-${module_name}-github-runner-stack"
if [[ ! $(aws cloudformation describe-stacks --stack-name "${stack_name}" --region "${aws_region}") ]]; then
echo "Stack (${stack_name}) does not exist. Creating..."
aws cloudformation create-stack --stack-name "${stack_name}" \
--template-body file://./template.yaml \
--capabilities CAPABILITY_NAMED_IAM \
--region "${aws_region}" \
--parameters ParameterKey=ProjectName,ParameterValue=${project_name} ParameterKey=ModuleName,ParameterValue=${module_name} ParameterKey=InstanceType,ParameterValue=t3.medium ParameterKey=GithubRepoUrl,ParameterValue=https://github.com/${repo_name}
else
echo "Stack (${stack_name}) exists. Creating ChangeSet..."
now=$(date +%s)
aws cloudformation create-change-set \
--stack-name "${stack_name}" \
--change-set-name "update-${now}" \
--template-body file://./template.yaml \
--capabilities CAPABILITY_NAMED_IAM \
--region "${aws_region}" \
--parameters ParameterKey=ProjectName,ParameterValue=${project_name} ParameterKey=ModuleName,ParameterValue=${module_name} ParameterKey=InstanceType,ParameterValue=t3.medium ParameterKey=GithubRepoUrl,ParameterValue=https://github.com/${repo_name}
echo "Waiting 20 seconds for the ChangeSet to create..."
sleep 20
echo "Executing ChangeSet..."
aws cloudformation execute-change-set \
--change-set-name "update-${now}" \
--stack-name "${stack_name}" \
--region "${aws_region}"
echo "ChangeSet execution complete."
fi
popd > /dev/null
}
usage() { echo "Usage: $0 [-p <project name: string>] [-m <module name: string>] [-r <aws region: string>] [-u <github username: string>]" 1>&2; exit 1; }
while getopts ":p:m:r:u:" o; do
case "${o}" in
p)
project=${OPTARG}
;;
m)
module=${OPTARG}
;;
r)
region=${OPTARG}
;;
u)
githubuser=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "${project}" ] || [ -z "${module}" ] || [ -z "${region}" ] || [ -z "${githubuser}" ]; then
usage
fi
create-update ${project} ${module} ${region} ${githubuser}
echo "View your Cloudformation Stack at https://${region}.console.aws.amazon.com/cloudformation/home?region=${region}#/stacks"