forked from liferay/liferay-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_liferay_common.sh
296 lines (224 loc) · 5.76 KB
/
_liferay_common.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
#!/bin/bash
#
# Do NOT edit this file unless you are editing this file in the root directory
# of the the liferay-docker repository. Edit that file, and then fork it to the
# repository where it is used.
#
function lc_background_run {
if [ -n "${LIFERAY_COMMON_DEBUG_ENABLED}" ]
then
lc_time_run "${@}"
else
lc_time_run "${@}" &
local pid=${!}
LIFERAY_COMMON_BACKGROUND_PIDS["${pid}"]="${@}"
fi
}
function lc_cd {
if [ -d "${1}" ]
then
cd "${1}" || exit "${LIFERAY_COMMON_EXIT_CODE_CD}"
else
lc_log ERROR "${1} directory does not exist."
exit "${LIFERAY_COMMON_EXIT_CODE_CD}"
fi
}
function lc_check_utils {
local exit_code=${LIFERAY_COMMON_EXIT_CODE_OK}
for util in "${@}"
do
if (! command -v "${util}" &>/dev/null)
then
lc_log ERROR "The utility ${util} is not installed."
exit_code="${LIFERAY_COMMON_EXIT_CODE_BAD}"
fi
done
return "${exit_code}"
}
function lc_date {
if [ -z ${1+x} ] || [ -z ${2+x} ]
then
if [ "$(uname)" == "Darwin" ]
then
/bin/date
elif [ -e /bin/date ]
then
/bin/date --iso-8601=seconds
else
/usr/bin/date --iso-8601=seconds
fi
else
if [ "$(uname)" == "Darwin" ]
then
/bin/date -jf "%a %b %e %H:%M:%S %Z %Y" "${1}" "${2}"
elif [ -e /bin/date ]
then
/bin/date -d "${1}" "${2}"
else
/usr/bin/date -d "${1}" "${2}"
fi
fi
}
function lc_docker_compose {
if [ -n "${LIFERAY_COMMON_DOCKER_COMPOSE}" ]
then
echo "${LIFERAY_COMMON_DOCKER_COMPOSE}"
return
fi
local LIFERAY_COMMON_DOCKER_COMPOSE="docker compose"
if (command -v docker-compose &>/dev/null)
then
LIFERAY_COMMON_DOCKER_COMPOSE="docker-compose"
fi
echo "${LIFERAY_COMMON_DOCKER_COMPOSE}"
}
function lc_download {
local file_url=${1}
if [ -z "${file_url}" ]
then
lc_log ERROR "File URL is not set."
return "${LIFERAY_COMMON_EXIT_CODE_BAD}"
fi
local file_name=${2}
if [ -z "${file_name}" ]
then
file_name=${file_url##*/}
fi
if [ -e "${file_name}" ]
then
lc_log DEBUG "Skipping the download of ${file_url} because it already exists."
return
fi
local cache_file="${LIFERAY_COMMON_DOWNLOAD_CACHE_DIR}/${file_url##*://}"
if [ -e "${cache_file}" ]
then
if [ -n "${LIFERAY_COMMON_DOWNLOAD_SKIP_CACHE}" ]
then
lc_log DEBUG "Deleting file from cache: ${cache_file}."
rm -f "${cache_file}"
else
lc_log DEBUG "Copying file from cache: ${cache_file}."
cp "${cache_file}" "${file_name}"
return
fi
fi
mkdir -p $(dirname "${cache_file}")
lc_log DEBUG "Downloading ${file_url}."
local current_date=$(lc_date)
local timestamp=$(lc_date "${current_date}" "+%Y%m%d%H%M%S")
if (! curl "${file_url}" --fail --output "${cache_file}.temp${timestamp}" --silent)
then
lc_log ERROR "Unable to download ${file_url}."
return "${LIFERAY_COMMON_EXIT_CODE_BAD}"
else
mv "${cache_file}.temp${timestamp}" "${cache_file}"
cp "${cache_file}" "${file_name}"
fi
}
function lc_get_property {
file=${1}
property_key=${2}
if [ "${file##*.}" == "bnd" ]
then
local property_value=$(grep -F "${property_key}: " "${file}")
echo "${property_value##*: }"
else
local property_value=$(sed -r "s/\\\r?\n[ \t]*//g" -z < "${file}" | grep -F "${property_key}=")
echo "${property_value##*=}"
fi
}
function lc_echo_time {
local seconds=${1}
printf "%02dh:%02dm:%02ds" $((seconds / 3600)) $((seconds % 3600 / 60)) $((seconds % 60))
}
function lc_log {
local level=${1}
local message=${2}
if [ "${level}" != "DEBUG" ] || [ "${LIFERAY_COMMON_LOG_LEVEL}" == "DEBUG" ]
then
echo "$(lc_date) [${level}] ${message}"
fi
}
function lc_next_step {
local step=$(cat "${LIFERAY_COMMON_STEP_FILE}")
step=$((step + 1))
echo ${step} > "${LIFERAY_COMMON_STEP_FILE}"
printf "%02d" ${step}
}
function lc_time_run {
local run_id=$(echo "${@}" | tr " /" "_")
local start_time=$(date +%s)
if [ -n "${LIFERAY_COMMON_LOG_DIR}" ]
then
mkdir -p "${LIFERAY_COMMON_LOG_DIR}"
local log_file="${LIFERAY_COMMON_LOG_DIR}/log_${LIFERAY_COMMON_START_TIME}_step_$(lc_next_step)_${run_id}.txt"
fi
echo "$(lc_date) > ${*}"
if [ -z "${LIFERAY_COMMON_DEBUG_ENABLED}" ] && [ -n "${LIFERAY_COMMON_LOG_DIR}" ]
then
"${@}" &> "${log_file}"
else
"${@}"
fi
local exit_code=${?}
local end_time=$(date +%s)
if [ "${exit_code}" == "${LIFERAY_COMMON_EXIT_CODE_SKIPPED}" ]
then
echo -e "$(lc_date) < ${*}: \e[1;34mSkip\e[0m"
else
local seconds=$((end_time - start_time))
if [ "${exit_code}" -gt 0 ]
then
echo -e "$(lc_date) ! ${*} exited with \e[1;31merror\e[0m in $(lc_echo_time ${seconds}) (exit code: ${exit_code})."
if [ -z "${LIFERAY_COMMON_DEBUG_ENABLED}" ] && [ -n "${LIFERAY_COMMON_LOG_DIR}" ]
then
echo "Full log file is at ${log_file}. Printing the last 100 lines:"
tail -n 100 "${log_file}"
fi
if (declare -F lc_time_run_error &>/dev/null)
then
LC_TIME_RUN_ERROR_EXIT_CODE="${exit_code}"
LC_TIME_RUN_ERROR_FUNCTION="${@}"
LC_TIME_RUN_ERROR_LOG_FILE="${log_file}"
lc_time_run_error
fi
exit ${exit_code}
else
echo -e "$(lc_date) < ${*}: \e[1;32mSuccess\e[0m in $(lc_echo_time ${seconds})"
fi
fi
}
function lc_wait {
for pid in ${!LIFERAY_COMMON_BACKGROUND_PIDS[@]}
do
wait "${pid}"
local exit_code=$?
if [ "${exit_code}" -ne 0 ]
then
exit "${exit_code}"
fi
done
LIFERAY_COMMON_BACKGROUND_PIDS=()
}
function _lc_init {
LIFERAY_COMMON_START_TIME=$(date +%s)
LIFERAY_COMMON_STEP_FILE=$(mktemp)
declare -A LIFERAY_COMMON_BACKGROUND_PIDS
if [ -z "${LIFERAY_COMMON_DOWNLOAD_CACHE_DIR}" ]
then
LIFERAY_COMMON_DOWNLOAD_CACHE_DIR=${HOME}/.liferay-common-cache
fi
LIFERAY_COMMON_EXIT_CODE_BAD=1
LIFERAY_COMMON_EXIT_CODE_CD=3
LIFERAY_COMMON_EXIT_CODE_HELP=2
LIFERAY_COMMON_EXIT_CODE_OK=0
LIFERAY_COMMON_EXIT_CODE_SKIPPED=4
if (locale -a | grep -q en_US.utf8)
then
export LC_ALL=en_US.utf8
else
export LC_ALL=C.utf8
fi
export TZ=UTC
}
_lc_init