theshire/.taskfiles/volsync/Taskfile.yaml

98 lines
4.6 KiB
YAML
Raw Permalink Normal View History

2024-07-08 10:53:12 -05:00
---
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: '3'
2024-07-08 10:53:12 -05:00
2024-11-06 10:46:07 -06:00
# Taskfile used to manage certain VolSync tasks for a given application, limitations are as followed.
2024-07-08 10:53:12 -05:00
# 1. Fluxtomization, HelmRelease, PVC, ReplicationSource all have the same name (e.g. plex)
# 2. ReplicationSource and ReplicationDestination are a Restic repository
2024-11-06 10:46:07 -06:00
# 3. Each application only has one PVC that is being replicated
2024-07-08 10:53:12 -05:00
vars:
VOLSYNC_RESOURCES_DIR: '{{.ROOT_DIR}}/.taskfiles/volsync/resources'
2024-07-08 10:53:12 -05:00
tasks:
2024-07-08 10:53:12 -05:00
state-*:
2024-11-06 10:46:07 -06:00
desc: Suspend or resume Volsync [CLUSTER=main]
2024-07-08 10:53:12 -05:00
cmds:
2024-11-06 10:46:07 -06:00
- flux --namespace flux-system {{.STATE}} kustomization volsync
- flux --namespace volsync-system {{.STATE}} helmrelease volsync
- kubectl --namespace volsync-system scale deployment volsync --replicas {{if eq .STATE "suspend"}}0{{else}}1{{end}}
2024-07-08 10:53:12 -05:00
vars:
STATE: '{{index .MATCH 0}}'
2024-07-08 10:53:12 -05:00
requires:
vars: [CLUSTER]
2024-11-06 10:46:07 -06:00
preconditions:
- '[[ "{{.STATE}}" == "suspend" || "{{.STATE}}" == "resume" ]]'
- which flux kubectl
2024-07-08 10:53:12 -05:00
unlock:
2024-11-06 10:46:07 -06:00
desc: Unlock all restic source repos [CLUSTER=main]
cmds:
- for: { var: SOURCES, split: "\n" }
cmd: kubectl --namespace {{splitList "," .ITEM | first}} patch --field-manager=flux-client-side-apply replicationsources {{splitList "," .ITEM | last}} --type merge --patch "{\"spec\":{\"restic\":{\"unlock\":\"{{now | unixEpoch}}\"}}}"
vars:
SOURCES:
sh: kubectl get replicationsources --all-namespaces --no-headers --output=jsonpath='{range .items[*]}{.metadata.namespace},{.metadata.name}{"\n"}{end}'
2024-07-08 10:53:12 -05:00
requires:
vars: [CLUSTER]
2024-11-06 10:46:07 -06:00
preconditions:
- which kubectl
2024-07-08 10:53:12 -05:00
snapshot:
2024-11-06 10:46:07 -06:00
desc: Snapshot an app [CLUSTER=main] [NS=default] [APP=required]
2024-07-08 10:53:12 -05:00
cmds:
- kubectl --namespace {{.NS}} patch replicationsources {{.APP}} --type merge -p '{"spec":{"trigger":{"manual":"{{now | unixEpoch}}"}}}'
- until kubectl --namespace {{.NS}} get job/{{.JOB}} &>/dev/null; do sleep 5; done
- kubectl --namespace {{.NS}} wait job/{{.JOB}} --for=condition=complete --timeout=120m
2024-07-08 10:53:12 -05:00
vars:
NS: '{{.NS | default "default"}}'
JOB: volsync-src-{{.APP}}
requires:
vars: [CLUSTER, APP]
2024-07-08 10:53:12 -05:00
preconditions:
- kubectl --namespace {{.NS}} get replicationsources {{.APP}}
2024-11-06 10:46:07 -06:00
- which kubectl
2024-07-08 10:53:12 -05:00
restore:
2024-11-06 10:46:07 -06:00
desc: Restore an app [CLUSTER=main] [NS=default] [APP=required] [PREVIOUS=required]
2024-07-08 10:53:12 -05:00
cmds:
2024-11-06 10:46:07 -06:00
# Suspend
- flux --namespace flux-system suspend kustomization {{.APP}}
- flux --namespace {{.NS}} suspend helmrelease {{.APP}}
- kubectl --namespace {{.NS}} scale {{.CONTROLLER}}/{{.APP}} --replicas 0
- kubectl --namespace {{.NS}} wait pod --for=delete --selector="app.kubernetes.io/name={{.APP}}" --timeout=5m
2024-11-06 10:46:07 -06:00
# Restore
- minijinja-cli {{.VOLSYNC_RESOURCES_DIR}}/replicationdestination.yaml.j2 | kubectl apply --server-side --filename -
- until kubectl --namespace {{.NS}} get job/volsync-dst-{{.APP}}-manual &>/dev/null; do sleep 5; done
- kubectl --namespace {{.NS}} wait job/volsync-dst-{{.APP}}-manual --for=condition=complete --timeout=120m
- kubectl --namespace {{.NS}} delete replicationdestination {{.APP}}-manual
# Resume
- flux --namespace flux-system resume kustomization {{.APP}}
- flux --namespace {{.NS}} resume helmrelease {{.APP}}
- flux --namespace {{.NS}} reconcile helmrelease {{.APP}} --force
- kubectl --namespace {{.NS}} wait pod --for=condition=ready --selector="app.kubernetes.io/name={{.APP}}" --timeout=5m
2024-07-08 10:53:12 -05:00
vars:
NS: '{{.NS | default "default"}}'
CONTROLLER:
sh: kubectl --namespace {{.NS}} get deployment {{.APP}} &>/dev/null && echo deployment || echo statefulset
2024-11-06 10:46:07 -06:00
env:
NS: '{{.NS}}'
APP: '{{.APP}}'
PREVIOUS: '{{.PREVIOUS}}'
CLAIM:
sh: kubectl --namespace {{.NS}} get replicationsources/{{.APP}} --output=jsonpath="{.spec.sourcePVC}"
ACCESS_MODES:
sh: kubectl --namespace {{.NS}} get replicationsources/{{.APP}} --output=jsonpath="{.spec.restic.accessModes}"
STORAGE_CLASS_NAME:
sh: kubectl --namespace {{.NS}} get replicationsources/{{.APP}} --output=jsonpath="{.spec.restic.storageClassName}"
PUID:
sh: kubectl --namespace {{.NS}} get replicationsources/{{.APP}} --output=jsonpath="{.spec.restic.moverSecurityContext.runAsUser}"
PGID:
sh: kubectl --namespace {{.NS}} get replicationsources/{{.APP}} --output=jsonpath="{.spec.restic.moverSecurityContext.runAsGroup}"
2024-11-06 10:46:07 -06:00
requires:
vars: [CLUSTER, APP, PREVIOUS]
2024-07-08 10:53:12 -05:00
preconditions:
- test -f {{.VOLSYNC_RESOURCES_DIR}}/replicationdestination.yaml.j2
2024-11-06 10:46:07 -06:00
- which flux kubectl minijinja-cli