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