-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfigure-cat.bash
More file actions
306 lines (265 loc) · 8.99 KB
/
configure-cat.bash
File metadata and controls
306 lines (265 loc) · 8.99 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
# CONSTANTS
SONARQUBE_URL="http://localhost:9000"
##
# Parameters :
# - 1 : status
# - 2 : errors
#
log_error(){
msg="[ERROR] docker-cat, $1 due to : $2."
echo ${msg}
}
##
# Parameters :
# - 1 : status
# - 2 : errors
#
log_warning(){
msg="[WARNING] docker-cat, $1 due to : $2."
echo ${msg}
}
##
# Parameters
# - 1 : status
log_info(){
msg="[INFO] docker-cat, $1."
echo ${msg}
}
#
# Parameters (not optional):
# - 1 : metric_name
# - 2 : metric_key
# - 3 : metric_operator [EQ,NE, LT or GT]
# - 4 : gate_id
# - 5 : metric's error threshold ("none" if not to set)
add_condition(){
metric_name=$1
metric_key=$2
metric_operator=$3
gate_id=$4
metric_errors=$5
log_info "adding CNES quality gate condition : ${metric_name} ${metric_operator} thresholds : [ errors: ${metric_errors} ]"
if [ "${metric_errors}" != "none" ]
then
threshold="&error=${metric_errors}"
fi
echo "threshold=${threshold}"
RES=$(curl -su admin:admin -X POST "${SONARQUBE_URL}/api/qualitygates/create_condition?gateId=${gate_id}&metric=${metric_key}&op=${metric_operator}${threshold}")
if [ "$(echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "metric $metric_name condition succesfully added."
else
log_warning "impossible to add $metric_name condition" "$( echo ${RES} | jq '.errors[].msg' )"
fi
}
create_quality_gates(){
FILE=$1
NAME=$(jq -r '.name' "$FILE")
log_info "creating '$NAME' quality gate."
res=$(curl -su "admin:admin" \
--data-urlencode "name=$NAME" \
"${SONARQUBE_URL}/api/qualitygates/create")
if [ "$(echo "${res}" | jq '(.errors | length)')" == "0" ]
then
log_info "successfully created '$NAME' quality gate... now configuring it."
else
log_warning "impossible to create quality gate" "$(echo "${res}" | jq '.errors[].msg')"
fi
# Retrieve CNES quality gates ID
log_info "retrieving '$NAME' quality gate ID."
res=$(curl -su "admin:admin" \
-G \
--data-urlencode "name=$NAME" \
"${SONARQUBE_URL}/api/qualitygates/show")
if [ "$(echo "${res}" | jq '(.errors | length)')" == "0" ]
then
GATEID="$(echo "${res}" | jq -r '.id')"
log_info "successfully retrieved quality gate ID (ID=$GATEID)."
else
log_error "impossible to reach quality gate ID" "$(echo "${res}" | jq '.errors[].msg')"
fi
# Setting it as default quality gate
if [ "$NAME" = "CNES CAYCode FromScratch" ]
then
log_info "setting CNES quality gate as default gate."
res=$(curl -su "admin:admin" \
--data-urlencode "id=${GATEID}" \
"${SONARQUBE_URL}/api/qualitygates/set_as_default")
if [ -z "$res" ]
then
log_info "successfully set CNES quality gate as default gate."
else
log_error "impossible to set CNES quality gate as default gate" "$(echo "${res}" | jq '.errors[].msg')"
fi
fi
# add quality gate conditions
# Adding all conditions of the JSON file
log_info "adding all conditions of $FILE to the gate."
len=$(jq '(.conditions | length)' "$FILE")
cnes_quality_gate=$(jq '(.conditions)' "$FILE")
actual_quality_gate=$(curl -su "admin:admin" \
-G \
--data-urlencode "name=$NAME" \
"${SONARQUBE_URL}/api/qualitygates/show")
conditions=$(echo "$actual_quality_gate" | jq -r '.conditions[]')
for i in $(seq 0 $((len - 1)))
do
metric=$(echo "$cnes_quality_gate" | jq -r '(.['"$i"'].metric)')
op=$(echo "$cnes_quality_gate" | jq -r '(.['"$i"'].op)')
error=$(echo "$cnes_quality_gate" | jq -r '(.['"$i"'].error)')
add_condition_to_quality_gate "$GATEID" "$conditions" "$metric" "$op" "$error"
done
}
# add_condition_to_quality_gate
#
# This function adds a condition to an existing Quality Gate
# on a SonarQube server.
#
# Parameters:
# 1: gate_id
# 2: conditions
# 3: metric_key
# 4: metric_operator (EQ, NE, LT or GT)
# 5: metric's error threshold ("none" not to set it)
#
# Example:
# $ add_condition_to_quality_gate "blocker_violations" "GT" "$GATEID" 0
add_condition_to_quality_gate()
{
gate_id=$1
conditions=$2
metric_key=$3
metric_operator=$4
metric_errors=$5
# Check if the metric is already configured
existing_condition=$(echo "${conditions}" | jq -r "select(.metric == \"${metric_key}\")")
# If the metric is already configured, update it
if [ -n "$existing_condition" ]; then
log_info "The metric '${metric}' is already configured. Updating it."
condition_id=$(echo "${existing_condition}" | jq -r ".id")
update_condition "$condition_id" "$metric_key" "$metric_operator" "$metric_errors"
else
# Add the new condition
log_info "adding CNES quality gate condition: ${metric_key} ${metric_operator} ${metric_errors}."
threshold=()
if [ "${metric_errors}" != "none" ]
then
threshold=("--data-urlencode" "error=${metric_errors}")
fi
res=$(curl -su "admin:admin" \
--data-urlencode "gateId=${gate_id}" \
--data-urlencode "metric=${metric_key}" \
--data-urlencode "op=${metric_operator}" \
"${threshold[@]}" \
"${SONARQUBE_URL}/api/qualitygates/create_condition")
if [ "$(echo "${res}" | jq '(.errors | length)')" != "0" ]; then
log_warning "impossible to add ${metric_key} condition" "$(echo "${res}" | jq '.errors[].msg')"
fi
fi
}
# update_condition
#
# Updates a condition in an existing Quality Gate
# on a SonarQube server.
#
# Parameters:
# 1: condition_id
# 2: metric_key
# 3: metric_operator (EQ, NE, LT or GT)
# 4: metric's error threshold ("none" not to set it)
#
# Example:
# $ add_condition_to_quality_gate "blocker_violations" "GT" "$GATEID" 0
update_condition()
{
condition_id=$1
metric_key=$2
metric_operator=$3
metric_errors=$4
threshold=()
if [ "${metric_errors}" != "none" ]
then
threshold=("--data-urlencode" "error=${metric_errors}")
fi
res=$(curl -su "admin:admin" \
--data-urlencode "id=${condition_id}" \
--data-urlencode "metric=${metric_key}" \
--data-urlencode "op=${metric_operator}" \
"${threshold[@]}" \
"${SONARQUBE_URL}/api/qualitygates/update_condition")
if [ "$(echo "${res}" | jq '(.errors | length)')" != "0" ]; then
log_warning "Impossible to update ${metric_key} condition" "$(echo "${res}" | jq '.errors[].msg')"
fi
}
########################################################
# function add_profile
#
# Parameters :
# - 1 : Quality profile's file to import
#
# Description :
# Add quality profile in parameter in Sonarqube's
#
################################################################################
add_profile(){
file=$1
log_info "processing profile addition for file ${file}"
RES=$(curl POST -su admin:admin "${SONARQUBE_URL}/api/qualityprofiles/restore" --form backup=@${file})
if [ "$(echo ${RES} | jq '(.errors | length)')" == "0" ]
then
log_info "quality profile ${file} successfully created."
else
log_warning "impossible to create ${file} quality profile" "$( echo ${RES} | jq '.errors[].msg' )"
fi
}
########################################################
# function create_quality_profiles
#
# Description :
# Periodically verify Sonarqube's server status and wait until it's UP
# Once run, import quality profiles from /tmp/conf directory (Regex : *-quality-profile.xml)
################################################################################
create_quality_profiles(){
sonar_status="DOWN"
log_info "initiating connection with Sonarqube"
sleep 15
while [ "${sonar_status}" != "UP" ]
do
sleep 20
log_info "retrieving Sonarqube's service status."
sonar_status=$(curl -s -X GET "${SONARQUBE_URL}/api/system/status" | jq -r '.status')
log_info "detected status ${sonar_status} for Sonarqube, expecting it to be UP."
done
log_info "detected status ${sonar_status} for Sonarqube, starting configuration of quality profiles."
for file in $(find /opt/sonarqube/conf/quality_profiles -mindepth 2 -maxdepth 2 -type f)
do
add_profile "${file}"
done
log_info "added all quality profiles."
}
run_sonarqube(){
set -e
if [ "${1:0:1}" != '-' ]; then
exec "$@"
fi
chown -R sonarqube:sonarqube $SONARQUBE_HOME
exec gosu sonarqube \
java -jar $SONARQUBE_HOME/lib/sonar-application-$SONAR_VERSION.jar \
-Dsonar.log.console=true \
-Dsonar.jdbc.username="$SONARQUBE_JDBC_USERNAME" \
-Dsonar.jdbc.password="$SONARQUBE_JDBC_PASSWORD" \
-Dsonar.jdbc.url="$SONARQUBE_JDBC_URL" \
-Dsonar.web.javaAdditionalOpts="$SONARQUBE_WEB_JVM_OPTS -Djava.security.egd=file:/dev/./urandom" \
"$@"
}
stop_sonarqube(){
pkill -u sonarqube
}
create_quality_profiles
for qg_file in /opt/sonarqube/conf/quality_gates/*
do
create_quality_gates "$qg_file"
done
echo "[INFO] Docker CAT is ready to go and find bugs!"
exit 0