Esempio n. 1
0
def main():
    args = docopt(usage)
    app_yaml = os.path.join(_ROOT, "app.yaml")
    if args['--config'] is not None:
      config_paths = args['--config'].split(",")
    else:
      config_paths = list()
    config_files = [app_yaml] + config_paths
    builder, settings_dict = build(*config_files)
    verbs = [function for (name, function) in verb_map.items() if args[name]]
    assert len(verbs) == 1
    verbs[0](args, settings_dict['search'])
Esempio n. 2
0
def main():
    args = docopt(usage)
    app_yaml = os.path.join(_ROOT, "app.yaml")
    if args['--config'] is not None:
      config_paths = args['--config'].split(",")
    else:
      config_paths = list()
    config_files = [app_yaml] + config_paths
    builder, settings_dict = build(*config_files)
    verbs = [function for (name, function) in verb_map.items() if args[name]]
    assert len(verbs) == 1
    verbs[0](args, settings_dict['MongoConnections'])
Esempio n. 3
0
def get_config(yaml, spec=None):
    if not yaml:
        print 'config required'
        sys.exit(1)

    if not spec:
        spec = os.path.join(os.path.dirname(__file__), './app.yaml')

    builder, config = parser.build(spec, yaml)
    validate = builder.validate(config)
    if validate:
        sys.stderr.write(
            'insufficient config, missing:\n' +
            '\n'.join([(4*' ')+':'.join(x) for x in validate.iterkeys()]) +
            '\n')
        sys.exit(1)

    return config
Esempio n. 4
0
def get_envs(YAMLs=[]):
    """
    return a configuration env for each supplied yaml file.
    """
    base = os.path.join(os.path.dirname(__file__), 'app.yaml')

    if not YAMLs:
        raise Exception('AWS config yaml required.')

    ret = []
    for YAML in YAMLs:
        builder, config = parser.build(base, YAML)
        validate = builder.validate(config)
        if validate:
            sys.stderr.write(
                'insufficient config, missing:\n' +
                '\n'.join([(4*' ')+':'.join(x) for x in validate.iterkeys()]) +
                '\n')
            return 1
        ret.append(config)
    return ret
Esempio n. 5
0
def validate_main():
    usage = "usage: %prog [options] yaml [yaml ...]"
    parser = OptionParser(usage=usage)
    parser.add_option("-i", "--ignore_requirements", dest="ignored",
                      default=None,
                      help="requirements to ignore for validation purposes.")
    parser.add_option("-f", "--format", dest="format",
                      default="yaml",
                      help="Output format: yaml, json, sh, make are supported.")
    parser.add_option("-o", "--output", dest="output",
                      help="Output destination: path where to write output. If not provided, stdout is used.")
    parser.add_option("-t", "--test", dest="test",
                      help="Tests to see if the contents in the file match whats specified by the yamls.")

    (options, yamls) = parser.parse_args()
    if not yamls:
        parser.print_usage()
        exit(-1)
    if options.test and options.output:
        parser.print_usage()
        exit(-1)

    b, params = build(*yamls)
    errs = b.validate(params, ignored=options.ignored.split(",") if options.ignored else [])
    if errs:
        d = defaultdict(dict)
        for (section, key), err in errs.iteritems():
            d[section][key] = str(err)
        unparse(sys.stderr, dict(d), default_flow_style=False)
        sys.exit(1)
        return

    if options.test:
      render_fd = NamedTemporaryFile()
    else:
      render_fd = safeopen(options.output)

    with render_fd as output:
        if options.format == 'yaml':
            unparse(output, dict(params.iteritems()), default_flow_style=False)
        elif options.format == 'pickle':
            pickle.dump(dict(params), output)
        elif options.format == 'json':
            json.dump(dict(params),
                      output,
                      sort_keys=True,
                      indent=2,
                      separators=(',', ': '))
        elif options.format == 'sh':
            for section in params:
                for key, value in params[section].iteritems():
                    if value is None:
                        print >> output, "# %s__%s is unset" % (_norm_sh_key(section), _norm_sh_key(key))
                    else:
                        print >> output, "read -r -d '' %s__%s<<EOF\n%s\nEOF\n" % (_norm_sh_key(section), _norm_sh_key(key), str(value))
                        print >> output, "export %s__%s\n" % (_norm_sh_key(section), _norm_sh_key(key))
        elif options.format == 'make':
            for section in params:
                for key, value in params[section].iteritems():
                    if value is None:
                        print >> output, "# %s__%s is unset" % (_norm_sh_key(section), _norm_sh_key(key))
                    else:
                        print >> output, "define %s__%s\n%s\nendef\n" % (_norm_sh_key(section), _norm_sh_key(key), str(value))
        else:
            print >> sys.stderr, "Invalid output format."
            sys.exit(2)


        if options.test:
          output.flush()
          same = _check_same(options.test, output.name)
          if not same:
            print >> sys.stderr, "Config mismatch!"
            sys.exit(3)