This repository was archived by the owner on Feb 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontextTree.sh
More file actions
executable file
·1862 lines (1518 loc) · 63.2 KB
/
contextTree.sh
File metadata and controls
executable file
·1862 lines (1518 loc) · 63.2 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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# GEN3 Context Tree Functions
#
# This script is designed to be sourced into other scripts
# -- Composites --
function parse_stack_filename() {
local file="$1"; shift
# Parse file name for key values
# Assume Account is part of the stack filename
if contains "$(fileName "${file}")" "([a-z0-9]+)-(.+)-([1-9][0-9]{10})-([a-z]{2}-[a-z]+-[1-9])(-pseudo)?-(.+)"; then
stack_level="${BASH_REMATCH[1]}"
stack_deployment_unit="${BASH_REMATCH[2]}"
stack_account="${BASH_REMATCH[3]}"
stack_region="${BASH_REMATCH[4]}"
return 0
fi
if contains "$(fileName "${file}")" "([a-z0-9]+)-(.+-.+)-(.+)-([a-z]{2}-[a-z]+-[1-9])(-pseudo)?-(.+)"; then
stack_level="${BASH_REMATCH[1]}"
stack_deployment_unit="${BASH_REMATCH[2]}"
stack_region="${BASH_REMATCH[4]}"
stack_account=""
return 0
fi
if contains "$(fileName "${file}")" "([a-z0-9]+)-(.+)-([a-z]{2}-[a-z]+-[1-9])(-pseudo)?-(.+)"; then
stack_level="${BASH_REMATCH[1]}"
stack_deployment_unit="${BASH_REMATCH[2]}"
stack_region="${BASH_REMATCH[3]}"
stack_account=""
return 0
fi
return 1
}
function create_pseudo_stack() {
local comment="$1"; shift
local file="$1"; shift
local pairs=("$@")
pushTempDir "${FUNCNAME[0]}_XXXXXX"
local tmp_file="$(getTopTempDir)/create_pseudo_stack.json"
local return_status
# TODO(mfl): Probably a more elegant way to do this with jq
# Create the name/value pairs
cat << EOF > "${tmp_file}"
{
"Stacks": [
{
"Comment": "${comment}",
"Outputs": [
EOF
# now the keypairs
for ((i=0; i<${#pairs[@]}; i++)); do
[[ (i -gt 0) && (i%2 -eq 0) ]] && echo "," >> "${tmp_file}"
[[ i%2 -eq 0 ]] && \
cat << EOF >> "${tmp_file}"
{
"OutputKey": "${pairs[i]}",
EOF
[[ i%2 -ne 0 ]] && \
cat << EOF >> "${tmp_file}"
"OutputValue": "${pairs[i]}"
}
EOF
done
cat << EOF >> "${tmp_file}"
]
}
]
}
EOF
runJQ --indent 4 "." < "${tmp_file}" > "${file}"; return_status=$?
popTempDir
return ${return_status}
}
function getFilesAsJSON() {
local result_file="$1";shift
local files=("$@")
local file_array=()
local return_status
pushTempDir "${FUNCNAME[0]}_XXXXXX"
local tmp_dir="$(getTopTempDir)"
# Create standardised versions of the files
if [[ $(arraySize "files") -ne 0 ]]; then
for file in "${files[@]}"; do
local file_name="$( fileName ${file} )"
local relative_file_path="$( filePath ${file} )"
local file_path="$( cd "${relative_file_path}"; pwd )"
local tmp_file="$( getTempFile "XXXXXX" "${tmp_dir}" )"
addToArray "file_array" "${tmp_file}"
if $(contains "${file}" "asFile"); then
# Only want the filename
echo {} | jq --arg file_name "${file_name}" --arg file_path "${file_path}" \
'{ "FileName" : $file_name, "FilePath" : $file_path, "Content" : [] }' >> "${tmp_file}"
else
# Want file content - handle a few content variants
case "$(fileExtension "${file_name}")" in
json)
jq --arg file_name "${file_name}" --arg file_path "${file_path}" \
'{ "FileName" : $file_name, "FilePath" : $file_path, "Content" : [.] }' < "${file}" > "${tmp_file}"
;;
escjson)
jq --arg file_name "${file_name}" --arg file_path "${file_path}" \
'{ "FileName" : $file_name, "FilePath" : $file_path, "Content" : [tojson] }' < "${file}" > "${tmp_file}"
;;
*)
# Assume raw input
jq -sR --arg file_name "${file_name}" --arg file_path "${file_path}" \
'{ "FileName" : $file_name, "FilePath" : $file_path, "Content" : [.] }' < "${file}" > "${tmp_file}"
;;
esac
fi
done
# Slurp all of the standardised files and put them into a single file as an array
# Freemarker doesn't support the null json value in ?eval so we need to remove any null values
runJQ --indent 2 -s -f "${GENERATION_BASE_DIR}/execution/nullStrip.jq" "${file_array[@]}" > "${result_file}"; return_status=$?
else
echo "[]" > "${result_file}"; return_status=0
fi
popTempDir
return ${return_status}
}
function convertFilesToJSONObject() {
local ancestors=($1); shift
local root_dir="$1";shift
local base_dir="$1";shift
local result_file="$1";shift
local files=("$@")
local filter='{ "RootDirectory" : $root_dir, "BaseDirectory" : $base_dir, "Files" : . }'
local return_status
pushTempDir "${FUNCNAME[0]}_XXXXXX"
local tmp_dir="$(getTopTempDir)"
# First assemble all the files
getFilesAsJSON "${tmp_dir}/files.json" "${files[@]}"; return_status=$?
if [[ $return_status -eq 0 ]]; then
# Add any ancestors
for (( index=${#ancestors[@]}-1 ; index >= 0 ; index-- )) ; do
filter="{\"${ancestors[index]}\" : ${filter} }"
done
# Add the root and base directories
jq --arg root_dir "${root_dir}" --arg base_dir "${base_dir}" \
"${filter}" < "${tmp_dir}/files.json" > "${result_file}"; return_status=$?
fi
popTempDir
return ${return_status}
}
function assemble_settings() {
local root_dir="${1:-${ROOT_DIR}}"; shift
local result_file="${1:-${COMPOSITE_SETTINGS}}"; shift
pushTempDir "${FUNCNAME[0]}_XXXXXX"
local tmp_dir="$(getTopTempDir)"
local tmp_file_list=()
local id
local name
local tmp_file
local return_status
# Process accounts
readarray -t account_files < <(find "${root_dir}" \( \
-name account.json \
-and -not -path "*/.*/*" \) | sort)
# debug "Account=${account_files[@]}"
# Settings
for account_file in "${account_files[@]}"; do
id="$(getJSONValue "${account_file}" ".Account.Id")"
name="$(getJSONValue "${account_file}" ".Account.Name")"
[[ -z "${name}" ]] && name="${id}"
[[ (-n "${ACCOUNT}") && ("${ACCOUNT,,}" != "${name,,}") ]] && continue
local account_dir="$(findGen3AccountSettingsDir "${root_dir}" "${name}")"
debug "Processing account dir ${account_dir} ..."
pushd "${account_dir}" > /dev/null 2>&1 || continue
readarray -t setting_files < <(find . -type f -name "*.json" )
if ! arrayIsEmpty "setting_files" ; then
tmp_file="${tmp_dir}/accounts_settings_${name}.json"
convertFilesToJSONObject "Accounts Settings ${name}" "${root_dir}" "${account_dir}" "${tmp_file}" "${setting_files[@]}" || return 1
tmp_file_list+=("${tmp_file}")
fi
popd > /dev/null
done
# Process products
readarray -t product_files < <(find "${root_dir}" \( \
-name product.json \
-and -not -path "*/.*/*" \) | sort)
# debug "Products=${product_files[@]}"
for product_file in "${product_files[@]}"; do
id="$(getJSONValue "${product_file}" ".Product.Id")"
name="$(getJSONValue "${product_file}" ".Product.Name")"
[[ -z "${name}" ]] && name="${id}"
[[ (-n "${PRODUCT}") && ("${PRODUCT,,}" != "${name,,}") ]] && continue
# Settings
local settings_dir="$(findGen3ProductSettingsDir "${root_dir}" "${name}")"
if [[ -d "${settings_dir}" ]]; then
debug "Processing settings dir ${settings_dir} ..."
pushd "${settings_dir}" > /dev/null 2>&1 || continue
readarray -t setting_files < <(find . -type f \( \
-not \( -name ".*" -or -path "*/.*/*" \) \) )
if ! arrayIsEmpty "setting_files" ; then
tmp_file="${tmp_dir}/products_settings_${name}.json"
convertFilesToJSONObject "Products Settings ${name}" "${root_dir}" "${settings_dir}" "${tmp_file}" "${setting_files[@]}" || return 1
tmp_file_list+=("${tmp_file}")
fi
popd > /dev/null
fi
# Builds
local builds_dir="$(findGen3ProductBuildsDir "${root_dir}" "${name}")"
if [[ (-d "${builds_dir}") && ("${settings_dir}" != "${builds_dir}") ]]; then
debug "Processing builds dir ${builds_dir} ..."
pushd "${builds_dir}" > /dev/null
readarray -t build_files < <(find . -type f \( \
-not \( -name ".*" -or -path "*/.*/*" \) \) )
if ! arrayIsEmpty "build_files" ; then
tmp_file="${tmp_dir}/products_builds_${name}.json"
convertFilesToJSONObject "Products Builds ${name}" "${root_dir}" "${builds_dir}" "${tmp_file}" "${build_files[@]}" || return 1
tmp_file_list+=("${tmp_file}")
fi
popd > /dev/null
fi
# Operations
local operations_dir="$(findGen3ProductOperationsDir "${root_dir}" "${name}")"
if [[ -d "${operations_dir}" ]]; then
debug "Processing operations dir ${operations_dir} ..."
pushd "${operations_dir}" > /dev/null
readarray -t operations_files < <(find . -type f \( \
-not \( -name ".*" -or -path "*/.*/*" \) \) )
if ! arrayIsEmpty "operations_files" ; then
tmp_file="${tmp_dir}/products_operations_${name}.json"
convertFilesToJSONObject "Products Operations ${name}" "${root_dir}" "${operations_dir}" "${tmp_file}" "${operations_files[@]}" || return 1
tmp_file_list+=("${tmp_file}")
fi
popd > /dev/null
fi
done
# Generate the merged output
debug "Generating ${result_file} ..."
jqMerge "${tmp_file_list[@]}" > "${result_file}"; return_status=$?
popTempDir
return ${return_status}
}
function assemble_composite_definitions() {
local tmp_file="$( getTempFile "definitions_XXXXXX" "${tmp_dir}" )"
# Gather the relevant definitions
local restore_nullglob=$(shopt -p nullglob)
local restore_globstar=$(shopt -p globstar)
shopt -s nullglob
shopt -s globstar
local definitions_array=()
[[ (-n "${ACCOUNT}") ]] &&
addToArray "definitions_array" "${ACCOUNT_STATE_DIR}"/cf/shared/**/defn*-definition.json
[[ (-n "${PRODUCT}") && (-n "${REGION}") ]] &&
addToArray "definitions_array" "${PRODUCT_STATE_DIR}"/cf/shared/**/defn*-"${REGION}"*-definition.json
[[ (-n "${ENVIRONMENT}") && (-n "${SEGMENT}") && (-n "${REGION}") ]] &&
addToArray "definitions_array" "${PRODUCT_STATE_DIR}/cf/${ENVIRONMENT}/${SEGMENT}"/**/*${ACCOUNT}*-definition.json
${restore_globstar}
${restore_nullglob}
debug "DEFINITIONS=${definitions_array[*]}"
jqMerge "${definitions_array[@]}" > "${tmp_file}"
# Escape any freemarker markup
export COMPOSITE_DEFINITIONS="${CACHE_DIR}/composite_definitions.json"
sed 's/${/$\\{/g' < "${tmp_file}" > "${COMPOSITE_DEFINITIONS}"
}
function assemble_composite_stack_outputs() {
pushTempDir "${FUNCNAME[0]}_XXXXXX"
local tmp_dir="$(getTopTempDir)"
# Create the composite stack outputs
local restore_nullglob=$(shopt -p nullglob)
local restore_globstar=$(shopt -p globstar)
shopt -s nullglob
shopt -s globstar
local stack_array=()
[[ (-n "${ACCOUNT}") ]] &&
addToArray "stack_array" "${ACCOUNT_STATE_DIR}"/*/shared/**/acc*-stack.json
[[ (-n "${ENVIRONMENT}") && (-n "${SEGMENT}") && (-n "${REGION}") ]] &&
addToArray "stack_array" "${PRODUCT_STATE_DIR}"/*/"${ENVIRONMENT}/${SEGMENT}"/**/*${ACCOUNT}*-stack.json
${restore_globstar}
${restore_nullglob}
debug "STACK_OUTPUTS=${stack_array[*]}"
export COMPOSITE_STACK_OUTPUTS="${CACHE_DIR}/composite_stack_outputs.json"
getFilesAsJSON "${COMPOSITE_STACK_OUTPUTS}" "${stack_array[@]}"; return_status=$?
popTempDir
return ${return_status}
}
function getBluePrintParameter() {
local patterns=("$@")
getJSONValue "${COMPOSITE_BLUEPRINT}" "${patterns[@]}"
}
# -- GEN3 directory structure --
function findGen3RootDir() {
local current="$1"; shift
# First check for an explicitly marked root
local marked_root_dir="$(findAncestorDir root.json "${current}")"
[[ -n "${marked_root_dir}" ]] && echo -n "${marked_root_dir}" && return 0
# Now look for the first dir containing both a config and infrastructure directory
local config_root_dir="$(filePath "$(findAncestorDir config "${current}")")"
local infrastructure_root_dir="$(filePath "$(findAncestorDir infrastructure "${current}")")"
local root_dir="${config_root_dir:-${infrastructure_root_dir}}"
[[ (-d "${root_dir}/config") && (-d "${root_dir}/infrastructure") ]] && echo -n "${root_dir}" && return 0
return 1
}
function findGen3TenantDir() {
local root_dir="$1"; shift
local tenant="$1"; shift
findDir "${root_dir}" \
"${tenant}/tenant.json" \
"${tenant}/config/tenant.json" \
"tenant.json"
}
function findGen3TenantInfrastructureDir() {
local root_dir="$1"; shift
local tenant="$1"; shift
findDir "${root_dir}" \
"${tenant}/infrastructure"
}
function findGen3AccountDir() {
local root_dir="$1"; shift
local account="$1"; shift
findDir "${root_dir}" \
"${account}/account.json" \
"${account}/config/account.json"
}
function findGen3AccountSettingsDir() {
local root_dir="$1"; shift
local account="$1"; shift
local account_dir="$(findGen3AccountDir "${root_dir}" "${account}")"
if [[ -n "${account_dir}" ]]; then
echo -n "${account_dir}/settings"
return 0
fi
return 1
}
function findGen3AccountInfrastructureDir() {
local root_dir="$1"; shift
local account="$1"; shift
findDir "${root_dir}" \
"${account}/infrastructure" \
"infrastructure/**/${account}"
}
# It would be very rare that accounts would have standard settings and operations
# settings as normally the accounts tree is controlled by the operations team.
# Thus the settings directory should be adequate. Nonetheless it allows consistency
# if the operations team always put their settings in the operations directory,
# regardless of repo.
function findGen3AccountOperationsDir() {
local root_dir="$1"; shift
local account="$1"; shift
# TODO(mfl): Remove infrastructure checks when all repos converted to >=v2.0.0
findDir "${root_dir}" \
"${account}/operations/settings" \
"operations/**/${account}/settings" \
"${account}/infrastructure/operations" \
"infrastructure/**/${account}/operations"
}
function findGen3AccountStateDir() {
local root_dir="$1"; shift
local account="$1"; shift
# TODO(mfl): Remove infrastructure checks when all repos converted to >=v2.0.0
findDir "${root_dir}" \
"${account}/state" \
"state/**/${account}" \
"${account}/infrastructure" \
"infrastructure/**/${account}"
}
function findGen3ProductDir() {
local root_dir="$1"; shift
local product="$1"; shift
findDir "${root_dir}" \
"${product}/product.json" \
"${product}/config/product.json"
}
function findGen3ProductSettingsDir() {
local root_dir="$1"; shift
local product="$1"; shift
local product_dir="$(findGen3ProductDir "${root_dir}" "${product}")"
if [[ -n "${product_dir}" ]]; then
echo -n "${product_dir}/settings"
return 0
fi
return 1
}
function findGen3ProductInfrastructureDir() {
local root_dir="$1"; shift
local product="$1"; shift
findDir "${root_dir}" \
"${product}/infrastructure" \
"infrastructure/**/${product}"
}
function findGen3ProductOperationsDir() {
local root_dir="$1"; shift
local product="$1"; shift
# TODO(mfl): Remove infrastructure checks when all repos converted to >=v2.0.0
findDir "${root_dir}" \
"${product}/operations/settings" \
"operations/**/${product}/settings" \
"${product}/infrastructure/operations" \
"infrastructure/**/${product}/operations"
}
function findGen3ProductSolutionsDir() {
local root_dir="$1"; shift
local product="$1"; shift
# TODO(mfl): Remove config checks when all repos converted to >=v2.0.0
findDir "${root_dir}" \
"${product}/infrastructure/solutions" \
"infrastructure/**/${product}/solutions" \
"${product}/config/solutionsv2" \
"config/**/${product}/solutionsv2"
}
function findGen3ProductBuildsDir() {
local root_dir="$1"; shift
local product="$1"; shift
# TODO(mfl): Remove config checks when all repos converted to >=v2.0.0
findDir "${root_dir}" \
"${product}/infrastructure/builds" \
"infrastructure/**/${product}/builds" \
"${product}/config/settings" \
"config/**/${product}/settings"
}
function findGen3ProductStateDir() {
local root_dir="$1"; shift
local product="$1"; shift
# TODO(mfl): Remove infrastructure checks when all repos converted to >=v2.0.0
findDir "${root_dir}" \
"${product}/state" \
"state/**/${product}" \
"${product}/infrastructure" \
"infrastructure/**/${product}"
}
function findGen3ProductEnvironmentDir() {
local root_dir="$1"; shift
local product="$1"; shift
local environment="$1"; shift
local solutions_dir="$(findGen3ProductSolutionsDir "${root_dir}" "${product}")"
[[ -z "${solutions_dir}" ]] && return 1
findDir "${solutions_dir}" "${environment}/environment.json"
}
function findGen3ProductEnvironmentSegmentDir() {
local root_dir="$1"; shift
local product="$1"; shift
local environment="$1"; shift
local segment="$1"; shift
local environment_dir="$(findGen3ProductEnvironmentDir "${root_dir}" "${product}" "${environment}")"
[[ -z "${environment_dir}" ]] && return 1
findDir "${environment_dir}" "${segment}/segment.json"
}
function getGen3Env() {
local env="$1"; shift
local prefix="$1"; shift
local env_name="${prefix}${env}"
echo "${!env_name}"
}
function setGen3DirEnv() {
local env="$1"; shift
local prefix="$1"; shift
local directories=("$@")
local env_name="${prefix}${env}"
for directory in "${directories[@]}"; do
if [[ -d "${directory}" ]]; then
declare -gx "${env_name}=${directory}"
return 0
fi
done
error $(locationMessage "Can't locate ${env} directory.")
return 1
}
function findGen3Dirs() {
local root_dir="$1"; shift
local tenant="${1:-${TENANT}}"; shift
local account="${1:-${ACCOUNT}}"; shift
local product="${1:-${PRODUCT}}"; shift
local environment="${1:-${ENVIRONMENT}}"; shift
local segment="${1:-${SEGMENT}}"; shift
local prefix="$1"; shift
setGen3DirEnv "ROOT_DIR" "${prefix}" "${root_dir}"
debug "ROOT_DIR=${ROOT_DIR}"
setGen3DirEnv "TENANT_DIR" "${prefix}" \
"$(findGen3TenantDir "${root_dir}" "${tenant}" )" || return 1
debug "TENANT_DIR=${TENANT_DIR}"
setGen3DirEnv "ACCOUNT_DIR" "${prefix}" \
"$(findGen3AccountDir "${root_dir}" "${account}" )" || return 1
debug "ACCOUNT_DIR=${ACCOUNT_DIR}"
setGen3DirEnv "ACCOUNT_SETTINGS_DIR" "${prefix}" \
"$(findGen3AccountSettingsDir "${root_dir}" "${account}" )" || return 1
debug "ACCOUNT_SETTINGS_DIR=${ACCOUNT_SETTINGS_DIR}"
setGen3DirEnv "ACCOUNT_INFRASTRUCTURE_DIR" "${prefix}" \
"$(findGen3AccountInfrastructureDir "${root_dir}" "${account}" )" || return 1
debug "ACCOUNT_INFRASTRUCTURE_DIR=${ACCOUNT_INFRASTRUCTURE_DIR}"
setGen3DirEnv "ACCOUNT_OPERATIONS_DIR" "${prefix}" \
"$(findGen3AccountOperationsDir "${root_dir}" "${account}" )" || return 1
debug "ACCOUNT_OPERATIONS_DIR=${ACCOUNT_OPERATIONS_DIR}"
setGen3DirEnv "ACCOUNT_STATE_DIR" "${prefix}" \
"$(findGen3AccountStateDir "${root_dir}" "${account}" )" || return 1
debug "ACCOUNT_STATE_DIR=${ACCOUNT_STATE_DIR}"
if [[ -n "${product}" ]]; then
setGen3DirEnv "PRODUCT_DIR" "${prefix}" \
"$(findGen3ProductDir "${root_dir}" "${product}")" || return 1
debug "PRODUCT_DIR=${PRODUCT_DIR}"
setGen3DirEnv "PRODUCT_SETTINGS_DIR" "${prefix}" \
"$(findGen3ProductSettingsDir "${root_dir}" "${product}" )" || return 1
debug "PRODUCT_SETTINGS_DIR=${PRODUCT_SETTINGS_DIR}"
setGen3DirEnv "PRODUCT_INFRASTRUCTURE_DIR" "${prefix}" \
"$(findGen3ProductInfrastructureDir "${root_dir}" "${product}")" || return 1
debug "PRODUCT_INFRASTRUCTURE_DIR=${PRODUCT_INFRASTRUCTURE_DIR}"
setGen3DirEnv "PRODUCT_OPERATIONS_DIR" "${prefix}" \
"$(findGen3ProductOperationsDir "${root_dir}" "${product}" )" || return 1
debug "PRODUCT_OPERATIONS_DIR=${PRODUCT_OPERATIONS_DIR}"
setGen3DirEnv "PRODUCT_SOLUTIONS_DIR" "${prefix}" \
"$(findGen3ProductSolutionsDir "${root_dir}" "${product}")" || return 1
debug "PRODUCT_SOLUTIONS_DIR=${PRODUCT_SOLUTIONS_DIR}"
setGen3DirEnv "PRODUCT_BUILDS_DIR" "${prefix}" \
"$(findGen3ProductBuildsDir "${root_dir}" "${product}")" || return 1
debug "PRODUCT_BUILDS_DIR=${PRODUCT_BUILDS_DIR}"
setGen3DirEnv "PRODUCT_STATE_DIR" "${prefix}" \
"$(findGen3ProductStateDir "${root_dir}" "${product}" )" || return 1
debug "PRODUCT_STATE_DIR=${PRODUCT_STATE_DIR}"
declare -gx ${prefix}PRODUCT_SHARED_SETTINGS_DIR=$(getGen3Env "PRODUCT_SETTINGS_DIR" "${prefix}")/shared
declare -gx ${prefix}PRODUCT_SHARED_SOLUTIONS_DIR=$(getGen3Env "PRODUCT_SOLUTIONS_DIR" "${prefix}")/shared
declare -gx ${prefix}PRODUCT_SHARED_OPERATIONS_DIR=$(getGen3Env "PRODUCT_OPERATIONS_DIR" "${prefix}")/shared
if [[ -n "${environment}" ]]; then
debug "ENVIRONMENT_DIR=$(findGen3ProductEnvironmentDir "${root_dir}" "${product}" "${environment}")"
declare -gx ${prefix}ENVIRONMENT_SHARED_SETTINGS_DIR=$(getGen3Env "PRODUCT_SETTINGS_DIR" "${prefix}")/${environment}
declare -gx ${prefix}ENVIRONMENT_SHARED_SOLUTIONS_DIR=$(getGen3Env "PRODUCT_SOLUTIONS_DIR" "${prefix}")/${environment}
declare -gx ${prefix}ENVIRONMENT_SHARED_OPERATIONS_DIR=$(getGen3Env "PRODUCT_OPERATIONS_DIR" "${prefix}")/${environment}
if [[ -n "${segment}" ]]; then
debug "SEGMENT_DIR=$(findGen3ProductEnvironmentSegmentDir "${root_dir}" "${product}" "${environment}" "${segment}")"
declare -gx ${prefix}SEGMENT_SHARED_SETTINGS_DIR=$(getGen3Env "PRODUCT_SETTINGS_DIR" "${prefix}")/shared/${segment}
declare -gx ${prefix}SEGMENT_SHARED_SOLUTIONS_DIR=$(getGen3Env "PRODUCT_SOLUTIONS_DIR" "${prefix}")/shared/${segment}
declare -gx ${prefix}SEGMENT_SHARED_OPERATIONS_DIR=$(getGen3Env "PRODUCT_OPERATIONS_DIR" "${prefix}")/shared/${segment}
declare -gx ${prefix}SEGMENT_SETTINGS_DIR=$(getGen3Env "PRODUCT_SETTINGS_DIR" "${prefix}")/${environment}/${segment}
declare -gx ${prefix}SEGMENT_BUILDS_DIR=$(getGen3Env "PRODUCT_BUILDS_DIR" "${prefix}")/${environment}/${segment}
declare -gx ${prefix}SEGMENT_SOLUTIONS_DIR=$(getGen3Env "PRODUCT_SOLUTIONS_DIR" "${prefix}")/${environment}/${segment}
declare -gx ${prefix}SEGMENT_OPERATIONS_DIR=$(getGen3Env "PRODUCT_OPERATIONS_DIR" "${prefix}")/${environment}/${segment}
fi
fi
fi
return 0
}
function checkInRootDirectory() {
local location="${1:-${LOCATION}}"
[[ ! ("root" =~ ${location}) ]] && fatalDirectory "root"
}
function checkInAccountDirectory() {
local location="${1:-${LOCATION}}"
[[ ! ("account" =~ ${location}) ]] && fatalDirectory "account"
}
function checkInProductDirectory() {
local location="${1:-${LOCATION}}"
[[ ! ("product" =~ ${location}) ]] && fatalDirectory "product"
}
function checkInEnvironmentDirectory() {
local location="${1:-${LOCATION}}"
[[ ! ("environment" =~ ${location}) ]] && fatalDirectory "environment"
}
function checkInSegmentDirectory() {
local location="${1:-${LOCATION}}"
[[ ! ("segment" =~ ${location}) ]] && fatalDirectory "segment"
}
function fatalProductOrSegmentDirectory() {
fatalDirectory "product or segment"
}
# -- Deployment Units --
function isValidUnit() {
local level="${1,,}"; shift
local unit="${1,,}"; shift
local result=0
# Known levels
declare -gA unit_required=( \
["unitlist"]="false" \
["blueprint"]="false" \
["buildblueprint"]="true" \
["account"]="true" \
["product"]="true" \
["application"]="true" \
["solution"]="true" \
["segment"]="true" \
["multiple"]="true" )
# Ensure arguments have been provided
[[ (-z "${unit_required[${level}]}") || ((-z "${unit}") && ("${unit_required[${level}]}" == "true")) ]] && return 1
# Check unit if required
if [[ "${unit_required[${level}]}" == "true" ]]; then
# Default deployment units for each level
declare -ga ACCOUNT_UNITS_ARRAY=("iam" "lg" "audit" "s3" "cert" "roles" "apigateway" "waf" "sms" "console")
declare -ga PRODUCT_UNITS_ARRAY=("s3" "sns" "cert" "cmk")
declare -ga BUILDBLUEPRINT_UNITS_ARRAY=(${unit})
declare -ga APPLICATION_UNITS_ARRAY=(${unit})
declare -ga SOLUTION_UNITS_ARRAY=(${unit})
declare -ga SEGMENT_UNITS_ARRAY=(${unit})
declare -ga MULTIPLE_UNITS_ARRAY=("iam" "dashboard")
# Apply explicit unit lists and check for presence of unit
# Allow them to be separated by commas or spaces in line with the separator
# definitions in setContext.sh for the automation framework
if namedef_supported; then
declare -n UNITS_SOURCE="${level^^}_UNITS"
declare -n UNITS_ARRAY="${level^^}_UNITS_ARRAY"
else
eval "declare UNITS_SOURCE=(\"\${${level^^}_UNITS}\")"
eval "declare UNITS_ARRAY=(\"\${${level^^}_UNITS_ARRAY[@]}\")"
fi
[[ -n "${UNITS_SOURCE}" ]] && IFS=", " read -ra UNITS_ARRAY <<< "${UNITS_SOURCE}"
# Return result
grep -iw "${unit}" <<< "${UNITS_ARRAY[*]}" >/dev/null 2>&1
result=$?
fi
return ${result}
}
function formatUnitCFDir() {
local base_dir="${1}"; shift
local level="${1,,}"; shift
local unit="${1,,}"; shift
local placement="${1,,}"; shift
local region="${1,,}"; shift
# Determine placement by region if not explicitly provided
if [[ -z "${placement}" ]]; then
case "${region}" in
us-east-1)
local placement="global"
;;
*)
local placement="default"
;;
esac
fi
echo -n "${base_dir}/${unit}/${placement}"
return 0
}
function getUnitCFDir() {
local base_dir="${1}"; shift
local level="${1,,}"; shift
local unit="${1,,}"; shift
local placement="${1,,}"; shift
local region="${1,,}"; shift
# Check for legacy files that don't contain the deployment unit
case "${level}" in
account)
if [[
("${unit}" == "s3") && (-f "${base_dir}/account-${region}-template.json") ]]; then
echo -n "${base_dir}" && return 0
fi
;;
product)
if [[
("${unit}" == "cmk") && (-f "${base_dir}/product-${region}-template.json") ]]; then
echo -n "${base_dir}" && return 0
fi
;;
solution)
if [[ -f "${base_dir}/solution-${region}-template.json" ]]; then
echo -n "${base_dir}" && return 0
fi
;;
segment)
if [[
(!("${unit}" =~ cmk|cert|dns )) &&
(-f "${base_dir}/container-${region}-template.json") ]]; then
echo -n "${base_dir}" && return 0
fi
if [[
("${unit}" == "cmk") &&
(
(-f "${base_dir}/seg-key-${region}-template.json") ||
(-f "${base_dir}/cont-key-${region}-template.json")
) ]]; then
echo -n "${base_dir}" && return 0
fi
;;
esac
formatUnitCFDir "${base_dir}" "${level}" "${unit}" "${placement}" "${region}"
}
# -- Upgrade CMDB --
function upgrade_build_ref() {
local legacy_file="$1"; shift
local upgraded_file="$1"; shift
IFS= " " read -ra build_array < "${legacy_file}"
[[ -z "${build_Array[0]}" ]] && fatal "Unable to upgrade build reference in ${file}" && return 1
echo -n "{\"Commit\" : \"${build_array[0]}\"" > "${upgraded_file}"
[[-n "${build_array[1]}" ]] && echo -n ", \"Tag\" : \"${build_array[1]}\"" >> "${upgraded_file}"
echo -n ", \"Formats\" : [\"docker\"]}" >> "${upgraded_file}"
return 0
}
function upgrade_shared_build_ref() {
local legacy_file="$1"; shift
local upgraded_file="$1"; shift
echo -n "{\"Reference\" : \"" > "${upgraded_file}"
cat "${legacy_file}" >> "${upgraded_file}"
echo -n "\"}" >> "${upgraded_file}"
return 0
}
function upgrade_credentials() {
local legacy_file="$1"; shift
local upgraded_file="$1"; shift
if [[ "$(jq ".Credentials | if .==null then [] else [1] end | length" < "${legacy_file}" )" -gt 0 ]]; then
runJQ ".Credentials" "${legacy_file}" > "${upgraded_file}"
else
cat "${legacy_file}" > "${upgraded_file}"
fi
}
# Remove .ref files, simplify credentials files
function upgrade_cmdb_repo_to_v1_0_0() {
local root_dir="$1";shift
local dry_run="$1";shift
pushTempDir "${FUNCNAME[0]}_$(fileName "${root_dir}")_XXXXXX"
local tmp_dir="$(getTopTempDir)"
local tmp_file
local return_status=0
# All build references now in json format
readarray -t legacy_files < <(find "${root_dir}" -type f \
-name "build.ref" )
for legacy_file in "${legacy_files[@]}"; do
debug "Checking ${legacy_file}..."
replacement_file="$(filePath "${legacy_file}")/build.json"
[[ -f "${replacement_file}" ]] && continue
info "Upgrading ${legacy_file} ..."
tmp_file="$(getTempFile "build_XXXXXX.json" "${tmp_dir}")"
upgrade_build_ref "${legacy_file}" "${tmp_file}" || { return_status=1; break; }
if [[ -n "${dry_run}" ]]; then
willLog "trace" && { echo; cat "${tmp_file}"; echo; }
continue
fi
cp "${tmp_file}" "${replacement_file}" || { return_status=1; break; }
done
# All shared build references now in json format
if [[ ${return_status} -eq 0 ]]; then
readarray -t legacy_files < <(find "${root_dir}" -type f \
\( -name "*.ref" -and -not -name "build.ref" \) )
for legacy_file in "${legacy_files[@]}"; do
debug "Checking ${legacy_file}..."
replacement_file="$(filePath "${legacy_file}")/shared_build.json"
[[ -f "${replacement_file}" ]] && continue
info "Upgrading ${legacy_file} ..."
tmp_file="$(getTempFile "shared_XXXXXX.json" "${tmp_dir}")"
upgrade_shared_build_ref "${legacy_file}" "${tmp_file}" || { return_status=1; break; }
if [[ -n "${dry_run}" ]]; then
willLog "trace" && { echo; cat "${tmp_file}"; echo; }
continue
fi
cp "${tmp_file}" "${replacement_file}" || { return_status=1; break; }
done
fi
# Strip top level "Credentials" attribute from credentials
if [[ ${return_status} -eq 0 ]]; then
readarray -t legacy_files < <(find "${root_dir}" -type f \
-name "credentials.json" )
for legacy_file in "${legacy_files[@]}"; do
debug "Checking ${legacy_file}..."
if [[ "$(jq ".Credentials | if .==null then [] else [1] end | length" < "${legacy_file}" )" -gt 0 ]]; then
upgrade_needed="true"
info "Upgrading ${legacy_file} ..."
tmp_file="$(getTempFile "credentials_XXXXXX.json" "${tmp_dir}")"
upgrade_credentials "${legacy_file}" "${tmp_file}" || { return_status=1; break; }
if [[ -n "${dry_run}" ]]; then
willLog "trace" && { echo; cat "${tmp_file}"; echo; }
continue
fi
cp "${tmp_file}" "${legacy_file}" || { return_status=1; break; }
fi
done
fi
# Change of naming from "container" to "segment"
if [[ ${return_status} -eq 0 ]]; then
readarray -t legacy_files < <(find "${root_dir}" -type f \
-name "container.json" )
for legacy_file in "${legacy_files[@]}"; do
debug "Checking ${legacy_file}..."
replacement_file="$(filePath "${legacy_file}")/segment.json"
[[ -f "${replacement_file}" ]] && continue
upgrade_needed="true"
info "Upgrading ${legacy_file} ..."
tmp_file="$(getTempFile "container_XXXXXX.json" "${tmp_dir}")"
cp "${legacy_file}" "${tmp_file}" || { return_status=1; break; }
if [[ -n "${dry_run}" ]]; then
willLog "trace" && { echo; cat "${tmp_file}"; echo; }
continue
fi
cp "${legacy_file}" "${replacement_file}" || { return_status=1; break; }
done
fi
popTempDir
return ${return_status}
}
function cleanup_cmdb_repo_to_v1_0_0() {
local root_dir="$1";shift
local dry_run="$1";shift
# All build references now in json format
readarray -t legacy_files < <(find "${root_dir}" -type f \
-name "build.ref" )
for legacy_file in "${legacy_files[@]}"; do
info "${dry_run}Deleting ${legacy_file} ..."
[[ -n "${dry_run}" ]] && continue
git_rm -f "${legacy_file}" || return 1
done
# All shared build references now in json format
readarray -t legacy_files < <(find "${root_dir}" -type f \
\( -name "*.ref" -and -not -name "build.ref" \) )
for legacy_file in "${legacy_files[@]}"; do
info "${dry_run}Deleting ${legacy_file} ..."
[[ -n "${dry_run}" ]] && continue
git_rm -f "${legacy_file}" || return 1
done
# Change of naming from "container" to "segment"
readarray -t legacy_files < <(find "${root_dir}" -type f \
-name "container.json" )
for legacy_file in "${legacy_files[@]}"; do
info "${dry_run}Deleting ${legacy_file} ..."
[[ -n "${dry_run}" ]] && continue
git_rm -f "${legacy_file}" || return 1
done
return 0
}
# Introduce separate environment and segment dirs
function upgrade_cmdb_repo_to_v1_1_0_settings() {
local root_dir="$1";shift
local dry_run="$1";shift
local target_dir="$1";shift