This repository was archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetContext.sh
More file actions
executable file
·307 lines (259 loc) · 12.3 KB
/
setContext.sh
File metadata and controls
executable file
·307 lines (259 loc) · 12.3 KB
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
#!/bin/bash
# Based on current directory location and existing environment,
# define additional environment variables to facilitate automation
#
# This script is designed to be sourced into other scripts
if [[ -n "${GENERATION_DEBUG}" ]]; then set ${GENERATION_DEBUG}; fi
# If the context has already been determined, there is nothing to do
if [[ -n "${GENERATION_CONTEXT_DEFINED}" ]]; then return 0; fi
export GENERATION_CONTEXT_DEFINED="true"
GENERATION_CONTEXT_DEFINED_LOCAL="true"
# Generate the list of files constituting the blueprint
pushd "$(pwd)" >/dev/null
BLUEPRINT_LIST=
CONTAINERS_ARRAY=("${GENERATION_DIR}/templates/containers/switch_start.ftl")
if [[ (-f "segment.json") || (-f "container.json") ]]; then
# segment directory
export LOCATION="${LOCATION:-segment}"
export SEGMENT_DIR="$(pwd)"
export SEGMENT="$(basename $(pwd))"
if [[ -f "segment.json" ]]; then
BLUEPRINT_LIST="${SEGMENT_DIR}/segment.json ${BLUEPRINT_LIST}"
fi
if [[ -f "container.json" ]]; then
BLUEPRINT_LIST="${SEGMENT_DIR}/container.json ${BLUEPRINT_LIST}"
fi
if [[ -f "solution.json" ]]; then
BLUEPRINT_LIST="${SEGMENT_DIR}/solution.json ${BLUEPRINT_LIST}"
fi
for CONTAINER in $(ls container_*.ftl 2> /dev/null); do
CONTAINERS_ARRAY+=("${SEGMENT_DIR}/${CONTAINER}")
done
cd ..
# solutions directory - only add files if present in segment directory
export SOLUTIONS_DIR="$(pwd)"
if [[ (-f "solution.json") && (!("${BLUEPRINT_LIST}" =~ solution.json)) ]]; then
BLUEPRINT_LIST="${SOLUTIONS_DIR}/solution.json ${BLUEPRINT_LIST}"
fi
for CONTAINER in $(ls container_*.ftl 2> /dev/null); do
if [[ !("${CONTAINERS_ARRAY[*]}" =~ $(basename ${CONTAINER})) ]]; then
CONTAINERS_ARRAY+=("${SOLUTIONS_DIR}/${CONTAINER}")
fi
done
cd ..
fi
if [[ -f "account.json" ]]; then
# account directory
# We check it before checking for a product as the account directory
# also acts as a product directory for shared infrastructure
# An account directory may also have no product information e.g.
# in the case of production environments in dedicated accounts.
export LOCATION="${LOCATION:-account}"
export GENERATION_DATA_DIR="$(cd ../..;pwd)"
fi
if [[ -f "product.json" ]]; then
# product directory
if [[ "${LOCATION}" == "account" ]]; then
export LOCATION="account|product"
else
export LOCATION="${LOCATION:-product}"
fi
export PRODUCT_DIR="$(pwd)"
export PRODUCT="$(basename $(pwd))"
BLUEPRINT_LIST="${PRODUCT_DIR}/product.json ${BLUEPRINT_LIST}"
export GENERATION_DATA_DIR="$(cd ../..;pwd)"
fi
if [[ -f "integrator.json" ]]; then
export LOCATION="${LOCATION:-integrator}"
export GENERATION_DATA_DIR="$(pwd)"
export INTEGRATOR="$(basename $(pwd))"
fi
if [[ (-d config) && (-d infrastructure) ]]; then
export LOCATION="${LOCATION:-root}"
export GENERATION_DATA_DIR="$(pwd)"
fi
if [[ -z "${GENERATION_DATA_DIR}" ]]; then
echo -e "\nCan't locate the root of the directory tree. Are we in the right place?"
usage
fi
# root directory
cd "${GENERATION_DATA_DIR}"
export ACCOUNT="$(basename $(pwd))"
popd >/dev/null
export CONFIG_DIR="${GENERATION_DATA_DIR}/config"
export INFRASTRUCTURE_DIR="${GENERATION_DATA_DIR}/infrastructure"
export TENANT_DIR="${CONFIG_DIR}/${ACCOUNT}"
export ACCOUNT_DIR="${CONFIG_DIR}/${ACCOUNT}"
export ACCOUNT_CREDENTIALS_DIR="${INFRASTRUCTURE_DIR}/${ACCOUNT}/credentials"
export ACCOUNT_APPSETTINGS_DIR="${ACCOUNT_DIR}/appsettings"
export ACCOUNT_CREDENTIALS="${ACCOUNT_CREDENTIALS_DIR}/credentials.json"
if [[ -f "${ACCOUNT_DIR}/account.json" ]]; then
BLUEPRINT_LIST="${ACCOUNT_DIR}/account.json ${BLUEPRINT_LIST}"
fi
if [[ -f "${TENANT_DIR}/tenant.json" ]]; then
BLUEPRINT_LIST="${TENANT_DIR}/tenant.json ${BLUEPRINT_LIST}"
fi
# Build the composite solution ( aka blueprint)
export COMPOSITE_BLUEPRINT="${CONFIG_DIR}/composite_blueprint.json"
if [[ -n "${BLUEPRINT_LIST}" ]]; then
${GENERATION_DIR}/manageJSON.sh -d -o ${COMPOSITE_BLUEPRINT} "${GENERATION_DIR}/data/masterData.json" ${BLUEPRINT_LIST}
else
echo "{}" > ${COMPOSITE_BLUEPRINT}
fi
# Extract key settings from the composite solution
# Ignore values generated by addition of default Id/Name attribute values
export TID=${TID:-$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Tenant.Id | select(.!="Tenant") | select(.!=null)')}
export TENANT=${TENANT:-$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Tenant.Name | select(.!="Tenant") | select(.!=null)')}
export AID=${AID:-$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Account.Id | select(.!="Account") | select(.!=null)')}
export ACCOUNT_REGION=${ACCOUNT_REGION:-$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Account.Region | select(.!=null)')}
export PID=${PID:-$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Product.Id | select(.!="Product") | select(.!=null)')}
export PRODUCT_REGION=${PRODUCT_REGION:-$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Product.Region | select(.!=null)')}
export SID=${SID:-$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Segment.Id | select(.!="Segment") | select(.!=null)')}
export REGION="${REGION:-$PRODUCT_REGION}"
# Perform a few consistency checks
if [[ -z "${REGION}" ]]; then
echo -e "\nThe region must be defined in the Account or Product blueprint section. Nothing to do."
usage
fi
BLUEPRINT_ACCOUNT=$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Account.Name | select(.!=null)')
BLUEPRINT_PRODUCT=$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Product.Name | select(.!=null)')
BLUEPRINT_SEGMENT=$(cat ${COMPOSITE_BLUEPRINT} | jq -r '.Segment.Name | select(.!=null)')
if [[ (-n "${ACCOUNT}") &&
("${BLUEPRINT_ACCOUNT}" != "Account") &&
("${ACCOUNT}" != "${BLUEPRINT_ACCOUNT}") ]]; then
echo -e "\nBlueprint account of ${BLUEPRINT_ACCOUNT} doesn't match expected value of ${ACCOUNT}"
usage
fi
if [[ (-n "${PRODUCT}") &&
("${BLUEPRINT_PRODUCT}" != "Product") &&
("${PRODUCT}" != "${BLUEPRINT_PRODUCT}") ]]; then
echo -e "\nBlueprint product of ${BLUEPRINT_PRODUCT} doesn't match expected value of ${PRODUCT}"
usage
fi
if [[ (-n "${SEGMENT}") &&
("${BLUEPRINT_SEGMENT}" != "Segment") &&
("${SEGMENT}" != "${BLUEPRINT_SEGMENT}") ]]; then
echo -e "\nBlueprint segment of ${BLUEPRINT_SEGMENT} doesn't match expected value of ${SEGMENT}"
usage
fi
# Build the composite containers list
export COMPOSITE_CONTAINERS="${CONFIG_DIR}/composite_containers.json"
for CONTAINER in $(find ${GENERATION_DIR}/templates/containers/container_*.ftl -maxdepth 1 2> /dev/null); do
if [[ !("${CONTAINERS_ARRAY[*]}" =~ $(basename ${CONTAINER})) ]]; then
CONTAINERS_ARRAY+=("${CONTAINER}")
fi
done
CONTAINERS_ARRAY+=("${GENERATION_DIR}/templates/containers/switch_end.ftl")
cat "${CONTAINERS_ARRAY[@]}" > ${COMPOSITE_CONTAINERS}
# Product specific context if the product is known
APPSETTINGS_LIST=
CREDENTIALS_LIST=
if [[ -n "${PRODUCT}" ]]; then
export SOLUTIONS_DIR="${CONFIG_DIR}/${PRODUCT}/solutions"
export APPSETTINGS_DIR="${CONFIG_DIR}/${PRODUCT}/appsettings"
export CREDENTIALS_DIR="${INFRASTRUCTURE_DIR}/${PRODUCT}/credentials"
# slice level appsettings
if [[ (-n "${SLICE}") ]]; then
# Confirm it is an application slice"
APPLICATION_SLICE_LIST=$(echo $(cat ${COMPOSITE_BLUEPRINT} | jq -r -f ${GENERATION_DIR}/listApplicationSlices.jq | dos2unix))
if [[ -n "$(echo ${APPLICATION_SLICE_LIST} | grep -i ${SLICE})" ]]; then
IS_APPLICATION_SLICE="true"
EFFECTIVE_SLICE="$SLICE"
if [[ -f "${APPSETTINGS_DIR}/${SEGMENT}/${SLICE}/slice.ref" ]]; then
EFFECTIVE_SLICE=$(cat "${APPSETTINGS_DIR}/${SEGMENT}/${SLICE}/slice.ref")
fi
if [[ -f "${APPSETTINGS_DIR}/${SEGMENT}/${EFFECTIVE_SLICE}/appsettings.json" ]]; then
APPSETTINGS_LIST="${APPSETTINGS_DIR}/${SEGMENT}/${EFFECTIVE_SLICE}/appsettings.json ${APPSETTINGS_LIST}"
fi
if [[ -f "${APPSETTINGS_DIR}/${SEGMENT}/${EFFECTIVE_SLICE}/build.ref" ]]; then
export BUILD_REFERENCE=$(cat "${APPSETTINGS_DIR}/${SEGMENT}/${EFFECTIVE_SLICE}/build.ref")
fi
else
IS_APPLICATION_SLICE="false"
fi
fi
# segment level appsettings/credentials
if [[ (-n "${SEGMENT}") ]]; then
if [[ -f "${APPSETTINGS_DIR}/${SEGMENT}/appsettings.json" ]]; then
APPSETTINGS_LIST="${APPSETTINGS_DIR}/${SEGMENT}/appsettings.json ${APPSETTINGS_LIST}"
fi
if [[ -f "${CREDENTIALS_DIR}/${SEGMENT}/credentials.json" ]]; then
CREDENTIALS_LIST="${CREDENTIALS_DIR}/${SEGMENT}/credentials.json ${CREDENTIALS_LIST}"
fi
fi
# product level appsettings
if [[ -f "${APPSETTINGS_DIR}/appsettings.json" ]]; then
APPSETTINGS_LIST="${APPSETTINGS_DIR}/appsettings.json ${APPSETTINGS_LIST}"
fi
# product level credentials
if [[ -f "${CREDENTIALS_DIR}/credentials.json" ]]; then
CREDENTIALS_LIST="${CREDENTIALS_DIR}/credentials.json ${CREDENTIALS_LIST}"
fi
# account level appsettings
if [[ -f "${ACCOUNT_APPSETTINGS_DIR}/appsettings.json" ]]; then
APPSETTINGS_LIST="${ACCOUNT_APPSETTINGS_DIR}/appsettings.json ${APPSETTINGS_LIST}"
fi
fi
# Build the composite appsettings
export COMPOSITE_APPSETTINGS="${CONFIG_DIR}/composite_appsettings.json"
if [[ -n "${APPSETTINGS_LIST}" ]]; then
${GENERATION_DIR}/manageJSON.sh -c -o ${COMPOSITE_APPSETTINGS} -c ${APPSETTINGS_LIST}
else
echo "{}" > ${COMPOSITE_APPSETTINGS}
fi
# Check for account level credentials
if [[ -f "${ACCOUNT_CREDENTIALS_DIR}/credentials.json" ]]; then
CREDENTIALS_LIST="${ACCOUNT_CREDENTIALS_DIR}/credentials.json ${CREDENTIALS_LIST}"
fi
# Build the composite credentials
export COMPOSITE_CREDENTIALS="${INFRASTRUCTURE_DIR}/composite_credentials.json"
if [[ -n "${CREDENTIALS_LIST}" ]]; then
${GENERATION_DIR}/manageJSON.sh -o ${COMPOSITE_CREDENTIALS} ${CREDENTIALS_LIST}
else
echo "{}" > ${COMPOSITE_CREDENTIALS}
fi
# Create the composite stack outputs
STACK_LIST=()
if [[ (-n "${ACCOUNT}") && (-d "${INFRASTRUCTURE_DIR}/${ACCOUNT}/aws/cf") ]]; then
STACK_LIST+=($(find "${INFRASTRUCTURE_DIR}/${ACCOUNT}/aws/cf" -name acc*-stack.json))
fi
if [[ (-n "${PRODUCT}") && (-n "${REGION}") && (-d "${INFRASTRUCTURE_DIR}/${PRODUCT}/aws/cf") ]]; then
STACK_LIST+=($(find "${INFRASTRUCTURE_DIR}/${PRODUCT}/aws/cf" -name product-${REGION}-stack.json))
fi
if [[ (-n "${SEGMENT}") && (-n "${REGION}") && (-d "${INFRASTRUCTURE_DIR}/${PRODUCT}/aws/${SEGMENT}/cf") ]]; then
STACK_LIST+=($(find "${INFRASTRUCTURE_DIR}/${PRODUCT}/aws/${SEGMENT}/cf" -name *-${REGION}-stack.json))
fi
export COMPOSITE_STACK_OUTPUTS="${INFRASTRUCTURE_DIR}/composite_stack_outputs.json"
if [[ "${#STACK_LIST[@]}" -gt 0 ]]; then
${GENERATION_DIR}/manageJSON.sh -f "[.[].Stacks | select(.!=null) | .[].Outputs | select(.!=null) | .[]]" -o ${COMPOSITE_STACK_OUTPUTS} "${STACK_LIST[@]}"
else
echo "[]" > ${COMPOSITE_STACK_OUTPUTS}
fi
# Set default AWS credentials if available (hook from Jenkins framework)
CHECK_AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-${ACCOUNT_TEMP_AWS_ACCESS_KEY_ID}}"
CHECK_AWS_ACCESS_KEY_ID="${CHECK_AWS_ACCESS_KEY_ID:-${!ACCOUNT_AWS_ACCESS_KEY_ID_VAR}}"
if [[ -n "${CHECK_AWS_ACCESS_KEY_ID}" ]]; then export AWS_ACCESS_KEY_ID="${CHECK_AWS_ACCESS_KEY_ID}"; fi
CHECK_AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-${ACCOUNT_TEMP_AWS_SECRET_ACCESS_KEY}}"
CHECK_AWS_SECRET_ACCESS_KEY="${CHECK_AWS_SECRET_ACCESS_KEY:-${!ACCOUNT_AWS_SECRET_ACCESS_KEY_VAR}}"
if [[ -n "${CHECK_AWS_SECRET_ACCESS_KEY}" ]]; then export AWS_SECRET_ACCESS_KEY="${CHECK_AWS_SECRET_ACCESS_KEY}"; fi
CHECK_AWS_SESSION_TOKEN="${AWS_SESSION_TOKEN:-${ACCOUNT_TEMP_AWS_SESSION_TOKEN}}"
if [[ -n "${CHECK_AWS_SESSION_TOKEN}" ]]; then export AWS_SESSION_TOKEN="${CHECK_AWS_SESSION_TOKEN}"; fi
# Set the profile for IAM access if AWS credentials not in the environment
if [[ ((-z "${AWS_ACCESS_KEY_ID}") || (-z "${AWS_SECRET_ACCESS_KEY}")) && (-n "${ACCOUNT}") ]]; then
export AWS_DEFAULT_PROFILE="${ACCOUNT}"
fi
# Handle some MINGW peculiarities
uname | grep -i "MINGW64" > /dev/null 2>&1
if [[ "$?" -eq 0 ]]; then
export MINGW64="true"
fi
# Detect if within a git repo
git status >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
export WITHIN_GIT_REPO="true"
export FILE_MV="git mv"
export FILE_RM="git rm"
else
export FILE_MV="mv"
export FILE_RM="rm"
fi