← Back to team overview

cloud-init-dev team mailing list archive

Re: [Merge] ~chad.smith/cloud-init:cc-ntp-schema-validation into cloud-init:master

 


Diff comments:

> diff --git a/cloudinit/config/cc_ntp.py b/cloudinit/config/cc_ntp.py
> index 5cc5453..f2eb5a6 100644
> --- a/cloudinit/config/cc_ntp.py
> +++ b/cloudinit/config/cc_ntp.py
> @@ -52,21 +54,79 @@ NR_POOL_SERVERS = 4
>  distros = ['centos', 'debian', 'fedora', 'opensuse', 'ubuntu']
>  
>  
> +# The schema definition for each cloud-config module is a strict contract for
> +# describing supported configuration parameters for each cloud-config section.
> +# It allows cloud-config to validate and alert users to invalid or ignored
> +# configuration options before actually attempting to deploy with said
> +# configuration.
> +
> +schema = {
> +    'id': 'cc_ntp',
> +    'name': 'NTP',
> +    'title': 'enable and configure ntp',
> +    'description': dedent("""

corrected. thanks

> +        Handle ntp configuration. If ntp is not installed on the system and
> +        ntp configuration is specified, ntp will be installed. If there is a
> +        default ntp config file in the image or one is present in the
> +        distro's ntp package, it will be copied to ``/etc/ntp.conf.dist``
> +        before any changes are made. A list of ntp pools and ntp servers can
> +        be provided under the ``ntp`` config key. If no ntp ``servers`` or 
> +        ``pools`` are provided, 4 pools will be used in the format
> +        ``{0-3}.{distro}.pool.ntp.org``."""),
> +    'distros': distros,
> +    'frequency': PER_INSTANCE,
> +    'type': 'object',
> +    'properties': {
> +        'ntp': {
> +            'type': ['object', 'null'],
> +            'properties': {
> +                'pools': {
> +                    'type': 'array',
> +                    'items': {
> +                        'type': 'string',
> +                        'format': 'hostname'
> +                    },
> +                    'uniqueItems': True,
> +                    'description': dedent("""
> +                        List of ntp pools. If both pools and servers are
> +                         empty, 4 default pool servers will be provided of
> +                         the format ``{0-3}.{distro}.pool.ntp.org``.""")
> +                },
> +                'servers': {
> +                    'type': 'array',
> +                    'items': {
> +                        'type': 'string',
> +                        'format': 'hostname'
> +                    },
> +                    'uniqueItems': True,
> +                    'description': dedent("""
> +                        List of ntp servers. If both pools and servers are
> +                         empty, 4 default pool servers will be provided with
> +                         the format ``{0-3}.{distro}.pool.ntp.org``.""")
> +                }
> +            },
> +            'required': [],
> +            'additionalProperties': False
> +        }
> +    }
> +}
> +
> +
>  def handle(name, cfg, cloud, log, _args):
>      """Enable and configure ntp."""
> -
>      if 'ntp' not in cfg:
>          LOG.debug(
>              "Skipping module named %s, not present or disabled by cfg", name)
>          return
> -
>      ntp_cfg = cfg.get('ntp', {})
>  
> +    # TODO drop this when validate_cloudconfig_schema is strict=True
>      if not isinstance(ntp_cfg, (dict)):
>          raise RuntimeError(("'ntp' key existed in config,"
>                              " but not a dictionary type,"
>                              " is a %s %instead"), type_utils.obj_name(ntp_cfg))
>  
> +    validate_cloudconfig_schema(cfg, schema)
>      rename_ntp_conf()
>      # ensure when ntp is installed it has a configuration file
>      # to use instead of starting up with packaged defaults
> diff --git a/tools/cloudconfig-schema b/tools/cloudconfig-schema
> new file mode 100755
> index 0000000..e26d671
> --- /dev/null
> +++ b/tools/cloudconfig-schema
> @@ -0,0 +1,62 @@
> +#!/usr/bin/env python3
> +# This file is part of cloud-init. See LICENSE file for license information.
> +
> +"""cloudconfig-schema
> +
> +Validate existing files against cloud-config schema or provide supported schema
> +documentation.
> +"""
> +
> +import argparse
> +import json
> +from jsonschema.exceptions import ValidationError
> +import os
> +import sys
> +
> +
> +if 'avoid-pep8-E402-import-not-top-of-file':
> +    _tdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
> +    sys.path.insert(0, _tdir)
> +    from cloudinit.config.schema import (
> +        CLOUD_CONFIG_HEADER, SchemaValidationError, get_schema_doc,
> +        get_schema, validate_cloudconfig_schema, validate_cloudconfig_file)
> +
> +
> +def error(message):
> +    print(message, file=sys.stderr)
> +    sys.exit(1)
> +
> +
> +def get_parser():
> +    parser = argparse.ArgumentParser()
> +    parser.add_argument('-c', '--config-file', 
> +                        help='Path of the cloud-config yaml file to validate')
> +    parser.add_argument('-d', '--doc', action="store_true", default=False,
> +                        help='Print schema documentation')
> +    parser.add_argument('-k', '--key',
> +                        help='Limit validation or docs to a section key')
> +    return parser
> +
> +
> +def main():
> +    parser = get_parser()
> +    args = parser.parse_args()
> +    if (not any([args.config_file, args.doc]) or
> +        all([args.config_file, args.doc])):
> +        error('Expected either --config-file argument or --doc')
> +        
> +    schema = get_schema()
> +    if args.config_file:
> +        try:
> +            validate_cloudconfig_file(args.config_file, schema)
> +        except (SchemaValidationError, RuntimeError) as e:
> +            error(str(e)) 
> +        print("Valid cloud-config file {}".format(args.config_file))

It's almost enough to make me move away from format... almost

> +    if args.doc:
> +        print(get_schema_doc(schema))
> +
> +if __name__ == '__main__':
> +    main()
> +
> +
> +# vi: ts=4 expandtab


-- 
https://code.launchpad.net/~chad.smith/cloud-init/+git/cloud-init/+merge/324640
Your team cloud-init commiters is requested to review the proposed merge of ~chad.smith/cloud-init:cc-ntp-schema-validation into cloud-init:master.