~clint-fewbar/pyjuju/repo-from-env

« back to all changes in this revision

Viewing changes to juju/control/config_set.py

  • Committer: Benjamin Saller
  • Date: 2012-01-20 22:34:20 UTC
  • mfrom: (440.1.4 config-set-filename)
  • Revision ID: bcsaller@gmail.com-20120120223420-qu3guvs7by4gihl7
Add --config <file.yaml> to juju set [f=902143] [r=gmb, hazmat]\n\nReplaces the unimplemented --filename argument with --config taking the same type of file as juju deploy --config

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import argparse
2
2
 
 
3
import yaml
 
4
 
3
5
from twisted.internet.defer import inlineCallbacks
4
6
 
 
7
from juju.charm.errors import ServiceConfigValueError
5
8
from juju.control.utils import get_environment
6
9
from juju.hooks.cli import parse_keyvalue_pairs
7
10
from juju.state.service import ServiceStateManager
21
24
    sub_parser.add_argument(
22
25
        "service_name",
23
26
        help="The name of the service the options apply to.")
 
27
 
 
28
    sub_parser.add_argument("--config",
 
29
                            type=argparse.FileType("r"),
 
30
                            help=(
 
31
                                "a filename containing a YAML dict of values "
 
32
                                "for the current service_name"))
 
33
 
24
34
    sub_parser.add_argument("service_options",
25
 
                            nargs="+",
 
35
                            nargs="*",
26
36
                            help="""name=value for option to set""")
27
37
 
28
38
    return sub_parser
39
49
 
40
50
    or
41
51
 
42
 
    $ juju set <service_name> --filename local.yaml
 
52
    $ juju set <service_name> --config local.yaml
43
53
 
44
54
    """
45
55
    environment = get_environment(options)
46
56
 
 
57
    if options.config:
 
58
        if options.service_options:
 
59
            raise ServiceConfigValueError(
 
60
                "--config and command line options cannot "
 
61
                "be used in a single invocation")
 
62
 
 
63
        yaml_data = options.config.read()
 
64
        try:
 
65
            data = yaml.load(yaml_data)
 
66
        except yaml.YAMLError:
 
67
            raise ServiceConfigValueError(
 
68
                "Config file %r not valid YAML" % options.config.name)
 
69
 
 
70
        if not data or not isinstance(data, dict):
 
71
            raise ServiceConfigValueError(
 
72
                "Config file %r invalid" % options.config.name
 
73
            )
 
74
        data = data.get(options.service_name)
 
75
 
 
76
        if data:
 
77
            # set data directly
 
78
            options.service_options = data
 
79
 
47
80
    return config_set(environment,
48
81
                      options.service_name,
49
82
                      options.service_options)
57
90
    client = yield provider.connect()
58
91
 
59
92
    # Get the service and the charm
60
 
    #
61
93
    service_manager = ServiceStateManager(client)
62
94
    service = yield service_manager.get_service_state(service_name)
63
95
    charm = yield service.get_charm_state()
65
97
    # Use the charm's ConfigOptions instance to validate the
66
98
    # arguments to config_set. Invalid options passed to this method
67
99
    # will thrown an exception.
68
 
    options = parse_keyvalue_pairs(service_options)
 
100
    if isinstance(service_options, dict):
 
101
        options = service_options
 
102
    else:
 
103
        options = parse_keyvalue_pairs(service_options)
69
104
 
70
105
    config = yield charm.get_config()
71
106
    # ignore the output of validate, we run it so it might throw an exception