-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathtemplatize.sh
executable file
·174 lines (161 loc) · 4.85 KB
/
templatize.sh
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
#!/bin/bash
PROJECT_ROOT_DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
# Default values
CLOUD="${CLOUD:-public}"
REGION="${REGION:-westus3}"
CXSTAMP="${CXSTAMP:-1}"
EXTRA_ARGS=""
PIPELINE_MODE="inspect"
DRY_RUN=""
# Function to display usage
usage() {
echo "Usage: $0 deploy_env input output [-c cloud] [-r region] [-x cxstamp] [-e]"
echo " deploy_env Deployment environment"
echo " input Optional input file"
echo " output Optional output file"
echo " -d Dry run"
echo " -i Set the input file same as second arg"
echo " -o Set the output file same as third arg"
echo " -c Set the cloud (default: public)"
echo " -r Set the region (default: westus3)"
echo " -x Set the cxstamp (default: 1)"
echo " -e Extra args for config interpolation"
echo " -p Pipeline to inspect"
echo " -s Pipeline step to inspect"
exit 1
}
# Check if at least one positional argument is provided
if [ "$#" -lt 1 ]; then
usage
fi
# Positional arguments
DEPLOY_ENV=$1
shift
if [ "$#" -ge 1 ] && [[ ! "$1" =~ ^- ]]; then
INPUT=$1
shift
fi
if [ "$#" -ge 1 ] && [[ ! "$1" =~ ^- ]]; then
OUTPUT=$1
shift
fi
# Parse optional flags
while getopts "c:dr:x:e:i:o:p:P:s:" opt; do
case ${opt} in
c)
CLOUD=${OPTARG}
;;
d)
DRY_RUN="--dry-run"
;;
r)
REGION=${OPTARG}
;;
x)
CXSTAMP=${OPTARG}
;;
e)
EXTRA_ARGS="--extra-args ${OPTARG}"
;;
i)
INPUT=${OPTARG}
;;
o)
OUTPUT=${OPTARG}
;;
p)
PIPELINE=${OPTARG}
;;
P)
PIPELINE_MODE=${OPTARG}
;;
s)
PIPELINE_STEP=${OPTARG}
;;
*)
usage
;;
esac
done
# Read region name from our sanitized serviceconfig.json and returns the region short name
REGION_SHORT=$(
"${PROJECT_ROOT_DIR}/tooling/templatize/serviceconfig-get-region-short-name.sh" "${REGION}"
)
if [ -z "$REGION_SHORT" ]; then
echo "Failed to get region short name for region: $REGION" >&2
fi
if [ "$DEPLOY_ENV" == "personal-dev" ]; then
REGION_STAMP="${REGION_SHORT}${USER:0:4}"
elif [ "$DEPLOY_ENV" == "nightly" ]; then
REGION_STAMP="nightly"
elif [ "$DEPLOY_ENV" == "personal-perfscale" ]; then
REGION_STAMP="${REGION_SHORT}p${USER:0:4}"
elif [ "$DEPLOY_ENV" == "dev" ] || [ "$DEPLOY_ENV" == "cs-pr" ]; then
CLEAN_DEPLOY_ENV=$(echo "${DEPLOY_ENV}" | tr -cd '[:alnum:]')
REGION_STAMP="${CLEAN_DEPLOY_ENV}"
else
REGION_STAMP=${REGION_SHORT}
fi
make -s -C ${PROJECT_ROOT_DIR}/tooling/templatize templatize
TEMPLATIZE="${PROJECT_ROOT_DIR}/tooling/templatize/templatize"
PERSIST_FLAG=""
if [ -z "$PERSIST" ] || [ "$PERSIST" == "false" ]; then
PERSIST_FLAG="--no-persist-tag"
fi
CONFIG_FILE=${CONFIG_FILE:-${PROJECT_ROOT_DIR}/config/config.yaml}
if [ -n "$INPUT" ] && [ -n "$OUTPUT" ]; then
$TEMPLATIZE generate \
--config-file=${CONFIG_FILE} \
--cloud=${CLOUD} \
--deploy-env=${DEPLOY_ENV} \
--region=${REGION} \
--region-short=${REGION_STAMP} \
--stamp=${CXSTAMP} \
--input=${INPUT} \
--output=${OUTPUT} \
${EXTRA_ARGS}
elif [ $PIPELINE_MODE == "inspect" ] && [ -n "$PIPELINE" ] && [ -n "$PIPELINE_STEP" ]; then
$TEMPLATIZE pipeline inspect \
--config-file=${CONFIG_FILE} \
--cloud=${CLOUD} \
--deploy-env=${DEPLOY_ENV} \
--region=${REGION} \
--region-short=${REGION_STAMP} \
--stamp=${CXSTAMP} \
--pipeline-file=${PIPELINE} \
--step=${PIPELINE_STEP} \
--scope vars \
--format makefile
elif [ $PIPELINE_MODE == "run" ] && [ -n "$PIPELINE" ] && [ -n "$PIPELINE_STEP" ]; then
$TEMPLATIZE pipeline run \
--config-file=${CONFIG_FILE} \
--cloud=${CLOUD} \
--deploy-env=${DEPLOY_ENV} \
--region=${REGION} \
--region-short=${REGION_STAMP} \
--stamp=${CXSTAMP} \
--pipeline-file=${PIPELINE} \
--step=${PIPELINE_STEP} \
${PERSIST_FLAG} \
${DRY_RUN}
elif [ $PIPELINE_MODE == "run" ] && [ -n "$PIPELINE" ]; then
$TEMPLATIZE pipeline run \
--config-file=${CONFIG_FILE} \
--cloud=${CLOUD} \
--deploy-env=${DEPLOY_ENV} \
--region=${REGION} \
--region-short=${REGION_STAMP} \
--stamp=${CXSTAMP} \
--pipeline-file=${PIPELINE} \
${PERSIST_FLAG} \
${DRY_RUN}
else
$TEMPLATIZE inspect \
--config-file=${CONFIG_FILE} \
--cloud=${CLOUD} \
--deploy-env=${DEPLOY_ENV} \
--region=${REGION} \
--region-short=${REGION_STAMP} \
--stamp=${CXSTAMP} \
${EXTRA_ARGS}
fi