-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetVersionedEnv.sh
executable file
·284 lines (253 loc) · 8.82 KB
/
setVersionedEnv.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
#!/usr/bin/env bash
#
# File: https://github.com/cloud-helpers/k8s-job-wrappers/tree/main/setVersionedEnv.sh
#
# Utility supporting executing tasks from within Kubernetes (k8s) pods
#
# Functions supporting the preparation of a versioned environment, such as
# configuration directories and files, as well as output directories,
# for instance for data transformation steps. The whole idea is to support
# the full reproducibility/observability of data transformation processes
#
# Dependencies:
# * setDistAndArch.sh (https://github.com/cloud-helpers/k8s-job-wrappers/tree/main/setDistAndArch.sh)
# * setGnuTools.sh (https://github.com/cloud-helpers/k8s-job-wrappers/tree/main/setGnuTools.sh)
# * setLogFunc.sh (https://github.com/cloud-helpers/k8s-job-wrappers/tree/main/setLogFunc.sh)
# * setCloudStorage.sh (https://github.com/cloud-helpers/k8s-job-wrappers/tree/main/setCloudStorage.sh)
#
#
THIS_KJW_SCRIPT_URL="https://github.com/cloud-helpers/k8s-job-wrappers/tree/main/setVersionedEnv.sh"
KJW_FUNC="default"
# Derive where KJW has been installed
# Reference: https://stackoverflow.com/a/246128/798053
KJW_CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd)"
# Set up environment variables for the Linux distribution, host name
# and architecture
source ${KJW_CURRENT_DIR}/setDistAndArch.sh
retval=$?
if [ "${retval}" != 0 ]
then
echo "Call to setDistAndArch.sh failed. Returned code: ${retval}" > /dev/stderr
return ${retval}
fi
# Set up envrionment variables for command-line tools differing on MacOS
source ${KJW_CURRENT_DIR}/setGnuTools.sh
retval=$?
if [ "${retval}" != 0 ]
then
echo "Call to setGnuTools.sh failed. Returned code: ${retval}" > /dev/stderr
return ${retval}
fi
# Set up log functions
source ${KJW_CURRENT_DIR}/setLogFunc.sh
retval=$?
if [ "${retval}" != 0 ]
then
echo "Call to setLogFunc.sh failed. Returned code: ${retval}" > /dev/stderr
return ${retval}
fi
# Set up cloud storage related functions
source ${KJW_CURRENT_DIR}/setCloudStorage.sh
retval=$?
if [ "${retval}" != 0 ]
then
echo "Call to setCloudStorage.sh failed. Returned code: ${retval}" > /dev/stderr
return ${retval}
fi
# Export a few variables with the various time-stamp components
setTSVar() {
local FUNC_CUR="${KJW_FUNC}"
KJW_FUNC="setTSVar"
log "Beginning of function"
# Time-stamp, used to derive the folder hierarchy on S3, to store both the
# test reports as well as the log files
NOW_TS="$(${DATE_TOOL} "+%Y-%m-%d-%H-%M-%S" --utc)"
# Derive the time-based folder structure
## Up to the day
TS_YEAR="$(echo "${NOW_TS}"|cut -d'-' -f1)"
TS_MONTH="$(echo "${NOW_TS}"|cut -d'-' -f2)"
TS_DAY="$(echo "${NOW_TS}"|cut -d'-' -f3)"
TS_UP2DAY_DIR="${TS_YEAR}/${TS_YEAR}-${TS_MONTH}/${TS_YEAR}-${TS_MONTH}-${TS_DAY}"
STS_DIR="${TS_UP2DAY_DIR}/${NOW_TS}"
## Up to the hour
TS_HOUR="$(echo "${NOW_TS}"|cut -d'-' -f4)"
TS_MINUTE="$(echo "${NOW_TS}"|cut -d'-' -f5)"
TS_UP2HR_DIR="${TS_UP2DAY_DIR}/${TS_YEAR}-${TS_MONTH}-${TS_DAY}-${TS_HOUR}"
TS_DIR="${TS_UP2HR_DIR}/${NOW_TS}"
# Metadata time-stamped folder
S3_TSMTD_URL="${S3_MTD_URL}/${STS_DIR}"
# Log time-stamped folder
S3_TSLOG_URL="${S3_LOG_URL}/${TS_DIR}"
#
export S3_URL="s3://${S3_BUCKET}"
S3_MTD_BROWSABLE_URL="$(translateS3Folder ${S3_MTD_URL})"
S3_TSMTD_BROWSABLE_URL="$(translateS3Folder ${S3_TSMTD_URL})"
S3_LOG_BROWSABLE_URL="$(translateS3Folder ${S3_LOG_URL})"
S3_TSLOG_BROWSABLE_URL="$(translateS3Folder ${S3_TSLOG_URL})"
DATA_S3_BASE_BROWSABLE_URL="$(translateS3Folder ${DATA_S3_BASE_URL})"
APP_DATA_S3_BROWSABLE_URL="$(translateS3Filepath ${APP_DATA_S3_URL})"
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
}
# Create the versioned tree structure on the local file-system
setTSDirs() {
local FUNC_CUR="${KJW_FUNC}"
KJW_FUNC="setTSDirs"
log "Beginning of function"
if [ -z "${KJW_DATA_DIR}" ]
then
logMulti "Error - The KJW_DATA_DIR environment variable should be set, but is not." \
". Current script: ${THIS_KJW_SCRIPT_URL}" \
". That script is usually launched from within a virtual machine."
#
log "End of function"
return 1
fi
if [ ! -d "${KJW_DATA_DIR}" ]
then
logMulti "Error - The ${KJW_DATA_DIR} local data directory should exist and accessible, but does not." \
". Current script: ${THIS_KJW_SCRIPT_URL}" \
". That script is usually launched from within a virtual machine."
#
log "End of function"
return 1
fi
# Meta-data directory
export KJW_MTD_DIR="${KJW_DATA_DIR}/metadata"
#mkdir -p ${KJW_MTD_DIR}
# Metadata file
export KJW_MTD_FILE="${KJW_MTD_DIR}/kjw-${S3_APP_DIR}.csv"
#rm -f ${KJW_MTD_FILE}
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
}
reportTSEnv() {
local FUNC_CUR="${KJW_FUNC}"
KJW_FUNC="setTSDirs"
log "Beginning of function"
# Reporting
log "File: ${SCRIPT_GIT_URL}"
logMulti "Versioned env" \
"=============" \
"* NOW_TS: ${NOW_TS}" \
"* STS_DIR: ${STS_DIR}" \
"* TS_DIR: ${TS_DIR}" \
"." \
"S3 bucket" \
"=========" \
"* KJW_S3_BUCKET: ${KJW_S3_BUCKET}" \
"* KJW_S3_URL: ${KJW_S3_URL}" \
"* S3_APP_DIR: ${S3_APP_DIR}" \
"* S3_MTD_URL: ${S3_MTD_URL}" \
" => ${S3_MTD_BROWSABLE_URL}" \
"* S3_TSMTD_URL: ${S3_TSMTD_URL}" \
" => ${S3_TSMTD_BROWSABLE_URL}" \
"* S3_LOG_URL: ${S3_LOG_URL}" \
" => ${S3_LOG_BROWSABLE_URL}" \
"* S3_TSLOG_URL: ${S3_TSLOG_URL}" \
" => ${S3_TSLOG_BROWSABLE_URL}" \
"* S3_APP_DATA_DIR: ${S3_APP_DATA_DIR}" \
"* DATA_S3_BASE_URL: ${DATA_S3_BASE_URL}" \
" => ${DATA_S3_BASE_BROWSABLE_URL}" \
"* APP_DATA_S3_URL: ${CARRIERS_DATA_S3_URL}" \
" => ${APP_DATA_S3_BROWSABLE_URL}" \
"."
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
}
# Create a metadata file and upload it on to AWS S3
updateMetadata() {
local FUNC_CUR="${KJW_FUNC}"
KJW_FUNC="updateMetadata"
log "Beginning of function"
# Sanity check
if [ -z "${KJW_MTD_DIR}" ]
then
logMulti "Error - KJW_MTD_DIR variable should be set, but is not." \
". Current script: ${THIS_KJW_SCRIPT_URL}" \
". That function (${FUNC_CUR}) is usually launched from within a virtual machine."
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
return 1
fi
if [ -z "${KJW_MTD_FILE}" ]
then
logMulti "Error - KJW_MTD_FILE variable should be set, but is not." \
". Current script: ${THIS_KJW_SCRIPT_URL}" \
". That function (${FUNC_CUR}) is usually launched from within a virtual machine."
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
return 1
fi
if [ -z "${S3_MTD_URL}" ]
then
logMulti "Error - S3_MTD_URL variable should be set, but is not." \
". Current script: ${THIS_KJW_SCRIPT_URL}" \
". That function (${FUNC_CUR}) is usually launched from within a virtual machine."
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
return 1
fi
if [ -z "${S3_TSMTD_URL}" ]
then
logMulti "Error - S3_TSMTD_URL variable should be set, but is not." \
". Current script: ${THIS_KJW_SCRIPT_URL}" \
". That function (${FUNC_CUR}) is usually launched from within a virtual machine."
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
return 1
fi
cat > ${KJW_MTD_FILE} << _EOF
NOW_TS^${NOW_TS}
ISNOT_CONTAINER^${ISNOT_CONTAINER}
KJW_DATA_DIR^${KJW_DATA_DIR}
TMP_DATA_DIR^${TMP_DATA_DIR}
KJW_MTD_DIR^${KJW_MTD_DIR}
KJW_MTD_FILE^${KJW_MTD_FILE}
KJW_LOG_FILE^${KJW_LOG_FILE}
STS_DIR^${STS_DIR}
TS_DIR^${TS_DIR}
APP_DATA_FILENAME^${APP_DATA_FILENAME}
APP_DATA_TYPE^${APP_DATA_TYPE}
APP_MTD_FILENAME^${APP_MTD_FILENAME}
KJW_S3_BUCKET^${KJW_S3_BUCKET}
KJW_S3_URL^${KJW_S3_URL}
S3_APP_DIR^${S3_APP_DIR}
S3_MTD_URL^${S3_MTD_URL}
S3_MTD_BROWSABLE_URL^${S3_MTD_BROWSABLE_URL}
S3_TSMTD_URL^${S3_TSMTD_URL}
S3_TSMTD_BROWSABLE_URL^${S3_TSMTD_BROWSABLE_URL}
S3_LOG_URL^${S3_LOG_URL}
S3_LOG_BROWSABLE_URL^${S3_LOG_BROWSABLE_URL}
S3_TSLOG_URL^${S3_TSLOG_URL}
S3_TSLOG_BROWSABLE_URL^${S3_TSLOG_BROWSABLE_URL}
S3_APP_DATA_DIR^${S3_APP_DATA_DIR}
DATA_S3_BASE_URL^${DATA_S3_BASE_URL}
DATA_S3_BASE_BROWSABLE_URL^${DATA_S3_BASE_BROWSABLE_URL}
APP_DATA_S3_URL^${APP_DATA_S3_URL}
APP_DATA_S3_BROWSABLE_URL^${APP_DATA_S3_BROWSABLE_URL}
curl-version^$(curl --version|head -1)
aws-cli-version^$(aws --version)
jq-version^$(jq --version)
_EOF
# Upload to (and check) the base metadata folder on S3
log "Uploading metadata file from ${KJW_MTD_DIR}/ to AWS S3 (${S3_MTD_URL} ; ${S3_MTD_BROWSABLE_URL})..."
logMulti "$(aws s3 sync --no-progress ${KJW_MTD_DIR}/ ${S3_MTD_URL} 2>&1)"
log "Content of AWS S3 (${S3_MTD_URL} ; ${S3_MTD_BROWSABLE_URL}):"
logMulti "$(aws s3 ls --human --summarize ${S3_MTD_URL}/ 2>&1)"
# Upload to (and check) the time-stamped metadata folder on S3
log "Uploading metadata file from ${KJW_MTD_DIR}/ to AWS S3 (${S3_TSMTD_URL} ; ${S3_TSMTD_BROWSABLE_URL})..."
logMulti "$(aws s3 sync --no-progress ${KJW_MTD_DIR}/ ${S3_TSMTD_URL} 2>&1)"
log "Content of AWS S3 (${S3_TSMTD_URL} ; ${S3_TSMTD_BROWSABLE_URL}):"
logMulti "$(aws s3 ls --human --summarize ${S3_TSMTD_URL}/ 2>&1)"
#
log "End of function"
KJW_FUNC="${FUNC_CUR}"
}