Fix: Add vex v3 starter flavors for create scripts
[releng/global-jjb.git] / shell / jenkins-configure-clouds.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # Pulls global variable definitions out of a file.
12 #
13 # Configuration is read from $WORKSPACE/jenkins-config/clouds/openstack/$cloud/cloud.cfg
14 #
15 # Requirements: lftools must be installed to /tmp/v/lftools
16 #
17 # Parameters:
18 #
19 #     WORKSPACE:  The path to the local ci-management repository.
20 #     jenkins_silos:  Space separated list of Jenkins silos to push
21 #                     configuration to. This must match a configuration section
22 #                     in the config file located at
23 #                     ~/.config/jenkins_jobs/jenkins_jobs.ini config file.
24 #                     (default: jenkins)
25 #
26 # Local testing can be performed by exporting the parameters "WORKSPACE" and
27 # "jenkins_silos" as environment variables. For example:
28 #
29 #    export WORKSPACE=/tmp/ci-management
30 #    export jenkins_silos=sandbox
31 #    bash ./global-jjb/shell/jenkins-configure-clouds.sh
32 echo "---> jenkins-configure-clouds.sh"
33
34 if [ ! -d "$WORKSPACE/jenkins-config/clouds" ]; then
35     echo "WARN: jenkins-config/clouds does not exist. Skipping cloud management..."
36     exit 0
37 fi
38
39 GROOVY_SCRIPT_FILE="global-jjb/jenkins-admin/manage_clouds.groovy"
40 OS_CLOUD_DIR="$WORKSPACE/jenkins-config/clouds/openstack"
41 SCRIPT_DIR="$WORKSPACE/archives/groovy-inserts"
42 mkdir -p "$SCRIPT_DIR"
43
44 silos="${jenkins_silos:-jenkins}"
45
46
47 set -eu -o pipefail
48
49 testversion() {
50     local current_val="$1" operator="$2" test_value="$3"
51     awk -vv1="$current_val" -vv2="$test_value" 'BEGIN {
52         split(v1, a, /\:/);
53         if (a[2] == '"$test_value"') {
54             exit (a[2] == '"$test_value"') ? 0 : 1
55         }
56         else {
57             exit (a[2] '"$operator"' '"$test_value"') ? 0 : 1
58         }
59     }'
60 }
61
62 get_cfg() {
63     if [ -z ${3+x} ]; then
64         >&2 echo "Usage: get_cfg CFG_FILE SETTING DEFAULT"
65         exit 1
66     fi
67
68     local cfg_file="$1"
69     local setting="$2"
70     local default="$3"
71
72     if [ ! -f "$cfg_file" ]; then
73         >&2 echo "ERROR: Configuration file $cfg_file not found."
74         exit 1
75     fi
76
77     cfg=$(grep "^${setting^^}=" "$cfg_file" | tail -1 | awk -F'=' '{print $2}')
78     cfg=${cfg:-"$default"}
79     echo "$cfg"
80 }
81 export get_cfg
82
83
84 get_cloud_cfg() {
85     if [ -z "$1" ]; then
86         >&2 echo "Usage: get_cloud_cfg CFG_DIR"
87         exit 1
88     fi
89
90     local cfg_dir="$1"
91     local silo="$2"
92     local cfg_file="$cfg_dir/cloud.cfg"
93
94     cloud_name=$(basename "$cfg_dir")
95     cloud_url=$(get_cfg "$cfg_file" CLOUD_URL "")
96     cloud_ignore_ssl=$(get_cfg "$cfg_file" CLOUD_IGNORE_SSL "false")
97     cloud_zone=$(get_cfg "$cfg_file" CLOUD_ZONE "")
98     cloud_credential_id=$(get_cfg "$cfg_file" CLOUD_CREDENTIAL_ID "os-cloud")
99
100     echo "default_options = new SlaveOptions("
101     get_minion_options "$cfg_file" "$silo"
102     echo ")"
103
104     echo "cloud = new JCloudsCloud("
105     echo "    \"$cloud_name\","
106     echo "    \"$cloud_url\","
107     echo "    $cloud_ignore_ssl,"
108     echo "    \"$cloud_zone\","
109     echo "    default_options,"
110     echo "    templates,"
111     echo "    \"$cloud_credential_id\""
112     echo ")"
113 }
114
115
116 get_launcher_factory() {
117     if [ -z "$1" ]; then
118         >&2 echo "Usage: get_launcher_factory JNLP|SSH"
119         exit 1
120     fi
121
122     local connection_type="$1"
123
124     if [ "$connection_type" == "JNLP" ]; then
125         echo "new LauncherFactory.JNLP()"
126     elif [ "$connection_type" == "SSH" ]; then
127         echo "new LauncherFactory.SSH(\"$key_pair_name\", \"\")"
128     else
129         >&2 echo "Unknown connection type $connection_type"
130         exit 1
131     fi
132 }
133
134
135 get_minion_options() {
136     if [ -z "$1" ]; then
137         >&2 echo "Usage: get_minion_options CFG_FILE"
138         exit 1
139     fi
140
141     local cfg_file="$1"
142     local silo="${2:-}"
143
144     # Create a flavor mapping to manage hardware_id until OpenStack Cloud
145     # plugin supports using names
146     declare -A flavors
147     # ShellCheck 0.4.4 incorrectly flags these as unused vars
148     # Fails on first instance of each different associatve array prefix
149     # Fails when using single/double/no quotes, all of which are valid bash
150     # shellcheck disable=SC2154
151     flavors["acumos-highcpu-4-avx"]="c720c1f8-62e9-4695-823d-f7f54db46c86"
152     flavors["lf-highcpu-2"]="1051d06a-61ea-45e3-b9b4-93de92880b27"
153     flavors["lf-highcpu-4"]="35eb8e11-490f-4d1a-9f19-76091fc04547"
154     flavors["lf-highcpu-8"]="68af673f-54ee-4255-871c-158c18e4f643"
155     flavors["lf-standard-1"]="7d76cbb0-f547-4c2c-beaf-554f33832721"
156     flavors["lf-standard-2"]="ef454088-7839-42a0-bf23-5e0ab6386a27"
157     flavors["lf-standard-4"]="bd74e1e6-c2ed-475b-ab3f-2ce13936a215"
158     flavors["lf-standard-8"]="32d74024-8418-41b6-9675-b77816748148"
159     flavors["odl-highcpu-2"]="def1b86f-b7f8-4943-b430-4a0599170006"
160     flavors["odl-highcpu-4"]="0c8ec795-2ff8-4623-98cf-b4c1d92bb37c"
161     flavors["odl-highcpu-8"]="458d6499-e2c8-4580-aa88-a4a04a33ee25"
162     flavors["odl-standard-1"]="35800a3f-0c69-428d-b5cb-136d17d46c48"
163     flavors["odl-standard-2"]="8ead227a-acfe-4290-be70-fbab92e6dd2f"
164     flavors["odl-standard-4"]="f76fb18d-d5fb-4175-95c1-b29d8039d102"
165     flavors["odl-standard-8"]="ba38b1af-4f87-4e4e-860e-94e8329d0d78"
166     flavors["v1-standard-1"]="bbcb7eb5-5c8d-498f-9d7e-307c575d3566"
167     flavors["v1-standard-2"]="ca2a6e9c-2236-4107-8905-7ae9427132ff"
168     flavors["v1-standard-4"]="5cf64088-893b-46b5-9bb1-ee020277635d"
169     flavors["v1-standard-8"]="6eec77b4-2286-4e3b-b3f0-cac67aa2c727"
170     flavors["v1-standard-16"]="2f8730dd-7688-4b72-a512-99fb9a482414"
171     flavors["v1-standard-32"]="0da688af-bb0c-4116-a158-cbf37240a8b1"
172     flavors["v1-standard-48"]="69471d69-61fb-40dd-bdf3-e6b7f4e6daa3"
173     flavors["v1-standard-64"]="0c1d9008-f546-4608-9e8f-f8bdaec8dddd"
174     flavors["v1-standard-96"]="5741c775-92a4-4488-bd77-dd7b08e2be81"
175     flavors["v1-standard-128"]="e82d0a5b-8031-4526-9a5d-a15f7b4d48ff"
176     flavors["v2-highcpu-1"]="c04abb7a-2b61-4ed3-8ce8-6c40ad9df750"
177     flavors["v2-highcpu-2"]="03bdf34e-8905-46bc-a4b9-8dbf94b6e06d"
178     flavors["v2-highcpu-4"]="3b72e578-7875-4e0e-91b7-71ed292f3ca2"
179     flavors["v2-highcpu-8"]="221de281-95ec-414f-8e42-c86c9e0b318d"
180     flavors["v2-highcpu-16"]="ddd6863a-ef4f-475c-9aee-61d46898651d"
181     flavors["v2-highcpu-32"]="21dfb8a3-c472-4a2c-a8e1-4da8de415ff8"
182     flavors["v2-standard-1"]="52a01f6b-e660-48b5-8c06-5fb2a0fab0ec"
183     flavors["v2-standard-2"]="ac2c4d17-8d6f-4e3c-a9eb-57c155f0a949"
184     flavors["v2-standard-4"]="d9115351-defe-4fac-986b-1a1187e2c31c"
185     flavors["v2-standard-8"]="e6fe2e37-0e38-438c-8fa5-fc2d79d0a7bb"
186     flavors["v2-standard-16"]="9e4b01cd-6744-4120-aafe-1b5e17584919"
187     flavors["v3-standard-2"]="d6906d2a-e83f-42be-b33e-fbaeb5c511cb"
188     flavors["v3-standard-4"]="5f1eb09f-e764-4642-a16f-a7230ec025e7"
189     flavors["v3-standard-8"]="47d3707a-c6c6-46ea-a15b-095e336b1edc"
190     flavors["v3-standard-16"]="8587d458-69de-4fc5-be51-c5e671bc35d5"
191     flavors["v3-standard-32"]="3e01b39f-45a9-4b7b-b6dc-14378433dc36"
192     flavors["v3-standard-48"]="06a0e8b7-949a-439d-a185-208ae9e645b2"
193     flavors["v3-standard-64"]="402a2759-cc01-481d-a8b7-2c7056f153f7"
194     flavors["v3-standard-96"]="883b0564-dec6-4e51-88c7-83d86994fcf0"
195     flavors["v3-starter-2"]="b542cedb-d3b4-4446-a43f-5416711440ee"
196     flavors["v3-starter-4"]="5f93acce-e8dc-482b-9118-134728a77aa8"
197     flavors["v3-starter-8"]="35c0ddb3-4dd8-478c-887c-34620851a66a"
198     flavors["v3-starter-16"]="595dd716-6c7a-4365-9020-2ff10796e29c"
199     flavors["v3-starter-32"]="15949005-7952-4e93-be69-ca89dab5b884"
200     flavors["v3-starter-48"]="94eb4cec-3840-4171-ad50-a8bce2757d11"
201     flavors["v3-starter-64"]="4a6e52a2-8f64-4632-adde-72f81616d4f9"
202     flavors["v3-starter-96"]="8e7205fc-3ec7-456c-bff0-e38609e415c1"
203
204     image_name=$(get_cfg "$cfg_file" IMAGE_NAME "")
205     volume_size=$(get_cfg "$cfg_file" VOLUME_SIZE "")
206     hardware_id=$(get_cfg "$cfg_file" HARDWARE_ID "")
207     network_id=$(get_cfg "$cfg_file" NETWORK_ID "")
208     udi_default="$(get_cfg "$(dirname "$cfg_file")/cloud.cfg" USER_DATA_ID "jenkins-init-script")"
209     user_data_id=$(get_cfg "$cfg_file" USER_DATA_ID "$udi_default")
210
211     # Handle Sandbox systems that might have a different cap.
212     if [ "$silo" == "sandbox" ]; then
213         instance_cap=$(get_cfg "$cfg_file" SANDBOX_CAP "null")
214     else
215         instance_cap=$(get_cfg "$cfg_file" INSTANCE_CAP "null")
216     fi
217
218     floating_ip_pool=$(get_cfg "$cfg_file" FLOATING_IP_POOL "")
219     security_groups=$(get_cfg "$cfg_file" SECURITY_GROUPS "default")
220     availability_zone=$(get_cfg "$cfg_file" AVAILABILITY_ZONE "")
221     start_timeout=$(get_cfg "$cfg_file" START_TIMEOUT "600000")
222     kpn_default="$(get_cfg "$(dirname "$cfg_file")/cloud.cfg" KEY_PAIR_NAME "jenkins-ssh")"
223     key_pair_name=$(get_cfg "$cfg_file" KEY_PAIR_NAME "$kpn_default")
224     num_executors=$(get_cfg "$cfg_file" NUM_EXECUTORS "1")
225     jvm_options=$(get_cfg "$cfg_file" JVM_OPTIONS "")
226     fs_root=$(get_cfg "$cfg_file" FS_ROOT "/w")
227     connection_type=$(get_cfg "$cfg_file" CONNECTION_TYPE "SSH")
228     launcher_factory=$(get_launcher_factory "$connection_type")
229     node_properties=$(get_cfg "$cfg_file" NODE_PROPERTIES, "null")
230     retention_time=$(get_cfg "$cfg_file" RETENTION_TIME "0")
231     config_drive=$(get_cfg "$cfg_file" CONFIG_DRIVE, "null")
232
233
234     if [ -n "$volume_size" ]; then
235         echo "    new BootSource.VolumeFromImage(\"$image_name\", $volume_size),"
236     else
237         echo "    new BootSource.Image(\"$image_name\"),"
238     fi
239
240     echo "    \"${flavors[${hardware_id}]}\","
241     echo "    \"$network_id\","
242     echo "    \"$user_data_id\","
243     echo "    $instance_cap,"
244
245     # Handle specifying the minimum instance count across different versions
246     if testversion "$os_plugin_version" '>=' '2.47'
247     then
248         instance_min=$(get_cfg "$cfg_file" INSTANCE_MIN "null")
249         echo "    $instance_min,"
250     else
251         instance_min=$(get_cfg "$cfg_file" INSTANCE_MIN_CAPMAX "null")
252         echo "    $instance_min,"
253     fi
254
255     echo "    \"$floating_ip_pool\","
256     echo "    \"$security_groups\","
257     echo "    \"$availability_zone\","
258     echo "    $start_timeout,"
259     echo "    \"$key_pair_name\","
260     echo "    $num_executors,"
261     echo "    \"$jvm_options\","
262     echo "    \"$fs_root\","
263     echo "    $launcher_factory,"
264
265     if testversion "$os_plugin_version" '>=' '2.47'
266     then
267         echo "    $node_properties,"
268         echo "    $retention_time",
269         echo "    $config_drive"
270     else
271         echo "    $retention_time"
272     fi
273
274
275 }
276
277 get_template_cfg() {
278     if [ -z "$2" ]; then
279         >&2 echo "Usage: get_template_cfg CFG_FILE SILO [MINION_PREFIX]"
280         exit 1
281     fi
282
283     local cfg_file="$1"
284     local silo="${2}"
285     local minion_prefix="${3:-}"
286
287
288     template_name=$(basename "$cfg_file" .cfg)
289     labels=$(get_cfg "$cfg_file" LABELS "")
290
291     echo "minion_options = new SlaveOptions("
292     get_minion_options "$cfg_file" "$silo"
293     echo ")"
294
295     echo "template = new JCloudsSlaveTemplate("
296     # TODO: Figure out how to insert the "prd / snd" prefix into template name.
297     echo "    \"${minion_prefix}${template_name}\","
298     echo "    \"$template_name $labels\","
299     echo "    minion_options,"
300     echo ")"
301 }
302
303 # shellcheck disable=SC1090
304 . ~/lf-env.sh
305
306 lf-activate-venv --python python3 lftools
307
308 mapfile -t clouds < <(ls -d1 "$OS_CLOUD_DIR"/*/)
309
310 for silo in $silos; do
311
312     script_file="$SCRIPT_DIR/${silo}-cloud-cfg.groovy"
313     cp "$GROOVY_SCRIPT_FILE" "$script_file"
314
315     # Linux Foundation Jenkins systems use "prd-" and "snd-" to mark
316     # production and sandbox servers.
317     if [ "$silo" == "releng" ] || [ "$silo" == "production" ]; then
318         node_prefix="prd-"
319     elif [ "$silo" == "sandbox" ]; then
320         node_prefix="snd-"
321     else
322         node_prefix="${silo}-"
323     fi
324
325     set +x  # Disable `set -x` to prevent printing passwords
326     echo "Configuring $silo"
327     JENKINS_URL=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" url)
328     JENKINS_USER=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" user)
329     JENKINS_PASSWORD=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" password)
330     export JENKINS_URL
331     export JENKINS_USER
332     export JENKINS_PASSWORD
333
334     # JENKINS_{URL,USER,PASSWORD} env vars are required for the "lftools jenkins
335     # plugins list" call
336     os_plugin_version="$(lftools jenkins plugins list \
337         | grep -i 'OpenStack Cloud Plugin')"
338
339     echo "-----> Groovy script $script_file"
340     for cloud in "${clouds[@]}"; do
341         cfg_dir="${cloud}"
342         echo "Processing $cfg_dir"
343         insert_file="$SCRIPT_DIR/$silo/$(basename "$cloud")/cloud-cfg.txt"
344         mkdir -p "$(dirname "$insert_file")"
345         rm -f "$insert_file"
346
347         {
348             echo ""
349             echo "//////////////////////////////////////////////////"
350             echo "// Cloud config for $(basename "$cloud")"
351             echo "//////////////////////////////////////////////////"
352             echo ""
353         } >> "$insert_file"
354
355
356         echo "templates = []" >> "$insert_file"
357         mapfile -t templates < <(find "$cfg_dir" -maxdepth 1 -not -type d -not -name "cloud.cfg")
358         for template in "${templates[@]}"; do
359             get_template_cfg "$template" "$silo" "$node_prefix" >> "$insert_file"
360             echo "templates.add(template)" >> "$insert_file"
361         done
362
363         get_cloud_cfg "$cfg_dir" "$silo" >> "$insert_file"
364         echo "clouds.add(cloud)" >> "$insert_file"
365
366         cat "$insert_file" >> "$script_file"
367     done
368
369     lftools jenkins groovy "$script_file"
370 done