Add autodocumentation for roles and library

This patch adds automation to add documentation for each Ansible roles
and custom modules for TripleO-Validations.

Patch-Inspired-By: tripleo-ansible-Team++
Change-Id: I2c8ec698cdcbc304b5d9f3bd72bcede5bf2aafd3
Signed-off-by: Gael Chamoulaud <gchamoul@redhat.com>
This commit is contained in:
Gael Chamoulaud 2019-07-23 10:14:29 +02:00
parent 0204d7665c
commit 218bef9cbe
78 changed files with 972 additions and 14 deletions

View File

@ -0,0 +1,325 @@
# 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.
import imp
import os
from docutils import core
from docutils import nodes
from docutils.parsers.rst import Directive
from docutils.parsers import rst
from docutils.writers.html4css1 import Writer
import yaml
class AnsibleAutoPluginDirective(Directive):
directive_name = "ansibleautoplugin"
has_content = True
option_spec = {
'module': rst.directives.unchanged,
'role': rst.directives.unchanged,
'documentation': rst.directives.unchanged,
'examples': rst.directives.unchanged
}
@staticmethod
def _render_html(source):
return core.publish_parts(
source=source,
writer=Writer(),
writer_name='html',
settings_overrides={'no_system_messages': True}
)
def make_node(self, title, contents, content_type=None):
section = self._section_block(title=title)
if not content_type:
# Doc section
for content in contents['docs']:
for paragraph in content.split('\n'):
retnode = nodes.paragraph()
retnode.append(self._raw_html_block(data=paragraph))
section.append(retnode)
# Options Section
options_list = nodes.field_list()
options_section = self._section_block(title='Options')
for key, value in contents['options'].items():
options_list.append(
self._raw_fields(
data=value['description'],
field_name=key
)
)
else:
options_section.append(options_list)
section.append(options_section)
# Authors Section
authors_list = nodes.field_list()
authors_list.append(
self._raw_fields(
data=contents['author']
)
)
authors_section = self._section_block(title='Authors')
authors_section.append(authors_list)
section.append(authors_section)
elif content_type == 'yaml':
for content in contents:
section.append(
self._literal_block(
data=content,
dump_data=False
)
)
return section
@staticmethod
def load_module(filename):
return imp.load_source('__ansible_module__', filename)
@staticmethod
def build_documentation(module):
docs = yaml.safe_load(module.DOCUMENTATION)
doc_data = dict()
doc_data['docs'] = docs['description']
doc_data['author'] = docs.get('author', list())
doc_data['options'] = docs.get('options', dict())
return doc_data
@staticmethod
def build_examples(module):
examples = yaml.safe_load(module.EXAMPLES)
return_examples = list()
for example in examples:
return_examples.append(
yaml.safe_dump([example], default_flow_style=False)
)
return return_examples
def _raw_html_block(self, data):
html = self._render_html(source=data)
return nodes.raw('', html['body'], format='html')
def _raw_fields(self, data, field_name=''):
body = nodes.field_body()
if isinstance(data, list):
for item in data:
body.append(self._raw_html_block(data=item))
else:
body.append(self._raw_html_block(data=data))
field = nodes.field()
field.append(nodes.field_name(text=field_name))
field.append(body)
return field
@staticmethod
def _literal_block(data, language='yaml', dump_data=True):
if dump_data:
literal = nodes.literal_block(
text=yaml.safe_dump(
data,
default_flow_style=False
)
)
else:
literal = nodes.literal_block(text=data)
literal['language'] = 'yaml'
return literal
@staticmethod
def _section_block(title, text=None):
section = nodes.section(
title,
nodes.title(text=title),
ids=[nodes.make_id('-'.join(title))],
)
if text:
section_body = nodes.field_body()
section_body.append(nodes.paragraph(text=text))
section.append(section_body)
return section
def _yaml_section(self, to_yaml_data, section_title, section_text=None):
yaml_section = self._section_block(
title=section_title,
text=section_text
)
yaml_section.append(self._literal_block(data=to_yaml_data))
return yaml_section
def _run_role(self, role):
section = self._section_block(
title='Role Documentation',
text='Welcome to the "{}" role documentation.'.format(
os.path.basename(role)
)
)
defaults_file = os.path.join(role, 'defaults', 'main.yml')
if os.path.exists(defaults_file):
with open(defaults_file) as f:
role_defaults = yaml.safe_load(f.read())
section.append(
self._yaml_section(
to_yaml_data=role_defaults,
section_title='Role Defaults',
section_text='This section highlights all of the defaults'
' and variables set within the "{}"'
' role.'.format(
os.path.basename(role)
)
)
)
vars_path = os.path.join(role, 'vars')
if os.path.exists(vars_path):
for v_file in os.listdir(vars_path):
vars_file = os.path.join(vars_path, v_file)
with open(vars_file) as f:
vars_values = yaml.safe_load(f.read())
section.append(
self._yaml_section(
to_yaml_data=vars_values,
section_title='Role Variables: {}'.format(v_file)
)
)
test_section = self._section_block(
title='Molecule Scenarios',
text='Molecule is being used to test the "{}" role. The'
' following section highlights the drivers in service'
' and provides an example playbook showing how the role'
' is leveraged.'.format(
os.path.basename(role)
)
)
molecule_path = os.path.join(role, 'molecule')
if os.path.exists(molecule_path):
for test in os.listdir(molecule_path):
molecule_section = self._section_block(
title='Scenario: {}'.format(test)
)
molecule_file = os.path.join(
molecule_path,
test,
'molecule.yml'
)
with open(molecule_file) as f:
molecule_conf = yaml.safe_load(f.read())
molecule_section.append(
self._yaml_section(
to_yaml_data=molecule_conf,
section_title='Example {} configuration'.format(test)
)
)
provisioner_data = molecule_conf.get('provisioner')
if provisioner_data:
inventory = provisioner_data.get('inventory')
if inventory:
molecule_section.append(
self._yaml_section(
to_yaml_data=inventory,
section_title='Molecule Inventory'
)
)
molecule_playbook_path = os.path.join(
molecule_path,
test,
'playbook.yml'
)
with open(molecule_playbook_path) as f:
molecule_playbook = yaml.safe_load(f.read())
molecule_section.append(
self._yaml_section(
to_yaml_data=molecule_playbook,
section_title='Example {} playbook'.format(test)
)
)
test_section.append(molecule_section)
else:
section.append(test_section)
self.run_returns.append(section)
# Document any libraries nested within the role
library_path = os.path.join(role, 'library')
if os.path.exists(library_path):
self.options['documentation'] = True
self.options['examples'] = True
for lib in os.listdir(library_path):
if lib.endswith('.py'):
self._run_module(
module=self.load_module(
filename=os.path.join(
library_path,
lib
)
),
module_title='Embedded module: {}'.format(lib),
example_title='Examples for embedded module'
)
def _run_module(self, module, module_title="Module Documentation",
example_title="Example Tasks"):
if self.options.get('documentation'):
docs = self.build_documentation(module=module)
self.run_returns.append(
self.make_node(
title=module_title,
contents=docs
)
)
if self.options.get('examples'):
examples = self.build_examples(module=module)
self.run_returns.append(
self.make_node(
title=example_title,
contents=examples,
content_type='yaml'
)
)
def run(self):
self.run_returns = list()
if self.options.get('module'):
module = self.load_module(filename=self.options['module'])
self._run_module(module=module)
if self.options.get('role'):
self._run_role(role=self.options['role'])
return self.run_returns
def setup(app):
classes = [
AnsibleAutoPluginDirective,
]
for directive_class in classes:
app.add_directive(directive_class.directive_name, directive_class)
return {'version': '0.2'}

View File

@ -87,8 +87,9 @@ def build_detail(group, validations):
- **parameters**: {parameters}
- **roles**: {roles}
`View source code for the role <https://opendev.org/openstack/tripleo-validations/src/branch/master/roles/{roles}/>`__.
.. toctree::
roles/role-{roles}
"""
.format(label=(group + '_' + validation['id']),
title=validation['id'],

View File

@ -27,6 +27,7 @@ sys.path.insert(0, os.path.join(os.path.abspath('.'), '_exts'))
extensions = [
'sphinx.ext.autodoc',
'generate_validations_doc',
'ansible-autodoc',
'openstackdocstheme'
]
@ -41,7 +42,7 @@ source_suffix = '.rst'
master_doc = 'index'
# General information about the project.
copyright = u'2016, OpenStack Foundation'
copyright = u'2019, OpenStack Foundation'
# If true, '()' will be appended to :func: etc. cross-reference text.
add_function_parentheses = True
@ -72,9 +73,6 @@ latex_documents = [
u'OpenStack Foundation', 'manual'),
]
# Example configuration for intersphinx: refer to the Python standard library.
#intersphinx_mapping = {'http://docs.python.org/': None}
# openstackdocstheme options
repository_name = 'openstack/tripleo-validations'
bug_project = 'tripleo'

View File

@ -15,6 +15,8 @@ Contents:
installation
usage
contributing
roles
modules
Existing validations:

10
doc/source/modules.rst Normal file
View File

@ -0,0 +1,10 @@
Documented modules in TripleO-Validations
=========================================
Contents:
.. toctree::
:glob:
modules/*

View File

@ -0,0 +1,15 @@
========================
Module - advanced_format
========================
This module provides for the following ansible plugin:
* advanced_format
.. ansibleautoplugin::
:module: library/advanced_format.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
======================
Module - check_flavors
======================
This module provides for the following ansible plugin:
* check_flavors
.. ansibleautoplugin::
:module: library/check_flavors.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
=================================
Module - check_ironic_boot_config
=================================
This module provides for the following ansible plugin:
* check_ironic_boot_config
.. ansibleautoplugin::
:module: library/check_ironic_boot_config.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
=============================
Module - check_package_update
=============================
This module provides for the following ansible plugin:
* check_package_update
.. ansibleautoplugin::
:module: library/check_package_update.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
=====================
Module - docker_facts
=====================
This module provides for the following ansible plugin:
* docker_facts
.. ansibleautoplugin::
:module: library/docker_facts.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
=====================
Module - haproxy_conf
=====================
This module provides for the following ansible plugin:
* haproxy_conf
.. ansibleautoplugin::
:module: library/haproxy_conf.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
==============
Module - hiera
==============
This module provides for the following ansible plugin:
* hiera
.. ansibleautoplugin::
:module: library/hiera.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
==================
Module - icmp_ping
==================
This module provides for the following ansible plugin:
* icmp_ping
.. ansibleautoplugin::
:module: library/icmp_ping.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
============
Module - ini
============
This module provides for the following ansible plugin:
* ini
.. ansibleautoplugin::
:module: library/ini.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
=================
Module - ip_range
=================
This module provides for the following ansible plugin:
* ip_range
.. ansibleautoplugin::
:module: library/ip_range.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
============================
Module - network_environment
============================
This module provides for the following ansible plugin:
* network_environment
.. ansibleautoplugin::
:module: library/network_environment.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
===================
Module - node_disks
===================
This module provides for the following ansible plugin:
* node_disks
.. ansibleautoplugin::
:module: library/node_disks.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
====================
Module - overcloudrc
====================
This module provides for the following ansible plugin:
* overcloudrc
.. ansibleautoplugin::
:module: library/overcloudrc.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
================================
Module - ovs_dpdk_pmd_cpus_check
================================
This module provides for the following ansible plugin:
* ovs_dpdk_pmd_cpus_check
.. ansibleautoplugin::
:module: library/ovs_dpdk_pmd_cpus_check.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
==================
Module - pacemaker
==================
This module provides for the following ansible plugin:
* pacemaker
.. ansibleautoplugin::
:module: library/pacemaker.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
=====================
Module - switch_vlans
=====================
This module provides for the following ansible plugin:
* switch_vlans
.. ansibleautoplugin::
:module: library/switch_vlans.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
========================
Module - verify_profiles
========================
This module provides for the following ansible plugin:
* verify_profiles
.. ansibleautoplugin::
:module: library/verify_profiles.py
:documentation: true
:examples: true

View File

@ -0,0 +1,15 @@
=============
Module - warn
=============
This module provides for the following ansible plugin:
* warn
.. ansibleautoplugin::
:module: library/warn.py
:documentation: true
:examples: true

10
doc/source/roles.rst Normal file
View File

@ -0,0 +1,10 @@
Documented roles in TripleO-Validations
=======================================
Contents:
.. toctree::
:glob:
roles/*

View File

@ -0,0 +1,7 @@
============================
advanced-format-512e-support
============================
.. ansibleautoplugin::
:role: roles/advanced-format-512e-support

View File

@ -0,0 +1,7 @@
=================
ceilometerdb-size
=================
.. ansibleautoplugin::
:role: roles/ceilometerdb-size

View File

@ -0,0 +1,7 @@
====
ceph
====
.. ansibleautoplugin::
:role: roles/ceph

View File

@ -0,0 +1,7 @@
==========================
check-latest-minor-version
==========================
.. ansibleautoplugin::
:role: roles/check-latest-minor-version

View File

@ -0,0 +1,7 @@
=====================
check-network-gateway
=====================
.. ansibleautoplugin::
:role: roles/check-network-gateway

View File

@ -0,0 +1,7 @@
===================================
collect-flavors-and-verify-profiles
===================================
.. ansibleautoplugin::
:role: roles/collect-flavors-and-verify-profiles

View File

@ -0,0 +1,7 @@
===============================
containerized-undercloud-docker
===============================
.. ansibleautoplugin::
:role: roles/containerized-undercloud-docker

View File

@ -0,0 +1,7 @@
================
controller-token
================
.. ansibleautoplugin::
:role: roles/controller-token

View File

@ -0,0 +1,7 @@
==================
controller-ulimits
==================
.. ansibleautoplugin::
:role: roles/controller-ulimits

View File

@ -0,0 +1,7 @@
=================
ctlplane-ip-range
=================
.. ansibleautoplugin::
:role: roles/ctlplane-ip-range

View File

@ -0,0 +1,7 @@
==================
default-node-count
==================
.. ansibleautoplugin::
:role: roles/default-node-count

View File

@ -0,0 +1,7 @@
================
dhcp-validations
================
.. ansibleautoplugin::
:role: roles/dhcp-validations

View File

@ -0,0 +1,7 @@
===
dns
===
.. ansibleautoplugin::
:role: roles/dns

View File

@ -0,0 +1,7 @@
=======
haproxy
=======
.. ansibleautoplugin::
:role: roles/haproxy

View File

@ -0,0 +1,7 @@
===========
image-serve
===========
.. ansibleautoplugin::
:role: roles/image-serve

View File

@ -0,0 +1,7 @@
=========================
ironic-boot-configuration
=========================
.. ansibleautoplugin::
:role: roles/ironic-boot-configuration

View File

@ -0,0 +1,7 @@
======================
mysql-open-files-limit
======================
.. ansibleautoplugin::
:role: roles/mysql-open-files-limit

View File

@ -0,0 +1,7 @@
===================
network-environment
===================
.. ansibleautoplugin::
:role: roles/network-environment

View File

@ -0,0 +1,7 @@
====================
neutron-sanity-check
====================
.. ansibleautoplugin::
:role: roles/neutron-sanity-check

View File

@ -0,0 +1,7 @@
==========================
no-op-firewall-nova-driver
==========================
.. ansibleautoplugin::
:role: roles/no-op-firewall-nova-driver

View File

@ -0,0 +1,7 @@
=====
no-op
=====
.. ansibleautoplugin::
:role: roles/no-op

View File

@ -0,0 +1,7 @@
==========
node-disks
==========
.. ansibleautoplugin::
:role: roles/node-disks

View File

@ -0,0 +1,7 @@
===========
node-health
===========
.. ansibleautoplugin::
:role: roles/node-health

View File

@ -0,0 +1,7 @@
===================
nova-event-callback
===================
.. ansibleautoplugin::
:role: roles/nova-event-callback

View File

@ -0,0 +1,7 @@
===========
nova-status
===========
.. ansibleautoplugin::
:role: roles/nova-status

View File

@ -0,0 +1,7 @@
===
ntp
===
.. ansibleautoplugin::
:role: roles/ntp

View File

@ -0,0 +1,7 @@
======================
openshift-on-openstack
======================
.. ansibleautoplugin::
:role: roles/openshift-on-openstack

View File

@ -0,0 +1,7 @@
===================
openstack-endpoints
===================
.. ansibleautoplugin::
:role: roles/openstack-endpoints

View File

@ -0,0 +1,7 @@
============
ovs-dpdk-pmd
============
.. ansibleautoplugin::
:role: roles/ovs-dpdk-pmd

View File

@ -0,0 +1,7 @@
================
pacemaker-status
================
.. ansibleautoplugin::
:role: roles/pacemaker-status

View File

@ -0,0 +1,7 @@
===============
rabbitmq-limits
===============
.. ansibleautoplugin::
:role: roles/rabbitmq-limits

View File

@ -0,0 +1,7 @@
=====
repos
=====
.. ansibleautoplugin::
:role: roles/repos

View File

@ -0,0 +1,7 @@
==============
service-status
==============
.. ansibleautoplugin::
:role: roles/service-status

View File

@ -0,0 +1,7 @@
============
stack-health
============
.. ansibleautoplugin::
:role: roles/stack-health

View File

@ -0,0 +1,7 @@
==============
stonith-exists
==============
.. ansibleautoplugin::
:role: roles/stonith-exists

View File

@ -0,0 +1,7 @@
============
switch-vlans
============
.. ansibleautoplugin::
:role: roles/switch-vlans

View File

@ -0,0 +1,7 @@
==============
tls-everywhere
==============
.. ansibleautoplugin::
:role: roles/tls-everywhere

View File

@ -0,0 +1,7 @@
==============
undercloud-cpu
==============
.. ansibleautoplugin::
:role: roles/undercloud-cpu

View File

@ -0,0 +1,7 @@
================
undercloud-debug
================
.. ansibleautoplugin::
:role: roles/undercloud-debug

View File

@ -0,0 +1,7 @@
=====================
undercloud-disk-space
=====================
.. ansibleautoplugin::
:role: roles/undercloud-disk-space

View File

@ -0,0 +1,7 @@
=============================
undercloud-heat-purge-deleted
=============================
.. ansibleautoplugin::
:role: roles/undercloud-heat-purge-deleted

View File

@ -0,0 +1,7 @@
========================
undercloud-process-count
========================
.. ansibleautoplugin::
:role: roles/undercloud-process-count

View File

@ -0,0 +1,7 @@
==============
undercloud-ram
==============
.. ansibleautoplugin::
:role: roles/undercloud-ram

View File

@ -0,0 +1,7 @@
=======================
undercloud-selinux-mode
=======================
.. ansibleautoplugin::
:role: roles/undercloud-selinux-mode

View File

@ -0,0 +1,7 @@
=========================
undercloud-service-status
=========================
.. ansibleautoplugin::
:role: roles/undercloud-service-status

View File

@ -0,0 +1,7 @@
=====================
undercloud-tokenflush
=====================
.. ansibleautoplugin::
:role: roles/undercloud-tokenflush

View File

@ -0,0 +1,8 @@
===============
xfs-check-ftype
===============
.. ansibleautoplugin::
:role: roles/xfs-check-ftype

View File

@ -23,8 +23,8 @@ module: check_flavors
short_description: Check that assigned flavors exist and are configured
description:
- Validate that the flavors assigned to roles exist and have the correct
settings. Right now, that means that boot_option is unset or set to 'local'
, or if set to 'netboot', issue a warning.
settings. Right now, that means that boot_option is unset or set to
'local', or if set to 'netboot', issue a warning.
options:
roles_info:
required: true

View File

@ -21,10 +21,10 @@ from ansible.module_utils.basic import AnsibleModule # noqa
DOCUMENTATION = '''
---
module: check_ironic_boot_config
short_description: >
short_description:
- Check that overcloud nodes have the correct associated ramdisk and kernel
image
description: >
description:
- Each overcloud node needs to have the correct associated ramdisk and
kernel image according to its architecture and platform. When it does
appear that the correct image is associated, we also need to check that

View File

@ -25,6 +25,8 @@ DOCUMENTATION = '''
---
module: check_package_update
short_description: Check for available updates for a given package
description:
- Check for available updates for a given package
options:
package:
required: true
@ -45,7 +47,7 @@ EXAMPLES = '''
- name: Get available updates for packages
check_package_update:
package: python-tripleoclient
pkg_mgr: {{ ansible_pkg_mgr}}
pkg_mgr: "{{ ansible_pkg_mgr}}"
'''
SUPPORTED_PKG_MGRS = (

View File

@ -22,7 +22,7 @@ module: node_disks
short_description: Check disks, flavors and root device hints
description:
- Check if each node has a root device hint set if there is more
than one disk and compare flavors to disk sizes.
than one disk and compare flavors to disk sizes.
options:
nodes:
required: true

View File

@ -35,8 +35,8 @@ module: switch_vlans
short_description: Check configured VLANs against Ironic introspection data
description:
- Validate that the VLANs defined in TripleO nic config files are in the
LLDP info received from network switches. The LLDP data is stored in
Ironic introspection data per interface.
LLDP info received from network switches. The LLDP data is stored in
Ironic introspection data per interface.
options:
path:
required: true

View File

@ -33,3 +33,14 @@
--init-path=roles {{ role_name }}
args:
creates: "roles/{{ role_name }}"
- name: Create role documentation
copy:
content: |
{{ '=' * (role_name | length) }}
{{ role_name }}
{{ '=' * (role_name | length) }}
.. ansibleautoplugin::
:role: roles/{{ role_name }}
dest: "doc/source/roles/role-{{ role_name }}.rst"

View File

@ -1 +0,0 @@
---