This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
forked from LiferayCloud/stack-upgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade-workspace.sh
executable file
·476 lines (350 loc) · 14.9 KB
/
upgrade-workspace.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/bin/bash
shopt -s dotglob
readonly BLADE_VERSION=${BLADE_VERSION:-"3.9.2"}
readonly GRADLE_LCP_IMAGE_7_0=${GRADLE_LCP_IMAGE_7_0:-"liferay/dxp:7.0.10-sp13-202003272332"}
readonly GRADLE_LCP_IMAGE_7_1=${GRADLE_LCP_IMAGE_7_1:-"liferay/dxp:7.1.10-sp4-202004031130"}
readonly GRADLE_LCP_IMAGE_7_2=${GRADLE_LCP_IMAGE_7_2:-"liferay/dxp:7.2.10-sp2-202005120922"}
readonly LIFERAY_LCP_IMAGE_7_0=${LIFERAY_LCP_IMAGE_7_0:-"liferaycloud/liferay-dxp:7.0-4.0.0"}
readonly LIFERAY_LCP_IMAGE_7_1=${LIFERAY_LCP_IMAGE_7_1:-"liferaycloud/liferay-dxp:7.1-4.0.0"}
readonly LIFERAY_LCP_IMAGE_7_2=${LIFERAY_LCP_IMAGE_7_2:-"liferaycloud/liferay-dxp:7.2-4.0.0"}
readonly SEARCH_LCP_IMAGE_2=${SEARCH_LCP_IMAGE_2:-"liferaycloud/elasticsearch:2.4.6-4.0.0"}
readonly SEARCH_LCP_IMAGE_6=${SEARCH_LCP_IMAGE_6:-"liferaycloud/elasticsearch:6.8.6-4.0.0"}
readonly BACKUP_LCP_IMAGE=${BACKUP_LCP_IMAGE:-"liferaycloud/backup:4.0.0"}
readonly DATABASE_LCP_IMAGE=${DATABASE_LCP_IMAGE:-"liferaycloud/database:4.0.0"}
readonly CI_LCP_IMAGE=${CI_LCP_IMAGE:-"liferaycloud/jenkins:2.222.1-4.0.0"}
readonly WEBSERVER_LCP_IMAGE=${WEBSERVER_LCP_IMAGE:-"liferaycloud/nginx:1.16.1-4.0.0"}
if [[ "$OSTYPE" == "darwin"* ]]; then
readonly SED_ARGS='-i .wksbck'
else
readonly SED_ARGS='-i'
fi
readonly INFRASTRUCTURE=liferay.cloud
readonly API_URL=https://api.${INFRASTRUCTURE}
readonly LOGIN_URL=${API_URL}/login
readonly PROJECTS_URL=${API_URL}/projects
main() {
validate_program_installation
print_opening_instructions
prompt_for_database_secret_variables
prompt_for_liferay_version
prompt_for_environments
prompt_for_elasticsearch_plugins
create_database_secrets
checkout_upgrade_workspace_branch
upgrade_backup_service
upgrade_ci_service
upgrade_database_service
upgrade_search_service
upgrade_liferay_service
upgrade_webserver_service
move_custom_service_folders
cleanup_obsolete_files
}
checkout_upgrade_workspace_branch() {
if ! grep "upgrade-workspace" .gitignore &>/dev/null; then
printf '\nupgrade-workspace.sh' >>.gitignore
git add .gitignore && git commit -m 'Add upgrade script to .gitignore'
fi
git checkout -b upgrade-workspace
}
prompt_for_database_secret_variables() {
printf "\n"
read -p "Please enter your project id: " -r PROJECT_ID
echo 'Please login to DXP Cloud Console'
lcp login
LCP_CONFIG_FILE=$HOME/.lcp
if [[ -f "$LCP_CONFIG_FILE" ]]; then
echo "$LCP_CONFIG_FILE exists"
else
echo "$LCP_CONFIG_FILE does not exist!"
exit 1
fi
TOKEN=$(grep -A 2 "infrastructure=${INFRASTRUCTURE}" "$LCP_CONFIG_FILE" | awk -F "=" '/token/ {print $2}')
readonly PORTAL_ALL_PROPERTIES_LOCATION=lcp/liferay/config/common/portal-all.properties
DATABASE_PASSWORD=$(grep "jdbc.default.password" "${PORTAL_ALL_PROPERTIES_LOCATION}" | cut -d '=' -f 2)
[[ -z "${DATABASE_PASSWORD}" ]] &&
read -p "Could not find jdbc.default.password in ${PORTAL_ALL_PROPERTIES_LOCATION}. Please enter your database password: " -r DATABASE_PASSWORD
}
validate_program_installation() {
if ! git status &>/dev/null; then
echo >&2 "This script must be run from a git repository"
exit
fi
if ! java -version &>/dev/null; then
echo >&2 "This script requires java to be installed"
exit
fi
if ! curl --version &>/dev/null; then
CURL=false
fi
if ! wget --version &>/dev/null; then
WGET=false
fi
if [[ $CURL == false ]] && [[ $WGET == false ]]; then
echo >&2 "This script requires curl or wget to be installed"
exit
fi
if ! lcp version &>/dev/null; then
echo >&2 "This script requires lcp to be installed"
exit
fi
}
create_database_secrets() {
for env in "${ENVIRONMENTS[@]}"; do
[[ "$env" == common ]] && continue
local secrets
local env_id="${PROJECT_ID}-${env}"
if [ "$WGET" != false ]; then
secrets=$(
wget "${PROJECTS_URL}/${env_id}/secrets" \
--header="Authorization: Bearer ${TOKEN}" \
--header='content-type: application/x-www-form-urlencoded' \
--auth-no-challenge \
-O -
)
else
secrets=$(
curl "${PROJECTS_URL}/${env_id}/secrets" \
-X GET \
-H "Authorization: Bearer ${TOKEN}"
)
fi
create_secret "${env_id}" "${secrets}" 'lcp-secret-database-name' 'lportal'
create_secret "${env_id}" "${secrets}" 'lcp-secret-database-user' 'dxpcloud'
create_secret "${env_id}" "${secrets}" 'lcp-secret-database-password' "${DATABASE_PASSWORD}"
done
}
create_secret() {
local env_id="${1}"
local secrets="${2}"
local secret_name="${3}"
local secret_value="${4}"
if echo "${secrets}" | grep "${secret_name}" &>/dev/null; then
echo "The secret '${secret_name}' already exists, skipping secret creation"
return
fi
echo "creating secret for ${env_id} ${secret_name}=${secret_value}"
if [ "$WGET" != false ]; then
wget "${PROJECTS_URL}/${env_id}/secrets" \
--header="Authorization: Bearer ${TOKEN}" \
--header='content-type: application/x-www-form-urlencoded' \
--auth-no-challenge \
--post-data="name=${secret_name}&value=${secret_value}"
else
curl "${PROJECTS_URL}/${env_id}/secrets" \
-X POST \
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"name": "'"${secret_name}"'",
"value": "'"${secret_value}"'"
}'
fi
}
move_custom_service_folders() {
mv lcp/*/ .
git add --all && git commit -m 'Move custom service folders to root'
}
print_opening_instructions() {
printf "\n### DXP Cloud Project Workspace Upgrade ###\n\n"
printf "The script creates a commit on the current branch that adds itself to .gitignore.\n"
printf "Next, a new branch called 'upgrade-workspace' is checked out, and all the changes for each service are committed separately.\n"
printf "The workspace upgrade assumes a clean working branch, and that wget/curl, java, and lcp are installed.\n"
printf "After the upgrade has run, you can completely undo and rerun it with the following commands:\n\n"
printf "\tgit checkout <original-branch-name> && git reset --hard && git branch -D upgrade-workspace; ./upgrade-workspace.sh\n\n"
read -rs -p "Press enter to continue: "
}
prompt_for_liferay_version() {
printf "\n"
PS3='Please select the Liferay DXP version, which will determine the Liferay CLOUD image set in liferay/LCP.json and the Liferay image set in liferay/gradle.properties: '
options=("7.0" "7.1" "7.2")
select opt in "${options[@]}"; do
DXP_VERSION=$opt
case $opt in
7.0)
GRADLE_LCP_IMAGE=${GRADLE_LCP_IMAGE_7_0}
LIFERAY_LCP_IMAGE=${LIFERAY_LCP_IMAGE_7_0}
break
;;
7.1)
GRADLE_LCP_IMAGE=${GRADLE_LCP_IMAGE_7_1}
LIFERAY_LCP_IMAGE=${LIFERAY_LCP_IMAGE_7_1}
break
;;
7.2)
GRADLE_LCP_IMAGE=${GRADLE_LCP_IMAGE_7_2}
LIFERAY_LCP_IMAGE=${LIFERAY_LCP_IMAGE_7_2}
DXP_VERSION=$opt
break
;;
*) echo >&2 "Invalid selection; please select 1..3" ;;
esac
done
echo "Using Liferay DXP version $DXP_VERSION"
echo ""
echo "The image in liferay/LCP.json will be set to $LIFERAY_LCP_IMAGE"
echo "The image in liferay/gradle.properties will be set to $GRADLE_LCP_IMAGE"
}
prompt_for_environments() {
printf '\nPlease enter a comma-delimited list of the different environments in your project, apart from the "common" environment.'
printf '\nFor example, you can write "dev,prd". This script will only copy files from these environments and the common environment.'
printf '\nTaking the webserver service as an example, if you enter "dev", files will be copied from lcp/webserver/config/dev to webserver/configs/dev/conf.d.'
printf '\nHowever, files in lcp/webserver/config/anotherenv would be ignored and deleted.\n\n'
IFS=',' read -p 'Please enter a comma-delimited list of environments: ' -ra ENVIRONMENTS
ENVIRONMENTS=("common" "${ENVIRONMENTS[@]}")
printf "\nThis script will create an environment folder in each service for the following environments:\n"
for env in "${ENVIRONMENTS[@]}"; do
echo "$env"
done
}
prompt_for_elasticsearch_plugins() {
printf '\nPlease enter a comma-delimited list of elasticsearch plugins you would like to install, if any.'
printf '\nThis script will create an ENV var called LCP_SERVICE_SEARCH_ES_PLUGINS in search/LCP.json with the value that you set.'
printf '\nIf you want, you can easily do this later, and you can set LCP_SERVICE_SEARCH_ES_PLUGINS per environment as well.'
printf '\nThis script sets LCP_SERVICE_SEARCH_ES_PLUGINS globally for all environments.\n'
read -p 'Please enter a comma-delimited list of elasticsearch plugins: ' -r ES_PLUGINS
if [[ -n $ES_PLUGINS ]]; then
echo "Will set LCP_SERVICE_SEARCH_ES_PLUGINS=$ES_PLUGINS in LCP.json"
else
echo "No plugins will be set in LCP.json"
fi
}
replaceImage() {
echo "Setting image in $1/LCP.json to $2"
sed $SED_ARGS "s|\(\\w*\"image\": \).*\$|\1\"${2}\",|" "$1"/LCP.json
[[ -f "$1"/LCP.json.wksbck ]] && rm "$1"/LCP.json.wksbck
}
upgrade_backup_service() {
printf "\n### Upgrading backup service folder structure ###\n\n"
mkdir backup
mv lcp/backup/* backup/
replaceImage backup "${BACKUP_LCP_IMAGE}"
for env in "${ENVIRONMENTS[@]}"; do
mkdir -p backup/configs/"$env"/scripts
touch backup/configs/"$env"/scripts/.keep
mv backup/script/"$env"/* backup/configs/"$env"/scripts
done
rm -rf backup/script
git add --all && git commit -m 'Upgrade backup service folder structure'
}
upgrade_database_service() {
printf "\n ### Upgrading database service folder structure ###\n\n"
mkdir database
mv lcp/database/* database/
replaceImage database "${DATABASE_LCP_IMAGE}"
git add --all && git commit -m 'Upgrade database service folder structure'
}
upgrade_ci_service() {
printf "\n ### Upgrading ci service folder structure ###\n\n"
mkdir ci
mv lcp/ci/* ci/
replaceImage ci "${CI_LCP_IMAGE}"
echo 'Moving Jenkinsfile to ci dir and commenting it out so that default Jenkinsfile provided by the Jenkins service is used'
mv Jenkinsfile ci/#Jenkinsfile
touch 'ci/#Jenkinsfile-after-all' 'ci/#Jenkinsfile-before-all' 'ci/#Jenkinsfile-before-cloud-build' 'ci/#Jenkinsfile-before-cloud-deploy' 'ci/#Jenkinsfile-post-always'
git add --all && git commit -m 'Upgrade ci service folder structure'
}
upgrade_search_service() {
printf "\n### Upgrading search service folder structure ###\n\n"
mkdir search
mv lcp/search/* search/
SEARCH_LCP_IMAGE=${SEARCH_LCP_IMAGE_6}
if [ "$DXP_VERSION" = "7.0" ]; then
SEARCH_LCP_IMAGE=${SEARCH_LCP_IMAGE_2}
fi
replaceImage search "${SEARCH_LCP_IMAGE}"
for env in "${ENVIRONMENTS[@]}"; do
mkdir -p search/configs/"$env"/config
touch search/configs/"$env"/config/.keep
mkdir -p search/configs/"$env"/license
touch search/configs/"$env"/license/.keep
mkdir -p search/configs/"$env"/scripts
touch search/configs/"$env"/scripts/.keep
mv search/config/"$env"/* search/configs/"$env"/config
mv search/license/"$env"/* search/configs/"$env"/license
mv search/script/"$env"/* search/configs/"$env"/scripts
done
rm -rf search/config search/deploy search/license search/script
[[ -n "$ES_PLUGINS" ]] && sed $SED_ARGS "s/\(\\w*\"env\": {\)/\1\n \"LCP_SERVICE_SEARCH_ES_PLUGINS\": \"${ES_PLUGINS}\",/" search/LCP.json
[[ -f search/LCP.json.wksbck ]] && rm search/LCP.json.wksbck
git add --all && git commit -m 'Upgrade search service folder structure'
}
upgrade_liferay_service() {
printf "\n### Upgrading liferay service folder structure ###\n\n"
echo "Deleting obsolete files in git repo root related to the Liferay service..."
rm -rf \
build.gradle \
gradlew \
gradlew.bat \
settings.gradle \
gradle \
.gradle
if [ "$WGET" != false ]; then
wget -O blade.jar https://repo1.maven.org/maven2/com/liferay/blade/com.liferay.blade.cli/${BLADE_VERSION}/com.liferay.blade.cli-${BLADE_VERSION}.jar
else
curl --output blade.jar https://repo1.maven.org/maven2/com/liferay/blade/com.liferay.blade.cli/${BLADE_VERSION}/com.liferay.blade.cli-${BLADE_VERSION}.jar
fi
rm -rf liferay/*
java -jar blade.jar init -v ${DXP_VERSION} liferay
rm blade.jar
echo 'Moving gradle.properties to liferay dir and commenting it out'
mv gradle.properties liferay/#gradle.properties
mv lcp/liferay/* liferay/
replaceImage liferay "${LIFERAY_LCP_IMAGE}"
mv modules liferay/
mv themes liferay/
mv wars liferay/
for env in "${ENVIRONMENTS[@]}"; do
mkdir -p liferay/configs/"$env"/osgi/configs
touch liferay/configs/"$env"/osgi/configs/.keep
mkdir -p liferay/configs/"$env"/deploy
touch liferay/configs/"$env"/deploy/.keep
mkdir -p liferay/configs/"$env"/scripts
touch liferay/configs/"$env"/scripts/.keep
mkdir -p liferay/configs/"$env"/patching
touch liferay/configs/"$env"/patching/.keep
mv liferay/config/"$env"/portal-*.properties liferay/configs/"$env"
[[ "$env" != common ]] && echo "include-and-override=portal-all.properties
include-and-override=portal-env.properties" >liferay/configs/"$env"/portal-ext.properties
mv liferay/config/"$env"/*.config liferay/configs/"$env"/osgi/configs
mv liferay/config/"$env"/*.cfg liferay/configs/"$env"/osgi/configs
mv liferay/deploy/"$env"/* liferay/configs/"$env"/deploy
mv liferay/script/"$env"/* liferay/configs/"$env"/scripts
mv liferay/hotfix/"$env"/* liferay/configs/"$env"/patching
mv liferay/license/"$env"/* liferay/configs/"$env"/deploy
done
rm -rf liferay/config liferay/deploy liferay/script liferay/hotfix liferay/license
rm -rf liferay/configs/prod
sed $SED_ARGS "s|\(liferay\.workspace\.docker\.image\.liferay=\).*\$|\1${GRADLE_LCP_IMAGE}|" liferay/gradle.properties
sed $SED_ARGS "s|\#liferay\.workspace\.docker\.image\.liferay=|liferay\.workspace\.docker\.image\.liferay=|" liferay/gradle.properties
[[ -f liferay/gradle.properties.wksbck ]] && rm liferay/gradle.properties.wksbck
git add --all && git commit -m 'Upgrade liferay service folder structure'
}
upgrade_webserver_service() {
printf "\n### Upgrading webserver service folder structure ###\n\n"
mkdir webserver
mv lcp/webserver/* webserver/
replaceImage webserver "${WEBSERVER_LCP_IMAGE}"
for env in "${ENVIRONMENTS[@]}"; do
mkdir -p webserver/configs/"$env"/conf.d
touch webserver/configs/"$env"/conf.d/.keep
mkdir -p webserver/configs/"$env"/public
touch webserver/configs/"$env"/public/.keep
mkdir -p webserver/configs/"$env"/scripts
touch webserver/configs/"$env"/scripts/.keep
mv webserver/config/"$env"/* webserver/configs/"$env"/conf.d
mv webserver/deploy/"$env"/* webserver/configs/"$env"/public
mv webserver/script/"$env"/* webserver/configs/"$env"/scripts
done
rm -rf webserver/config webserver/deploy webserver/script
git add --all && git commit -m 'Upgrade webserver service folder structure'
}
cleanup_obsolete_files() {
printf '\n### Deleting obsolete files in the root directory ### \n\n'
rm -rf \
README-dxpcloud.md \
README.md
rm -rf lcp
git add --all && git commit -m 'Delete obsolete files in the root directory'
}
main