From: Thanh Ha Date: Wed, 14 Nov 2018 07:58:34 +0000 (+0800) Subject: Migrate stack create and delete scripts X-Git-Tag: v0.28.0~10^2 X-Git-Url: https://gerrit.linuxfoundation.org/infra/gitweb?a=commitdiff_plain;h=3957e7acf87be74dbfaccee20400b6f401a568a3;p=releng%2Fglobal-jjb.git Migrate stack create and delete scripts Pull in OpenDaylight's opendaylight-infra-stack and releng-openstack-stack-delete macros. This patch generalizes the OS HEAT workflow utilized by ODL to spin up CSIT labs to make it reusable by non-OpenDaylight projects. Issue: RELENG-1437 Change-Id: Ie706706e24079b0b87f4537342fd3e8955f63fef Signed-off-by: Thanh Ha --- diff --git a/.jjb-test/lf-openstack.yaml b/.jjb-test/lf-openstack.yaml deleted file mode 100644 index a8780f81..00000000 --- a/.jjb-test/lf-openstack.yaml +++ /dev/null @@ -1,16 +0,0 @@ ---- -- job: - name: openstack-tester - parameters: - - string: - name: STACK_NAME - default: test-lf-stack-delete - publishers: - - lf-stack-delete - -- project: - name: openstack-jobs-view - views: - - project-view - - project-name: openstack diff --git a/.jjb-test/lf-openstack/lf-stack-create-minimal.yaml b/.jjb-test/lf-openstack/lf-stack-create-minimal.yaml new file mode 100644 index 00000000..eaa8f27f --- /dev/null +++ b/.jjb-test/lf-openstack/lf-stack-create-minimal.yaml @@ -0,0 +1,36 @@ +--- +- job-template: + name: stack-create-test + + ##################### + # Default variables # + ##################### + + openstack-cloud: lf-cloud + openstack-heat-template: csit-2-instance-type.yaml + openstack-heat-template-dir: 'openstack-hot' + + odl_system_count: 1 + odl_system_flavor: odl-highcpu-4 + odl_system_image: ZZCI - CentOS 7 - builder - x86_64 - 20181010-215635.956 + tools_system_count: 2 + tools_system_flavor: odl-highcpu-2 + tools_system_image: ZZCI - Ubuntu 16.04 - mininet-ovs-25 - 20181029-223449.514 + + ##################### + # Job configuration # + ##################### + + builders: + - lf-infra-pre-build + - lf-stack-create: + openstack-cloud: '{openstack-cloud}' + openstack-heat-template: '{openstack-heat-template}' + openstack-heat-template-dir: '{openstack-heat-template-dir}' + openstack-heat-parameters: | + vm_0_count: '{odl_system_count}' + vm_0_flavor: '{odl_system_flavor}' + vm_0_image: '{odl_system_image}' + vm_1_count: '{tools_system_count}' + vm_1_flavor: '{tools_system_flavor}' + vm_1_image: '{tools_system_image}' diff --git a/.jjb-test/lf-openstack/lf-stack-delete-minimal.yaml b/.jjb-test/lf-openstack/lf-stack-delete-minimal.yaml new file mode 100644 index 00000000..bb1f2f9b --- /dev/null +++ b/.jjb-test/lf-openstack/lf-stack-delete-minimal.yaml @@ -0,0 +1,17 @@ +--- +- job-template: + name: stack-delete-test + + ##################### + # Default variables # + ##################### + + openstack-cloud: lf-cloud + + ##################### + # Job configuration # + ##################### + + publishers: + - lf-stack-delete: + openstack-cloud: '{openstack-cloud}' diff --git a/docs/jjb/lf-openstack-heat.rst b/docs/jjb/lf-openstack-heat.rst new file mode 100644 index 00000000..2e96a01d --- /dev/null +++ b/docs/jjb/lf-openstack-heat.rst @@ -0,0 +1,111 @@ +############## +OpenStack Heat +############## + +This section contains a series of macros for projects that need to spin up full +test labs using HEAT scripts. + +Job Setup +========= + +The 2 macros :ref:`lf-stack-create` & :ref:`lf-stack-delete` are companion +macros and used together when constructing a job template that needs to spin up +a full integration lab using Heat Orchestration Templates (HOT). + +Example Usage: + +.. code-block:: yaml + + - job-template: + name: csit-test + + ##################### + # Default variables # + ##################### + + openstack-cloud: vex + openstack-heat-template: csit-2-instance-type.yaml + openstack-heat-template-dir: 'openstack-hot' + + odl_system_count: 1 + odl_system_flavor: odl-highcpu-4 + odl_system_image: ZZCI - CentOS 7 - builder - x86_64 - 20181010-215635.956 + tools_system_count: 1 + tools_system_flavor: odl-highcpu-2 + tools_system_image: ZZCI - Ubuntu 16.04 - mininet-ovs-25 - 20181029-223449.514 + + ##################### + # Job configuration # + ##################### + + builders: + - lf-infra-pre-build + - lf-stack-create: + openstack-cloud: '{openstack-cloud}' + openstack-heat-template: '{openstack-heat-template}' + openstack-heat-template-dir: '{openstack-heat-template-dir}' + openstack-heat-parameters: | + vm_0_count: '{odl_system_count}' + vm_0_flavor: '{odl_system_flavor}' + vm_0_image: '{odl_system_image}' + vm_1_count: '{tools_system_count}' + vm_1_flavor: '{tools_system_flavor}' + vm_1_image: '{tools_system_image}' + + publishers: + - lf-stack-delete: + openstack-cloud: '{openstack-cloud}' + + +Macros +====== + +.. _lf-stack-create: + +lf-stack-create +--------------- + +Creates an OpenStack stack as configured by the job. Name pattern of stack is +``$SILO-$JOB_NAME-$BUILD_NUMBER``. + +Requires ``lf-infra-pre-build`` macro to run first to install the +``openstack`` and ``lftools`` packages. + +Requires a Config File Provider configuration for clouds.yaml named +``clouds-yaml``. + +:Required Parameters: + + :openstack-cloud: The ``OS_CLOUD`` variable to pass to OpenStack client. + (Docs: https://docs.openstack.org/python-openstackclient) + :openstack-heat-template: Name of template file to use when running stack + create. + :openstack-heat-template-dir: Directory in the ci-management repo + containing the OpenStack heat templates. + +Example: + +.. literalinclude:: ../../.jjb-test/lf-openstack/lf-stack-create-minimal.yaml + +.. _lf-stack-delete: + +lf-stack-delete +--------------- + +Deletes the stack associated with this job. Name pattern of stack is +``$SILO-$JOB_NAME-$BUILD_NUMBER``. + +Requires ``lf-infra-pre-build`` macro to run first to install the +``openstack`` and ``lftools`` packages. + +Requires a Config File Provider configuration for clouds.yaml named +``clouds-yaml``. + +:Required Parameters: + + :openstack-cloud: The ``OS_CLOUD`` variable to pass to OpenStack client. + (Docs: https://docs.openstack.org/python-openstackclient) + +Example: + +.. literalinclude:: ../../.jjb-test/lf-openstack/lf-stack-delete-minimal.yaml diff --git a/jjb/lf-macros.yaml b/jjb/lf-macros.yaml index f608fedb..04d5e37b 100644 --- a/jjb/lf-macros.yaml +++ b/jjb/lf-macros.yaml @@ -535,24 +535,6 @@ - '**/*.jenkins-trigger' fail-build: false -- publisher: - name: lf-stack-delete - publishers: - - postbuildscript: - builders: - - role: BOTH - build-on: - - ABORTED - - FAILURE - - SUCCESS - - UNSTABLE - build-steps: - - shell: | - #!/bin/bash -l - echo "Deleting $STACK_NAME" - lftools openstack stack delete "$STACK_NAME" - mark-unstable-if-failed: false - ####### # SCM # ####### diff --git a/jjb/lf-openstack-heat.yaml b/jjb/lf-openstack-heat.yaml new file mode 100644 index 00000000..ee60fe26 --- /dev/null +++ b/jjb/lf-openstack-heat.yaml @@ -0,0 +1,43 @@ +--- +- builder: + name: lf-stack-create + builders: + - inject: + properties-content: | + OS_CLOUD={openstack-cloud} + OS_STACK_NAME=$SILO-$JOB_NAME-$BUILD_NUMBER + OS_STACK_TEMPLATE={openstack-heat-template} + OS_STACK_TEMPLATE_DIR={openstack-heat-template-dir} + - config-file-provider: + files: + - file-id: clouds-yaml + target: '$HOME/.config/openstack/clouds.yaml' + - shell: !include-raw: ../shell/openstack-stack-parameters.sh + - shell: !include-raw-escape: ../shell/openstack-stack-create.sh + - shell: !include-raw-escape: ../shell/openstack-stack-copy-ssh-keys.sh + +- publisher: + name: lf-stack-delete + publishers: + - postbuildscript: + builders: + - role: BOTH + build-on: + - ABORTED + - FAILURE + - SUCCESS + - UNSTABLE + build-steps: + - inject: + properties-content: | + OS_CLOUD={openstack-cloud} + OS_STACK_NAME=$SILO-$JOB_NAME-$BUILD_NUMBER + - config-file-provider: + files: + - file-id: clouds-yaml + target: '$HOME/.config/openstack/clouds.yaml' + - shell: | + #!/bin/bash -l + echo "Deleting $OS_STACK_NAME" + lftools openstack --os-cloud "$OS_CLOUD" stack delete "$OS_STACK_NAME" + mark-unstable-if-failed: false diff --git a/releasenotes/notes/heat-macros-d3e694add6081aac.yaml b/releasenotes/notes/heat-macros-d3e694add6081aac.yaml new file mode 100644 index 00000000..45fbf62d --- /dev/null +++ b/releasenotes/notes/heat-macros-d3e694add6081aac.yaml @@ -0,0 +1,14 @@ +--- +features: + - | + New ``lf-stack-create`` macro allows job-templates to setup a OpenStack + Heat stack, useful for spinning up CSIT labs to run integration tests + against. Use with the ``lf-stack-delete`` macro. + +upgrade: + - | + ``lf-stack-delete`` has been modified to be a companion macro to + ``lf-stack-create`` in order to cleanup the stack at the end of a job run. + It now includes a required parameter **openstack-cloud** to choose the + ``clouds.yaml`` cloud configuration for the project. Existing users of + this macro will need to update their job templates accordingly. diff --git a/shell/openstack-stack-copy-ssh-keys.sh b/shell/openstack-stack-copy-ssh-keys.sh new file mode 100644 index 00000000..463a386e --- /dev/null +++ b/shell/openstack-stack-copy-ssh-keys.sh @@ -0,0 +1,73 @@ +#!/bin/bash -l +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2017, 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +echo "---> Copy SSH public keys to CSIT lab" + +os_cloud="${OS_CLOUD:-vex}" +stack_name="${OS_STACK_NAME}" + +function copy_ssh_keys() { + if [ -z "$1" ]; then + >&2 echo "ERROR: Missing required arguments." + >&2 echo "Usage: copy_ssh_keys IP_ADDRESS" + exit 1 + fi + + local ip_address="$1" + RETRIES=60 + + for i in $(seq 1 $RETRIES); do + if ssh-copy-id -i /home/jenkins/.ssh/id_rsa.pub "jenkins@${ip_address}" > /dev/null 2>&1; then + ssh "jenkins@${ip_address}" 'echo "$(facter ipaddress_eth0) $(/bin/hostname)" | sudo tee -a /etc/hosts' + echo "Successfully copied public keys to slave ${ip_address}" + break + elif [ "$i" -eq $RETRIES ]; then + echo "SSH not responding on ${ip_address} after $RETIRES tries. Giving up." + + server=$(openstack port list -f value -c device_id --fixed-ip ip-address="${ip_address}") + echo "Dumping console logs for $server ${ip_address}" + openstack --os-cloud "$os_cloud" console log show "$server" + + exit 1 + else + echo "SSH not responding on ${ip_address}. Retrying in 10 seconds..." + sleep 10 + fi + + # ping test to see if connectivity is available + if ping -c1 "${ip_address}" &> /dev/null; then + echo "Ping to ${ip_address} successful." + else + echo "Ping to ${ip_address} failed." + fi + done +} + +# IP Addresses are returned as a space separated list so word splitting is ok +# shellcheck disable=SC2207 +ip_addresses=($(openstack --os-cloud "$os_cloud" stack show -f json -c outputs "$stack_name" | + jq -r '.outputs[] | + select(.output_key | match("^vm_[0-9]+_ips$")) | + .output_value | .[]')) +pids="" +for ip in "${ip_addresses[@]}"; do + ( copy_ssh_keys "$ip" ) & + # Store PID of process + pids+=" $!" +done +for p in $pids; do + if wait "$p"; then + echo "Process $p ready." + else + echo "Process $p timed out waiting for SSH." + exit 1 + fi +done +echo "SSH ready on all stack servers." diff --git a/shell/openstack-stack-create.sh b/shell/openstack-stack-create.sh new file mode 100644 index 00000000..478ac5be --- /dev/null +++ b/shell/openstack-stack-create.sh @@ -0,0 +1,26 @@ +#!/bin/bash -l +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2017, 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +echo "---> Create HEAT stack" + +os_cloud="${OS_CLOUD:-vex}" +stack_name="${OS_STACK_NAME}" +stack_template="${OS_STACK_TEMPLATE}" +stack_template_dir="/opt/ciman/${OS_STACK_TEMPLATE_DIR:-openstack-hot}" +stack_parameters="$WORKSPACE/stack-parameters.yaml" + +set -eux -o pipefail + +openstack --os-cloud "$os_cloud" limits show --absolute + +pushd "$stack_template_dir" || exit 1 +lftools openstack --os-cloud "$os_cloud" stack create \ + "$stack_name" "$stack_template" "$stack_parameters" +popd diff --git a/shell/openstack-stack-parameters.sh b/shell/openstack-stack-parameters.sh new file mode 100644 index 00000000..6302dc48 --- /dev/null +++ b/shell/openstack-stack-parameters.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +echo "---> Create parameters file for OpenStack HOT" +stack_parameters="$WORKSPACE/stack-parameters.yaml" +tmp_params="$WORKSPACE/params.yaml" +JOB_SUM=$(echo "$JOB_NAME" | sum | awk '{{ print $1 }}') +VM_NAME="$JOB_SUM-$BUILD_NUMBER" + +cat > "$tmp_params" << EOF +{openstack-heat-parameters} +job_name: '$VM_NAME' +silo: '$SILO' +EOF + +echo "OpenStack Heat parameters generated" +echo "-----------------------------------" +echo "parameters:" > "$stack_parameters" +cat "$tmp_params" | sed 's/^/ /' >> "$stack_parameters" +cat "$stack_parameters" +rm "$tmp_params"