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 pathcreateSolutionTemplate.sh
More file actions
executable file
·131 lines (110 loc) · 3.59 KB
/
createSolutionTemplate.sh
File metadata and controls
executable file
·131 lines (110 loc) · 3.59 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
#!/bin/bash
function usage() {
echo -e "\nCreate a solution specific CloudFormation template"
echo -e "\nUsage: $(basename $0) -s SLICE"
echo -e "\nwhere\n"
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 ""
exit 1
}
# Parse options
while getopts ":hs:" opt; do
case $opt in
h)
usage
;;
s)
SLICE=$OPTARG
;;
\?)
echo -e "\nInvalid option: -$OPTARG"
usage
;;
:)
echo -e "\nOption -$OPTARG requires an argument"
usage
;;
esac
done
BIN="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && 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"
PROJECT_CREDS_DIR=${CREDS_DIR}/${PID}
CONTAINER_CREDS_DIR=${PROJECT_CREDS_DIR}/${CONTAINER}
ORGANISATIONFILE="../../organisation.json"
ACCOUNTFILE="../../account.json"
PROJECTFILE="../project.json"
CONTAINERFILE="container.json"
CREDENTIALSFILE="${CONTAINER_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 [[ -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="createSolution.ftl"
TEMPLATEDIR="${BIN}/templates"
if [[ "${SLICE}" != "" ]]; then
ARGS="-v slice=${SLICE}"
OUTPUT="${CF_DIR}/soln-${SLICE}-${REGION}-template.json"
else
ARGS=""
OUTPUT="${CF_DIR}/solution-${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 masterData=$BIN/data/masterData.json"
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}