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 pathcreateApplicationTemplate.sh
More file actions
executable file
·192 lines (165 loc) · 5.65 KB
/
createApplicationTemplate.sh
File metadata and controls
executable file
·192 lines (165 loc) · 5.65 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
#!/bin/bash
function usage() {
echo -e "\nCreate an application specific CloudFormation template"
echo -e "\nUsage: $(basename $0) -c CONFIGREFERENCE -s SLICE -d DEPLOYMENT_SLICE"
echo -e "\nwhere\n"
echo -e "(m) -c CONFIGREFERENCE is the id of the configuration (commit id, branch id, tag)"
echo -e "(o) -d DEPLOYMENT_SLICE is the slice of the solution to be used to obtain deployment information"
echo -e " -h shows this text"
echo -e "(o) -s SLICE is the slice of the solution to be included in the template"
echo -e "\nNOTES:\n"
echo -e "1) You must be in the container specific directory when running this script"
echo -e "2) If no DEPLOYMENT_SLICE is provided, SLICE is used to obtain deployment information"
echo -e ""
exit 1
}
# Parse options
while getopts ":c:d:hs:" opt; do
case $opt in
c)
CONFIGREFERENCE=$OPTARG
;;
d)
DEPLOYMENT_SLICE=$OPTARG
;;
h)
usage
;;
s)
SLICE=$OPTARG
;;
\?)
echo -e "\nInvalid option: -$OPTARG"
usage
;;
:)
echo -e "\nOption -$OPTARG requires an argument"
usage
;;
esac
done
# Ensure mandatory parameters have been provided
if [[ "${CONFIGREFERENCE}" == "" ]]; then
echo -e "\nInsufficient arguments"
usage
fi
BIN="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
OAID="$(basename $(cd $BIN/../..;pwd))"
PID="$(basename $(cd ..;pwd))"
CONTAINER="$(basename $(pwd))"
ROOT_DIR="$(cd $BIN/../..;pwd)"
AWS_DIR="${ROOT_DIR}/infrastructure/aws"
PROJECT_DIR="${AWS_DIR}/${PID}"
CONTAINER_DIR="${PROJECT_DIR}/${CONTAINER}"
CF_DIR="${CONTAINER_DIR}/cf"
CREDS_DIR="${ROOT_DIR}/infrastructure/credentials"
ACCOUNT_CREDS_DIR="${CREDS_DIR}/${OAID}"
PROJECT_CREDS_DIR="${CREDS_DIR}/${PID}"
CONTAINER_CREDS_DIR="${PROJECT_CREDS_DIR}/${CONTAINER}"
DEPLOYMENTS_DIR="${ROOT_DIR}/config/deployments"
PROJECT_DEPLOY_DIR="${DEPLOYMENTS_DIR}/${PID}"
CONTAINER_DEPLOY_DIR="${PROJECT_DEPLOY_DIR}/${CONTAINER}"
DEPLOY_DIR="${CONTAINER_DEPLOY_DIR}"
if [[ "${DEPLOYMENT_SLICE}" != "" ]]; then
DEPLOY_DIR="${DEPLOY_DIR}/${DEPLOYMENT_SLICE}"
else
if [[ "${SLICE}" != "" ]]; then
DEPLOY_DIR="${DEPLOY_DIR}/${SLICE}";
fi
fi
ORGANISATIONFILE="../../organisation.json"
ACCOUNTFILE="../../account.json"
PROJECTFILE="../project.json"
CONTAINERFILE="container.json"
CREDENTIALSFILE="${CONTAINER_CREDS_DIR}/credentials.json"
ACCOUNTCREDENTIALSFILE="${ACCOUNT_CREDS_DIR}/credentials.json"
if [[ -f solution.json ]]; then
SOLUTIONFILE="solution.json"
else
SOLUTIONFILE="../solution.json"
fi
if [[ ! -f ${CONTAINERFILE} ]]; then
echo -e "\nNo \"${CONTAINERFILE}\" file in current directory. Are we in a container directory? Nothing to do."
usage
fi
if [[ -d ${DEPLOY_DIR} ]]; then
BUILDFILE="${DEPLOY_DIR}/build.ref"
CONFIGURATIONFILE="${DEPLOY_DIR}/config.json"
if [[ ! -f ${CONFIGURATIONFILE} ]]; then
echo -e "\nNo \"${CONFIGURATIONFILE}\" file present. Assuming no deployment configuration required.\n"
CONFIGURATIONFILE=
fi
if [[ ! -f ${BUILDFILE} ]]; then
echo -e "\nNo \"${BUILDFILE}\" file present. Assuming no build reference required.\n"
else
BUILDREFERENCE=$(cat ${BUILDFILE})
fi
else
echo -e "\nNo \"${DEPLOY_DIR}\" directory present. Assuming no deployment information required.\n"
fi
if [[ -e ${ACCOUNTFILE} ]]; then
ACCOUNTREGION=$(grep '"Region"' ${ACCOUNTFILE} | cut -d '"' -f 4)
fi
if [[ "${ACCOUNTREGION}" == "" ]]; then
echo -e "\nThe account region must be defined in the account configuration file."
echo -e "Are we in the correct directory? Nothing to do."
usage
fi
REGION=$(grep '"Region"' ${CONTAINERFILE} | cut -d '"' -f 4)
if [[ "${REGION}" == "" && -e ${SOLUTIONFILE} ]]; then
REGION=$(grep '"Region"' ${SOLUTIONFILE} | cut -d '"' -f 4)
fi
if [[ "${REGION}" == "" && -e ${ACCOUNTFILE} ]]; then
REGION=$(grep '"Region"' ${ACCOUNTFILE} | cut -d '"' -f 4)
fi
if [[ "${REGION}" == "" ]]; then
echo -e "\nThe region must be defined in the container/solution/account configuration files (in this preference order)."
echo -e "Are we in the correct directory? Nothing to do."
usage
fi
if [[ ! -d ${CF_DIR} ]]; then mkdir -p ${CF_DIR}; fi
TEMPLATE="createApplication.ftl"
if [[ -f ${TEMPLATE} ]]; then
TEMPLATEDIR="./"
else
TEMPLATEDIR="../"
fi
if [[ "${SLICE}" != "" ]]; then
ARGS="-v slice=${SLICE}"
OUTPUT="${CF_DIR}/app-${SLICE}-${REGION}-template.json"
else
ARGS=""
OUTPUT="${CF_DIR}/application-${REGION}-template.json"
fi
ARGS="${ARGS} -v organisation=${ORGANISATIONFILE}"
ARGS="${ARGS} -v account=${ACCOUNTFILE}"
ARGS="${ARGS} -v project=${PROJECTFILE}"
ARGS="${ARGS} -v solution=${SOLUTIONFILE}"
ARGS="${ARGS} -v container=${CONTAINERFILE}"
ARGS="${ARGS} -v credentials=${CREDENTIALSFILE}"
ARGS="${ARGS} -v accountCredentials=${ACCOUNTCREDENTIALSFILE}"
ARGS="${ARGS} -v masterData=$BIN/data/masterData.json"
if [[ "${BUILDREFERENCE}" != "" ]]; then
ARGS="${ARGS} -v \"buildReference=${BUILDREFERENCE}\""
fi
ARGS="${ARGS} -v configurationReference=$CONFIGREFERENCE"
if [[ "${CONFIGURATIONFILE}" != "" ]]; then
ARGS="${ARGS} -v configuration=${CONFIGURATIONFILE}"
fi
pushd ${CF_DIR} > /dev/null 2>&1
STACKCOUNT=0
for f in $( ls cont*-${REGION}-stack.json sol*-${REGION}-stack.json 2> /dev/null); do
PREFIX=$(echo $f | awk -F "-${REGION}-stack.json" '{print $1}' | sed 's/-//g')
ARGS="${ARGS} -v ${PREFIX}Stack=${CF_DIR}/${f}"
if [[ ${STACKCOUNT} > 0 ]]; then
STACKS="${STACKS},"
fi
STACKS="${STACKS}\\\\\\\"${PREFIX}Stack\\\\\\\""
STACKCOUNT=${STACKCOUNT}+1
done
popd > /dev/null 2>&1
ARGS="${ARGS} -v stacks=[${STACKS}]"
CMD="${BIN}/gsgen.sh -t $TEMPLATE -d $TEMPLATEDIR -o $OUTPUT $ARGS"
eval $CMD
EXITSTATUS=$?
exit ${EXITSTATUS}