summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Cacqueray <tdecacqu@redhat.com>2020-04-09 15:45:02 +0000
committerTristan Cacqueray <tdecacqu@redhat.com>2020-04-09 15:45:02 +0000
commit2a33a9e675842fdb0a4fc01159a8b10abd209a08 (patch)
tree11de5937c5dafe7f8ff2cf11dcae445bb6bc83a3
parent330e8cc98a6ac1f78fe51d72f371c3fc1ed91624 (diff)
Cherrypick the dhall-diff job proposed in upstream project
This will at least let us re-use the dhall-diff job in other project. Change-Id: I0ac79aba57d759b31e6304ee4b6681e4b31a3826
-rw-r--r--playbooks/dhall/diff.yaml3
-rw-r--r--playbooks/dhall/prepare.yaml25
-rw-r--r--roles/ensure-dhall/README.rst10
-rw-r--r--roles/ensure-dhall/defaults/main.yaml9
-rw-r--r--roles/ensure-dhall/tasks/main.yaml33
-rw-r--r--roles/run-dhall-diff/README.rst15
-rw-r--r--roles/run-dhall-diff/defaults/main.yaml4
-rw-r--r--roles/run-dhall-diff/tasks/main.yaml38
-rw-r--r--zuul.d/general-jobs.yaml21
9 files changed, 158 insertions, 0 deletions
diff --git a/playbooks/dhall/diff.yaml b/playbooks/dhall/diff.yaml
new file mode 100644
index 0000000..f78b477
--- /dev/null
+++ b/playbooks/dhall/diff.yaml
@@ -0,0 +1,3 @@
+- hosts: all
+ roles:
+ - run-dhall-diff
diff --git a/playbooks/dhall/prepare.yaml b/playbooks/dhall/prepare.yaml
new file mode 100644
index 0000000..3a77d05
--- /dev/null
+++ b/playbooks/dhall/prepare.yaml
@@ -0,0 +1,25 @@
+- hosts: all
+ tasks:
+ - name: Check for tools
+ command: type make git bzip2
+ failed_when: false
+ register: _tools
+
+ - name: Install tools
+ package:
+ name:
+ - git
+ - bzip2
+ - make
+ become: true
+ when: _tools.rc != 0
+
+ - name: Check for dhall command
+ command: type -p dhall-to-json
+ failed_when: false
+ register: _dhall_tools
+
+ - name: Install dhall
+ include_role:
+ name: ensure-dhall
+ when: _dhall_tools.rc != 0
diff --git a/roles/ensure-dhall/README.rst b/roles/ensure-dhall/README.rst
new file mode 100644
index 0000000..0838959
--- /dev/null
+++ b/roles/ensure-dhall/README.rst
@@ -0,0 +1,10 @@
+Ensure dhall is installed
+
+Installs the specified version of the haskell implementation.
+
+**Role Variables**
+
+.. zuul:rolevar:: dhall_version
+ :default: 1.31.1
+
+ The dhall version.
diff --git a/roles/ensure-dhall/defaults/main.yaml b/roles/ensure-dhall/defaults/main.yaml
new file mode 100644
index 0000000..a0c9443
--- /dev/null
+++ b/roles/ensure-dhall/defaults/main.yaml
@@ -0,0 +1,9 @@
+dhall_version: 1.31.1
+dhall_versions:
+ "1.31.1":
+ - url: https://github.com/dhall-lang/dhall-haskell/releases/download/1.31.1/dhall-1.31.1-x86_64-linux.tar.bz2
+ checksum: sha256:f8312727bbd4af74d183efce2e22f7b7807246a600fcc85600945f4790e4294a
+ - url: https://github.com/dhall-lang/dhall-haskell/releases/download/1.31.1/dhall-json-1.6.3-x86_64-linux.tar.bz2
+ checksum: sha256:7ec6b54f70f077d8876bb40633905f2a0a2c69e83a3ce0bd3d9f7577e1210489
+ - url: https://github.com/dhall-lang/dhall-haskell/releases/download/1.31.1/dhall-yaml-1.0.3-x86_64-linux.tar.bz2
+ checksum: sha256:04d95773f9e96340a29f2857a55fb718735b83596abacf1d7b9fa3f6004067cb
diff --git a/roles/ensure-dhall/tasks/main.yaml b/roles/ensure-dhall/tasks/main.yaml
new file mode 100644
index 0000000..62e3750
--- /dev/null
+++ b/roles/ensure-dhall/tasks/main.yaml
@@ -0,0 +1,33 @@
+- name: Create temp directory
+ tempfile:
+ state: directory
+ register: dhall_archive_tempdir
+
+- name: Check requested version
+ fail:
+ msg: |
+ Unknown dhall version: {{ dhall_version }}.
+ It needs to be defined in {{ opendev_url }}/roles/ensure-dhall/defaults/main.yaml
+ when: dhall_versions[dhall_version] is not defined
+ vars:
+ opendev_url: https://opendev.org/zuul/zuul-jobs/src/branch/master/
+
+- name: Download tools
+ get_url:
+ url: "{{ dhall_tool_item.url }}"
+ dest: "{{ dhall_archive_tempdir.path }}/{{ dhall_tool_item.url | basename }}"
+ checksum: "{{ dhall_tool_item.checksum }}"
+ loop: "{{ dhall_versions[dhall_version] }}"
+ loop_control:
+ loop_var: dhall_tool_item
+
+- name: Unpack tools
+ command: "tar -xf {{ dhall_archive_tempdir.path }}/{{ dhall_tool_item.url | basename }} --strip-components=2 -j --mode='a+x' -C /usr/local/bin"
+ become: yes
+ # ANSIBLE0006: Skip linting since it triggers on the "tar" command,
+ # but we prefer the command above
+ tags:
+ - skip_ansible_lint
+ loop: "{{ dhall_versions[dhall_version] }}"
+ loop_control:
+ loop_var: dhall_tool_item
diff --git a/roles/run-dhall-diff/README.rst b/roles/run-dhall-diff/README.rst
new file mode 100644
index 0000000..0fe4c7c
--- /dev/null
+++ b/roles/run-dhall-diff/README.rst
@@ -0,0 +1,15 @@
+Run dhall render command and ensure there are no resulting differences.
+
+Ensure that generated configuration files are idempotent.
+
+**Role Variables**
+
+.. zuul:jobvar:: dhall_render_command
+ :default: make
+
+ The command that render the configuration files.
+
+.. zuul:rolevar:: zuul_work_dir
+ :default: {{ zuul.project.src_dir }}
+
+ Directory to run the render command in.
diff --git a/roles/run-dhall-diff/defaults/main.yaml b/roles/run-dhall-diff/defaults/main.yaml
new file mode 100644
index 0000000..1daf057
--- /dev/null
+++ b/roles/run-dhall-diff/defaults/main.yaml
@@ -0,0 +1,4 @@
+dhall_render_command: make
+
+zuul_work_dir: "{{ zuul.project.src_dir }}"
+dhall_diff_ignore_errors: false
diff --git a/roles/run-dhall-diff/tasks/main.yaml b/roles/run-dhall-diff/tasks/main.yaml
new file mode 100644
index 0000000..164f291
--- /dev/null
+++ b/roles/run-dhall-diff/tasks/main.yaml
@@ -0,0 +1,38 @@
+- name: "Run render command: {{ dhall_render_command }}"
+ command: "{{ dhall_render_command }}"
+ args:
+ chdir: "{{ zuul_work_dir }}"
+
+- name: Check for diff
+ command: git diff
+ args:
+ chdir: "{{ zuul_work_dir }}"
+ register: dhall_git_diff
+ # ANSIBLE0006: Skip linting since it triggers on the "git" command,
+ # but diff is not supported by ansible git module.
+ tags:
+ - skip_ansible_lint
+
+- name: Abord on diff
+ when:
+ - dhall_git_diff.stdout
+ - not dhall_diff_ignore_errors
+ failed_when: true
+ debug:
+ msg: |
+ The repository content is not consistent.
+ Please run `{{ dhall_render_command }}` before `git commit`
+
+ {% if dhall_git_diff.stdout | regex_search('^-') %}
+ You commited change in a configuration file without using the equivalent Dhall file.
+ Please update the Dhall file first and run make.
+
+ {% else %}
+ You commited change in a Dhall file without running make.
+ Please run `{{ dhall_render_command }}` before `git commit`.
+
+ {% endif %}
+
+ The difference is:
+
+ {{ dhall_git_diff.stdout }}
diff --git a/zuul.d/general-jobs.yaml b/zuul.d/general-jobs.yaml
index 85d4ceb..909ceda 100644
--- a/zuul.d/general-jobs.yaml
+++ b/zuul.d/general-jobs.yaml
@@ -93,3 +93,24 @@
Override the default searching above with explicit
domain/path references (see validate-zone-db role)
run: playbooks/validate-zone-db/run.yaml
+
+- job:
+ name: dhall-diff
+ description: |
+ Ensure that generated configuration files are idempotent.
+
+ This job runs a render command and check that no files are
+ modified.
+
+ .. zuul:jobvar:: dhall_render_command
+ :default: make
+
+ The command that render the configuration files.
+
+ .. zuul:jobvar:: dhall_version
+ :default: 1.31.1
+
+ The dhall version.
+
+ pre-run: playbooks/dhall/prepare.yaml
+ run: playbooks/dhall/diff.yaml