From 67e0a3b78ec1544c8b67d851cea73ff3777e5c8b Mon Sep 17 00:00:00 2001
From: Dimitra Chatzichrysou <dimitra.chatzichrysou@cern.ch>
Date: Mon, 5 Jul 2021 14:55:33 +0000
Subject: [PATCH] Add helm charts for cluster tasks and run tasks with a job

---
 chart/cluster-tasks/Chart.yaml                | 13 ++++++
 .../cluster-tasks/templates/clear-cache.yaml  | 34 +++++++++++++++
 .../templates/database-backup.yaml            | 35 ++++++++++++++++
 .../templates/database-restore.yaml           | 35 ++++++++++++++++
 chart/cluster-tasks/values.yaml               |  4 ++
 examples/clear-cache-taskrun.yaml             |  6 ++-
 examples/database-backup-taskrun.yaml         |  6 ++-
 examples/database-restore-taskrun.yaml        |  6 ++-
 examples/publish-unpublish-taskrun.yaml       | 16 --------
 roles/role.yaml                               | 41 -------------------
 tasks/clear-cache.yaml                        | 25 -----------
 tasks/database-backup.yaml                    | 25 -----------
 tasks/database-restore.yaml                   | 25 -----------
 tasks/publish-unpublish.yaml                  | 26 ------------
 14 files changed, 136 insertions(+), 161 deletions(-)
 create mode 100644 chart/cluster-tasks/Chart.yaml
 create mode 100644 chart/cluster-tasks/templates/clear-cache.yaml
 create mode 100644 chart/cluster-tasks/templates/database-backup.yaml
 create mode 100644 chart/cluster-tasks/templates/database-restore.yaml
 create mode 100644 chart/cluster-tasks/values.yaml
 delete mode 100644 examples/publish-unpublish-taskrun.yaml
 delete mode 100644 roles/role.yaml
 delete mode 100644 tasks/clear-cache.yaml
 delete mode 100644 tasks/database-backup.yaml
 delete mode 100644 tasks/database-restore.yaml
 delete mode 100644 tasks/publish-unpublish.yaml

diff --git a/chart/cluster-tasks/Chart.yaml b/chart/cluster-tasks/Chart.yaml
new file mode 100644
index 0000000..f9d8e2d
--- /dev/null
+++ b/chart/cluster-tasks/Chart.yaml
@@ -0,0 +1,13 @@
+apiVersion: v2
+name: drupal-operations
+home: https://gitlab.cern.ch/drupal/paas/drupal-operations
+description: A Helm chart for Deployment of Cluster Tasks
+version: 0.1.0
+icon: https://raw.githubusercontent.com/cdfoundation/artwork/main/tekton/horizontal/color/tekton-horizontal-color.png
+keywords:
+- drupal
+- tekton
+- clustertasks
+maintainers:
+- name: CERN IT-CDA-WF
+  email: drupal-admins@cern.ch
diff --git a/chart/cluster-tasks/templates/clear-cache.yaml b/chart/cluster-tasks/templates/clear-cache.yaml
new file mode 100644
index 0000000..73ad510
--- /dev/null
+++ b/chart/cluster-tasks/templates/clear-cache.yaml
@@ -0,0 +1,34 @@
+apiVersion: tekton.dev/v1beta1
+kind: ClusterTask
+metadata:
+  name: clear-cache
+spec:
+  params:
+    - name: drupalSite
+      type: string
+      description: The name of the drupal site
+    - name: namespace
+      type: string
+      description: The namespace of the drupal site
+  workspaces:
+    - name: job
+  steps:
+    - name: clear-cache
+      image: {{ .Values.openshiftCli.image }}
+      command:
+        - /bin/sh
+        - '-c'
+      args:
+      - |-
+        echo "--------------------------- Clear cache ---------------------------"
+        echo "Drupal site name: $(params.drupalSite)"
+        export TASK=clear-cache
+        export DRUPALSITE=$(params.drupalSite)
+        export NAMESPACE=$(params.namespace)
+        export SERVING_POD_IMAGE=$(oc get -n $(params.namespace) drupalsite/$(params.drupalSite) -o=jsonpath='{.status.servingPodImage}')
+        envsubst < $(workspaces.job.path)/{{ .Values.jobTemplateFile }} | oc create -n $(params.namespace) -f -
+        oc wait --for=condition=complete job/$TASK-$(params.drupalSite)
+        jobStatus=$(oc get job/$TASK-$(params.drupalSite) -o=jsonpath='{.status.conditions[*].type}' | grep -i -E 'failed|complete' || echo "Failed")
+        echo "Job status: $jobStatus"
+        echo "Job logs:"
+        oc logs job/$TASK-$DRUPALSITE -n $(params.namespace)
diff --git a/chart/cluster-tasks/templates/database-backup.yaml b/chart/cluster-tasks/templates/database-backup.yaml
new file mode 100644
index 0000000..b6003d4
--- /dev/null
+++ b/chart/cluster-tasks/templates/database-backup.yaml
@@ -0,0 +1,35 @@
+apiVersion: tekton.dev/v1beta1
+kind: ClusterTask
+metadata:
+  name: database-backup
+spec:
+  params:
+    - name: drupalSite
+      type: string
+      description: The name of the drupal site
+    - name: namespace
+      type: string
+      description: The namespace of the drupal site
+  workspaces:
+    - name: job
+  steps:
+    - name: database-backup
+      image: {{ .Values.openshiftCli.image }}
+      command:
+        - /bin/sh
+        - '-c'
+      args:
+      - |-
+        echo "--------------------------- Database backup ---------------------------"
+        echo "Drupal site name: $(params.drupalSite)"
+        export TASK=database-backup
+        export DRUPALSITE=$(params.drupalSite)
+        export NAMESPACE=$(params.namespace)
+        export SERVING_POD_IMAGE=$(oc get -n $(params.namespace) drupalsite/$(params.drupalSite) -o=jsonpath='{.status.servingPodImage}')
+        export ARGS="-f dbBackUp.sql"
+        envsubst < $(workspaces.job.path)/{{ .Values.jobTemplateFile }} | oc create -n $(params.namespace) -f -
+        oc wait --for=condition=complete job/$TASK-$(params.drupalSite)
+        jobStatus=$(oc get job/$TASK-$(params.drupalSite) -o=jsonpath='{.status.conditions[*].type}' | grep -i -E 'failed|complete' || echo "Failed")
+        echo "Job status: $jobStatus"
+        echo "Job logs:"
+        oc logs job/$TASK-$DRUPALSITE -n $(params.namespace)
diff --git a/chart/cluster-tasks/templates/database-restore.yaml b/chart/cluster-tasks/templates/database-restore.yaml
new file mode 100644
index 0000000..45c44ba
--- /dev/null
+++ b/chart/cluster-tasks/templates/database-restore.yaml
@@ -0,0 +1,35 @@
+apiVersion: tekton.dev/v1beta1
+kind: ClusterTask
+metadata:
+  name: database-restore
+spec:
+  params:
+    - name: drupalSite
+      type: string
+      description: The name of the drupal site
+    - name: namespace
+      type: string
+      description: The namespace of the drupal site
+  workspaces:
+    - name: job
+  steps:
+    - name: database-restore
+      image: {{ .Values.openshiftCli.image }}
+      command:
+        - /bin/sh
+        - '-c'
+      args:
+      - |-
+        echo "--------------------------- Database restore ---------------------------"
+        echo "Drupal site name: $(params.drupalSite)"
+        export TASK=database-restore
+        export DRUPALSITE=$(params.drupalSite)
+        export NAMESPACE=$(params.namespace)
+        export SERVING_POD_IMAGE=$(oc get -n $(params.namespace) drupalsite/$(params.drupalSite) -o=jsonpath='{.status.servingPodImage}')
+        export ARGS="-f dbBackUp.sql"
+        envsubst < $(workspaces.job.path)/{{ .Values.jobTemplateFile }} | oc create -n $(params.namespace) -f -
+        oc wait --for=condition=complete job/$TASK-$(params.drupalSite)
+        jobStatus=$(oc get job/$TASK-$(params.drupalSite) -o=jsonpath='{.status.conditions[*].type}' | grep -i -E 'failed|complete' || echo "Failed")
+        echo "Job status: $jobStatus"
+        echo "Job logs:"
+        oc logs job/$TASK-$DRUPALSITE -n $(params.namespace)
diff --git a/chart/cluster-tasks/values.yaml b/chart/cluster-tasks/values.yaml
new file mode 100644
index 0000000..aa18145
--- /dev/null
+++ b/chart/cluster-tasks/values.yaml
@@ -0,0 +1,4 @@
+openshiftCli:
+  image: gitlab-registry.cern.ch/drupal/paas/drupal-operations/openshift-cli:latest
+
+jobTemplateFile: job-operations-template.yaml
diff --git a/examples/clear-cache-taskrun.yaml b/examples/clear-cache-taskrun.yaml
index 4ba9e0f..3adb40c 100644
--- a/examples/clear-cache-taskrun.yaml
+++ b/examples/clear-cache-taskrun.yaml
@@ -1,7 +1,7 @@
 apiVersion: tekton.dev/v1beta1
 kind: TaskRun
 metadata:
-  name: clear-cache
+  generateName: clear-cache-
 spec:
   taskRef:
     name: clear-cache
@@ -11,4 +11,8 @@ spec:
       value: test-dimitra-drupalsite
     - name: namespace
       value: test-dimitra
+  workspaces:
+    - name: job
+      configmap:
+        name: job-operations-template-configmap
   serviceAccountName: tektoncd
diff --git a/examples/database-backup-taskrun.yaml b/examples/database-backup-taskrun.yaml
index eff8764..266e50e 100644
--- a/examples/database-backup-taskrun.yaml
+++ b/examples/database-backup-taskrun.yaml
@@ -1,7 +1,7 @@
 apiVersion: tekton.dev/v1beta1
 kind: TaskRun
 metadata:
-  name: database-backup
+  generateName: database-backup-
 spec:
   taskRef:
     name: database-backup
@@ -11,4 +11,8 @@ spec:
       value: test-dimitra-drupalsite
     - name: namespace
       value: test-dimitra
+  workspaces:
+    - name: job
+      configmap:
+        name: job-operations-template-configmap
   serviceAccountName: tektoncd
diff --git a/examples/database-restore-taskrun.yaml b/examples/database-restore-taskrun.yaml
index 0fcc44a..ab0b9ae 100644
--- a/examples/database-restore-taskrun.yaml
+++ b/examples/database-restore-taskrun.yaml
@@ -1,7 +1,7 @@
 apiVersion: tekton.dev/v1beta1
 kind: TaskRun
 metadata:
-  name: database-restore
+  generateName: database-restore-
 spec:
   taskRef:
     name: database-restore
@@ -11,4 +11,8 @@ spec:
       value: test-dimitra-drupalsite
     - name: namespace
       value: test-dimitra
+  workspaces:
+    - name: job
+      configmap:
+        name: job-operations-template-configmap
   serviceAccountName: tektoncd
diff --git a/examples/publish-unpublish-taskrun.yaml b/examples/publish-unpublish-taskrun.yaml
deleted file mode 100644
index ed775b6..0000000
--- a/examples/publish-unpublish-taskrun.yaml
+++ /dev/null
@@ -1,16 +0,0 @@
-apiVersion: tekton.dev/v1beta1
-kind: TaskRun
-metadata:
-  name: publish-unpublish
-spec:
-  taskRef:
-    name: publish-unpublish
-    kind: ClusterTask
-  params:
-    - name: drupalSite
-      value: test-dimitra-drupalsite
-    - name: namespace
-      value: test-dimitra
-    - name: publish
-      value: "true"
-  serviceAccountName: tektoncd
diff --git a/roles/role.yaml b/roles/role.yaml
deleted file mode 100644
index 40505eb..0000000
--- a/roles/role.yaml
+++ /dev/null
@@ -1,41 +0,0 @@
-
-apiVersion: rbac.authorization.k8s.io/v1
-kind: Role
-metadata:
-  name: tektoncd-role
-  namespace: test-dimitra
-  annotations:
-    tekton.cern.ch/requiredForTask: clear-cache
-rules:
-- apiGroups:
-  - ""
-  resources:
-  - pods
-  - services
-  - endpoints
-  - persistentvolumeclaims
-  - events
-  - configmaps
-  - secrets
-  - pods/log
-  - pods/exec
-  - limitranges
-  - drupalsites
-  verbs:
-  - '*'
-- apiGroups:
-  - "drupal.webservices.cern.ch"
-  resources:
-  - pods
-  - services
-  - endpoints
-  - persistentvolumeclaims
-  - events
-  - configmaps
-  - secrets
-  - pods/log
-  - pods/exec
-  - limitranges
-  - drupalsites
-  verbs:
-  - '*'
\ No newline at end of file
diff --git a/tasks/clear-cache.yaml b/tasks/clear-cache.yaml
deleted file mode 100644
index 80df1fa..0000000
--- a/tasks/clear-cache.yaml
+++ /dev/null
@@ -1,25 +0,0 @@
-apiVersion: tekton.dev/v1beta1
-kind: ClusterTask
-metadata:
-  name: clear-cache
-spec:
-  params:
-    - name: drupalSite
-      type: string
-      description: The name of the drupal site
-    - name: namespace
-      type: string
-      description: The namespace of the drupal site
-  steps:
-    - name: clear-cache
-      image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
-      command:
-        - /bin/sh
-        - '-c'
-      args:
-      - |-
-        echo "--------------------------- Clear cache ---------------------------"
-        echo "Drupal site name: $(params.drupalSite)"
-        deploymentPod=$(oc get pods -n $(params.namespace) -l drupalSite=$(params.drupalSite) -l app=drupal -o custom-columns=POD:.metadata.name --no-headers)
-        echo "Deployment pod: $deploymentPod"
-        oc exec $deploymentPod -c php-fpm drush cr
diff --git a/tasks/database-backup.yaml b/tasks/database-backup.yaml
deleted file mode 100644
index e289174..0000000
--- a/tasks/database-backup.yaml
+++ /dev/null
@@ -1,25 +0,0 @@
-apiVersion: tekton.dev/v1beta1
-kind: ClusterTask
-metadata:
-  name: database-backup
-spec:
-  params:
-    - name: drupalSite
-      type: string
-      description: The name of the drupal site
-    - name: namespace
-      type: string
-      description: The namespace of the drupal site
-  steps:
-    - name: database-backup
-      image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
-      command:
-        - /bin/sh
-        - '-c'
-      args:
-      - |-
-        echo "--------------------------- Database backup ---------------------------"
-        echo "Drupal site name: $(params.drupalSite)"
-        deploymentPod=$(oc get pods -n $(params.namespace) -l drupalSite=$(params.drupalSite) -l app=drupal -o custom-columns=POD:.metadata.name --no-headers)
-        echo "Deployment pod: $deploymentPod"
-        oc exec $deploymentPod -c php-fpm -- /bin/sh '-c' 'drush sql-dump > /drupal-data/dbdump.sql'
diff --git a/tasks/database-restore.yaml b/tasks/database-restore.yaml
deleted file mode 100644
index 406a161..0000000
--- a/tasks/database-restore.yaml
+++ /dev/null
@@ -1,25 +0,0 @@
-apiVersion: tekton.dev/v1beta1
-kind: ClusterTask
-metadata:
-  name: database-restore
-spec:
-  params:
-    - name: drupalSite
-      type: string
-      description: The name of the drupal site
-    - name: namespace
-      type: string
-      description: The namespace of the drupal site
-  steps:
-    - name: database-restore
-      image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
-      command:
-        - /bin/sh
-        - '-c'
-      args:
-      - |-
-        echo "--------------------------- Database restore ---------------------------"
-        echo "Drupal site name: $(params.drupalSite)"
-        deploymentPod=$(oc get pods -n $(params.namespace) -l drupalSite=$(params.drupalSite) -l app=drupal -o custom-columns=POD:.metadata.name --no-headers)
-        echo "Deployment pod: $deploymentPod"
-        oc exec drupal-test-dimitra-drupalsite-8f5478886-d79s6 -c php-fpm -- /bin/sh '-c' 'drush sql-drop -y; drush sql-connect < /drupal-data/dbdump.sql'
diff --git a/tasks/publish-unpublish.yaml b/tasks/publish-unpublish.yaml
deleted file mode 100644
index 0edd041..0000000
--- a/tasks/publish-unpublish.yaml
+++ /dev/null
@@ -1,26 +0,0 @@
-apiVersion: tekton.dev/v1beta1
-kind: ClusterTask
-metadata:
-  name: publish-unpublish
-spec:
-  params:
-    - name: drupalSite
-      type: string
-      description: The name of the drupal site
-    - name: namespace
-      type: string
-      description: The namespace of the drupal site
-    - name: publish
-      type: string
-      description: Publish a drupal site or not
-  steps:
-    - name: publish-unpublish
-      image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
-      command:
-        - /bin/sh
-        - '-c'
-      args:
-      - |-
-        echo "--------------------------- Publsih/Unpublish ---------------------------"
-        echo "Drupal site name: $(params.drupalSite)"
-        oc patch --type=merge drupalsites.drupal.webservices.cern.ch/$(params.drupalSite) -p '{"spec":{"publish":$(params.publish)}}' -n $(params.namespace)
-- 
GitLab