openstack-image-cleanup: false
openstack-image-cleanup-age: 42
openstack-image-protect: false
+ openstack-server-cleanup: false
openstack-stack-cleanup: false
openstack-volume-cleanup: false
:Required Parameters:
+ :jjb-cache: Location of Jenkins Job Builder (JJB) cache used for jjb
+ jobs.
:jjb-version: Version of Jenkins Job Builder (JJB) to install and use in
the jjb jobs.
:build-days-to-keep: Days to keep build logs in Jenkins. (default: 7)
:build-timeout: Timeout in minutes before aborting build. (default: 10)
:git-url: URL clone project from. (default: $GIT_URL/$PROJECT)
+ :jjb-cache: JJB cache location. (default: $HOME/.cache/jenkins_jobs)
:jjb-version: JJB version to install. (default: see job-template)
:stream: Keyword that can be used to represent a release code-name.
Often the same as the branch. (default: master)
:build-days-to-keep: Days to keep build logs in Jenkins. (default: 7)
:build-timeout: Timeout in minutes before aborting build. (default: 10)
:git-url: URL clone project from. (default: $GIT_URL/$PROJECT)
+ :jjb-cache: JJB cache location. (default: $HOME/.cache/jenkins_jobs)
:jjb-version: JJB version to install. (default: see job-template)
:stream: Keyword that can be used to represent a release code-name.
Often the same as the branch. (default: master)
:build-days-to-keep: Days to keep build logs in Jenkins. (default: 7)
:build-timeout: Timeout in minutes before aborting build. (default: 10)
:git-url: URL clone project from. (default: $GIT_URL/$PROJECT)
+ :jjb-cache: JJB cache location. (default: $HOME/.cache/jenkins_jobs)
:jjb-version: JJB version to install. (default: see job-template)
:stream: Keyword that can be used to represent a release code-name.
Often the same as the branch. (default: master)
removal. (default: 30)
:openstack-image-protect: Whether or not to run the image protect script.
(default: true)
+ :openstack-server-cleanup: Whether or not to run the server cleanup script.
+ (default: true)
:openstack-stack-cleanup: Whether or not to run the stack cleanup script.
(default: true)
:openstack-volume-cleanup: Whether or not to run the volume cleanup script.
name: JJB_VERSION
default: '{jjb-version}'
description: Jenkins Job Builder version to download and install.
+ - string:
+ name: XDG_CACHE_HOME
+ default: '{jjb-cache}'
+ description: Jenkins Job Builder cache location.
- parameter:
name: lf-packer-parameters
build-timeout: 10
disable-job: false
github-url: 'https://github.com'
+ jjb-cache: '$HOME/.cache/jenkins_jobs'
jjb-version: 2.2.1
stream: master
submodule-recursive: true
branch: '{branch}'
lftools-version: '{lftools-version}'
- lf-infra-jjb-parameters:
+ jjb-cache: '{jjb-cache}'
jjb-version: '{jjb-version}'
wrappers:
parameters:
- lf-infra-jjb-parameters:
+ jjb-cache: '{jjb-cache}'
jjb-version: '{jjb-version}'
triggers:
openstack-image-cleanup: true
openstack-image-cleanup-age: 30
openstack-image-protect: true
+ openstack-server-cleanup: true
openstack-stack-cleanup: true
openstack-volume-cleanup: true
stream: master
condition-expression: '{openstack-stack-cleanup}'
steps:
- shell: !include-raw-escape: ../shell/openstack-cleanup-orphaned-stacks.sh
+ # Servers
+ - conditional-step:
+ condition-kind: boolean-expression
+ condition-expression: '{openstack-server-cleanup}'
+ steps:
+ - shell: !include-raw-escape: ../shell/openstack-cleanup-orphaned-servers.sh
# Volumes
- conditional-step:
condition-kind: boolean-expression
--- /dev/null
+---
+features:
+ - |
+ Add the ability to configure the location of JJB's cache directory
+ for CI jobs.
--- /dev/null
+---
+features:
+ - |
+ The **openstack-cron** job now has the ability to remove orphaned
+ servers.
--- /dev/null
+#!/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
+##############################################################################
+# Scans OpenStack for orphaned servers
+echo "---> Orphaned servers"
+
+os_cloud="${OS_CLOUD:-vex}"
+jenkins_urls="${JENKINS_URLS:-}"
+
+minion_in_jenkins() {
+ # Usage: minion_in_jenkins STACK_NAME JENKINS_URL [JENKINS_URL...]
+ # Returns: 0 If stack is in Jenkins and 1 if stack is not in Jenkins.
+
+ MINION="${1}"
+
+ minions=()
+ for jenkins in "${@:2}"; do
+ JENKINS_URL="$jenkins/computer/api/json?tree=computer[displayName]"
+ resp=$(curl -s -w "\\n\\n%{http_code}" --globoff -H "Content-Type:application/json" "$JENKINS_URL")
+ json_data=$(echo "$resp" | head -n1)
+ status=$(echo "$resp" | awk 'END {print $NF}')
+
+ if [ "$status" != 200 ]; then
+ >&2 echo "ERROR: Failed to fetch data from $JENKINS_URL with status code $status"
+ >&2 echo "$resp"
+ exit 1
+ fi
+
+ # We purposely want to wordsplit here to combine the arrays
+ # shellcheck disable=SC2206,SC2207
+ minions=(${minions[@]} $(echo "$json_data" | \
+ jq -r '.computer[].displayName' | grep -v master)
+ )
+ done
+
+ if [[ "${minions[*]}" =~ $MINION ]]; then
+ return 0
+ fi
+
+ return 1
+}
+
+##########################
+## FETCH ACTIVE MINIONS ##
+##########################
+# Fetch server list before fetching active minions to minimize race condition
+# where we might be trying to delete servers while jobs are trying to start
+
+mapfile -t OS_SERVERS < <(openstack --os-cloud "$os_cloud" server list -f value -c "Name" | grep -E 'prd|snd')
+
+echo "-----> Active servers"
+for server in "${OS_SERVERS[@]}"; do
+ echo "$server"
+done
+
+
+#############################
+## DELETE ORPHANED SERVERS ##
+#############################
+echo "-----> Delete orphaned servers"
+
+# Search for servers not in use by any active Jenkins systems and remove them.
+for server in "${OS_SERVERS[@]}"; do
+ # jenkins_urls intentially needs globbing to be passed a separate params.
+ # needs to be globbed.
+ # shellcheck disable=SC2153,SC2086
+ if minion_in_jenkins "$server" $jenkins_urls; then
+ # No need to delete server if it is still attached to Jenkins
+ continue
+ else
+ echo "Deleting orphaned server: $server"
+ lftools openstack --os-cloud "$os_cloud" \
+ server remove --minutes 15 "$server"
+ fi
+done