Make sure that jjb-cleanup.sh allows unbound vars
[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 ./jjb/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="jjb/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 set -eu -o pipefail
47
48 version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
49
50 get_cfg() {
51     if [ -z ${3+x} ]; then
52         >&2 echo "Usage: get_cfg CFG_FILE SETTING DEFAULT"
53         exit 1
54     fi
55
56     local cfg_file="$1"
57     local setting="$2"
58     local default="$3"
59
60     if [ ! -f "$cfg_file" ]; then
61         >&2 echo "ERROR: Configuration file $cfg_file not found."
62         exit 1
63     fi
64
65     cfg=$(grep "^${setting^^}=" "$cfg_file" | tail -1 | awk -F'=' '{print $2}')
66     cfg=${cfg:-"$default"}
67     echo "$cfg"
68 }
69 export get_cfg
70
71 get_cloud_cfg() {
72     if [ -z $1 ]; then
73         >&2 echo "Usage: get_cloud_cfg CFG_DIR"
74         exit 1
75     fi
76
77     local cfg_dir="$1"
78     local silo="$2"
79     local cfg_file="$cfg_dir/cloud.cfg"
80
81     cloud_name=$(basename "$cfg_dir")
82     cloud_url=$(get_cfg "$cfg_file" CLOUD_URL "")
83     cloud_ignore_ssl=$(get_cfg "$cfg_file" CLOUD_IGNORE_SSL "false")
84     cloud_zone=$(get_cfg "$cfg_file" CLOUD_ZONE "")
85     cloud_credential_id=$(get_cfg "$cfg_file" CLOUD_CREDENTIAL_ID "os-cloud")
86
87     echo "default_options = new SlaveOptions("
88     get_minion_options "$cfg_file" "$silo"
89     echo ")"
90
91     echo "cloud = new JCloudsCloud("
92     echo "    \"$cloud_name\","
93     echo "    \"$cloud_url\","
94     echo "    $cloud_ignore_ssl,"
95     echo "    \"$cloud_zone\","
96     echo "    default_options,"
97     echo "    templates,"
98     echo "    \"$cloud_credential_id\""
99     echo ")"
100 }
101
102 get_launcher_factory() {
103     if [ -z $1 ]; then
104         >&2 echo "Usage: get_launcher_factory JNLP|SSH"
105         exit 1
106     fi
107
108     local connection_type="$1"
109
110     if [ "$connection_type" == "JNLP" ]; then
111         echo "new LauncherFactory.JNLP()"
112     elif [ "$connection_type" == "SSH" ]; then
113         echo "new LauncherFactory.SSH(\"$key_pair_name\", \"\")"
114     else
115         >&2 echo "Unknown connection type $connection_type"
116         exit 1
117     fi
118 }
119
120 get_minion_options() {
121     if [ -z $1 ]; then
122         >&2 echo "Usage: get_minion_options CFG_FILE"
123         exit 1
124     fi
125
126     local cfg_file="$1"
127     local silo="${2:-}"
128
129     # Create a flavor mapping to manage hardware_id until OpenStack Cloud
130     # plugin supports using names
131     declare -A flavors
132     flavors["v1-standard-1"]="bbcb7eb5-5c8d-498f-9d7e-307c575d3566"
133     flavors["v1-standard-2"]="ca2a6e9c-2236-4107-8905-7ae9427132ff"
134     flavors["v1-standard-4"]="5cf64088-893b-46b5-9bb1-ee020277635d"
135     flavors["v1-standard-8"]="6eec77b4-2286-4e3b-b3f0-cac67aa2c727"
136     flavors["v1-standard-16"]="2f8730dd-7688-4b72-a512-99fb9a482414"
137     flavors["v1-standard-32"]="0da688af-bb0c-4116-a158-cbf37240a8b1"
138     flavors["v1-standard-48"]="69471d69-61fb-40dd-bdf3-e6b7f4e6daa3"
139     flavors["v1-standard-64"]="0c1d9008-f546-4608-9e8f-f8bdaec8dddd"
140     flavors["v1-standard-96"]="5741c775-92a4-4488-bd77-dd7b08e2be81"
141     flavors["v1-standard-128"]="e82d0a5b-8031-4526-9a5d-a15f7b4d48ff"
142     flavors["v2-standard-1"]="52a01f6b-e660-48b5-8c06-5fb2a0fab0ec"
143     flavors["v2-standard-2"]="ac2c4d17-8d6f-4e3c-a9eb-57c155f0a949"
144     flavors["v2-standard-4"]="d9115351-defe-4fac-986b-1a1187e2c31c"
145     flavors["v2-standard-8"]="e6fe2e37-0e38-438c-8fa5-fc2d79d0a7bb"
146     flavors["v2-standard-16"]="9e4b01cd-6744-4120-aafe-1b5e17584919"
147     flavors["v2-standard-360"]="f0d27f44-a410-4f0f-9781-d722f5b5489e"
148     flavors["v2-highcpu-1"]="c04abb7a-2b61-4ed3-8ce8-6c40ad9df750"
149     flavors["v2-highcpu-2"]="03bdf34e-8905-46bc-a4b9-8dbf94b6e06d"
150     flavors["v2-highcpu-4"]="3b72e578-7875-4e0e-91b7-71ed292f3ca2"
151     flavors["v2-highcpu-8"]="221de281-95ec-414f-8e42-c86c9e0b318d"
152     flavors["v2-highcpu-16"]="ddd6863a-ef4f-475c-9aee-61d46898651d"
153     flavors["v2-highcpu-32"]="21dfb8a3-c472-4a2c-a8e1-4da8de415ff8"
154     flavors["odl-highcpu-2"]="def1b86f-b7f8-4943-b430-4a0599170006"
155     flavors["odl-highcpu-4"]="0c8ec795-2ff8-4623-98cf-b4c1d92bb37c"
156     flavors["odl-highcpu-8"]="458d6499-e2c8-4580-aa88-a4a04a33ee25"
157     flavors["odl-standard-1"]="35800a3f-0c69-428d-b5cb-136d17d46c48"
158     flavors["odl-standard-2"]="8ead227a-acfe-4290-be70-fbab92e6dd2f"
159     flavors["odl-standard-4"]="f76fb18d-d5fb-4175-95c1-b29d8039d102"
160     flavors["odl-standard-8"]="ba38b1af-4f87-4e4e-860e-94e8329d0d78"
161
162     image_name=$(get_cfg "$cfg_file" IMAGE_NAME "")
163     volume_size=$(get_cfg "$cfg_file" VOLUME_SIZE "")
164     hardware_id=$(get_cfg "$cfg_file" HARDWARE_ID "")
165     network_id=$(get_cfg "$cfg_file" NETWORK_ID "")
166
167     udi_default="$(get_cfg "$(dirname $cfg_file)/cloud.cfg" USER_DATA_ID "jenkins-init-script")"
168     user_data_id=$(get_cfg "$cfg_file" USER_DATA_ID "$udi_default")
169
170     # Handle Sandbox systems that might have a different cap.
171     instance_cap=$(get_cfg "$cfg_file" INSTANCE_CAP "null")
172     if [ "$silo" == "sandbox" ]; then
173         instance_cap=$(get_cfg "$cfg_file" SANDBOX_CAP "null")
174     fi
175     min_instance_cap=$(get_cfg "$cfg_file" MIN_INSTANCE_CAP "null")
176
177     floating_ip_pool=$(get_cfg "$cfg_file" FLOATING_IP_POOL "")
178     security_groups=$(get_cfg "$cfg_file" SECURITY_GROUPS "default")
179     availability_zone=$(get_cfg "$cfg_file" AVAILABILITY_ZONE "")
180     start_timeout=$(get_cfg "$cfg_file" START_TIMEOUT "600000")
181
182     kpn_default="$(get_cfg "$(dirname $cfg_file)/cloud.cfg" KEY_PAIR_NAME "jenkins-ssh")"
183     key_pair_name=$(get_cfg "$cfg_file" KEY_PAIR_NAME "$kpn_default")
184
185     num_executors=$(get_cfg "$cfg_file" NUM_EXECUTORS "1")
186     jvm_options=$(get_cfg "$cfg_file" JVM_OPTIONS "")
187     fs_root=$(get_cfg "$cfg_file" FS_ROOT "/w")
188     retention_time=$(get_cfg "$cfg_file" RETENTION_TIME "0")
189     connection_type=$(get_cfg "$cfg_file" CONNECTION_TYPE "SSH")
190     launcher_factory=$(get_launcher_factory "$connection_type")
191
192     OS_PLUGIN_VER="$(lftools jenkins plugins list \
193         | grep -i 'OpenStack Cloud Plugin' | awk -F':' '{print $2}')"
194     if version_ge "$OS_PLUGIN_VER" "2.35"; then
195         if [ ! -z "$volume_size" ]; then
196             echo "    new BootSource.VolumeFromImage(\"$image_name\", $volume_size),"
197         else
198             echo "    new BootSource.Image(\"$image_name\"),"
199         fi
200         echo "    \"${flavors[${hardware_id}]}\","
201         echo "    \"$network_id\","
202         echo "    \"$user_data_id\","
203         echo "    $instance_cap,"
204         echo "    $min_instance_cap,"
205         echo "    \"$floating_ip_pool\","
206         echo "    \"$security_groups\","
207         echo "    \"$availability_zone\","
208         echo "    $start_timeout,"
209         echo "    \"$key_pair_name\","
210         echo "    $num_executors,"
211         echo "    \"$jvm_options\","
212         echo "    \"$fs_root\","
213         echo "    $launcher_factory,"
214         echo "    $retention_time"
215
216     else  # SlaveOptions() structure for versions <= 2.34
217         if [ ! -z "$volume_size" ]; then
218             echo "    new BootSource.VolumeFromImage(\"$image_name\", $volume_size),"
219         else
220             echo "    new BootSource.Image(\"$image_name\"),"
221         fi
222         echo "    \"${flavors[${hardware_id}]}\","
223         echo "    \"$network_id\","
224         echo "    \"$user_data_id\","
225         echo "    $instance_cap,"
226         echo "    \"$floating_ip_pool\","
227         echo "    \"$security_groups\","
228         echo "    \"$availability_zone\","
229         echo "    $start_timeout,"
230         echo "    \"$key_pair_name\","
231         echo "    $num_executors,"
232         echo "    \"$jvm_options\","
233         echo "    \"$fs_root\","
234         echo "    $launcher_factory,"
235         echo "    $retention_time"
236     fi
237 }
238
239 get_template_cfg() {
240     if [ -z $2 ]; then
241         >&2 echo "Usage: get_template_cfg CFG_FILE SILO [MINION_PREFIX]"
242         exit 1
243     fi
244
245     local cfg_file="$1"
246     local silo="${2}"
247     local minion_prefix="${3:-}"
248
249
250     template_name=$(basename $cfg_file .cfg)
251     labels=$(get_cfg "$cfg_file" LABELS "")
252
253     echo "minion_options = new SlaveOptions("
254     get_minion_options "$cfg_file" "$silo"
255     echo ")"
256
257     echo "template = new JCloudsSlaveTemplate("
258     # TODO: Figure out how to insert the "prd / snd" prefix into template name.
259     echo "    \"${minion_prefix}${template_name}\","
260     echo "    \"$template_name $labels\","
261     echo "    minion_options,"
262     echo ")"
263 }
264
265 mapfile -t clouds < <(ls -d1 $OS_CLOUD_DIR/*/)
266
267 for silo in $silos; do
268
269     script_file="$SCRIPT_DIR/${silo}-cloud-cfg.groovy"
270     cp "$GROOVY_SCRIPT_FILE" "$script_file"
271
272     # Linux Foundation Jenkins systems use "prd-" and "snd-" to mark
273     # production and sandbox servers.
274     if [ "$silo" == "releng" ] || [ "$silo" == "production" ]; then
275         node_prefix="prd-"
276     elif [ "$silo" == "sandbox" ]; then
277         node_prefix="snd-"
278     else
279         node_prefix="${silo}-"
280     fi
281
282     set +x  # Disable `set -x` to prevent printing passwords
283     echo "Configuring $silo"
284     JENKINS_URL=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" url)
285     JENKINS_USER=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" user)
286     JENKINS_PASSWORD=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" password)
287     export JENKINS_URL
288     export JENKINS_USER
289     export JENKINS_PASSWORD
290
291     echo "-----> Groovy script $script_file"
292     for cloud in "${clouds[@]}"; do
293         cfg_dir="${cloud}"
294         echo "Processing $cfg_dir"
295         insert_file="$SCRIPT_DIR/$silo/$(basename $cloud)/cloud-cfg.txt"
296         mkdir -p "$(dirname $insert_file)"
297         rm -f "$insert_file"
298
299         echo "" >> "$insert_file"
300         echo "//////////////////////////////////////////////////" >> "$insert_file"
301         echo "// Cloud config for $(basename $cloud)" >> "$insert_file"
302         echo "//////////////////////////////////////////////////" >> "$insert_file"
303         echo "" >> "$insert_file"
304
305         echo "templates = []" >> $insert_file
306         mapfile -t templates < <(find $cfg_dir -maxdepth 1 -not -type d -not -name "cloud.cfg")
307         for template in "${templates[@]}"; do
308             get_template_cfg "$template" "$silo" "$node_prefix" >> "$insert_file"
309             echo "templates.add(template)" >> "$insert_file"
310         done
311
312         get_cloud_cfg "$cfg_dir" "$silo" >> "$insert_file"
313         echo "clouds.add(cloud)" >> "$insert_file"
314
315         cat "$insert_file" >> "$script_file"
316     done
317
318     lftools jenkins groovy "$script_file"
319 done