forked from keptn-sandbox/keptn-on-k3s
-
Notifications
You must be signed in to change notification settings - Fork 2
/
create-keptn-project-from-template.sh
executable file
·280 lines (235 loc) · 12.1 KB
/
create-keptn-project-from-template.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env bash
# Usage: create-keptn-project-from-template.sh project-template-folder [email protected] new-project-name
# create-keptn-project-from-template.sh quality-gate-dynatrace [email protected] quality-gate
# Here is a sample project-template-folder folder structure with the explanation what will happen
# project-template-folder\
# shipyard.yaml --> this is the only MANDATORY file. it will be the shipyard for keptn create project
# dynatrace\
# dynatrace.conf.yaml --> will be uploaded to the project level
# service_demo --> if a foler with service_ exists it will either create or onboard a new service depending on whether there is a subfolder charts
# charts --> if the charts subfolder exists it will do a keptn onboard service demo --charts=./charts otherwise keptn create service
# stage_quality-gate --> this folder will be iterated and content will be uploaded to the quality-gate stage
# dynatrace\
# dynatrace.conf.yaml --> this will now be a stage specific file
# demo\
# jmeter\
# jmeter.conf.yaml --> this file will be a jmeter.conf.yaml specific to the demo service in the quality-gate stage
# The files in the repo can contain certain PLACEHOLDERS which will be replaced before uploaded.
# The replacement happens in .tmp files - so - no original files will be changed. Here the list of REPLACE options
# REPLACE_KEPTN_BRIDGE with -> KEPTN_BRIDGE_PROJECT_ESCAPED
# REPLACE_OWNER_EMAIL with -> OWNER_EMAIL
# REPLACE_KEPTN_INGRESS with -> KEPTN_INGRESS
# REPLACE_KEPTN_STAGING_INGRESS with -> KEPTN_STAGING_INGRESS
# REPLACE_KEPTN_PRODUCTION_INGRESS with -> KEPTN_PRODUCTION_INGRESS
# REPLACE_SYNTHETIC_LOCATION with -> SYNTHETIC_LOCATION (defaults to GEOLOCATION-45AB48D9D6925ECC)
# REPLACE_KEPTN_PROJECT with -> Keptn Project Name
# default template project directory
TEMPLATE_DIRECTORY="keptn_project_templates"
# Parameters for Script - they have to be passed to the script!
TEMPLATE_NAME=${1:-none}
OWNER_EMAIL=${2:-none}
PROJECT_NAME=${3:-none}
SYNTHETIC_LOCATION=${SYNTHETIC_LOCATION:-GEOLOCATION-45AB48D9D6925ECC}
# INSTANCE_COUNT_XXX=${4:-1}
INSTANCE_ARRAY_FILE=${4:-none}
if [[ "$INSTANCE_ARRAY_FILE" == "none" ]]; then
INSTANCE_ARRAY=(AAAA)
else
source $4
fi
# Expected Env Variables that should be set!
# KEPTN_ENDPOINT="https://keptn.yourkeptndomain.abc
# KEPTN_INGRESS="yourkeptndomain.abc"
KEPTN_BRIDGE_PROJECT="${KEPTN_ENDPOINT}/bridge/project/${PROJECT_NAME}"
KEPTN_BRIDGE_PROJECT_ESCAPED="${KEPTN_BRIDGE_PROJECT//\//\\/}"
if [[ "$TEMPLATE_NAME" == "none" ]]; then
echo "You have to set TEMPLATE_NAME to a template keptn project name such as quality-gate-dynatrace. You find all available templates in the ${TEMPLATE_DIRECTORY} directory"
echo "Usage: $0 project-template-folder [email protected] new-project-name"
echo "Example: $0 quality-gate-dynatrace [email protected] quality-gate"
exit 1
fi
if [[ "$OWNER_EMAIL" == "none" ]]; then
echo "You have to set OWNER_EMAIL to a valid email as this might be used in e.g: Dynatrace Dashboards .."
echo "Usage: $0 project-template-folder [email protected] new-project-name"
echo "Example: $0 quality-gate-dynatrace [email protected] quality-gate"
exit 1
fi
if [[ "$PROJECT_NAME" == "none" ]]; then
echo "You have to set PROJECT_NAME to the project name you want to create based on the template"
echo "Usage: $0 project-template-folder [email protected] new-project-name"
echo "Example: $0 quality-gate-dynatrace [email protected] quality-gate"
exit 1
fi
if [[ "$KEPTN_ENDPOINT" == "" ]]; then
echo "You have to export KEPTN_ENDPOINT and set it to your Keptn Endpoint URL, e.g: https://keptn.yourkeptndomain.com"
echo "Its needed when e.g: creating Dynatrace dashboards that point back to the Keptn Bridge"
exit 1
fi
if [[ "$KEPTN_INGRESS" == "" ]]; then
echo "You have to export KEPTN_INGRESS and set it to your Ingress host , e.g: yourkeptndomain.com"
echo "Its needed when e.g: creating Dynatrace dashboards that point back to the Keptn Bridge"
exit 1
fi
if [[ "$KEPTN_STAGING_INGRESS" == "" ]]; then
echo "Default KEPTN_STAGING_INGRESS to KEPTN_INGRESS($KEPTN_INGRESS)"
KEPTN_STAGING_INGRESS="$KEPTN_INGRESS"
fi
if [[ "$KEPTN_PRODUCTION_INGRESS" == "" ]]; then
echo "Default KEPTN_PRODUCTION_INGRESS to KEPTN_INGRESS($KEPTN_INGRESS)"
KEPTN_PRODUCTION_INGRESS="$KEPTN_INGRESS"
fi
## Now - lets validate if all tools are installed that are needed
if ! [ -x "$(command -v tree)" ]; then
echo "Tree command not installed. This is required"
exit 1
fi
if ! [ -x "$(command -v keptn)" ]; then
echo "Keptn CLI is not installed. This is required"
exit 1
fi
## Validate that keptn cli is successfull authenticated
if keptn status | grep -q "Successfully authenticated"; then
echo "Keptn CLI connected successfully!"
else
echo "Keptn CLI not authenticated. Please authenticate first and then run script again"
exit 1
fi
## Validate that the keptn project doesnt already exist
if keptn get project $PROJECT_NAME | grep -q "No project"; then
echo "Validated that Keptn Project $PROJECT_NAME doesnt yet exist. Continue creation of project"
else
echo "Keptn Project $PROJECT_NAME already exists. Please specify a different project name of delete existing project first"
exit 1
fi
## Validate that there is a template directory that we can use to create projects from
if ! [ -d "${TEMPLATE_DIRECTORY}" ]; then
echo "Can't find Keptn Project Template: ${TEMPLATE_DIRECTORY}"
exit 1
fi
## Validate that the specific project template exists
if ! [ -d "${TEMPLATE_DIRECTORY}/${TEMPLATE_NAME}" ]; then
echo "Can't find Keptn Project Template: ${TEMPLATE_DIRECTORY}/${TEMPLATE_NAME}"
exit 1
fi
## Validate that the specific project template has a shipyard
if ! [ -f "${TEMPLATE_DIRECTORY}/${TEMPLATE_NAME}/shipyard.yaml" ]; then
echo "Keptn Project Template doesnt have a shipyard.yaml: ${TEMPLATE_DIRECTORY}/${TEMPLATE_NAME}/shipyard.yaml"
exit 1
fi
## switch to template directory
currDir=$(pwd)
echo "Switching to Template Directory"
cd "${TEMPLATE_DIRECTORY}/${TEMPLATE_NAME}"
#
# Now lets create that Keptn project
#
echo "Create Keptn Project: ${PROJECT_NAME} from ${TEMPLATE_NAME}"
keptn create project "${PROJECT_NAME}" --shipyard=./shipyard.yaml
#
# Validate that project was created
if keptn get project $PROJECT_NAME | grep -q "No project"; then
echo "Create Project failed or not finished yet. Waiting for 5 seconds and trying this again."
sleep 5
if keptn get project $PROJECT_NAME | grep -q "No project"; then
echo "Create Project failed. Aborting creation of project. Please check your Keptn installation"
exit 1
fi
fi
#
# Now we iterate through the template folder and add all resources
#
for localFileName in $(tree -i -f)
do
# if this is a directory we ignore it
if [ -d "$localFileName" ]; then continue; fi
# if this is a service directory and we found the servicedir.txt then we create or onboard the service
if [[ "${localFileName}" == *"servicedir.txt"* ]]; then
# lets check whether its a service_
if [[ "${localFileName}" == *"/service_"* ]]; then
RESOURCE_SERVICE_NAME=$(echo "${localFileName##*/service_}")
SERVICE_NAME=$(echo "${RESOURCE_SERVICE_NAME%%/*}")
# take into consideration that we may need to create multiple service instances if it contains the XXX placeholder
instanceCount=1
if [[ "$SERVICE_NAME" == *"XXX"* ]]; then
# instanceCount=${INSTANCE_COUNT_XXX}
instanceCount=${#INSTANCE_ARRAY[@]}
fi
# now either create a single or multiple instances
for (( instanceIx=0; instanceIx<instanceCount; instanceIx++ ))
do
INSTANCE_NAME=${INSTANCE_ARRAY[$instanceIx]}
INSTANCE_SERVICE_NAME=$(echo "${SERVICE_NAME//XXX/$INSTANCE_NAME}")
# INSTANCE_SERVICE_NAME=$(echo "${SERVICE_NAME//XXX/$instanceIx}")
if [ -d "./service_${SERVICE_NAME}/charts" ]; then
echo "Onboard Keptn Service: ${INSTANCE_SERVICE_NAME} for project ${PROJECT_NAME} with provided helm charts"
keptn onboard service $INSTANCE_SERVICE_NAME --project="${PROJECT_NAME}" --chart="./service_${SERVICE_NAME}/charts"
else
echo "Create Keptn Service: ${INSTANCE_SERVICE_NAME} for project ${PROJECT_NAME}"
keptn create service $INSTANCE_SERVICE_NAME --project="${PROJECT_NAME}"
fi
done
fi;
continue;
fi
# Lets validate that the file is actually a file!
if ! [ -f "$localFileName" ]; then continue; fi
# Any other file within a service_SERVICENAME folder will be ignored. if you need to upload files for a service simply put it in the stage_STAGENAME folder
if [[ "${localFileName}" == *"/service_"* ]]; then continue; fi
# we are not re-uploading the shipyard.yaml and we ignore the emptydir.txt
if [[ "${localFileName}" == *"shipyard.yaml"* ]]; then continue; fi
# Validate if the file is in stage_STAGENAME directory. If so set STAGE_NAME lets remove that directory for the uploaded resourceUri
RESOURCE_STAGE_NAME=""
REMOVE_FROM_REMOTE_FILENAME=""
if [[ "${localFileName}" == *"/stage_"* ]]; then
RESOURCE_STAGE_NAME=$(echo "${localFileName##*/stage_}")
RESOURCE_STAGE_NAME=$(echo "${RESOURCE_STAGE_NAME%%/*}")
REMOVE_FROM_REMOTE_FILENAME="stage_${RESOURCE_STAGE_NAME}"
echo $REMOVE_FROM_REMOTE_FILENAME
fi
#
# create a tmp file so we can do any IN-FILE replacements
cp ${localFileName} ${localFileName}.tmp
sed -i "s/REPLACE_KEPTN_BRIDGE/${KEPTN_BRIDGE_PROJECT_ESCAPED}/" ${localFileName}.tmp
sed -i "s/REPLACE_OWNER_EMAIL/${OWNER_EMAIL}/" ${localFileName}.tmp
sed -i "s/REPLACE_KEPTN_INGRESS/${KEPTN_INGRESS}/" ${localFileName}.tmp
sed -i "s/REPLACE_KEPTN_STAGING_INGRESS/${KEPTN_STAGING_INGRESS}/" ${localFileName}.tmp
sed -i "s/REPLACE_KEPTN_PRODUCTION_INGRESS/${KEPTN_PRODUCTION_INGRESS}/" ${localFileName}.tmp
sed -i "s/REPLACE_KEPTN_PROJECT/${PROJECT_NAME}/" ${localFileName}.tmp
sed -i "s/REPLACE_SYNTHETIC_LOCATION/${SYNTHETIC_LOCATION}/" ${localFileName}.tmp
#
# Create remote file name, e.g: replace any filename placeholders and remove leading ./
remoteFileName=$(echo "${localFileName/.\//}")
remoteFileName=$(echo "${remoteFileName/$REMOVE_FROM_REMOTE_FILENAME/}")
remoteFileName=$(echo "${remoteFileName/KEPTN_PROJECT/$PROJECT_NAME}")
remoteFileName=$(echo "${remoteFileName/KEPTN_STAGE/$STAGE_NAME}")
remoteFileName=$(echo "${remoteFileName/KEPTN_SERVICE/$SERVICE_NAME}")
# echo "Processing localFileName: ${localFileName}"
# echo "Using remoteFileName: ${remoteFileName}"
# Lets upload our file
# take into consideration that we may need to create multiple service instances if it contains the XXX placeholder
instanceCount=1
if [[ "$remoteFileName" == *"XXX"* ]]; then
# instanceCount=${INSTANCE_COUNT_XXX}
instanceCount=${#INSTANCE_ARRAY[@]}
fi
# now either create a single or multiple instances
for (( instanceIx=0; instanceIx<instanceCount; instanceIx++ ))
do
INSTANCE_NAME=${INSTANCE_ARRAY[$instanceIx]}
# replace XXX in the remote file name
# remoteFileInstanceName=$(echo "${remoteFileName//XXX/$instanceIx}")
remoteFileInstanceName=$(echo "${remoteFileName//XXX/$INSTANCE_NAME}")
# replace any occurance in a special tmp.xxx file
cp ${localFileName}.tmp ${localFileName}.tmp.xxx
# sed -i "s/XXX/${instanceIx}/" ${localFileName}.tmp.xxx
sed -i "s/XXX/${INSTANCE_NAME}/" ${localFileName}.tmp.xxx
# adding the file
keptn add-resource --project="${PROJECT_NAME}" --stage="${RESOURCE_STAGE_NAME}" --resource="${localFileName}.tmp.xxx" --resourceUri="${remoteFileInstanceName}"
# remove the tmp.xxx file
rm ${localFileName}.tmp.xxx
done
# remove tmp file
rm ${localFileName}.tmp
done
# switch back to prev working dir
cd "${currDir}"