From d2d30f1fc3257341424014c1096fa1fe35fdcd91 Mon Sep 17 00:00:00 2001 From: jk464 Date: Thu, 8 Feb 2024 14:41:54 +0000 Subject: [PATCH] Added job to remove uninstalled packs from Mongo database Useful when changing the mounted pack image, that removes previously installed packs --- templates/_helpers.tpl | 31 ++++++++++++++++ .../configmaps_cleanup-packs-script.yaml | 35 +++++++++++++++++++ templates/jobs.yaml | 5 +++ 3 files changed, 71 insertions(+) create mode 100644 templates/configmaps_cleanup-packs-script.yaml diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl index 4b98b21c..ccedff04 100644 --- a/templates/_helpers.tpl +++ b/templates/_helpers.tpl @@ -390,6 +390,37 @@ Merge packs and virtualenvs from st2 with those from st2packs images {{- end }} {{- end -}} +{{- define "stackstorm-ha.cleanup-packs" -}} + {{- if or $.Values.st2.packs.images $.Values.st2.packs.volumes.enabled }} +# Clean Up packs - unregister packs no longer installed +- name: st2-cleanup-packs + image: '{{ .Values.jobs.image.registry }}/sswann/jq:latest' + imagePullPolicy: {{ .Values.image.pullPolicy }} + volumeMounts: + - name: st2-packs-vol + mountPath: /opt/stackstorm/packs-shared + - name: st2-virtualenvs-vol + mountPath: /opt/stackstorm/virtualenvs-shared + - name: st2-cleanup-packs + mountPath: /st2-cleanup-packs.sh + subPath: st2-cleanup-packs.sh + command: + - 'sh' + - '-ec' + - /st2-cleanup-packs.sh + {{- with .Values.securityContext }} + securityContext: {{- toYaml . | nindent 8 }} + {{- end }} + envFrom: + - secretRef: + name: {{ include "stackstorm-ha.secrets.st2Auth" . }} + env: + - name: ST2_AUTH_URL + value: "https://{{ .Release.Name }}-st2auth:9100" + - name: ST2_API_URL + value: "https://{{ .Release.Name }}-st2api:9111" + {{- end }} +{{- end -}} {{/* For custom st2packs-pullSecrets reduce duplicity by defining them here once diff --git a/templates/configmaps_cleanup-packs-script.yaml b/templates/configmaps_cleanup-packs-script.yaml new file mode 100644 index 00000000..e9c8102d --- /dev/null +++ b/templates/configmaps_cleanup-packs-script.yaml @@ -0,0 +1,35 @@ +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ .Release.Name }}-st2-cleanup-packs + labels: {{- include "stackstorm-ha.labels" (list $ "st2") | nindent 4 }} +data: + st2-cleanup-packs.sh: | + #!/bin/bash + + # Generate Token for StackStorm from Credentials + ST2_TOKEN=$(curl --fail -sk -X POST -u ${ST2_AUTH_USERNAME}:${ST2_AUTH_PASSWORD} ${ST2_AUTH_URL}/tokens | jq -r .token) + + # Get the list of all packs from the StackStorm API + packs_in_db=$(curl --fail -sk -H "X-Auth-Token: $ST2_TOKEN" ${ST2_API_URL}/v1/packs | jq -r '.[].ref') + + # Get the list of packs in the /opt/stackstorm/packs-shared directory + packs_in_dir=$(ls -1 /opt/stackstorm/packs-shared) + + # Get the packs in the Database that aren't installed + packs_to_remove=() + for pack in $packs_in_db; do + if ! echo "$packs_in_dir" | grep -q "^$pack$"; then + packs_to_remove+=("$pack") + fi + done + + # Purge old packs from DB + packs_to_remove_json_string=$(printf '%s\n' "${packs_to_remove[@]}" | jq -R . | jq -cs .) + if [ "$packs_to_remove_json_string" == '[""]' ]; then + echo "No packs to remove" + else + echo "Removing the folling packs: ${packs_to_remove_json_string}" + curl --fail -sk -X POST -H "X-Auth-Token: $ST2_TOKEN" -H 'Content-Type: application/json' -d "{\"packs\":${packs_to_remove_json_string}}" ${ST2_API_URL}/v1/packs/uninstall + fi diff --git a/templates/jobs.yaml b/templates/jobs.yaml index 3e77c179..4ebb5048 100644 --- a/templates/jobs.yaml +++ b/templates/jobs.yaml @@ -407,6 +407,7 @@ spec: initContainers: {{- include "stackstorm-ha.init-containers-wait-for-db" . | nindent 6 }} {{- include "stackstorm-ha.packs-initContainers" . | nindent 6 }} + {{- include "stackstorm-ha.cleanup-packs" . | nindent 6 }} {{- if $.Values.jobs.preRegisterContentCommand }} - name: st2-register-content-custom-init image: '{{ template "stackstorm-ha.imageRepository" . }}/st2actionrunner:{{ tpl (.Values.jobs.image.tag | default (.Values.st2actionrunner.image.tag | default .Values.image.tag)) . }}' @@ -459,6 +460,10 @@ spec: {{- include "stackstorm-ha.st2-config-volume" . | nindent 8 }} {{- include "stackstorm-ha.packs-volumes" . | nindent 8 }} {{- include "stackstorm-ha.pack-configs-volume" . | nindent 8 }} + - name: st2-cleanup-packs + configMap: + name: {{ .Release.Name }}-st2-cleanup-packs + defaultMode: 0777 {{- range $.Values.jobs.extra_volumes }} - name: {{ required "Each volume must have a 'name' in jobs.extra_volumes" .name }} {{- tpl (required "Each volume must have a 'volume' definition in jobs.extra_volumes" .volume | toYaml) $ | nindent 10 }}