This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
preview_deploy
executable file
·235 lines (198 loc) · 6.58 KB
/
preview_deploy
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
#!/usr/bin/env bash
# Config
if [ -z ${DOKKU_CMD+x} ] || [ -z ${DOKKU_HOSTNAME+x} ] || [ -z ${DOMAIN+x} ] || [ -z ${PHX_SECRET+x} ] || [ -z ${EMAIL_ADDR+x} ] || [ -z ${SMTP_RELAY+x} ] || [ -z ${SMTP_PORT+x} ] || [ -z ${SMTP_USERNAME+x} ] || [ -z ${SMTP_PASSWORD+x} ]; then
if [ ! -f ".preview_env" ]; then
cat << HINT
You need to provide the following environment variables:
- DOKKU_CMD The Dokku (remote) command, e.g. ssh dokku-host
- DOKKU_HOSTNAME The Dokku hostname, e.g. dokku-host
- DOMAIN The domain, e.g. dev.host (will become PREVIEW_NAME.dev.host)
- PHX_SECRET A Phoenix secret, best generate via `./dev mix phx.gen.secret`
- EMAIL_ADDR The email address for Let's Encrypt
- KAFKA_HOSTS The Kafka hosts (f.e. localhost:9092)
- SMTP_RELAY The SMTP server host
- SMTP_PORT The SMTP server port
- SMTP_USERNAME The SMTP user
- SMTP_PASSWORD The SMTP password
You can set the following variables if desired:
- SUPER_USER_EMAIL The email address of the super user
- SUPER_USER_PASSWORD The password of the super user
You can override the following variables:
- APP_PREFIX
- PROJECTORS
For convenience you can copy the template and replace the variable values.
cp preview_env.dist .preview_env
HINT
exit 1
fi
source .preview_env
fi
# Optional
APP_PREFIX=${APP_PREFIX:="dbo"}
PROJECTORS=${PROJECTORS:="local"}
DATABASES=("projection:DATABASE_URL" "event_store:EVENT_STORE_DATABASE_URL")
# Mandatory
DOKKU_CMD=${DOKKU_CMD:="ssh your-host"}
DOKKU_HOSTNAME=${DOKKU_HOSTNAME:="your-host"}
APP_CONFIG=("HOSTNAME:###app_name###.${DOMAIN}" "SECRET_KEY_BASE:${PHX_SECRET}" "PROJECTORS:${PROJECTORS}" "KAFKA_HOSTS:${KAFKA_HOSTS}" "SMTP_RELAY:${SMTP_RELAY}" "SMTP_PORT:${SMTP_PORT}" "SMTP_USERNAME:${SMTP_USERNAME}" "SMTP_PASSWORD:${SMTP_PASSWORD}")
EMAIL_ADDR=${EMAIL_ADDR:="[email protected]"}
# Functions
function get_deployment_name {
local prefix=${1}
local custom_branch=${2}
local branch=${custom_branch:=$(git rev-parse --abbrev-ref HEAD)}
local name=$(echo ${branch} | sed 's/[^[:alnum:]]/-/g' | tr '[:upper:]' '[:lower:]')
echo "${prefix}-${name}"
}
function list_apps {
${DOKKU_CMD} apps:list
}
function ensure_app {
local app_name=${1}
does_app_exist ${app_name}
exists=$?
if [ "${exists}" -eq 0 ]; then
echo "Deployment ${app_name} already created."
else
echo "Deployment ${app_name} will be created."
create_app ${app_name}
fi
}
function does_app_exist {
${DOKKU_CMD} apps:exists ${1} >/dev/null 2>&1
}
function create_app {
${DOKKU_CMD} apps:create ${1}
}
function set_config {
local app_name=${1}
local key=${2}
local value=${3}
${DOKKU_CMD} config:set --no-restart ${app_name} ${key}=${value} >/dev/null 2>&1
}
function ensure_database {
local app_name=${1}
local db_name=${2}
local db_alias=${3}
does_database_exist ${db_name}
exists=$?
if [ "${exists}" -eq 0 ]; then
echo "Database ${db_name} already created."
else
echo "Database ${db_name} will be created."
create_database ${app_name} ${db_name} ${db_alias}
fi
}
function does_database_exist {
${DOKKU_CMD} postgres:exists ${1} >/dev/null 2>&1
}
function create_database {
local app_name=${1}
local db_name=${2}
local db_alias=${3}
echo "Creating database ${db_name} for ${app_name} aliased via ${db_alias}"
${DOKKU_CMD} postgres:create ${db_name} >/dev/null 2>&1
${DOKKU_CMD} postgres:link ${db_name} ${app_name} -a ${db_alias} >/dev/null 2>&1
}
function setup_git {
local app_name=${app_name}
git remote | grep ${app_name}
local exists=$?
if [ "${exists}" -eq 0 ]; then
echo "Remote ${app_name} already added."
else
echo "Remote ${app_name} will be added."
git remote add ${app_name} ${DOKKU_HOSTNAME}:${app_name}
fi
}
function deploy_application {
local app_name=${1}
local branch=${2}
git push ${app_name} ${branch}:master
if [ "$?" -gt 0 ]; then
exit 1
fi
}
function enable_ssl {
local app_name=${1}
local email_address=${2}
${DOKKU_CMD} config:set --no-restart ${app_name} DOKKU_LETSENCRYPT_EMAIL=${email_address} >/dev/null 2>&1
${DOKKU_CMD} letsencrypt ${app_name}
}
function setup_superuser {
local app_name=${1}
if [ -z "${SUPER_USER_EMAIL}" ] || [ -z "${SUPER_USER_PASSWORD}" ]; then
echo "No super user is being set"
return
fi
set_config ${app_name} SUPER_USER_EMAIL ${SUPER_USER_EMAIL}
set_config ${app_name} SUPER_USER_PASSWORD ${SUPER_USER_PASSWORD}
echo "Super user config set"
}
function deploy {
local short_sha=$(git rev-parse --short HEAD)
local branch_name=$(git rev-parse --abbrev-ref HEAD)
local app_name=$(get_deployment_name ${APP_PREFIX})
echo "Running preview deployment named ${app_name} on commit ${short_sha}"
list_apps
ensure_app ${app_name}
for config in "${APP_CONFIG[@]}"; do
local key="${config%%:*}"
local value="${config##*:}"
case ${key} in
"HOSTNAME")
value=$(echo ${value} | sed -e "s/###app_name###/${app_name}/")
;;
esac
set_config ${app_name} ${key} ${value}
done
for database in "${DATABASES[@]}"; do
local db_prefix="${database%%:*}"
local db_name=$(get_deployment_name "${APP_PREFIX}-${db_prefix}")
local db_alias="${database##*:}"
ensure_database ${app_name} ${db_name} ${db_alias}
done
setup_superuser ${app_name}
setup_git ${app_name}
deploy_application ${app_name} ${branch_name}
enable_ssl ${app_name} ${EMAIL_ADDR}
cat << HELP
*******************************************************************************
*
* Deployment finished.
*
* The backoffice is reachable via https://${app_name}.${DOMAIN}
* Please do not forget to cleanup when done via ${0} --cleanup
*
*******************************************************************************
HELP
}
function undeploy {
local branch=${1}
local app_name=$(get_deployment_name ${APP_PREFIX} ${branch})
echo "Removing app ${app_name}"
${DOKKU_CMD} apps:destroy ${app_name} --force
for database in "${DATABASES[@]}"; do
local db_prefix="${database%%:*}"
local db_name=$(get_deployment_name "${APP_PREFIX}-${db_prefix}" ${branch})
echo "Destroying database ${db_name}"
${DOKKU_CMD} postgres:destroy ${db_name} --force
done
echo "Removing remote from Git"
git remote remove ${app_name}
${DOKKU_CMD} apps:list
${DOKKU_CMD} postgres:list
echo "Deployment '${app_name}' has been successfully removed."
}
function main {
local cleanup=${1}
if [ "${cleanup}" == "--cleanup" ]; then
local branch=${2}
echo "Cleaning up preview deployment"
undeploy ${branch}
else
echo "Deploying preview"
deploy
fi
}
main $@