-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmake.sh
executable file
·157 lines (125 loc) · 3.75 KB
/
make.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
#!/bin/bash
#
# variables
#
# AWS variables
AWS_PROFILE=default
AWS_REGION=eu-west-3
# project name
PROJECT_NAME=lambda-terraform-github-actions
# the directory containing the script file
dir="$(cd "$(dirname "$0")"; pwd)"
cd "$dir"
log() { echo -e "\e[30;47m ${1^^} \e[0m ${@:2}"; } # $1 uppercase background white
info() { echo -e "\e[48;5;28m ${1^^} \e[0m ${@:2}"; } # $1 uppercase background green
warn() { echo -e "\e[48;5;202m ${1^^} \e[0m ${@:2}" >&2; } # $1 uppercase background orange
error() { echo -e "\e[48;5;196m ${1^^} \e[0m ${@:2}" >&2; } # $1 uppercase background red
# log $1 in underline then $@ then a newline
under() {
local arg=$1
shift
echo -e "\033[0;4m${arg}\033[0m ${@}"
echo
}
usage() {
under usage 'call the Makefile directly: make dev
or invoke this file directly: ./make.sh dev'
}
create-env() {
# check if user already exists (return something if user exists, otherwise return nothing)
local exists=$(aws iam list-user-policies \
--user-name $PROJECT_NAME \
--profile $AWS_PROFILE \
2>/dev/null)
[[ -n "$exists" ]] && { error abort user $PROJECT_NAME already exists; return; }
# create a user named $PROJECT_NAME
log create iam user $PROJECT_NAME
aws iam create-user \
--user-name $PROJECT_NAME \
--profile $AWS_PROFILE \
1>/dev/null
aws iam attach-user-policy \
--user-name $PROJECT_NAME \
--policy-arn arn:aws:iam::aws:policy/PowerUserAccess \
--profile $AWS_PROFILE
local key=$(aws iam create-access-key \
--user-name $PROJECT_NAME \
--query 'AccessKey.{AccessKeyId:AccessKeyId,SecretAccessKey:SecretAccessKey}' \
--profile $AWS_PROFILE \
2>/dev/null)
local AWS_ACCESS_KEY_ID=$(echo "$key" | jq '.AccessKeyId' --raw-output)
log AWS_ACCESS_KEY_ID $AWS_ACCESS_KEY_ID
local AWS_SECRET_ACCESS_KEY=$(echo "$key" | jq '.SecretAccessKey' --raw-output)
log AWS_SECRET_ACCESS_KEY $AWS_SECRET_ACCESS_KEY
# envsubst tips : https://unix.stackexchange.com/a/294400
# create .env file
cd "$dir"
# export variables for envsubst
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
envsubst < .env.tmpl > .env
info created file .env
}
# create env + terraform init
setup() {
create-env
# terraform init
tf-init
}
# delete env + terraform destroy
delete() {
# delete a user named $PROJECT_NAME
log delete iam user $PROJECT_NAME
aws iam detach-user-policy \
--user-name $PROJECT_NAME \
--policy-arn arn:aws:iam::aws:policy/PowerUserAccess \
--profile $AWS_PROFILE \
2>/dev/null
source "$dir/.env"
aws iam delete-access-key \
--user-name $PROJECT_NAME \
--access-key-id $AWS_ACCESS_KEY_ID \
2>/dev/null
aws iam delete-user \
--user-name $PROJECT_NAME \
--profile $AWS_PROFILE
cd "$dir"
rm --force .env
# terraform destroy
tf-destroy
}
tf-init() {
cd "$dir/infra"
terraform init
}
tf-validate() {
cd "$dir/infra"
terraform fmt -recursive
terraform validate
}
tf-apply() {
cd "$dir/infra"
terraform plan -out=terraform.plan
terraform apply -auto-approve terraform.plan
}
tf-destroy() {
cd "$dir/infra"
terraform destroy \
-auto-approve
}
# hello-dev
hello-dev() {
cd "$dir/infra"
curl $(terraform output -raw hello_dev)
}
# hello-prod
hello-prod() {
cd "$dir/infra"
curl $(terraform output -raw hello_prod)
}
# if `$1` is a function, execute it. Otherwise, print usage
# compgen -A 'function' list all declared functions
# https://stackoverflow.com/a/2627461
FUNC=$(compgen -A 'function' | grep $1)
[[ -n $FUNC ]] && { info execute $1; eval $1; } || usage;
exit 0