Merge "Adds roles to install and run hashicorp packer"

This commit is contained in:
Zuul 2020-04-15 16:01:18 +00:00 committed by Gerrit Code Review
commit 21928eab52
18 changed files with 426 additions and 0 deletions

View File

@ -10,4 +10,5 @@ Jobs
docker-jobs docker-jobs
go-jobs go-jobs
helm-jobs helm-jobs
packer-jobs
system-jobs system-jobs

View File

@ -0,0 +1,4 @@
Packer Jobs
===========
.. zuul:autojob:: packer

View File

@ -0,0 +1,5 @@
Packer Roles
============
.. zuul:autorole:: ensure-packer
.. zuul:autorole:: packer

View File

@ -20,6 +20,7 @@ Roles
js-roles js-roles
kubernetes-roles kubernetes-roles
launchpad-roles launchpad-roles
packer-roles
puppet-roles puppet-roles
python-roles python-roles
system-roles system-roles

View File

@ -0,0 +1,9 @@
- hosts: all
pre_tasks:
- name: Install unzip
package:
name: unzip
become: yes
roles:
- revoke-sudo
- ensure-packer

View File

@ -0,0 +1,3 @@
- hosts: all
roles:
- packer

View File

@ -0,0 +1,24 @@
Install packer
**Role Variables**
.. zuul:rolevar:: packer_install_dir
:default: {{ ansible_user_dir }}/.local/bin/
Directory to install packer in.
.. zuul:rolevar:: packer_version
:default: 1.5.5
Version of packer to install.
Zuul will skip the installation if this matches an already installed version of packer.
.. zuul:rolevar:: packer_os
:default: {{ ansible_system | lower }}
OS target of package to install.
.. zuul:rolevar:: packer_arch
:default: amd64 / 386
Architecture target of package to install.

View File

@ -0,0 +1,7 @@
hashicorp_releases_fqdn: https://releases.hashicorp.com
packer_version: 1.5.5
packer_install_dir: "{{ ansible_user_dir }}/.local/bin/"
packer_os: "{{ ansible_system | lower }}"
packer_arch: "{{ packer_arch_translation[ansible_architecture] }}"
packer_package: "packer_{{ packer_version }}_{{ packer_os }}_{{ packer_arch }}"
packer_executable: "{{ ansible_user_dir }}/.local/bin/packer"

View File

@ -0,0 +1,59 @@
- name: Get packer checksums
uri:
url: "{{ hashicorp_releases_fqdn }}/\
packer/{{ packer_version }}/packer_{{ packer_version }}_SHA256SUMS"
return_content: true
register: packer_version_checksums
- name: Set packer checksum
set_fact:
packer_checksum: "{{\
packer_version_checksums.content |\
regex_search( '[a-z0-9]+ ' + packer_package) |\
regex_replace( '(?P<checksum>[a-z0-9]+) ' + packer_package, '\\g<checksum>')
}}"
- name: Create temp directory
tempfile:
state: directory
register: packer_install_tempdir
- name: Download packer archive
get_url:
url: "{{ hashicorp_releases_fqdn }}/\
packer/{{ packer_version }}/{{ packer_package }}.zip"
dest: "{{ packer_install_tempdir.path }}/{{ packer_package }}.zip"
checksum: "sha256:{{ packer_checksum }}"
- name: Create packer package directory
file:
path: "{{ packer_install_tempdir.path }}/{{ packer_package }}"
state: directory
- name: Unarchive packer
unarchive:
src: "{{ packer_install_tempdir.path }}/{{ packer_package }}.zip"
dest: "{{ packer_install_tempdir.path }}/{{ packer_package }}"
remote_src: yes
- name: Make sure installation directory exists
file:
path: "{{ packer_install_dir }}"
state: directory
- name: Install packer
copy:
src: "{{ packer_install_tempdir.path }}/{{ packer_package }}/packer"
dest: "{{ packer_install_dir }}/packer"
mode: '0755'
owner: "{{ ansible_user }}"
remote_src: yes
- name: Set packer executable fact
set_fact:
packer_executable: "{{ packer_install_dir }}/packer"
cacheable: true
- name: Output packer version
command: "{{ packer_executable }} version"

View File

@ -0,0 +1,11 @@
- name: Check installed packer version
command: "{{ packer_executable }} version"
register: packer_installed_version
failed_when: false
- name: Install packer
include_tasks: install-packer.yaml
when:
- packer_installed_version.rc != 0 or
"packer_version != (packer_installed_version.msg | \
regex_replace(packer_version_pattern, '\\g<version>'))"

View File

@ -0,0 +1,7 @@
packer_arch_translation:
amd64: amd64
x86_64: amd64
i386: 386
packer_version_pattern: ^Packer v(?P<version>\d+\.\d+\.\d+).*$

32
roles/packer/README.rst Normal file
View File

@ -0,0 +1,32 @@
Run packer command. Assumes the appropriate version of packer has been installed.
**Role Variables**
.. zuul:rolevar:: packer_executable
:default: {{ ansible_user_dir }}/.local/bin/packer
Path to packer executable to use.
.. zuul:rolevar:: packer_command
:default: build
Packer command to run.
Examples are "build", "validate"
.. zuul:rolevar:: packer_template
:default: packer.json
Packer template file to use when executing packer.
.. zuul:rolevar:: packer_extra_args
String of extra command line options to pass to packer.
.. zuul:rolevar:: packer_environemnt
Environment variables to set in packer command.
.. zuul:rolevar:: zuul_workdir
:default: {{ zuul.project.src_dir }}
Directory to run packer in.

View File

@ -0,0 +1,6 @@
packer_command: build
packer_executable: "{{ ansible_user_dir }}/.local/bin/packer"
packer_workdir: "{{ zuul.project.src_dir }}"
packer_template: "packer.json"
packer_extra_args: ""
packer_environment: {}

View File

@ -0,0 +1,36 @@
- name: Require packer executable
fail:
msg: packer_executable not defined
when: packer_executable is not defined
- name: Create packer variable tempfile
tempfile:
register: packer_variable_tempfile
when: packer_variables is defined
- name: Create packer variables file
copy:
content: |
{{ packer_variables | to_json }}
dest: "{{ packer_variable_tempfile.path }}"
when: packer_variables is defined
no_log: true # We don't want to log this since credentials could be passed this way
- block:
- name: Run packer
command: >-
{{ packer_executable }}
{{ packer_command }}
{% if packer_variables is defined %}
-var-file={{ packer_variable_tempfile.path }}
{% endif %}
{{ packer_extra_args }}
{{ packer_template }}
environment: "{{ packer_environment }}"
args:
chdir: "{{ packer_workdir }}"
always:
- name: Delete packer variables file
file:
state: absent
path: "{{ packer_variable_tempfile.path }}"

View File

@ -0,0 +1,11 @@
{
"variables": {
"packer_test_file": "{{env `PACKER_TEST_FILE`}}"
},
"builders": [{
"type": "file",
"content": "{{user `packer_test_content`}}",
"target": "{{user `packer_test_file` }}"
}]
}

View File

@ -0,0 +1,9 @@
- hosts: all
tasks:
- name: Ensure test_variable was written to file
lineinfile:
line: "{{ packer_variables.packer_test_content | replace('\n', '') }}"
dest: "{{ packer_environment.PACKER_TEST_FILE }}"
check_mode: yes
register: test_variable_presence
failed_when: test_variable_presence.changed

View File

@ -0,0 +1,144 @@
- job:
name: test-packer
parent: packer
description: Test packer job
post-run: test-playbooks/packer/post.yaml
tags: all-platforms
files:
- roles/ensure-packer/.*
- roles/packer/.*
- test-playbooks/packer/.*
- zuul.d/packer-jobs.yaml
vars:
packer_install_dir: '{{ ansible_user_dir }}/packer'
packer_template: test-playbooks/packer/packer.json
packer_environment:
PACKER_TEST_FILE: testfile
packer_variables:
packer_test_content: "abc\n"
- job:
name: test-packer-centos-7
description: Test packer job on centos-7
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: centos-7
label: centos-7
- job:
name: test-packer-centos-8
description: Test packer job on centos-8
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: centos-8
label: centos-8
- job:
name: test-packer-debian-stretch
description: Test packer job on debian-stretch
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: debian-stretch
label: debian-stretch
- job:
name: test-packer-fedora-30
description: Test packer job on fedora-30
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: fedora-30
label: fedora-30
- job:
name: test-packer-gentoo-17-0-systemd
description: Test packer job on gentoo-17-0-systemd
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: gentoo-17-0-systemd
label: gentoo-17-0-systemd
- job:
name: test-packer-opensuse-15
description: Test packer job on opensuse-15
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: opensuse-15
label: opensuse-15
- job:
name: test-packer-opensuse-tumbleweed-nv
voting: false
description: Test packer job on opensuse-tumbleweed
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: opensuse-tumbleweed
label: opensuse-tumbleweed
- job:
name: test-packer-ubuntu-bionic
description: Test packer job on ubuntu-bionic
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: ubuntu-bionic
label: ubuntu-bionic
- job:
name: test-packer-ubuntu-bionic-plain-nv
voting: false
description: Test packer job on ubuntu-bionic-plain
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: ubuntu-bionic-plain
label: ubuntu-bionic-plain
- job:
name: test-packer-ubuntu-xenial
description: Test packer job on ubuntu-xenial
parent: test-packer
tags: auto-generated
nodeset:
nodes:
- name: ubuntu-xenial
label: ubuntu-xenial
- project:
check:
jobs:
- test-packer-centos-7
- test-packer-centos-8
- test-packer-debian-stretch
- test-packer-fedora-30
- test-packer-gentoo-17-0-systemd
- test-packer-opensuse-15
- test-packer-opensuse-tumbleweed-nv
- test-packer-ubuntu-bionic
- test-packer-ubuntu-bionic-plain-nv
- test-packer-ubuntu-xenial
gate:
jobs:
- test-packer-centos-7
- test-packer-centos-8
- test-packer-debian-stretch
- test-packer-fedora-30
- test-packer-gentoo-17-0-systemd
- test-packer-opensuse-15
- test-packer-ubuntu-bionic
- test-packer-ubuntu-xenial

57
zuul.d/packer-jobs.yaml Normal file
View File

@ -0,0 +1,57 @@
- job:
name: packer
description: |
Base job for packaer operations
Responds to these variables:
.. zuul:jobvar:: packer_command
:default: build
Command to pass to packer.
.. zuul:jobvar:: packer_variables
Dictionary with variables to pass to packer.
.. zuul:jobvar:: packer_extra_args
:default: ""
String containing extra arguments to append to the packer command line.
.. zuul:jobvar:: packer_environemnt
Environment variables to set in packer command.
.. zuul:jobvar:: packer_template
:default: packer.json
Packer template json file to use.
.. zuul:jobvar:: packer_install_dir
:default: {{ ansible_user_dir }}/packer/
Path to install packer in.
.. zuul:jobvar:: packer_version
:default: 1.5.5
The version of packer to use.
.. zuul:jobvar:: packer_os
:default: {{ ansible_system | lower }}
OS to use when choosing packer version.
.. zuul:jobvar:: packer_arch
:default: amd64 / 386
Architecture to use when choosing packer version
.. zuul:jobvar:: zuul_work_dir
:default: {{ zuul.project.src_dir }}
Path to operate in.
pre-run: playbooks/packer/pre.yaml
run: playbooks/packer/run.yaml