-
Notifications
You must be signed in to change notification settings - Fork 24
/
setting-vhost-route53-backlog.sh
313 lines (262 loc) · 9 KB
/
setting-vhost-route53-backlog.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#!/bin/sh
#
# [Shell] git Webhooks Auto Deployment (Backlog.jp)
# Server Deployment Script using Backlog v4.0.1
# ----------
# By Katz Ueno
# INSTRUCTION:
# ----------
# Read https://github.com/katzueno/git-Webhooks-Auto-Deploy-PHP-Script for the detail
#
#
# THIS IS FOR BACKLOG.JP
#
#
# USE IT AT YOUR OWN RISK!
# ----------
# COMMAND Options
# ----------
# sh setup_cooding.sh [SUBDOMAIN] [Backlog Proj Name] [GIT Name] [BRANCH] [BASIC AUTH USERNAME] [PASSWORD] [DEPLOY KEY] [NPM OPTION]
# e.g.) sh setup_cooding.sh coding PROJ test master coding 123456 ABCDEFG123456 tailwind
# $1 [SUBDOMAIN]
# $2 [Backlog Proj Name]
# $3 [GIT Name]
# $4 [GIT BRANCH]
# $5 [BASIC AUTH USERNAME]
# $6 [PASSWORD]
# $7 [DEPLOY KEY]
# $8 [NPM Option]
# --------------------
# GET PARAMETERS
# --------------------
SUBDOMAIN=$1
GIT_NAME=$3
GIT_BRANCH=$4
BASICAUTH_USERNAME=$5
BASICAUTH_PASSWORD=$6
DEPLOY_KEY=$7
NPM_OTION=$8
# --------------------
# SET PARAMETERS
# --------------------
# Domains & Basic Auth
MAIN_DOMAIN="EXAMPLE.COM"
MAIN_BASICAUTH_ID="username"
MAIN_BASICAUTH_PASS="password"
DIR_VHOST="/var/www/vhosts/"
DIR_CURRENT="/var/www/vhosts/EXAMPLE.COM/"
DIR_NGINX_CONF="/etc/nginx/conf.d/"
DIR_WEBROOT="${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}"
DIR_OWNER="nginx:nginx"
WEB_USER="nginx"
# AWS Related Info
## Make these string null or empty if you don't want to execute Route53 changes
AWS_HOSTED_ZONE="XXXXXXXXXXX"
AWS_EIP="XXX.XXX.XXX.XXX"
# Backlog
BACKLOG_SPACE="katzueno"
# Change your main deploy script remote
GIT_DEPLOY_URL="https://${BACKLOG_SPACE}.backlog.jp/git/XXXXX/XXXXXXXXXX/blob/master/${SUBDOMAIN}.php"
# --------------------------------------------------------------------------------
# DO NOT TOUCH BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING
# --------------------------------------------------------------------------------
echo "===================================="
echo "= GIT DEPLOY SERVER SETUP ="
echo "===================================="
# --------------------
# SET PARAMETERS FROM PARAMETERS
# --------------------
# Wiki Deploy PHP Location
GIT_WEB="https://${BACKLOG_SPACE}.backlog.jp/git/${BACKLOG_PROJECTNAME}/${GIT_NAME}/tree/${GIT_BRANCH}"
GIT_SSH="${BACKLOG_SPACE}@${BACKLOG_SPACE}.git.backlog.jp:/${BACKLOG_PROJECTNAME}/${GIT_NAME}.git"
GIT_DEPLOY_WEBHOOK="https://${MAIN_BASICAUTH_ID}:${MAIN_BASICAUTH_PASS}@${MAIN_DOMAIN}/${SUBDOMAIN}.php?key=${DEPLOY_KEY}"
# --------------------
# Function: Main Menu
# --------------------
show_main_menu()
{
echo "-- Parameter Check --"
echo "# Subdomain"
echo "Subdomain: ${SUBDOMAIN}.${MAIN_DOMAIN}"
echo "# Backlog"
echo "ProjectName: ${BACKLOG_PROJECTNAME}"
echo "Git Name: ${GIT_NAME}"
echo "Git Branch: ${GIT_BRANCH}"
echo "Git SSH: ${GIT_SSH}"
echo "Git Web: ${GIT_WEB}"
echo "# Basic Auth"
echo "Username: ${BASICAUTH_USERNAME}"
echo "Password: ${BASICAUTH_PASSWORD}"
echo "# PHP Deployment"
echo "Deploy PHP: ${GIT_DEPLOY_URL}"
echo "Deploy Key: ${DEPLOY_KEY}"
echo "NPM Option: ${NPM_OTION}"
echo " -- -- -- -- -- -- -- -- -- -- --"
echo "[y]. Proceed?"
echo "[q]. Quit?"
}
# --------------------
# Function: Process Main Menu
# --------------------
do_main_menu()
{
show_main_menu
read -p "Enter your selection: (y/q): " yesno
case "$yesno" in [yY]*) ;; *) echo "Sorry, see you soon!" ; exit ;; esac
do_create
do_route53
do_tailwind
show_wiki
echo "---------------------------"
echo "--- Complete! ---"
echo "---------------------------"
exit 0
}
# --------------------
# Function: Create vhosts directory area, git clone, Nginx config change
# --------------------
do_create() {
# STEP 1: Make a directory in vhosts
echo "**NOW** Making vhost directory"
echo "${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}"
cd ${DIR_VHOST}
mkdir ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}
sudo chown -R ${DIR_OWNER} ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}
sudo chmod -R 775 ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}
# STEP 2: Clone git
echo "**NOW** Cloning git"
echo "sudo -u ${WEB_USER} git clone ${GIT_SSH} ./"
cd ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}/
sudo -u ${WEB_USER} git clone ${GIT_SSH} ./
sudo -u ${WEB_USER} git checkout ${GIT_BRANCH}
# STEP 3: Copy ${WEB_USER} config
echo "**NOW** Copying ${WEB_USER} config"
echo "${DIR_NGINX_CONF}$(date "+%Y%m%d")_vhost_${SUBDOMAIN}.${MAIN_DOMAIN}.conf"
cd ${DIR_NGINX_CONF}
sudo cp ${DIR_NGINX_CONF}00000000_vhost_sample.conf.template ${DIR_NGINX_CONF}$(date "+%Y%m%d")_vhost_${SUBDOMAIN}.${MAIN_DOMAIN}.conf
# STEP 4: Setting up Nginx Config
echo "**NOW** Setting up Nginx Config"
sudo sed -i "s/SUBDOMAIN.${MAIN_DOMAIN}/${SUBDOMAIN}.${MAIN_DOMAIN}/g" ${DIR_NGINX_CONF}$(date "+%Y%m%d")_vhost_${SUBDOMAIN}.${MAIN_DOMAIN}.conf
## Add /dist to web root folder for tailwind
if [ "${NPM_OTION}" = "tailwind" ]; then
echo "**NOW** Setting up Nginx Config for Tailwind"
DIR_WEBROOT_TAILWIND="${DIR_WEBROOT}/dist"
sudo sed -i "s/${DIR_WEBROOT}/${DIR_WEBROOT_TAILWIND}/g" ${DIR_NGINX_CONF}$(date "+%Y%m%d")_vhost_${SUBDOMAIN}.${MAIN_DOMAIN}.conf
DIR_WEBROOT="${DIR_WEBROOT_TAILWIND}"
fi
# STEP 5: Restarting Nginx
echo "**NOW** Restarting Nginx"
sudo nginx -t
sudo systemctl restart nginx
# STEP 6: Setting up Basic Auth
echo "**NOW** Setting up Basic Auth"
echo "Username: ${BASICAUTH_USERNAME}"
echo "Password: ${BASICAUTH_PASSWORD}"
sudo echo "${BASICAUTH_USERNAME}:$(openssl passwd -apr1 ${BASICAUTH_PASSWORD})" >> ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}/.htpasswd
# STEP 7: Copying auto deploy php from template
echo "**NOW** Copying deployment php file from template"
echo "${DIR_VHOST}${MAIN_DOMAIN}/${SUBDOMAIN}.php"
sudo -u ${WEB_USER} cp ${DIR_VHOST}${MAIN_DOMAIN}/deploy.php.sample ${DIR_VHOST}${MAIN_DOMAIN}/${SUBDOMAIN}.php
# STEP 8: Setting up auto deploy php
echo "**NOW** Setting up auto-deploy php"
sudo sed -i "s/EnterYourSecretKeyHere/${DEPLOY_KEY}/g" ${DIR_VHOST}${MAIN_DOMAIN}/${SUBDOMAIN}.php
sudo sed -i "s/SUBDOMAIN/${SUBDOMAIN}/g" ${DIR_VHOST}${MAIN_DOMAIN}/${SUBDOMAIN}.php
echo "Webhook URL:"
echo "${GIT_DEPLOY_WEBHOOK}"
# STEP 9: Committing the git deploy changes to Backlog
cd ${DIR_VHOST}${MAIN_DOMAIN}/
sudo -u ${WEB_USER} git add .
sudo -u ${WEB_USER} git commit -m "${SUBDOMAIN}.${MAIN_DOMAIN} added"
sudo -u ${WEB_USER} git push
}
# --------------------
# Function: Register subdomain to DNS zone via Route 53
# --------------------
do_route53() {
echo "**NOW** Checking if Route53 parameters are not empty"
if [ -n "${AWS_HOSTED_ZONE}" ] && [ -n "${AWS_EIP}" ]; then
echo "**NOW** Creating route53.json file"
ROUTE53_JSON=$(cat << EOS
{
"Comment": "CREATE/DELETE/UPSERT a record ",
"Changes": [{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "${SUBDOMAIN}.${MAIN_DOMAIN}",
"Type": "A",
"TTL": 300,
"ResourceRecords": [{ "Value": "${AWS_EIP}"}]
}}]
}
EOS
)
cd ${DIR_CURRENT}
echo ${ROUTE53_JSON} > route53.json
echo "**NOW** Applying Route53 Change"
aws route53 change-resource-record-sets --hosted-zone-id ${AWS_HOSTED_ZONE} --change-batch file://route53.json
else
echo "Skipping Route53 Registration"
fi
}
# --------------------
# Function: Install & Build Tailwind CSS
# --------------------
do_tailwind(){
echo "**NOW** Checking if NPM option is tailwind"
if [ "${NPM_OTION}" = "tailwind" ]; then
echo "**NOW** Creating post-merge file for Tailwind CSS"
POST_MERGE=$(cat << EOS
#!/bin/bash
cd ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}
echo 'npm-installing'
npm install -D
echo 'npm-building:'
npm run build
EOS
)
cd ${DIR_CURRENT}
echo ${POST_MERGE} > post-merge
echo "**NOW** Copying post-merge file to git hook"
sudo cp post-merge ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}/.git/hooks/
sudo chown ${DIR_OWNER} ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}/.git/hooks/post-merge
echo "**NOW** Execute initial npm build"
sudo -u ${WEB_USER} sh ${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}/.git/hooks/post-merge
else
echo "Skipping Tailwind"
fi
}
# --------------------
# Function: Create Markdown for Wiki
# --------------------
show_wiki() {
cat << EOS
==============================
Wiki Document Server/Coding
==============================
[toc]
# Basic Auth
| ID | Password
|:- | :- |
| ${BASICAUTH_USERNAME} | ${BASICAUTH_PASSWORD} |
https://${SUBDOMAIN}.${MAIN_DOMAIN}/
# Git Info
----|------
Git | ${GIT_WEB}
Branch | ${GIT_BRANCH}
Reset hard | Yes
NPM Option | ${NPM_OTION}
Deploy script | ${GIT_DEPLOY_URL}
* deploy script does not change branch, you must git checkout on the server directly
* サーバー上でブランチを変更したい場合は git checkout コマンドを直接サーバー上で実行すること
# Server Info
## Document Root
${DIR_VHOST}${SUBDOMAIN}.${MAIN_DOMAIN}
==============================
EOS
echo "# Webhook URL:"
echo "${GIT_DEPLOY_WEBHOOK}"
}
# --------------------
# Bootstrap
# --------------------
do_main_menu