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 pathaddSegment.sh
More file actions
executable file
·163 lines (148 loc) · 5.4 KB
/
addSegment.sh
File metadata and controls
executable file
·163 lines (148 loc) · 5.4 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
#!/bin/bash
if [[ -n "${GENERATION_DEBUG}" ]]; then set ${GENERATION_DEBUG}; fi
trap '. ${GENERATION_DIR}/cleanupContext.sh; exit ${RESULT:-1}' EXIT SIGHUP SIGINT SIGTERM
function usage() {
echo -e "\nAdd a new segment"
echo -e "\nUsage: $(basename $0) -l TITLE -n SEGMENT -d DESCRIPTION -s SID -e EID -o DOMAIN -r AWS_REGION -u"
echo -e "\nwhere\n"
echo -e "(o) -d DESCRIPTION is the segment description"
echo -e "(o) -e EID is the ID of the environment of which this segment is part"
echo -e " -h shows this text"
echo -e "(o) -l TITLE is the segment title"
echo -e "(m) -n SEGMENT is the human readable form (one word, lowercase and no spaces) of the segment id"
echo -e "(o) -o DOMAIN is the default DNS domain to be used for the segment"
echo -e "(o) -r AWS_REGION is the default AWS region for the segment"
echo -e "(o) -s SID is the segment id"
echo -e "(o) -u if details should be updated"
echo -e "\nDEFAULTS (creation only):\n"
echo -e "EID=SID"
echo -e "SID=EID"
echo -e "TITLE from environment master data for EID"
echo -e "\nNOTES:\n"
echo -e "1. Subdirectories are created in the config and infrastructure subtrees"
echo -e "2. The segment information is saved in the segment profile"
echo -e "3. To update the details, the update option must be explicitly set"
echo -e "4. The environment must exist in the masterData"
echo -e "5. EID or SID are required if creating a segment"
echo -e ""
exit
}
# Parse options
while getopts ":d:e:hl:n:o:r:s:u" opt; do
case $opt in
d)
DESCRIPTION="${OPTARG}"
;;
e)
EID="${OPTARG}"
;;
h)
usage
;;
l)
TITLE="${OPTARG}"
;;
n)
SEGMENT="${OPTARG}"
;;
o)
DOMAIN="${OPTARG}"
;;
r)
AWS_REGION="${OPTARG}"
;;
s)
SID="${OPTARG}"
;;
u)
UPDATE_SEGMENT="true"
;;
\?)
echo -e "\nInvalid option: -${OPTARG}"
usage
;;
:)
echo -e "\nOption -${OPTARG} requires an argument"
usage
;;
esac
done
# Ensure mandatory arguments have been provided
if [[ (-z "${SEGMENT}") ]]; then
echo -e "\nInsufficient arguments"
usage
fi
# Set up the context
. ${GENERATION_DIR}/setContext.sh
# Ensure we are in the root of the product tree
if [[ ! ("product" =~ ${LOCATION}) ]]; then
echo -e "\nWe don't appear to be in the product directory. Are we in the right place?"
usage
fi
# Create the directories for the segment
SEGMENT_SOLUTIONS_DIR="${SOLUTIONS_DIR}/${SEGMENT}"
SEGMENT_APPSETTINGS_DIR="${APPSETTINGS_DIR}/${SEGMENT}"
SEGMENT_CREDENTIALS_DIR="${CREDENTIALS_DIR}/${SEGMENT}"
mkdir -p ${SEGMENT_SOLUTIONS_DIR}
if [[ ! -d ${SEGMENT_APPSETTINGS_DIR} ]]; then
mkdir -p ${SEGMENT_APPSETTINGS_DIR}
echo "{}" > ${SEGMENT_APPSETTINGS_DIR}/appsettings.json
fi
mkdir -p ${SEGMENT_CREDENTIALS_DIR}
# Check whether the segment profile is already in place
SEGMENT_PROFILE=${SEGMENT_SOLUTIONS_DIR}/segment.json
if [[ -f ${SEGMENT_PROFILE} ]]; then
if [[ "${UPDATE_SEGMENT}" != "true" ]]; then
echo -e "\nSegment profile already exists. Maybe try using update option?"
usage
fi
else
if [[ (-z "${EID}") && (-z "${SID}") ]]; then
echo -e "\nOne of EID and SID required for segment creation"
usage
fi
echo "{\"Segment\":{}}" > ${SEGMENT_PROFILE}
EID=${EID:-${SID}}
SID=${SID:-${EID}}
ENVIRONMENT_TITLE=$(cat ${COMPOSITE_BLUEPRINT} | jq -r ".Environments[\"${EID}\"].Title | select(.!=null)")
if [[ -z "${ENVIRONMENT_TITLE}" ]]; then
echo -e "\nEnvironment not defined in masterData.json. Was SID or EID provided?"
usage
fi
TITLE=${TITLE:-$ENVIRONMENT_TITLE}
fi
# Generate the filter
CERTIFICATE_ID="${PRODUCT}-${SEGMENT}"
FILTER="."
if [[ -n "${SID}" ]]; then FILTER="${FILTER} | .Segment.Id=\$SID"; fi
if [[ -n "${SEGMENT}" ]]; then FILTER="${FILTER} | .Segment.Name=\$SEGMENT"; fi
if [[ -n "${TITLE}" ]]; then FILTER="${FILTER} | .Segment.Title=\$TITLE"; fi
if [[ -n "${DESCRIPTION}" ]]; then FILTER="${FILTER} | .Segment.Description=\$DESCRIPTION"; fi
if [[ -n "${EID}" ]]; then FILTER="${FILTER} | .Segment.Environment=\$EID"; fi
if [[ -n "${AWS_REGION}" ]]; then FILTER="${FILTER} | .Product.Region=\$AWS_REGION"; fi
if [[ -n "${DOMAIN}" ]]; then FILTER="${FILTER} | .Product.Domain.Stem=\$DOMAIN"; fi
if [[ -n "${DOMAIN}" ]]; then FILTER="${FILTER} | .Product.Domain.Certificate.Id=\$CERTIFICATE_ID"; fi
# Generate the segment profile
cat ${SEGMENT_PROFILE} | jq --indent 4 \
--arg SID "${SID}" \
--arg SEGMENT "${SEGMENT}" \
--arg TITLE "${TITLE}" \
--arg DESCRIPTION "${DESCRIPTION}" \
--arg EID "${EID}" \
--arg AWS_REGION "${AWS_REGION}" \
--arg DOMAIN "${DOMAIN}" \
--arg CERTIFICATE_ID "${CERTIFICATE_ID}" \
"${FILTER}" > ${SEGMENT_SOLUTIONS_DIR}/temp_segment.json
RESULT=$?
if [[ ${RESULT} -eq 0 ]]; then
mv ${SEGMENT_SOLUTIONS_DIR}/temp_segment.json ${SEGMENT_SOLUTIONS_DIR}/segment.json
else
echo -e "\nError creating segment profile"
exit
fi
# Provide an empty credentials profile for the segment
if [[ ! -f ${SEGMENT_CREDENTIALS_DIR}/credentials.json ]]; then
echo "{\"Credentials\" : {}}" | jq --indent 4 '.' > ${SEGMENT_CREDENTIALS_DIR}/credentials.json
fi
# All good
RESULT=0