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 pathcreateContainerTemplate.sh
More file actions
executable file
·117 lines (100 loc) · 3.24 KB
/
createContainerTemplate.sh
File metadata and controls
executable file
·117 lines (100 loc) · 3.24 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
#!/bin/bash
function usage() {
echo -e "\nCreate a container 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 (currently \"s3\", \"vpc\" or \"eip\")"
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"
ORGANISATIONFILE="../../organisation.json"
ACCOUNTFILE="../../account.json"
PROJECTFILE="../project.json"
CONTAINERFILE="container.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
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
# Ensure the aws tree for the templates exists
if [[ ! -d ${CF_DIR} ]]; then mkdir -p ${CF_DIR}; fi
TEMPLATE="createContainer.ftl"
TEMPLATEDIR="${BIN}/templates"
if [[ "${SLICE}" != "" ]]; then
ARGS="-v slice=${SLICE}"
OUTPUT="${CF_DIR}/cont-${SLICE}-${REGION}-template.json"
else
ARGS=""
OUTPUT="${CF_DIR}/container-${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 masterData=$BIN/data/masterData.json"
pushd ${CF_DIR} > /dev/null 2>&1
if [[ $(ls cont*-${REGION}-stack.json 2> /dev/null | wc) != 0 ]]; then
STACKCOUNT=0
for f in $( ls cont*-${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
fi
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}