-
Notifications
You must be signed in to change notification settings - Fork 5
/
replace.sh
executable file
·97 lines (77 loc) · 1.88 KB
/
replace.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
#!/bin/bash
OS_NAME="$(uname | awk '{print tolower($0)}')"
command -v tput >/dev/null && TPUT=true
_echo() {
if [ "${TPUT}" != "" ] && [ "$2" != "" ]; then
echo -e "$(tput setaf $2)$1$(tput sgr0)"
else
echo -e "$1"
fi
}
_warn() {
echo
_echo "- $@" 5
}
_result() {
echo
_echo "# $@" 4
}
_command() {
echo
_echo "$ $@" 3
}
_success() {
echo
_echo "+ $@" 2
exit 0
}
_error() {
echo
_echo "- $@" 1
exit 1
}
_replace() {
if [ "${OS_NAME}" == "darwin" ]; then
sed -i "" -e "$1" "$2"
else
sed -i -e "$1" "$2"
fi
}
_find_replace() {
if [ "${OS_NAME}" == "darwin" ]; then
find . -name "$2" -exec sed -i "" -e "$1" {} \;
else
find . -name "$2" -exec sed -i -e "$1" {} \;
fi
}
_main() {
# variable
export ACCOUNT_ID=$(aws sts get-caller-identity | jq .Account -r)
export REGION="$(aws configure get region)"
export BUCKET="terraform-workshop-${1:-${ACCOUNT_ID}}"
export LOCK_TABLE="terraform-resource-lock"
_result "ACCOUNT_ID = ${ACCOUNT_ID}"
_result "REGION = ${REGION}"
_result "BUCKET = ${BUCKET}"
# create s3 bucket
COUNT=$(aws s3 ls | grep ${BUCKET} | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_command "aws s3 mb s3://${BUCKET}"
aws s3 mb s3://${BUCKET} --region ${REGION}
fi
# create dynamodb table
COUNT=$(aws dynamodb list-tables | jq -r .TableNames | grep ${LOCK_TABLE} | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_command "aws dynamodb create-table --table-name ${LOCK_TABLE}"
aws dynamodb create-table \
--table-name ${LOCK_TABLE} \
--attribute-definitions AttributeName=LockID,AttributeType=S \
--key-schema AttributeName=LockID,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--region ${REGION} | jq .
fi
# replace
_find_replace "s/terraform-workshop-[[:alnum:]]*/${BUCKET}/g" "*.tf"
_success
}
_main