blob: 58c29c5a5afd61d2f199d3bcf200ca3a33f4187a [file] [log] [blame]
martin f. krafftf37f0682013-06-14 16:36:20 +02001#
2# -*- coding: utf-8 -*-
3#
4# This file is part of reclass (http://github.com/madduck/reclass)
5#
6# Copyright © 2007–13 martin f. krafft <madduck@madduck.net>
7# Released under the terms of the Artistic Licence 2.0
8#
9import yaml, os, optparse, posix, sys
10
11def _make_parser(name, version, description, defaults={}):
12 parser = optparse.OptionParser(version=version)
13 parser.prog = name
14 parser.version = version
15 parser.description = description
16 parser.usage = '%prog [options] ( --inventory | --nodeinfo <nodename> )'
17
18 options_group = optparse.OptionGroup(parser, 'Options',
19 'Configure the way {0} works'.format(name))
20 options_group.add_option('-t', '--storage-type', dest='storage_type',
21 default=defaults.get('storage_type', 'yaml_fs'),
22 help='the type of storage backend to use [%default]')
23 options_group.add_option('-u', '--nodes-uri', dest='nodes_uri',
24 default=defaults.get('nodes_uri', None),
25 help='the URI to the nodes storage [%default]'),
26 options_group.add_option('-c', '--classes-uri', dest='classes_uri',
27 default=defaults.get('classes_uri', None),
28 help='the URI to the classes storage [%default]')
29 options_group.add_option('-o', '--output', dest='output',
30 default=defaults.get('output', 'yaml'),
31 help='output format (yaml or json) [%default]')
32 options_group.add_option('-p', '--pretty-print', dest='pretty_print',
33 default=defaults.get('pretty_print', False),
34 action="store_true",
35 help='try to make the output prettier [%default]')
36 parser.add_option_group(options_group)
37
38 run_modes = optparse.OptionGroup(parser, 'Modes',
39 'Specify one of these to determine what to do.')
40 run_modes.add_option('-i', '--inventory', action='store_false', dest='node',
41 help='output the entire inventory')
42 run_modes.add_option('-n', '--nodeinfo', action='store', dest='node',
43 default=None,
44 help='output information for a specific node')
45 parser.add_option_group(run_modes)
46
47 return parser
48
49def _parse_and_check_options(parser):
50 options, args = parser.parse_args()
51
52 def usage_error(msg):
53 sys.stderr.write(msg + '\n\n')
54 parser.print_help(sys.stderr)
55 sys.exit(posix.EX_USAGE)
56
57 if len(args) > 0:
58 usage_error('No arguments allowed')
59 elif options.node is None:
60 usage_error('You need to either pass --inventory or --nodeinfo <nodename>')
61 elif options.output not in ('json', 'yaml'):
62 usage_error('Unknown output format: {0}'.format(options.output))
63 elif options.nodes_uri is None:
64 usage_error('Must specify at least --nodes-uri')
65
66 options.classes_uri = options.classes_uri or options.nodes_uri
67
68 return options
69
70def read_config_file(path):
71 if os.path.exists(path):
72 return yaml.safe_load(file(path))
73 else:
74 return {}
75
76def get_options(name, version, description, config_file=None):
77 config_data = {}
78 if config_file is not None:
79 config_data.update(read_config_file(config_file))
80 parser = _make_parser(name, version, description, config_data)
81 return _parse_and_check_options(parser)