generated from wabarc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup
executable file
·229 lines (209 loc) · 5.02 KB
/
setup
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
#!/bin/bash
#
# Setup Heroku app
# vim: set et ts=2 sw=2
set -eu
dest='/tmp/on-heroku'
repo='https://github.com/wabarc/on-heroku.git'
ver='v12.18.3'
node="node-${ver}-linux-x64"
loc="/tmp/${node}.tar.xz"
red() {
red='\e[91m'
none='\e[0m'
echo -e "${red}$*${none}"
}
err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}
prepare() {
arch="$(arch)"
if [ -z "${arch##X86_64}" ]; then
err "Unsupport arch..."
exit 1
fi
i='0'
os="$(cat </etc/os-release | grep 'NAME=' | sed 's/.*\"\(.*\)\".*/\1/g')"
if [ -z "${os##*Debian*}" ] || [ -z "${os##*Ubuntu*}" ]; then
i='1'
apt-get update -y && apt-get install -y --no-install-recommends git wget xz-utils openssh-client ca-certificates
elif [ -z "${os##*Fedora*}" ] || [ -z "${os##*CentOS*}" ]; then
i='1'
yum update -y && yum install -y xz git wget openssh-clients ca-certificates
elif [ -z "${os##*Alpine*}" ]; then
apk update && apk add git wget openssh-client ca-certificates nodejs
i='1'
else
err "Unsupport OS"
exit 1
fi
if [ "${i}" = "1" ]; then
echo "Downloading the Node.js pre-built installer"
wget "https://nodejs.org/dist/v12.18.3/${node}.tar.xz" -O "${loc}"
if [ -f "${loc}" ]; then
tar -xJf "${loc}" -C /tmp
PATH="${PATH}:/tmp/${node}/bin"
else
err "Download Node.js failure..."
exit 1
fi
if [ -z "$(command -v heroku)" ]; then
npm i -g heroku
fi
else
err "Requirement missing..."
exit 1
fi
}
login() {
heroku_user="$(heroku auth:whoami || true)"
if [ -z "${heroku_user}" ]; then
heroku login -i
else
echo "Logged in as ${heroku_user}"
fi
}
add_credential() {
if [ ! -f ~/.ssh/id_rsa.pub ]; then
ssh-keygen -b 4096 -f ~/.ssh/id_rsa -N ""
fi
pubkey="$(cat ~/.ssh/id_rsa.pub)"
heroku_keys="$(heroku keys -l)"
if [ -n "${heroku_keys}" ] && [ -z "${heroku_keys##*$pubkey*}" ]; then
echo "Heroku has public key. $(red "skip")"
rkeys=0
else
echo "Uploading public key to Heroku..."
heroku keys:add
rkeys=1
fi
}
clone_wabarc_repo() {
if [ -d "${dest}" ]; then
s="$(cat </dev/random | tr -dc "[:lower:]" | head -c 4)"
echo "Moving ${dest} to ${dest}-bak-${s} ..."
mv "${dest}" "${dest}-bak-${s}"
fi
git clone "${repo}" "${dest}"
cd "${dest}" || exit
}
setup_heroku_repo() {
while [ -z "${scope:+x}" ] || [ -z "$(echo "${scope}" | grep -w "\bcreate\|add\b")" ]; do
read -rp "Create or add a heroku app (type $(red "create") or $(red "add")): " scope
done
# If add exists the Heroku app, it requires to type the app name.
if [ -z "${scope##*add*}" ]; then
while [ -z "${appname:+x}" ]; do
read -rp "Please type a heroku app name: " appname
done
else
read -rp "Please type a heroku app name or create by heroku: " appname
fi
if [ -z "${appname:+x}" ] && [ -z "${scope##*create*}" ]; then
echo "Heroku app name: $(red 'random name by heroku')"
else
echo "Heroku app name: $(red "${appname}")"
fi
while [ -z "${confirm:+x}" ] || [ -z "$(echo "${confirm}" | grep -w "\by\|n\|yes\|no\b")" ]; do
read -rp "Continue Deploy? (y/n): " confirm
done
if [ -z "$(echo "${confirm}" | grep -w "\by\|yes\b")" ]; then
clean
exit 1
fi
if [ -z "${scope##*create*}" ]; then
heroku create --ssh-git "${appname}"
elif [ -z "${scope##*add*}" ]; then
heroku git:remote --ssh-git -a "${appname}"
else
err "Unknow option"
clean
exit 1
fi
}
setup_heroku_stack() {
heroku stack:set container || true
}
setup_heroku_conf() {
read -rp "Please type run wayback app config vars: " vars
if [ -n "${vars}" ]; then
IFS="," read -ra fields <<<"${vars}"
for var in "${fields[@]}"; do
heroku config:set "${var}"
done
fi
}
deploy() {
while [ -z "${d:+x}" ] || [ -z "$(echo "${d}" | grep -w "\by\|n\|yes\|no\b")" ]; do
read -rp "Continue Deploy? (y/n): " d
done
if [ -n "${d##*y*}" ] || [ -n "${d##*yes*}" ]; then
git config --local user.name 'nobody'
git config --local user.email '[email protected]'
git commit --allow-empty -m "Trigger Build"
git push heroku main -f
else
echo "Deploy Heroku app: $(red "skip")"
fi
}
start_heroku_dynos() {
heroku ps:scale web=1 || true
sleep 3
heroku ps || true
}
clean() {
echo "Complete deploying, cleaning..."
if [ "${rkeys}" = "1" ]; then
heroku keys:remove "$(whoami)@$(hostname)"
fi
npm un -g heroku
rm -rf "${dest}"
rm -rf "/tmp/${node}*"
red "done"
}
main() {
target="${1:-}"
case "${target}" in
"prepare")
prepare
;;
"login")
login
;;
"credential")
add_credential
;;
"repo" | "clone")
clone_wabarc_repo
;;
"stack")
setup_heroku_stack
;;
"conf" | "config")
setup_heroku_conf
;;
"deploy")
deploy
;;
"start" | "scale")
start_heroku_dynos
;;
"clean")
clean
;;
*)
# Perform setup
prepare
login
add_credential
clone_wabarc_repo
setup_heroku_repo
setup_heroku_stack
setup_heroku_conf
deploy
start_heroku_dynos
clean
;;
esac
}
main "$@"