Skip to content

Instantly share code, notes, and snippets.

@gundalow
Last active May 1, 2020 20:09
Show Gist options
  • Save gundalow/54e31533d04608b3d2abc106716ce88b to your computer and use it in GitHub Desktop.
Save gundalow/54e31533d04608b3d2abc106716ce88b to your computer and use it in GitHub Desktop.
validate-routing.yml
#!/usr/bin/env python
"""Make sure the data in meta/routing.yml is valid"""
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import glob
from pathlib import Path
import os
import re
import sys
import yaml
import q
from voluptuous import All, Any, Match, MultipleInvalid, Required, Schema, ALLOW_EXTRA, PREVENT_EXTRA, All, Any, Invalid, Length, Required, Schema, Self, ValueInvalid
from voluptuous.humanize import humanize_error
from ansible.module_utils.six import string_types
def main():
"""Validate ansible-base' routing.yml"""
path = 'lib/ansible/config/routing.yml'
if not os.path.isfile(path):
# skip if routing file doesn't exist
print("skipping\n")
sys.exit(0)
try:
with open(path, 'r') as f_path:
routing = yaml.safe_load(f_path)
except yaml.error.MarkedYAMLError as ex:
print('%s:%d:%d: YAML load failed: %s' % (path, ex.context_mark.line +
1, ex.context_mark.column + 1, re.sub(r'\s+', ' ', str(ex))))
sys.exit()
except Exception as ex: # pylint: disable=broad-except
print('%s:%d:%d: YAML load failed: %s' %
(path, 0, 0, re.sub(r'\s+', ' ', str(ex))))
sys.exit()
list_string_types = list(string_types)
deprecation_schema = Schema(
{
Required('removal_date'): Any(*string_types),
Required('warning_text'): Any(*string_types),
},
extra=PREVENT_EXTRA
)
files_schema = Any(
Schema(*string_types),
Schema({
#Required('deprecation'): Any(deprecation_schema)
Required('redirect'): Any(*string_types),
})
)
list_dict_file_schema = [{str_type: files_schema}
for str_type in string_types]
modules_schema = Schema({
Required('action'): Any(None, *list_dict_file_schema),
Required('become'): Any(None, *list_dict_file_schema),
Required('cache'): Any(None, *list_dict_file_schema),
Required('callback'): Any(None, *list_dict_file_schema),
Required('cliconf'): Any(None, *list_dict_file_schema),
Required('connection'): Any(None, *list_dict_file_schema),
Required('doc_fragments'): Any(None, *list_dict_file_schema),
Required('filter'): Any(None, *list_dict_file_schema),
Required('httpapi'): Any(None, *list_dict_file_schema),
Required('inventory'): Any(None, *list_dict_file_schema),
Required('lookup'): Any(None, *list_dict_file_schema),
Required('modules'): Any(None, *list_dict_file_schema),
Required('module_utils'): Any(None, *list_dict_file_schema),
Required('netconf'): Any(None, *list_dict_file_schema),
Required('shell'): Any(None, *list_dict_file_schema),
Required('terminal'): Any(None, *list_dict_file_schema),
})
schema = Schema({
Required('plugin_routing'): Any(modules_schema),
})
# Ensure schema is valid
try:
schema(routing)
except MultipleInvalid as ex:
for error in ex.errors:
# No way to get line numbers
print('%s:%d:%d: %s' % (path, 0, 0, humanize_error(routing, error)))
for plugin, file_meta in routing['plugin_routing']['modules'].items():
namespace, collection, new_name = routing['plugin_routing']['modules'][plugin]['redirect'].split('.')
q.q(collection)
#q.q(Path('plugins/modules/').rglob('%s.py*' % file))
if namespace == 'community':
if not os.path.exists("/home/johnb/git/work/ansible_collections/%s/%s/plugins/modules/%s.py" % (namespace, collection, new_name)):
print("%s:%d:%d: Can't find 'plugins/%s.py' in %s.%s" %
(path, 0, 0, new_name, namespace, collection))
if not Path('plugins/modules/').rglob('%s.py*' % plugin):
# if not glob.glob('%/.py*' % file, recursive=True):
print("%s:%d:%d: Can't find 'plugins/%s.py' in this branch" %
(path, 0, 0, plugin))
# FIXME: Ensure files exist, 322a4dc691ce94bad6259f63dad1fe991dfed9c5
if __name__ == '__main__':
main()
# Possible future work
# * Schema for `macros:` - currently ignored due to team_ansible
# * Ensure that all $teams mention in `files:` exist in `$macros`
# * Validate GitHub names - possibly expensive lookup needed - No should be validated when module is added - gundalow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment