Skip to content
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

Added job to remove uninstalled packs from Mongo database #396

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions templates/configmaps_cleanup-packs-script.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions templates/jobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)) . }}'
Expand Down Expand Up @@ -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 }}
Expand Down
Loading