Fix API breakage caused by OS Plugin version scan
[releng/global-jjb.git] / shell / jenkins-configure-clouds.sh
index ab7f909..d8782c3 100644 (file)
 # Configuration is read from $WORKSPACE/jenkins-config/clouds/openstack/$cloud/cloud.cfg
 #
 # Requirements: lftools must be installed to /tmp/v/lftools
+#
 # Parameters:
+#
+#     WORKSPACE:  The path to the local ci-management repository.
 #     jenkins_silos:  Space separated list of Jenkins silos to push
-#                     configuration to. (default: jenkins)
+#                     configuration to. This must match a configuration section
+#                     in the config file located at
+#                     ~/.config/jenkins_jobs/jenkins_jobs.ini config file.
+#                     (default: jenkins)
+#
+# Local testing can be performed by exporting the parameters "WORKSPACE" and
+# "jenkins_silos" as environment variables. For example:
+#
+#    export WORKSPACE=/tmp/ci-management
+#    export jenkins_silos=sandbox
+#    bash ./jjb/global-jjb/shell/jenkins-configure-clouds.sh
 echo "---> jenkins-configure-clouds.sh"
 
 if [ ! -d "$WORKSPACE/jenkins-config/clouds" ]; then
@@ -35,6 +48,8 @@ silos="${jenkins_silos:-jenkins}"
 
 set -eu -o pipefail
 
+version_ge() { test "$(echo "$@" | tr " " "\n" | sort -rV | head -n 1)" == "$1"; }
+
 get_cfg() {
     if [ -z ${3+x} ]; then
         >&2 echo "Usage: get_cfg CFG_FILE SETTING DEFAULT"
@@ -121,8 +136,16 @@ get_minion_options() {
     flavors["v2-highcpu-8"]="221de281-95ec-414f-8e42-c86c9e0b318d"
     flavors["v2-highcpu-16"]="ddd6863a-ef4f-475c-9aee-61d46898651d"
     flavors["v2-highcpu-32"]="21dfb8a3-c472-4a2c-a8e1-4da8de415ff8"
+    flavors["odl-highcpu-2"]="def1b86f-b7f8-4943-b430-4a0599170006"
+    flavors["odl-highcpu-4"]="0c8ec795-2ff8-4623-98cf-b4c1d92bb37c"
+    flavors["odl-highcpu-8"]="458d6499-e2c8-4580-aa88-a4a04a33ee25"
+    flavors["odl-standard-1"]="35800a3f-0c69-428d-b5cb-136d17d46c48"
+    flavors["odl-standard-2"]="8ead227a-acfe-4290-be70-fbab92e6dd2f"
+    flavors["odl-standard-4"]="f76fb18d-d5fb-4175-95c1-b29d8039d102"
+    flavors["odl-standard-8"]="ba38b1af-4f87-4e4e-860e-94e8329d0d78"
 
     image_name=$(get_cfg "$cfg_file" IMAGE_NAME "")
+    volume_size=$(get_cfg "$cfg_file" VOLUME_SIZE "")
     hardware_id=$(get_cfg "$cfg_file" HARDWARE_ID "")
     network_id=$(get_cfg "$cfg_file" NETWORK_ID "")
     user_data_id=$(get_cfg "$cfg_file" USER_DATA_ID "jenkins-init-script")
@@ -132,48 +155,81 @@ get_minion_options() {
     if [ "$silo" == "sandbox" ]; then
         instance_cap=$(get_cfg "$cfg_file" SANDBOX_CAP "null")
     fi
+    min_instance_cap=$(get_cfg "$cfg_file" MIN_INSTANCE_CAP "null")
 
     floating_ip_pool=$(get_cfg "$cfg_file" FLOATING_IP_POOL "")
     security_groups=$(get_cfg "$cfg_file" SECURITY_GROUPS "default")
     availability_zone=$(get_cfg "$cfg_file" AVAILABILITY_ZONE "")
     start_timeout=$(get_cfg "$cfg_file" START_TIMEOUT "600000")
-    key_pair_name=$(get_cfg "$cfg_file" KEY_PAIR_NAME "jenkins")
+    key_pair_name=$(get_cfg "$cfg_file" KEY_PAIR_NAME "jenkins-ssh")
     num_executors=$(get_cfg "$cfg_file" NUM_EXECUTORS "1")
     jvm_options=$(get_cfg "$cfg_file" JVM_OPTIONS "")
     fs_root=$(get_cfg "$cfg_file" FS_ROOT "/w")
     retention_time=$(get_cfg "$cfg_file" RETENTION_TIME "0")
 
-    echo "    new BootSource.Image(\"$image_name\"),"
-    echo "    \"${flavors[${hardware_id}]}\","
-    echo "    \"$network_id\","
-    echo "    \"$user_data_id\","
-    echo "    $instance_cap,"
-    echo "    \"$floating_ip_pool\","
-    echo "    \"$security_groups\","
-    echo "    \"$availability_zone\","
-    echo "    $start_timeout,"
-    echo "    \"$key_pair_name\","
-    echo "    $num_executors,"
-    echo "    \"$jvm_options\","
-    echo "    \"$fs_root\","
-    echo "    new LauncherFactory.SSH(\"jenkins\", \"\"),"
-    echo "    $retention_time"
+    OS_PLUGIN_VER="$(lftools jenkins plugins list \
+        | grep -i 'OpenStack Cloud Plugin' | awk -F':' '{print $2}')"
+    if version_ge "$OS_PLUGIN_VER" "2.35"; then
+        if [ ! -z "$volume_size" ]; then
+            echo "    new BootSource.VolumeFromImage(\"$image_name\", $volume_size),"
+        else
+            echo "    new BootSource.Image(\"$image_name\"),"
+        fi
+        echo "    \"${flavors[${hardware_id}]}\","
+        echo "    \"$network_id\","
+        echo "    \"$user_data_id\","
+        echo "    $instance_cap,"
+        echo "    $min_instance_cap,"
+        echo "    \"$floating_ip_pool\","
+        echo "    \"$security_groups\","
+        echo "    \"$availability_zone\","
+        echo "    $start_timeout,"
+        echo "    \"$key_pair_name\","
+        echo "    $num_executors,"
+        echo "    \"$jvm_options\","
+        echo "    \"$fs_root\","
+        echo "    new LauncherFactory.SSH(\"$key_pair_name\", \"\"),"
+        echo "    $retention_time"
+
+    else  # SlaveOptions() structure for versions <= 2.34
+        if [ ! -z "$volume_size" ]; then
+            echo "    new BootSource.VolumeFromImage(\"$image_name\", $volume_size),"
+        else
+            echo "    new BootSource.Image(\"$image_name\"),"
+        fi
+        echo "    \"${flavors[${hardware_id}]}\","
+        echo "    \"$network_id\","
+        echo "    \"$user_data_id\","
+        echo "    $instance_cap,"
+        echo "    \"$floating_ip_pool\","
+        echo "    \"$security_groups\","
+        echo "    \"$availability_zone\","
+        echo "    $start_timeout,"
+        echo "    \"$key_pair_name\","
+        echo "    $num_executors,"
+        echo "    \"$jvm_options\","
+        echo "    \"$fs_root\","
+        echo "    new LauncherFactory.SSH(\"$key_pair_name\", \"\"),"
+        echo "    $retention_time"
+    fi
 }
 
 get_template_cfg() {
-    if [ -z $1 ]; then
-        >&2 echo "Usage: get_template_cfg CFG_FILE"
+    if [ -z $2 ]; then
+        >&2 echo "Usage: get_template_cfg CFG_FILE SILO [MINION_PREFIX]"
         exit 1
     fi
 
     local cfg_file="$1"
-    local minion_prefix="${2:-}"
+    local silo="${2}"
+    local minion_prefix="${3:-}"
+
 
     template_name=$(basename $cfg_file .cfg)
     labels=$(get_cfg "$cfg_file" LABELS "")
 
     echo "minion_options = new SlaveOptions("
-    get_minion_options "$cfg_file"
+    get_minion_options "$cfg_file" "$silo"
     echo ")"
 
     echo "template = new JCloudsSlaveTemplate("
@@ -201,6 +257,15 @@ for silo in $silos; do
         node_prefix="${silo}-"
     fi
 
+    set +x  # Disable `set -x` to prevent printing passwords
+    echo "Configuring $silo"
+    JENKINS_URL=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" url)
+    JENKINS_USER=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" user)
+    JENKINS_PASSWORD=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" password)
+    export JENKINS_URL
+    export JENKINS_USER
+    export JENKINS_PASSWORD
+
     echo "-----> Groovy script $script_file"
     for cloud in "${clouds[@]}"; do
         cfg_dir="${cloud}"
@@ -218,7 +283,7 @@ for silo in $silos; do
         echo "templates = []" >> $insert_file
         mapfile -t templates < <(find $cfg_dir -maxdepth 1 -not -type d -not -name "cloud.cfg")
         for template in "${templates[@]}"; do
-            get_template_cfg "$template" "$node_prefix" >> "$insert_file"
+            get_template_cfg "$template" "$silo" "$node_prefix" >> "$insert_file"
             echo "templates.add(template)" >> "$insert_file"
         done
 
@@ -228,13 +293,5 @@ for silo in $silos; do
         cat "$insert_file" >> "$script_file"
     done
 
-    set +x  # Disable `set -x` to prevent printing passwords
-    echo "Configuring $silo"
-    JENKINS_URL=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" url)
-    JENKINS_USER=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" user)
-    JENKINS_PASSWORD=$(crudini --get "$HOME"/.config/jenkins_jobs/jenkins_jobs.ini "$silo" password)
-    export JENKINS_URL
-    export JENKINS_USER
-    export JENKINS_PASSWORD
     lftools jenkins groovy "$script_file"
 done