-
Notifications
You must be signed in to change notification settings - Fork 105
pulp-cleanup: add nightly Jenkins job for stale Pulp resource cleanup #2644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import groovy.transform.Field | ||
|
|
||
| @Field String pulpServerUrl = "https://pulp.front.sepia.ceph.com" | ||
|
|
||
|
|
||
| pipeline { | ||
| agent { | ||
| label 'small' | ||
| } | ||
|
|
||
| options { | ||
| timeout(time: 1, unit: 'HOURS') | ||
| timestamps() | ||
| } | ||
|
|
||
| stages { | ||
| stage('Checkout') { | ||
| steps { | ||
| checkout scm | ||
| } | ||
| } | ||
|
|
||
| stage('Setup Pulp Client') { | ||
| steps { | ||
| script { | ||
| withCredentials([ | ||
| usernamePassword( | ||
| credentialsId: 'pulp-admin-auth', | ||
| usernameVariable: 'PULP_USERNAME', | ||
| passwordVariable: 'PULP_PASSWORD' | ||
| ) | ||
| ]) { | ||
| echo "Setting up Pulp Client..." | ||
| sh """#!/bin/bash -ex | ||
| export PULP_SERVER_URL="${pulpServerUrl}" && ./scripts/setup_pulp.sh | ||
| """ | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| stage('Setup yq') { | ||
| steps { | ||
| script { | ||
| echo "Setting up yq..." | ||
| sh """#!/bin/bash -ex | ||
| source ./scripts/setup_yq.sh | ||
| """ | ||
| } | ||
| } | ||
| } | ||
|
|
||
| stage('Execute Pulp Cleanup for Ceph') { | ||
| when { | ||
| expression { | ||
| params.CEPH | ||
| } | ||
| } | ||
| steps { | ||
| script { | ||
| def project = "ceph" | ||
| echo "Starting Pulp Cleanup for project: ${project}" | ||
| sh "./pulp-cleanup/build/cleanup.sh ${project}" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Purge stale Pulp repositories and distributions per purge-policy.yaml. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| readonly PROJECT="$1" | ||
| readonly PURGE_POLICY_FILE="${WORKSPACE}/scripts/purge-policy.yaml" | ||
|
|
||
| readonly LABEL="ref" | ||
| readonly PULP_LIST_LIMIT=1000 | ||
| readonly PULP_TYPES=(rpm deb) | ||
| readonly PULP_RESOURCES=(distribution repository) | ||
|
|
||
| # Default protection time in hours | ||
| readonly PROTECTION_TIME=24 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minutes. Do 1440. |
||
|
|
||
| log() { | ||
| echo "[pulp_cleanup] $*" >&2 | ||
| } | ||
|
|
||
| get_policy_refs() { | ||
| local project="$1" | ||
| local label="$2" | ||
|
|
||
| yq -r ".${project}.${label} | keys[]" "${PURGE_POLICY_FILE}" | ||
| } | ||
|
|
||
| read_default_purge_policy() { | ||
| local project="$1" | ||
| local -n _days="$2" | ||
| local policy_line | ||
|
|
||
| policy_line=$( | ||
| yq -r \ | ||
| ".${project}.default | [(.days // \"\")] | @tsv" \ | ||
| "${PURGE_POLICY_FILE}" | ||
| ) | ||
| IFS=$'\t' read -r _days <<< "${policy_line}" | ||
| } | ||
|
|
||
| fetch_pulp_resource_json() { | ||
| local type="$1" | ||
| local resource="$2" | ||
| local project="$3" | ||
| local label_select="$4" | ||
|
|
||
| pulp "${type}" "${resource}" list \ | ||
| --limit "${PULP_LIST_LIMIT}" \ | ||
| --ordering '-pulp_created' \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| --label-select project="${project}","${label_select}" \ | ||
| 2>&1 | sed -n '/^\[/,$p' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The job/script will fail silently with this. Make a failed |
||
| } | ||
|
|
||
| filter_resource_names_by_age() { | ||
| local pulp_json="$1" | ||
| local days="$2" | ||
|
|
||
| if [ -z "${days}" ]; then | ||
| echo "${pulp_json}" | jq -r '.[].name' | ||
| return 0 | ||
| fi | ||
|
|
||
| echo "${pulp_json}" | jq -r --arg days "${days}" ' | ||
| .[] | ||
| | select( | ||
| (.pulp_created | split(".")[0] + "Z" | fromdateiso8601) | ||
| < (now - ($days | tonumber) * 86400) | ||
| ) | ||
| | .name | ||
| ' | ||
|
Comment on lines
+64
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should filter on Also, we'll have a So if the repository matches any of the named branches in If the repository doesn't match a named branch in |
||
| } | ||
|
|
||
| list_stale_pulp_resources() { | ||
| local type="$1" | ||
| local resource="$2" | ||
| local project="$3" | ||
| local label_select="$4" | ||
| local days="$5" | ||
| local pulp_json resource_list | ||
|
|
||
| pulp_json=$( | ||
| fetch_pulp_resource_json \ | ||
| "${type}" "${resource}" "${project}" "${label_select}" | ||
| ) | ||
| resource_list=$( | ||
| filter_resource_names_by_age "${pulp_json}" "${days}" | ||
| ) | ||
| echo "${resource_list}" | ||
| } | ||
|
|
||
| destroy_pulp_resources() { | ||
| local type="$1" | ||
| local resource="$2" | ||
| local resource_list="$3" | ||
| local name | ||
|
|
||
| if [ -z "${resource_list}" ]; then | ||
| return 0 | ||
| fi | ||
|
|
||
| while IFS= read -r name; do | ||
| [ -z "${name}" ] && continue | ||
| log "Destroying ${type} ${resource}: ${name}" | ||
| if ! pulp "${type}" "${resource}" destroy \ | ||
| --name "${name}"; then | ||
| log "ERROR: Failed to destroy ${type} ${resource}: ${name}" | ||
| return 1 | ||
| fi | ||
| done <<< "${resource_list}" | ||
| } | ||
|
|
||
| collect_stale_resources_for_ref() { | ||
| local project="$1" | ||
| local label_select="$2" | ||
| local days="$3" | ||
| local type resource key | ||
|
|
||
| declare -n _stale_resources="$4" | ||
|
|
||
| for type in "${PULP_TYPES[@]}"; do | ||
| for resource in "${PULP_RESOURCES[@]}"; do | ||
| key="${type}_${resource}" | ||
| _stale_resources[$key]=$( | ||
| list_stale_pulp_resources \ | ||
| "${type}" "${resource}" "${project}" \ | ||
| "${label_select}" "${days}" | ||
| ) | ||
| log "${key}: ${_stale_resources[$key]:-}" | ||
| done | ||
| done | ||
| } | ||
|
|
||
| purge_resources() { | ||
| local project="$1" | ||
| local label_select="$2" | ||
| local days="$3" | ||
| local type resource | ||
|
|
||
| declare -A stale_resources=() | ||
| log "Purging days=${days:-unset} label_select=${label_select}" | ||
| collect_stale_resources_for_ref "${project}" "${label_select}" "${days}" \ | ||
| stale_resources | ||
|
|
||
| log "stale_resources: ${stale_resources[@]}" | ||
|
|
||
| for type in "${PULP_TYPES[@]}"; do | ||
| destroy_pulp_resources "${type}" "distribution" \ | ||
| "${stale_resources[${type}_distribution]:-}" | ||
| done | ||
| for type in "${PULP_TYPES[@]}"; do | ||
| destroy_pulp_resources "${type}" "repository" \ | ||
| "${stale_resources[${type}_repository]:-}" | ||
| done | ||
| } | ||
|
|
||
| run_package_cleanup() { | ||
| local project="$1" | ||
| local label="$2" | ||
| local days | ||
|
|
||
| mapfile -t policy_refs < <(get_policy_refs "${project}" "${label}") | ||
| _label_select=$( | ||
| printf "${label}!=%s\n" "${policy_refs[@]}" | paste -sd, - | ||
| ) | ||
|
Comment on lines
+162
to
+165
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If anything goes wrong here, this falls back to "delete everything" Gate on a yq failure, missing file, etc. Any error. |
||
| log "label_select: ${_label_select}" | ||
|
|
||
| read_default_purge_policy "${project}" days | ||
| log "days: ${days}" | ||
|
|
||
| purge_resources "${project}" "${_label_select}" "${days}" | ||
| } | ||
|
|
||
| run_orphan_cleanup() { | ||
| log "Applying orphan content purge policy" | ||
| if ! pulp orphan cleanup \ | ||
| --protection-time "${PROTECTION_TIME}"; then | ||
| log "ERROR: Failed to run orphan cleanup" | ||
| return 1 | ||
| fi | ||
| log "Orphan cleanup completed" | ||
| } | ||
|
|
||
| log "Cleaning up project: ${PROJECT}" | ||
|
|
||
| log "Running package cleanup" | ||
| run_package_cleanup "${PROJECT}" "${LABEL}" | ||
|
|
||
| log "Running orphan cleanup" | ||
| run_orphan_cleanup | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other job definitions are |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| - job: | ||
| name: pulp-cleanup-nightly | ||
| disabled: true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intentional? |
||
| display-name: 'Pulp: Cleanup Nightly' | ||
| project-type: pipeline | ||
| description: > | ||
| Nightly parameterized cleanup of stale Teuthology Pulp resources. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest |
||
| defaults: global | ||
|
|
||
| pipeline-scm: | ||
| scm: | ||
| - git: | ||
| url: https://github.com/ceph/ceph-build | ||
| branches: | ||
| - ${{CEPH_BUILD_BRANCH}} | ||
| shallow-clone: true | ||
| submodule: | ||
| disable: true | ||
| wipe-workspace: true | ||
| script-path: pulp-cleanup/build/Jenkinsfile | ||
| lightweight-checkout: true | ||
| do-not-fetch-tags: true | ||
|
|
||
| triggers: | ||
| - timed: '@midnight' | ||
|
|
||
| parameters: | ||
| - string: | ||
| name: CEPH_BUILD_BRANCH | ||
| description: 'Use the Jenkinsfile from this ceph-build branch' | ||
| default: 'main' | ||
| - bool: | ||
| name: CEPH | ||
| description: > | ||
| When true, clean up stale Pulp resources for the ceph project | ||
| per purge-policy.yaml. | ||
| default: true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes more sense to have this in |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| ceph: | ||
| default: | ||
| days: 14 | ||
| keep_minimum: 3 | ||
| ref: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
| reef-nvmeof: | ||
| keep_minimum: 2 | ||
| squid-nvmeof: | ||
| keep_minimum: 2 | ||
| squid-nvmeof-8.0: | ||
| keep_minimum: 2 | ||
| wip-rbd-cgsm-base: | ||
| keep_minimum: 2 | ||
| hammer: | ||
| days: 30 | ||
| keep_minimum: 5 | ||
| infernalis: | ||
| days: 30 | ||
| keep_minimum: 5 | ||
| jewel: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| kraken: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| luminous: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| mimic: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| nautilus: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| octopus: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| pacific: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| quincy: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| reef: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| squid: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| tentacle: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| umbrella: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
| vampire: | ||
| days: 14 | ||
| keep_minimum: 10 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sourcing the script just defines the function. Just run it directly.