From 3c4120fc28714f40e29c45a59fb911acab63c089 Mon Sep 17 00:00:00 2001 From: Liam Young Date: Thu, 27 Oct 2022 14:25:55 +0000 Subject: [PATCH] Reorganise into roles Reorganise the ansible config into distinct roles. This will make managing templates and default variables cleaner. I have tested this as best I can locally. Change-Id: Ib0be8dde453064336fd2e69afe466f4d64956743 --- playbooks/charmbuild.yaml | 77 +---------- playbooks/microk8s-cloud.yaml | 124 ------------------ playbooks/microk8s-juju-post.yaml | 56 -------- playbooks/zaza-smoke-test.yaml | 33 +---- roles/charmbuild/tasks/main.yaml | 73 +++++++++++ roles/collect-run-data/tasks/main.yaml | 54 ++++++++ roles/microk8s-cloud/tasks/main.yaml | 119 +++++++++++++++++ .../microk8s-cloud/templates}/hosts.j2 | 0 roles/zaza-smoke-test/tasks/main.yaml | 27 ++++ zuul.d/jobs.yaml | 3 +- 10 files changed, 281 insertions(+), 285 deletions(-) delete mode 100644 playbooks/microk8s-cloud.yaml delete mode 100644 playbooks/microk8s-juju-post.yaml create mode 100644 roles/charmbuild/tasks/main.yaml create mode 100644 roles/collect-run-data/tasks/main.yaml create mode 100644 roles/microk8s-cloud/tasks/main.yaml rename {playbooks => roles/microk8s-cloud/templates}/hosts.j2 (100%) create mode 100644 roles/zaza-smoke-test/tasks/main.yaml diff --git a/playbooks/charmbuild.yaml b/playbooks/charmbuild.yaml index 44c8e88..32f28c5 100644 --- a/playbooks/charmbuild.yaml +++ b/playbooks/charmbuild.yaml @@ -1,76 +1,3 @@ - hosts: all - tasks: - - - name: lxd apt packages are not present - apt: - name: - - lxd - - lxd-client - state: absent - purge: true - become: true - - - name: snapd is installed - apt: - name: snapd - become: true - - - name: lxd snap is installed - snap: - name: lxd - channel: latest/stable - become: true - - - name: lxd is initialised - command: lxd init --auto - become: true - - - name: current user is in lxd group - user: - name: "{{ ansible_user }}" - groups: lxd - append: true - become: true - - - name: reset ssh connection to apply permissions from new group - meta: reset_connection - - - name: charmcraft is installed - snap: - name: charmcraft - channel: latest/stable - classic: true - become: true - - - name: charm is packed - command: - cmd: charmcraft --verbosity trace pack - chdir: "{{ zuul.project.src_dir }}" - - - name: local charm is named consistently - command: - cmd: ./rename.sh - chdir: "{{ zuul.project.src_dir }}" - - - name: Compress built charm - archive: - dest: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm.gz" - path: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm" - format: gz - - - name: built charm is available in the zuul log root for auto artifact upload - fetch: - src: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm.gz" - dest: "{{ zuul.executor.log_root }}/" - flat: true - become: true - - - name: Upload artifacts - zuul_return: - data: - zuul: - artifacts: - - name: charm - url: "{{ charm_build_name }}.charm.gz" - metadata: - type: charm + roles: + - charmbuild diff --git a/playbooks/microk8s-cloud.yaml b/playbooks/microk8s-cloud.yaml deleted file mode 100644 index 50c71c0..0000000 --- a/playbooks/microk8s-cloud.yaml +++ /dev/null @@ -1,124 +0,0 @@ -- hosts: all - name: Microk8s setup - roles: - - use-docker-mirror - tasks: - - name: snapd is installed - apt: - name: snapd - become: true - - - name: microk8s is installed - snap: - name: microk8s - classic: true - become: true - - - name: current user is in microk8s group - user: - name: "{{ ansible_user }}" - groups: microk8s - append: true - become: true - - - name: reset ssh connection to apply permissions from new group - meta: reset_connection - - - name: microk8s status - command: - cmd: microk8s status - - - name: Create docker.io certs dir - when: - - docker_mirror is defined - file: - path: /var/snap/microk8s/current/args/certs.d/docker.io - state: directory - owner: root - group: microk8s - mode: '0770' - - - name: Render microk8s registry mirror template - when: - - docker_mirror is defined - template: - src: hosts.j2 - dest: /var/snap/microk8s/current/args/certs.d/docker.io/hosts.toml - group: microk8s - vars: - mirror_location: "{{ docker_mirror }}" - server: https://docker.io - - - name: Check docker.io hosts.toml - when: - - docker_mirror is defined - command: - cmd: cat /var/snap/microk8s/current/args/certs.d/docker.io/hosts.toml - - - name: microk8s is started - command: - cmd: microk8s start - - - name: microk8s is running and ready - command: - cmd: microk8s status --wait-ready - register: res - failed_when: '"is running" not in res.stdout' - - - name: microk8s dns addon is enabled - command: - cmd: microk8s enable dns - register: res - changed_when: '"already enabled" not in res.stdout' - - - name: microk8s hostpath storage addon is enabled - command: - cmd: microk8s enable hostpath-storage - register: res - changed_when: '"already enabled" not in res.stdout' - - - name: microk8s metallb addon is enabled - command: - # ip range is an arbitrary choice; may need to be changed later - cmd: microk8s enable metallb:10.170.0.1-10.170.0.100 - register: res - changed_when: '"already enabled" not in res.stdout' - - - name: microk8s addons are ready - command: - cmd: microk8s status --format short - register: res - retries: 18 - delay: 10 # 18 * 10 = 3 minutes - until: > - "core/dns: enabled" in res.stdout and - "core/hostpath-storage: enabled" in res.stdout and - "core/metallb: enabled" in res.stdout - changed_when: res.attempts > 1 - - - name: juju is installed - snap: - name: juju - classic: true - channel: "{{ juju_channel | default('latest/stable') }}" - become: true - - - name: juju is bootstrapped on microk8s - command: - cmd: juju bootstrap --agent-version=2.9.34 microk8s microk8s - register: res - changed_when: '"already exists" not in res.stderr' - failed_when: '"ERROR" in res.stderr and "already exists" not in res.stderr' - - - name: current juju controller is microk8s - command: - cmd: juju switch microk8s - register: res - changed_when: '"no change" not in res.stderr' - - - name: Collect snap versions - command: snap list - register: snap_out - - - name: Show snap versions - debug: msg="{{ snap_out.stdout }}" diff --git a/playbooks/microk8s-juju-post.yaml b/playbooks/microk8s-juju-post.yaml deleted file mode 100644 index 3ca28b3..0000000 --- a/playbooks/microk8s-juju-post.yaml +++ /dev/null @@ -1,56 +0,0 @@ -- hosts: all - tasks: - - name: Create destination for logs - file: - path: "{{ zuul.project.src_dir }}/log" - state: directory - mode: 0755 - - name: debug logs replay - args: - executable: /bin/bash - shell: | - set -o pipefail - MODEL="$(juju models --format=json | jq -r '.models[]["short-name"]' | grep '^zaza-')" - juju switch $MODEL - juju debug-log --replay > {{ zuul.project.src_dir }}/log/debug-hooks.txt - exit 0 - - name: debug describe pods - args: - executable: /bin/bash - shell: | - set -o pipefail - MODEL="$(juju models --format=json | jq -r '.models[]["short-name"]' | grep '^zaza-')" - microk8s.kubectl describe -n $MODEL pods > {{ zuul.project.src_dir }}/log/describe-pods.txt - exit 0 - - name: juju status - args: - executable: /bin/bash - shell: | - set -o pipefail - for model in $(juju models | grep zaza- | awk '{gsub(/\*?/,""); print $1}'); do - juju status -m $model > {{ zuul.project.src_dir }}/log/juju-status.$model.txt - juju status -m $model --format=yaml > {{ zuul.project.src_dir }}/log/juju-status.$model.yaml - done - - name: Collect var logs - args: - executable: /bin/bash - shell: | - set -o pipefail - MODEL_NAME=$(juju models --format=json | jq -r '.models[]["short-name"]' | grep '^zaza-') - UNITS=$(juju status --format oneline | awk '{print $2}' | sed -e 's!:!!' | grep -Ev '^$' | paste -s -d' ') - for UNIT_NAME in $UNITS; do - POD_NAME=$(echo $UNIT_NAME | sed -e 's!/!-!') - CONTAINERS=$(microk8s.kubectl get pods -n $MODEL_NAME $POD_NAME -o jsonpath='{.spec.containers[*].name}' | sed -e 's/charm //') - for CONTAINER in $CONTAINERS; do - juju ssh --container $CONTAINER -m $MODEL_NAME $UNIT_NAME "tar zcf /tmp/logs.tgz /var/log/" - juju scp --container $CONTAINER -m $MODEL_NAME $UNIT_NAME:/tmp/logs.tgz {{ zuul.project.src_dir }}/log/$POD_NAME-$CONTAINER.tgz - done - done - - name: fetch juju logs - synchronize: - dest: "{{ zuul.executor.log_root }}" - mode: pull - src: "{{ zuul.project.src_dir }}/log" - verify_host: true - owner: false - group: false diff --git a/playbooks/zaza-smoke-test.yaml b/playbooks/zaza-smoke-test.yaml index ff8786f..39ef0f2 100644 --- a/playbooks/zaza-smoke-test.yaml +++ b/playbooks/zaza-smoke-test.yaml @@ -1,29 +1,6 @@ - hosts: all - tasks: - - name: test runner packages are installed - apt: - name: - - tox - - jq - - gzip - become: true - - - name: built charm is present locally (artefact from previous job) - include_role: - name: download-artifact - vars: - download_artifact_api: "https://zuul.opendev.org/api/tenant/{{ zuul.tenant }}" - download_artifact_type: charm - download_artifact_pipeline: check - download_artifact_job: charmbuild - download_artifact_directory: "{{ zuul.project.src_dir }}" - - - name: Gunzip built charm - command: - cmd: gunzip {{ charm_build_name }}.charm.gz - chdir: "{{ zuul.project.src_dir }}" - - - name: run smoke tests - command: - cmd: tox -e func-smoke - chdir: "{{ zuul.project.src_dir }}" + roles: + - use-docker-mirror + - microk8s-cloud + - zaza-smoke-test + - collect-run-data diff --git a/roles/charmbuild/tasks/main.yaml b/roles/charmbuild/tasks/main.yaml new file mode 100644 index 0000000..1103bc7 --- /dev/null +++ b/roles/charmbuild/tasks/main.yaml @@ -0,0 +1,73 @@ +- name: lxd apt packages are not present + apt: + name: + - lxd + - lxd-client + state: absent + purge: true + become: true + +- name: snapd is installed + apt: + name: snapd + become: true + +- name: lxd snap is installed + snap: + name: lxd + channel: latest/stable + become: true + +- name: lxd is initialised + command: lxd init --auto + become: true + +- name: current user is in lxd group + user: + name: "{{ ansible_user }}" + groups: lxd + append: true + become: true + +- name: reset ssh connection to apply permissions from new group + meta: reset_connection + +- name: charmcraft is installed + snap: + name: charmcraft + channel: latest/stable + classic: true + become: true + +- name: charm is packed + command: + cmd: charmcraft --verbosity trace pack + chdir: "{{ zuul.project.src_dir }}" + +- name: local charm is named consistently + command: + cmd: ./rename.sh + chdir: "{{ zuul.project.src_dir }}" + +- name: Compress built charm + archive: + dest: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm.gz" + path: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm" + format: gz + +- name: built charm is available in the zuul log root for auto artifact upload + fetch: + src: "{{ zuul.project.src_dir }}/{{ charm_build_name }}.charm.gz" + dest: "{{ zuul.executor.log_root }}/" + flat: true + become: true + +- name: Upload artifacts + zuul_return: + data: + zuul: + artifacts: + - name: charm + url: "{{ charm_build_name }}.charm.gz" + metadata: + type: charm diff --git a/roles/collect-run-data/tasks/main.yaml b/roles/collect-run-data/tasks/main.yaml new file mode 100644 index 0000000..bd50b24 --- /dev/null +++ b/roles/collect-run-data/tasks/main.yaml @@ -0,0 +1,54 @@ +- name: Create destination for logs + file: + path: "{{ zuul.project.src_dir }}/log" + state: directory + mode: 0755 +- name: debug logs replay + args: + executable: /bin/bash + shell: | + set -o pipefail + MODEL="$(juju models --format=json | jq -r '.models[]["short-name"]' | grep '^zaza-')" + juju switch $MODEL + juju debug-log --replay > {{ zuul.project.src_dir }}/log/debug-hooks.txt + exit 0 +- name: debug describe pods + args: + executable: /bin/bash + shell: | + set -o pipefail + MODEL="$(juju models --format=json | jq -r '.models[]["short-name"]' | grep '^zaza-')" + microk8s.kubectl describe -n $MODEL pods > {{ zuul.project.src_dir }}/log/describe-pods.txt + exit 0 +- name: juju status + args: + executable: /bin/bash + shell: | + set -o pipefail + for model in $(juju models | grep zaza- | awk '{gsub(/\*?/,""); print $1}'); do + juju status -m $model > {{ zuul.project.src_dir }}/log/juju-status.$model.txt + juju status -m $model --format=yaml > {{ zuul.project.src_dir }}/log/juju-status.$model.yaml + done +- name: Collect var logs + args: + executable: /bin/bash + shell: | + set -o pipefail + MODEL_NAME=$(juju models --format=json | jq -r '.models[]["short-name"]' | grep '^zaza-') + UNITS=$(juju status --format oneline | awk '{print $2}' | sed -e 's!:!!' | grep -Ev '^$' | paste -s -d' ') + for UNIT_NAME in $UNITS; do + POD_NAME=$(echo $UNIT_NAME | sed -e 's!/!-!') + CONTAINERS=$(microk8s.kubectl get pods -n $MODEL_NAME $POD_NAME -o jsonpath='{.spec.containers[*].name}' | sed -e 's/charm //') + for CONTAINER in $CONTAINERS; do + juju ssh --container $CONTAINER -m $MODEL_NAME $UNIT_NAME "tar zcf /tmp/logs.tgz /var/log/" + juju scp --container $CONTAINER -m $MODEL_NAME $UNIT_NAME:/tmp/logs.tgz {{ zuul.project.src_dir }}/log/$POD_NAME-$CONTAINER.tgz + done + done +- name: fetch juju logs + synchronize: + dest: "{{ zuul.executor.log_root }}" + mode: pull + src: "{{ zuul.project.src_dir }}/log" + verify_host: true + owner: false + group: false diff --git a/roles/microk8s-cloud/tasks/main.yaml b/roles/microk8s-cloud/tasks/main.yaml new file mode 100644 index 0000000..c4ea0a9 --- /dev/null +++ b/roles/microk8s-cloud/tasks/main.yaml @@ -0,0 +1,119 @@ +- name: snapd is installed + apt: + name: snapd + become: true + +- name: microk8s is installed + snap: + name: microk8s + classic: true + become: true + +- name: current user is in microk8s group + user: + name: "{{ ansible_user }}" + groups: microk8s + append: true + become: true + +- name: reset ssh connection to apply permissions from new group + meta: reset_connection + +- name: microk8s status + command: + cmd: microk8s status + +- name: Create docker.io certs dir + when: + - docker_mirror is defined + file: + path: /var/snap/microk8s/current/args/certs.d/docker.io + state: directory + owner: root + group: microk8s + mode: '0770' + +- name: Render microk8s registry mirror template + when: + - docker_mirror is defined + template: + src: hosts.j2 + dest: /var/snap/microk8s/current/args/certs.d/docker.io/hosts.toml + group: microk8s + vars: + mirror_location: "{{ docker_mirror }}" + server: https://docker.io + +- name: Check docker.io hosts.toml + when: + - docker_mirror is defined + command: + cmd: cat /var/snap/microk8s/current/args/certs.d/docker.io/hosts.toml + +- name: microk8s is started + command: + cmd: microk8s start + +- name: microk8s is running and ready + command: + cmd: microk8s status --wait-ready + register: res + failed_when: '"is running" not in res.stdout' + +- name: microk8s dns addon is enabled + command: + cmd: microk8s enable dns + register: res + changed_when: '"already enabled" not in res.stdout' + +- name: microk8s hostpath storage addon is enabled + command: + cmd: microk8s enable hostpath-storage + register: res + changed_when: '"already enabled" not in res.stdout' + +- name: microk8s metallb addon is enabled + command: + # ip range is an arbitrary choice; may need to be changed later + cmd: microk8s enable metallb:10.170.0.1-10.170.0.100 + register: res + changed_when: '"already enabled" not in res.stdout' + +- name: microk8s addons are ready + command: + cmd: microk8s status --format short + register: res + retries: 18 + delay: 10 # 18 * 10 = 3 minutes + until: > + "core/dns: enabled" in res.stdout and + "core/hostpath-storage: enabled" in res.stdout and + "core/metallb: enabled" in res.stdout + changed_when: res.attempts > 1 + +- name: juju is installed + snap: + name: juju + classic: true + channel: "{{ juju_channel | default('latest/stable') }}" + become: true + +- name: juju is bootstrapped on microk8s + command: + cmd: juju bootstrap --agent-version=2.9.34 microk8s microk8s + register: res + changed_when: '"already exists" not in res.stderr' + failed_when: '"ERROR" in res.stderr and "already exists" not in res.stderr' + +- name: current juju controller is microk8s + command: + cmd: juju switch microk8s + register: res + changed_when: '"no change" not in res.stderr' + +- name: Collect snap versions + command: snap list + register: snap_out + +- name: Show snap versions + debug: msg="{{ snap_out.stdout }}" diff --git a/playbooks/hosts.j2 b/roles/microk8s-cloud/templates/hosts.j2 similarity index 100% rename from playbooks/hosts.j2 rename to roles/microk8s-cloud/templates/hosts.j2 diff --git a/roles/zaza-smoke-test/tasks/main.yaml b/roles/zaza-smoke-test/tasks/main.yaml new file mode 100644 index 0000000..33652e7 --- /dev/null +++ b/roles/zaza-smoke-test/tasks/main.yaml @@ -0,0 +1,27 @@ +- name: test runner packages are installed + apt: + name: + - tox + - jq + - gzip + become: true + +- name: built charm is present locally (artefact from previous job) + include_role: + name: download-artifact + vars: + download_artifact_api: "https://zuul.opendev.org/api/tenant/{{ zuul.tenant }}" + download_artifact_type: charm + download_artifact_pipeline: check + download_artifact_job: charmbuild + download_artifact_directory: "{{ zuul.project.src_dir }}" + +- name: Gunzip built charm + command: + cmd: gunzip {{ charm_build_name }}.charm.gz + chdir: "{{ zuul.project.src_dir }}" + +- name: run smoke tests + command: + cmd: tox -e func-smoke + chdir: "{{ zuul.project.src_dir }}" diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml index 893936d..2a95e38 100644 --- a/zuul.d/jobs.yaml +++ b/zuul.d/jobs.yaml @@ -3,11 +3,10 @@ run: playbooks/charmbuild.yaml provides: charm timeout: 3600 + - job: name: zaza-smoke-test - pre-run: playbooks/microk8s-cloud.yaml run: playbooks/zaza-smoke-test.yaml - post-run: playbooks/microk8s-juju-post.yaml requires: charm dependencies: - name: charmbuild