Merge "Add helper playbook to create new roles"

This commit is contained in:
Zuul 2019-06-13 17:53:06 +00:00 committed by Gerrit Code Review
commit 180edcfa9c
3 changed files with 159 additions and 1 deletions

View File

@ -5,3 +5,7 @@ rules:
line-length:
# matches hardcoded 160 value from ansible-lint
max: 160
ignore: |
zuul.d/jobs.yaml
zuul.d/layout.yaml

View File

@ -7,4 +7,55 @@ Adding roles into this project is easy and starts with a compatible skeleton.
From with the project root, creating a skeleton for the new role::
$ ansible-galaxy init --role-skeleton=_skeleton_role_ --init-path=tripleo_ansible/roles NEWROLENAME
$ ansible-galaxy init --role-skeleton=_skeleton_role_ --init-path=tripleo_ansible/roles ${NEWROLENAME}
Once the new role has been created, and is ready for testing add the role into
the `tox.ini` file as an test scenario.
.. code-block::
[testenv:mol-${NEWROLENAME}]
basepython={[testenv:mol]basepython}
deps={[testenv:mol]deps}
changedir = {toxinidir}/tripleo_ansible/roles/${NEWROLENAME}
envdir = {toxworkdir}/mol
commands =
python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py
When the role is ready for CI add a jobs entry into the `zuul.d/jobs.yaml`::
- job:
name: tripleo-ansible-centos:mol-${NEWROLENAME}
parent: tripleo-ansible-centos
files:
- ^tripleo_ansible/roles/${NEWROLENAME}/.*
vars:
tox_envlist: mol-${NEWROLENAME}
And finally add the job into the `zuul.d/layout.yaml` file::
- project:
check:
jobs:
- tripleo-ansible-centos:mol-${NEWROLENAME}
The role addition process is also automated using ansible. If ansible is
available on the development workstation change directory to the root of
the `tripleo-ansible` repository and run the the following command which
will perform all of the tasks noted above.
.. code-block::
$ ansible-playbook -i localhost, role-addition.yml -e role_name=${NEWROLENAME}
If this playbook is being executed from a virtual-environment be sure to activate
the virtual environment before running the playbook.
.. code-block::
$ . ~/bin/venvs/ansible/bin/activate
(ansible)$ ansible-playbook -i localhost, role-addition.yml -e role_name=${NEWROLENAME}

103
role-addition.yml Normal file
View File

@ -0,0 +1,103 @@
---
# Copyright 2019 Red Hat, Inc.
# All Rights Reserved.
#
# 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.
- name: Create a new role for TripleO-Ansible
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Check for role name
fail:
msg: >-
The required variable `role_name` is undefined. Check your settings.
when:
- role_name is undefined
- name: Create role
command: >-
ansible-galaxy init
--role-skeleton=_skeleton_role_
--init-path=tripleo_ansible/roles {{ role_name }}
args:
creates: "tripleo_ansible/roles/{{ role_name }}"
- name: Add tox config
ini_file:
path: tox.ini
section: "testenv:mol-{{ role_name }}"
option: "{{ item.key }}"
value: "{{ item.value }}"
with_items:
- key: "basepython"
value: "{[testenv:mol]basepython}"
- key: "deps"
value: "{[testenv:mol]deps}"
- key: "changedir"
value: "{toxinidir}/tripleo_ansible/roles/{{ role_name }}"
- key: "commands"
value: "python -m pytest --color=yes --html={envlogdir}/reports.html --self-contained-html {tty:-s} {toxinidir}/tests/test_molecule.py"
- name: Read zuul jobs file
slurp:
src: zuul.d/jobs.yaml
register: jobs_yaml
- name: Create jobs entry
copy:
content: |-
---
{% set jobs = jobs_yaml['content'] | b64decode | from_yaml %}
{% set job_index = [] %}
{% for job in jobs %}
{% if job.job.name == "tripleo-ansible-centos:mol-" ~ role_name %}
{% set _ = job_index.append("tripleo-ansible-centos:mol-" ~ role_name) %}
{% endif %}
{% endfor %}
{% if (job_index | length) < 1 %}
{% set new_job = {
"name": "tripleo-ansible-centos-7-molecule-" ~ role_name,
"parent": "tripleo-ansible-centos",
"files": [
"^tripleo_ansible/roles/" ~ role_name ~ "/.*"
],
"vars": {
"tox_envlist": "mol-" ~ role_name
}
}
%}
{% set _ = jobs.append({"job": new_job}) %}
{% endif %}
{{ jobs | to_nice_yaml(indent=2, width=1337) }}
dest: zuul.d/jobs.yaml
- name: Read zuul layout file
slurp:
src: zuul.d/layout.yaml
register: layout_yaml
- name: Create jobs entry
copy:
content: |-
---
{% set layouts = layout_yaml['content'] | b64decode | from_yaml %}
{% set new_job_name = "tripleo-ansible-centos-7-molecule-" ~ role_name %}
{% for layout in layouts %}
{% if not (new_job_name in layout.project.check.jobs) %}
{% set _ = layout.project.check.jobs.append(new_job_name) %}
{% endif %}
{% endfor %}
{{ layouts | to_nice_yaml(indent=2, width=1337) }}
dest: zuul.d/layout.yaml