From acd0d462b107361ba42c54e533a9325ecf4308e9 Mon Sep 17 00:00:00 2001
From: Tin Lam <tin@irrational.io>
Date: Fri, 11 Dec 2020 09:18:19 -0600
Subject: [PATCH] feat(charts): adds post job to publish charts

This patch set adds a zuul job that allows post publish of job tarballs.

Signed-off-by: Tin Lam <tin@irrational.io>
Change-Id: I95d0bfefe20e69a22dbe8ee76928edb90a5db8ae
---
 playbooks/publish/post.yaml | 58 +++++++++++++++++++++++++++++++++++++
 playbooks/publish/run.yaml  | 36 +++++++++++++++++++++++
 zuul.d/jobs.yaml            |  6 ++++
 zuul.d/project.yaml         |  1 +
 4 files changed, 101 insertions(+)
 create mode 100644 playbooks/publish/post.yaml
 create mode 100644 playbooks/publish/run.yaml

diff --git a/playbooks/publish/post.yaml b/playbooks/publish/post.yaml
new file mode 100644
index 00000000..3452d1d9
--- /dev/null
+++ b/playbooks/publish/post.yaml
@@ -0,0 +1,58 @@
+---
+# Copyright 2020 VEXXHOST, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+- hosts: all
+  tasks:
+    - name: Download current index
+      register: _get_url
+      failed_when: _get_url.status_code not in (200, 404)
+      get_url:
+        url: https://tarballs.opendev.org/airship/charts/index.yaml
+        dest: "{{ zuul.project.src_dir }}/charts/index.yaml"
+
+    - name: Create and merge index
+      block:
+        - name: Create a new index
+          when: _get_url.status_code == 404
+          shell: helm repo index {{ zuul.project.src_dir }}/charts --url https://tarballs.opendev.org/airship/charts
+
+        - name: Merge into existing index
+          when: _get_url.status_code == 200
+          shell: helm repo index {{ zuul.project.src_dir }}/charts --merge {{ zuul.project.src_dir }}/charts/index.yaml --url https://tarballs.opendev.org/airship/charts
+      when: _get_url.status_code in (200, 404)
+
+    - name: Ensure artifact directory exists
+      file:
+        path: "{{ zuul.executor.work_root }}/artifacts/"
+        state: directory
+      delegate_to: localhost
+
+    - name: Gather the artifacts
+      find:
+        file_type: file
+        paths: "{{ zuul.project.src_dir }}/charts"
+        patterns: "*.tar.gz,*.tgz,index.yaml"
+      register: result
+
+    - name: Update Helm repository
+      synchronize:
+        mode: pull
+        src: "{{ item.path }}"
+        dest: "{{ zuul.executor.work_root }}/artifacts/"
+        verify_host: true
+        owner: no
+        group: no
+      with_items: "{{ result.files }}"
+...
diff --git a/playbooks/publish/run.yaml b/playbooks/publish/run.yaml
new file mode 100644
index 00000000..52031f4f
--- /dev/null
+++ b/playbooks/publish/run.yaml
@@ -0,0 +1,36 @@
+---
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+- hosts: all
+  tasks:
+    - name: install helm3
+      become_user: root
+      shell: |
+        set -ex
+        TMP_DIR=$(mktemp -d)
+        if [[ -z ${HELM_VERSION} ]]; then
+          HELM_VERSION="$(curl -Ls https://github.com/helm/helm/releases | grep 'href="/helm/helm/releases/tag/v3.[0-9]*.[0-9]*\"' | grep -v no-underline | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}')"
+        fi
+        curl -sSL https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz | tar -zxv --strip-components=1 -C ${TMP_DIR}
+        sudo mv ${TMP_DIR}/helm /usr/bin/helm
+        rm -rf ${TMP_DIR}
+      environment:
+        HELM_VERSION: ""
+      args:
+        executable: /bin/bash
+
+    - name: make all
+      make:
+        chdir: "{{ zuul.project.src_dir }}/charts"
+        target: all
+...
diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml
index 2a280010..89d4bc31 100644
--- a/zuul.d/jobs.yaml
+++ b/zuul.d/jobs.yaml
@@ -42,3 +42,9 @@
       - name: git_mirror_credentials
         secret: airship-charts-airshipit-github-secret
         pass-to-parent: true
+
+- job:
+    name: airship-charts-publish
+    parent: publish-openstack-artifacts
+    run: playbooks/publish/run.yaml
+    post-run: playbooks/publish/post.yaml
diff --git a/zuul.d/project.yaml b/zuul.d/project.yaml
index 2e7c9ba1..3e4990f3 100644
--- a/zuul.d/project.yaml
+++ b/zuul.d/project.yaml
@@ -29,3 +29,4 @@
     promote:
       jobs:
         - promote-airship-project-docs: *docs
+        - airship-charts-publish