From 7ef4d9f3f5c393c99d7608cd35049d937ac7f05b Mon Sep 17 00:00:00 2001 From: Albin Vass Date: Wed, 1 Jul 2020 11:33:49 +0200 Subject: [PATCH] Allow deleting workspace after running terraform destroy Also fixes a bug where the workspace was created from the wrong directory. Change-Id: I28b54b9aa57a3f449254574956701ee663618e8b --- roles/terraform/README.rst | 6 + roles/terraform/defaults/main.yaml | 1 + roles/terraform/tasks/main.yaml | 27 +- .../terraform/test-terraform-role.yaml | 40 +++ zuul-tests.d/terraform-jobs-roles.yaml | 246 ++++++++++++++++++ zuul-tests.d/terraform-jobs.yaml | 125 --------- zuul.d/terraform-jobs.yaml | 6 + 7 files changed, 325 insertions(+), 126 deletions(-) create mode 100644 test-playbooks/terraform/test-terraform-role.yaml create mode 100644 zuul-tests.d/terraform-jobs-roles.yaml delete mode 100644 zuul-tests.d/terraform-jobs.yaml diff --git a/roles/terraform/README.rst b/roles/terraform/README.rst index 525c6f4b8..50a98a1ae 100644 --- a/roles/terraform/README.rst +++ b/roles/terraform/README.rst @@ -28,6 +28,12 @@ Run terraform command. Assumes the appropriate version of terraform has been ins Set to true if the workspace should automatically be created if doesn't already exist. +.. zuul:rolevar:: terraform_purge_workspace + :default: false + + Set to true if the workspace should be deleted + after running 'terraform destroy'. + .. zuul:rolevar:: terraform_comment :default: true diff --git a/roles/terraform/defaults/main.yaml b/roles/terraform/defaults/main.yaml index ad5ca5b94..d6caa23bc 100644 --- a/roles/terraform/defaults/main.yaml +++ b/roles/terraform/defaults/main.yaml @@ -1,5 +1,6 @@ terraform_executable: "{{ ansible_user_dir }}/.local/bin/terraform" terraform_extra_args: "" terraform_create_workspace: false +terraform_purge_workspace: false terraform_comment: true zuul_work_dir: "{{ zuul.project.src_dir }}" diff --git a/roles/terraform/tasks/main.yaml b/roles/terraform/tasks/main.yaml index 1468b9c7e..6fe8038fc 100644 --- a/roles/terraform/tasks/main.yaml +++ b/roles/terraform/tasks/main.yaml @@ -23,10 +23,11 @@ when: terraform_workspace is defined shell: | set -o pipefail - {{ terraform_executable }} workspace list -no-color | sed 's/* //' + {{ terraform_executable }} workspace list -no-color | sed 's/^..//' register: _terraform_workspace_list args: executable: /bin/bash + chdir: "{{ zuul_work_dir }}" environment: TF_IN_AUTOMATION: 1 @@ -36,6 +37,8 @@ - terraform_workspace not in _terraform_workspace_list.stdout_lines - terraform_create_workspace command: "{{ terraform_executable }} workspace new -no-color {{ terraform_workspace }}" + args: + chdir: "{{ zuul_work_dir }}" environment: TF_IN_AUTOMATION: 1 @@ -43,6 +46,8 @@ when: - terraform_workspace is defined command: "{{ terraform_executable }} workspace select -no-color {{ terraform_workspace }}" + args: + chdir: "{{ zuul_work_dir }}" environment: TF_IN_AUTOMATION: 1 @@ -59,6 +64,26 @@ environment: TF_IN_AUTOMATION: 1 +- name: Remove workspace + when: + - terraform_purge_workspace + - terraform_command == "destroy" + - terraform_workspace is defined + - terraform_workspace != "default" + block: + - name: Leave workspace + command: "{{ terraform_executable }} workspace select -no-color default" + args: + chdir: "{{ zuul_work_dir }}" + environment: + TF_IN_AUTOMATION: 1 + - name: Delete workspace + command: "{{ terraform_executable }} workspace delete -no-color {{ terraform_workspace }}" + args: + chdir: "{{ zuul_work_dir }}" + environment: + TF_IN_AUTOMATION: 1 + - name: Get path to main.tf relative to the repo root when: terraform_command == "plan" register: main_file_location diff --git a/test-playbooks/terraform/test-terraform-role.yaml b/test-playbooks/terraform/test-terraform-role.yaml new file mode 100644 index 000000000..14c5d4903 --- /dev/null +++ b/test-playbooks/terraform/test-terraform-role.yaml @@ -0,0 +1,40 @@ +- hosts: all + tasks: + - name: Create workspace + include_role: + name: terraform + vars: + zuul_work_dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' + terraform_command: plan + terraform_create_workspace: true + + - name: Make sure workspace was created + command: "{{ terraform_executable }} workspace select -no-color {{ terraform_workspace }}" + args: + chdir: "{{ zuul.project.src_dir }}/test-playbooks/terraform" + + - name: Select default workspace + command: "{{ terraform_executable }} workspace select default" + args: + chdir: "{{ zuul.project.src_dir }}/test-playbooks/terraform" + + - name: Use created workspace + include_role: + name: terraform + vars: + terraform_command: apply + + - name: Delete workspace + include_role: + name: terraform + vars: + zuul_work_dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' + terraform_command: destroy + terraform_purge_workspace: true + + - name: Make sure workspace was removed + register: terraform_workspace_stat + failed_when: terraform_workspace_stat.rc == 0 + command: "{{ terraform_executable }} workspace select -no-color {{ terraform_workspace }}" + args: + chdir: "{{ zuul.project.src_dir }}/test-playbooks/terraform" diff --git a/zuul-tests.d/terraform-jobs-roles.yaml b/zuul-tests.d/terraform-jobs-roles.yaml new file mode 100644 index 000000000..1adbdddde --- /dev/null +++ b/zuul-tests.d/terraform-jobs-roles.yaml @@ -0,0 +1,246 @@ +- job: + name: zuul-jobs-test-terraform + parent: terraform-plan + description: Test terraform job + tags: all-platforms + files: + - roles/ensure-terraform/.* + - roles/terraform/.* + - test-playbooks/terraform/.* + - zuul.d/terraform-jobs.yaml + - playbooks/terraform/.* + vars: + zuul_work_dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' + terraform_workspace: testing + terraform_create_workspace: true + terraform_overrides: + - dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' + content: | + module "test_module" { + source = "./other-module" + } + +- job: + name: zuul-jobs-test-terraform-centos-7 + description: Test terraform job on centos-7 + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: centos-7 + label: centos-7 + +- job: + name: zuul-jobs-test-terraform-centos-8 + description: Test terraform job on centos-8 + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: centos-8 + label: centos-8 + +- job: + name: zuul-jobs-test-terraform-debian-stretch + description: Test terraform job on debian-stretch + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: debian-stretch + label: debian-stretch + +- job: + name: zuul-jobs-test-terraform-fedora-31 + description: Test terraform job on fedora-31 + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: fedora-31 + label: fedora-31 + +- job: + name: zuul-jobs-test-terraform-gentoo-17-0-systemd + description: Test terraform job on gentoo-17-0-systemd + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: gentoo-17-0-systemd + label: gentoo-17-0-systemd + +- job: + name: zuul-jobs-test-terraform-opensuse-15 + description: Test terraform job on opensuse-15 + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: opensuse-15 + label: opensuse-15 + +- job: + name: zuul-jobs-test-terraform-opensuse-tumbleweed-nv + voting: false + description: Test terraform job on opensuse-tumbleweed + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: opensuse-tumbleweed + label: opensuse-tumbleweed + +- job: + name: zuul-jobs-test-terraform-ubuntu-bionic + description: Test terraform job on ubuntu-bionic + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-bionic + label: ubuntu-bionic + +- job: + name: zuul-jobs-test-terraform-ubuntu-xenial + description: Test terraform job on ubuntu-xenial + parent: zuul-jobs-test-terraform + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-xenial + label: ubuntu-xenial + +- job: + name: zuul-jobs-test-terraform-role + parent: terraform-base + run: test-playbooks/terraform/test-terraform-role.yaml + description: Test terraform roles + tags: all-platforms + files: + - roles/ensure-terraform/.* + - roles/terraform/.* + - test-playbooks/terraform/.* + - zuul.d/terraform-jobs.yaml + - playbooks/terraform/.* + vars: + zuul_work_dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' + terraform_workspace: testing + terraform_overrides: + - dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' + content: | + module "test_module" { + source = "./other-module" + } + +- job: + name: zuul-jobs-test-terraform-role-centos-7 + description: Test terraform roles on centos-7 + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: centos-7 + label: centos-7 + +- job: + name: zuul-jobs-test-terraform-role-centos-8 + description: Test terraform roles on centos-8 + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: centos-8 + label: centos-8 + +- job: + name: zuul-jobs-test-terraform-role-debian-stretch + description: Test terraform roles on debian-stretch + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: debian-stretch + label: debian-stretch + +- job: + name: zuul-jobs-test-terraform-role-fedora-31 + description: Test terraform roles on fedora-31 + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: fedora-31 + label: fedora-31 + +- job: + name: zuul-jobs-test-terraform-role-gentoo-17-0-systemd + description: Test terraform roles on gentoo-17-0-systemd + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: gentoo-17-0-systemd + label: gentoo-17-0-systemd + +- job: + name: zuul-jobs-test-terraform-role-opensuse-15 + description: Test terraform roles on opensuse-15 + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: opensuse-15 + label: opensuse-15 + +- job: + name: zuul-jobs-test-terraform-role-opensuse-tumbleweed-nv + voting: false + description: Test terraform roles on opensuse-tumbleweed + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: opensuse-tumbleweed + label: opensuse-tumbleweed + +- job: + name: zuul-jobs-test-terraform-role-ubuntu-bionic + description: Test terraform roles on ubuntu-bionic + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-bionic + label: ubuntu-bionic + +- job: + name: zuul-jobs-test-terraform-role-ubuntu-xenial + description: Test terraform roles on ubuntu-xenial + parent: zuul-jobs-test-terraform-role + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-xenial + label: ubuntu-xenial + +- project: + check: &id001 + jobs: + - zuul-jobs-test-terraform-centos-7 + - zuul-jobs-test-terraform-centos-8 + - zuul-jobs-test-terraform-debian-stretch + - zuul-jobs-test-terraform-fedora-31 + - zuul-jobs-test-terraform-gentoo-17-0-systemd + - zuul-jobs-test-terraform-opensuse-15 + - zuul-jobs-test-terraform-ubuntu-bionic + - zuul-jobs-test-terraform-ubuntu-xenial + - zuul-jobs-test-terraform-role-centos-7 + - zuul-jobs-test-terraform-role-centos-8 + - zuul-jobs-test-terraform-role-debian-stretch + - zuul-jobs-test-terraform-role-fedora-31 + - zuul-jobs-test-terraform-role-gentoo-17-0-systemd + - zuul-jobs-test-terraform-role-opensuse-15 + - zuul-jobs-test-terraform-role-ubuntu-bionic + - zuul-jobs-test-terraform-role-ubuntu-xenial + gate: *id001 diff --git a/zuul-tests.d/terraform-jobs.yaml b/zuul-tests.d/terraform-jobs.yaml deleted file mode 100644 index 82ae3c2d6..000000000 --- a/zuul-tests.d/terraform-jobs.yaml +++ /dev/null @@ -1,125 +0,0 @@ -- job: - name: zuul-jobs-test-terraform - parent: terraform-plan - description: Test terraform job - tags: all-platforms - files: - - roles/ensure-terraform/.* - - roles/terraform/.* - - test-playbooks/terraform/.* - - zuul.d/terraform-jobs.yaml - - playbooks/terraform/.* - vars: - zuul_work_dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' - terraform_workspace: testing - terraform_create_workspace: true - terraform_overrides: - - dir: '{{ zuul.project.src_dir }}/test-playbooks/terraform' - content: | - module "test_module" { - source = "./other-module" - } - -- job: - name: zuul-jobs-test-terraform-centos-7 - description: Test terraform job on centos-7 - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: centos-7 - label: centos-7 - -- job: - name: zuul-jobs-test-terraform-centos-8 - description: Test terraform job on centos-8 - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: centos-8 - label: centos-8 - -- job: - name: zuul-jobs-test-terraform-debian-stretch - description: Test terraform job on debian-stretch - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: debian-stretch - label: debian-stretch - -- job: - name: zuul-jobs-test-terraform-fedora-31 - description: Test terraform job on fedora-31 - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: fedora-31 - label: fedora-31 - -- job: - name: zuul-jobs-test-terraform-gentoo-17-0-systemd - description: Test terraform job on gentoo-17-0-systemd - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: gentoo-17-0-systemd - label: gentoo-17-0-systemd - -- job: - name: zuul-jobs-test-terraform-opensuse-15 - description: Test terraform job on opensuse-15 - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: opensuse-15 - label: opensuse-15 - -- job: - name: zuul-jobs-test-terraform-opensuse-tumbleweed-nv - voting: false - description: Test terraform job on opensuse-tumbleweed - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: opensuse-tumbleweed - label: opensuse-tumbleweed - -- job: - name: zuul-jobs-test-terraform-ubuntu-bionic - description: Test terraform job on ubuntu-bionic - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: ubuntu-bionic - label: ubuntu-bionic - -- job: - name: zuul-jobs-test-terraform-ubuntu-xenial - description: Test terraform job on ubuntu-xenial - parent: zuul-jobs-test-terraform - tags: auto-generated - nodeset: - nodes: - - name: ubuntu-xenial - label: ubuntu-xenial - -- project: - check: &id001 - jobs: - - zuul-jobs-test-terraform-centos-7 - - zuul-jobs-test-terraform-centos-8 - - zuul-jobs-test-terraform-debian-stretch - - zuul-jobs-test-terraform-fedora-31 - - zuul-jobs-test-terraform-gentoo-17-0-systemd - - zuul-jobs-test-terraform-opensuse-15 - - zuul-jobs-test-terraform-ubuntu-bionic - - zuul-jobs-test-terraform-ubuntu-xenial - gate: *id001 diff --git a/zuul.d/terraform-jobs.yaml b/zuul.d/terraform-jobs.yaml index 9b8465ed8..a4c90f2b9 100644 --- a/zuul.d/terraform-jobs.yaml +++ b/zuul.d/terraform-jobs.yaml @@ -20,6 +20,12 @@ Set to true if the workspace should automatically be created if doesn't already exist. + .. zuul:rolevar:: terraform_purge_workspace + :default: false + + Set to true if the workspace should be deleted + after running 'terraform destroy'. + .. zuul:rolevar:: terraform_comment :default: true