-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinit.sh
188 lines (150 loc) · 3.65 KB
/
tinit.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/env bash
set -e
. ./color
alias t=terraform
envvarfile=_common/env.auto.tfvars
githubvarfile1=codepipeline/github.auto.tfvars
githubvarfile2=codebuild/github.auto.tfvars
sshkey=codebuild/key_pair
if [[ ! -e $envvarfile ]]; then
echo -e "input info for ${RED}terraform${NOCOLOR}\n"
read -p 'region: ' region
read -p 'name: ' name
read -p 'profile: ' profile
read -p 'prefix: ' prefix
echo
echo
echo "---confirm----"
echo "region: ${region:=us-east-1}"
echo "name: $name"
echo "profile: $profile"
echo "prefix: $prefix"
echo "--------------"
read -p 'init (y/n)' key
if [[ "$key" != "y" ]]; then
echo "aborted"
exit 0
fi
cat <<EOF > $envvarfile
region = "$region"
profile = "$profile"
author = "$name"
EOF
echo -e "wrote to $envvarfile\n"
fi
if [[ ! -e $githubvarfile1 ]]; then
echo -e "input github info for ${GREEN}codepipeline${NOCOLOR}\n"
read -p 'github_token: ' token
read -p 'github_organization: ' org
read -p 'github_repo: ' repo
read -p 'github_branch:(master) ' branch
echo
echo
echo "--------confirm---------"
echo "github_token: $token"
echo "github_organization: $org"
echo "github_repo: $repo"
echo "github_branch: ${branch:=master}"
read -p 'init (y/n)' key
if [[ "$key" != "y" ]]; then
echo "aborted"
exit 0
fi
cat <<EOF > $githubvarfile1
github_token = "$token"
github_organization = "$org"
github_repo = "$repo"
github_branch = "$branch"
EOF
echo -e "wrote to $githubvarfile1\n"
fi
if [[ ! -e $githubvarfile2 ]]; then
echo -e "input github info for ${PURPLE}codebuild${NOCOLOR}\n"
read -p 'github_token:(same as above) ' token2
read -p 'github_organization:(same as above)' org2
read -p 'github_repo: ' repo2
read -p 'github_branch:(master) ' branch2
echo
echo
echo "--------confirm---------"
echo "github_token: ${token2:=$token}"
echo "github_organization: ${org2:=$org}"
echo "github_repo: ${repo2}"
echo "github_branch: ${branch2:=master}"
read -p 'init (y/n)' key
if [[ "$key" != "y" ]]; then
echo "aborted"
exit 0
fi
cat <<EOF > $githubvarfile2
github_token = "$token2"
github_organization = "$org2"
github_repo = "$repo2"
github_branch = "$branch2"
EOF
echo "wrote to $githubvarfile2"
cat <<EOF > codebuild/credentials.json
{
"serverType": "GITHUB",
"authType": "PERSONAL_ACCESS_TOKEN",
"token": "$token2"
}
EOF
opt="${profile:+--profile ${profile}} ${region:+--region ${region}}"
aws $opt codebuild import-source-credentials --cli-input-json file://codebuild/credentials.json && \
echo -e "${ORANGE}imported to aws${NOCOLOR}\n"
aws $opt codebuild list-source-credentials
fi
if [[ ! -e $sshkey ]]; then
ssh-keygen -t rsa -N "" -C "" -f $sshkey
echo -e "${ORANGE}create key_pair for packer${NOCOLOR}"
fi
function initbackend(){
echo
printbule "init backend"
echo
cd backend
t init
t workspace new $prefix
t apply -var="remote_state_bucket=null" -auto-approve
cat <<<'remote_state_bucket = "'$(t output bucket)'"' >> env.auto.tfvars
}
function initbase(){
echo
printbule "init base"
echo
cd base
sh inittf.sh
t apply -auto-approve
}
function initecr(){
echo
printbule "init ecr"
echo
cd ecr
sh inittf.sh
t apply -auto-approve
}
function initcodepipeline(){
echo
printbule "init codepipeline"
echo
cd codepipeline
sh inittf.sh
t apply -auto-approve
}
function initcodebuild(){
echo
printbule "init codebuild"
aws ${opt} codebuild list-source-credentials
echo
cd codebuild
sh inittf.sh
t apply -auto-approve
}
(initbackend)
(initbase)
(initecr)
(initcodepipeline)
(initcodebuild)
# vim:set foldmethod=marker: #