-
Notifications
You must be signed in to change notification settings - Fork 1
/
make.sh
executable file
·121 lines (98 loc) · 2.53 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
#!/bin/bash
#
# variables
#
# AWS variables
PROFILE=default
REGION=eu-west-3
DOCKER_IMAGE=jeromedecoster/vote
# terraform
export TF_VAR_profile=$PROFILE
# 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'
}
# local development with docker-compose
dev() {
docker-compose \
--file docker-compose.dev.yml \
--project-name compose_dev \
up
}
# build production image
build() {
VERSION=$(jq --raw-output '.version' vote/package.json)
docker image build \
--file vote/prod/Dockerfile \
--tag $DOCKER_IMAGE:latest \
--tag $DOCKER_IMAGE:$VERSION \
./vote
}
# run production image locally
prod() {
docker-compose \
--project-name compose_prod \
up
}
# push production image to docker hub
push() {
VERSION=$(jq --raw-output '.version' vote/package.json)
docker push $DOCKER_IMAGE:latest
docker push $DOCKER_IMAGE:$VERSION
}
# terraform init
tf-init() {
cd "$dir/infra"
terraform init
}
# terraform format + validate
tf-validate() {
cd "$dir/infra"
terraform fmt -recursive
terraform validate
}
# terraform plan + apply with auto approve
tf-apply() {
cd "$dir/infra"
terraform plan \
-out=terraform.plan
terraform apply \
-auto-approve \
terraform.plan
}
# terraform scale up to 3 instances
tf-scale-up() {
export TF_VAR_desired_count=3
tf-apply
}
# terraform scale down to 1 instance (warn: target deregistration take time)
tf-scale-down() {
export TF_VAR_desired_count=1
tf-apply
}
# terraform destroy all resources
tf-destroy() {
cd "$dir/infra"
terraform destroy \
-auto-approve
}
# 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