Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / config.py @ 3042fac1

History | View | Annotate | Download (6.3 kB)

1 e3f01d64 Stavros Sachtouris
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 7493ccb6 Stavros Sachtouris
#
3 7493ccb6 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 7493ccb6 Stavros Sachtouris
# without modification, are permitted provided that the following
5 7493ccb6 Stavros Sachtouris
# conditions are met:
6 7493ccb6 Stavros Sachtouris
#
7 7493ccb6 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 7493ccb6 Stavros Sachtouris
#      disclaimer.
10 7493ccb6 Stavros Sachtouris
#
11 7493ccb6 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 7493ccb6 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 7493ccb6 Stavros Sachtouris
#      provided with the distribution.
15 7493ccb6 Stavros Sachtouris
#
16 7493ccb6 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 7493ccb6 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 7493ccb6 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 7493ccb6 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 7493ccb6 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 7493ccb6 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 7493ccb6 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 43ee6ae1 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERaUPTION) HOWEVER CAUSED
24 7493ccb6 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 7493ccb6 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 7493ccb6 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 7493ccb6 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 7493ccb6 Stavros Sachtouris
#
29 7493ccb6 Stavros Sachtouris
# The views and conclusions contained in the software and
30 7493ccb6 Stavros Sachtouris
# documentation are those of the authors and should not be
31 7493ccb6 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 7493ccb6 Stavros Sachtouris
# or implied, of GRNET S.A.
33 7493ccb6 Stavros Sachtouris
34 234954d1 Stavros Sachtouris
from kamaki.cli import command
35 3fe56be4 Stavros Sachtouris
from kamaki.cli.argument import FlagArgument
36 436bd910 Stavros Sachtouris
from kamaki.cli.commands import _command_init, errors
37 d486baec Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
38 844a6bdb Stavros Sachtouris
from kamaki.cli.errors import CLIError, CLISyntaxError
39 234954d1 Stavros Sachtouris
40 a29d2f88 Stavros Sachtouris
config_cmds = CommandTree('config', 'Kamaki configurations')
41 e9a92550 Stavros Sachtouris
_commands = [config_cmds]
42 234954d1 Stavros Sachtouris
43 3fe56be4 Stavros Sachtouris
about_options = '\nAbout options:\
44 439826ec Stavros Sachtouris
    \n. syntax: [group.]option\
45 c626151a Stavros Sachtouris
    \n. example: global.log_file\
46 439826ec Stavros Sachtouris
    \n. special case: <option> is equivalent to global.<option>\
47 439826ec Stavros Sachtouris
    \n. configuration file syntax:\
48 439826ec Stavros Sachtouris
    \n.   [group]\
49 439826ec Stavros Sachtouris
    \n.   option=value\
50 c626151a Stavros Sachtouris
    \n.   (more options can be set per group)\
51 3185cd6d Stavros Sachtouris
    \n.\
52 144b3551 Stavros Sachtouris
    \n. special case: named clouds.\
53 3185cd6d Stavros Sachtouris
    \n. example: cloud.demo.url\
54 c626151a Stavros Sachtouris
    \n. E.g. for a cloud "demo":\
55 144b3551 Stavros Sachtouris
    \n.   [cloud "demo"]\
56 c626151a Stavros Sachtouris
    \n.   url = <http://single/authentication/url/for/demo/site>\
57 3185cd6d Stavros Sachtouris
    \n.   token = <auth_token_from_demo_site>'
58 3fe56be4 Stavros Sachtouris
59 234954d1 Stavros Sachtouris
60 d486baec Stavros Sachtouris
@command(config_cmds)
61 5eae854d Stavros Sachtouris
class config_list(_command_init):
62 3fe56be4 Stavros Sachtouris
    """List all configuration options
63 3fe56be4 Stavros Sachtouris
    FAQ:
64 3fe56be4 Stavros Sachtouris
    Q: I haven't set any options!
65 3fe56be4 Stavros Sachtouris
    A: Defaults are used (override with /config set )
66 3fe56be4 Stavros Sachtouris
    Q: There are more options than I have set
67 3fe56be4 Stavros Sachtouris
    A: Default options remain if not explicitly replaced or deleted
68 3fe56be4 Stavros Sachtouris
    """
69 7493ccb6 Stavros Sachtouris
70 436bd910 Stavros Sachtouris
    @errors.generic.all
71 436bd910 Stavros Sachtouris
    def _run(self):
72 7493ccb6 Stavros Sachtouris
        for section in sorted(self.config.sections()):
73 57ecec97 Stavros Sachtouris
            items = self.config.items(section)
74 7493ccb6 Stavros Sachtouris
            for key, val in sorted(items):
75 144b3551 Stavros Sachtouris
                if section in ('cloud',):
76 844a6bdb Stavros Sachtouris
                    prefix = '%s.%s' % (section, key)
77 844a6bdb Stavros Sachtouris
                    for k, v in val.items():
78 3185cd6d Stavros Sachtouris
                        self.writeln('%s.%s = %s' % (prefix, k, v))
79 844a6bdb Stavros Sachtouris
                else:
80 3185cd6d Stavros Sachtouris
                    self.writeln('%s.%s = %s' % (section, key, val))
81 7493ccb6 Stavros Sachtouris
82 436bd910 Stavros Sachtouris
    def main(self):
83 436bd910 Stavros Sachtouris
        self._run()
84 436bd910 Stavros Sachtouris
85 234954d1 Stavros Sachtouris
86 d486baec Stavros Sachtouris
@command(config_cmds)
87 5eae854d Stavros Sachtouris
class config_get(_command_init):
88 436bd910 Stavros Sachtouris
    """Show a configuration option"""
89 3fe56be4 Stavros Sachtouris
90 3fe56be4 Stavros Sachtouris
    __doc__ += about_options
91 7493ccb6 Stavros Sachtouris
92 436bd910 Stavros Sachtouris
    @errors.generic.all
93 436bd910 Stavros Sachtouris
    def _run(self, option):
94 7493ccb6 Stavros Sachtouris
        section, sep, key = option.rpartition('.')
95 844a6bdb Stavros Sachtouris
        if not sep:
96 844a6bdb Stavros Sachtouris
            match = False
97 844a6bdb Stavros Sachtouris
            for k in self.config.keys(key):
98 844a6bdb Stavros Sachtouris
                match = True
99 144b3551 Stavros Sachtouris
                if option != 'cloud':
100 3185cd6d Stavros Sachtouris
                    self.write('%s.%s =' % (option, k))
101 844a6bdb Stavros Sachtouris
                self._run('%s.%s' % (option, k))
102 844a6bdb Stavros Sachtouris
            if match:
103 844a6bdb Stavros Sachtouris
                return
104 844a6bdb Stavros Sachtouris
            section = 'global'
105 144b3551 Stavros Sachtouris
        prefix = 'cloud.'
106 844a6bdb Stavros Sachtouris
        get, section = (
107 144b3551 Stavros Sachtouris
            self.config.get_cloud, section[len(prefix):]) if (
108 844a6bdb Stavros Sachtouris
                section.startswith(prefix)) else (self.config.get, section)
109 844a6bdb Stavros Sachtouris
        value = get(section, key)
110 844a6bdb Stavros Sachtouris
        if isinstance(value, dict):
111 844a6bdb Stavros Sachtouris
            for k, v in value.items():
112 3185cd6d Stavros Sachtouris
                self.writeln('%s.%s.%s = %s' % (section, key, k, v))
113 844a6bdb Stavros Sachtouris
        elif value:
114 3185cd6d Stavros Sachtouris
            self.writeln(value)
115 7493ccb6 Stavros Sachtouris
116 436bd910 Stavros Sachtouris
    def main(self, option):
117 436bd910 Stavros Sachtouris
        self._run(option)
118 436bd910 Stavros Sachtouris
119 234954d1 Stavros Sachtouris
120 d486baec Stavros Sachtouris
@command(config_cmds)
121 5eae854d Stavros Sachtouris
class config_set(_command_init):
122 7493ccb6 Stavros Sachtouris
    """Set a configuration option"""
123 7493ccb6 Stavros Sachtouris
124 3fe56be4 Stavros Sachtouris
    __doc__ += about_options
125 3fe56be4 Stavros Sachtouris
126 436bd910 Stavros Sachtouris
    @errors.generic.all
127 436bd910 Stavros Sachtouris
    def _run(self, option, value):
128 7493ccb6 Stavros Sachtouris
        section, sep, key = option.rpartition('.')
129 144b3551 Stavros Sachtouris
        prefix = 'cloud.'
130 844a6bdb Stavros Sachtouris
        if section.startswith(prefix):
131 466636c9 Stavros Sachtouris
            cloudname = section[len(prefix):]
132 466636c9 Stavros Sachtouris
            if cloudname:
133 466636c9 Stavros Sachtouris
                self.config.set_cloud(cloudname, key, value)
134 466636c9 Stavros Sachtouris
            else:
135 466636c9 Stavros Sachtouris
                raise CLISyntaxError(
136 466636c9 Stavros Sachtouris
                    'Empty cloud alias (%s)' % option, importance=2)
137 144b3551 Stavros Sachtouris
        elif section in ('cloud',):
138 844a6bdb Stavros Sachtouris
            raise CLISyntaxError(
139 844a6bdb Stavros Sachtouris
                'Invalid syntax for cloud definition', importance=2, details=[
140 144b3551 Stavros Sachtouris
                    'To define a cloud "%s"' % key,
141 844a6bdb Stavros Sachtouris
                    'set the cloud\'s authentication url and token:',
142 144b3551 Stavros Sachtouris
                    '  /config set cloud.%s.url <URL>' % key,
143 144b3551 Stavros Sachtouris
                    '  /config set cloud.%s.token <t0k3n>' % key])
144 844a6bdb Stavros Sachtouris
        else:
145 844a6bdb Stavros Sachtouris
            section = section or 'global'
146 844a6bdb Stavros Sachtouris
            self.config.set(section, key, value)
147 7493ccb6 Stavros Sachtouris
        self.config.write()
148 8eb8c540 Stavros Sachtouris
        self.config.reload()
149 7493ccb6 Stavros Sachtouris
150 436bd910 Stavros Sachtouris
    def main(self, option, value):
151 436bd910 Stavros Sachtouris
        self._run(option, value)
152 436bd910 Stavros Sachtouris
153 234954d1 Stavros Sachtouris
154 d486baec Stavros Sachtouris
@command(config_cmds)
155 5eae854d Stavros Sachtouris
class config_delete(_command_init):
156 3fe56be4 Stavros Sachtouris
    """Delete a configuration option
157 3fe56be4 Stavros Sachtouris
    Default values are not removed by default. To alter this behavior in a
158 3fe56be4 Stavros Sachtouris
    session, use --default.
159 3fe56be4 Stavros Sachtouris
    """
160 3fe56be4 Stavros Sachtouris
161 3fe56be4 Stavros Sachtouris
    arguments = dict(
162 3fe56be4 Stavros Sachtouris
        default=FlagArgument(
163 436bd910 Stavros Sachtouris
            'Remove default value as well (persists until end of session)',
164 3fe56be4 Stavros Sachtouris
            '--default')
165 3fe56be4 Stavros Sachtouris
    )
166 7493ccb6 Stavros Sachtouris
167 436bd910 Stavros Sachtouris
    @errors.generic.all
168 436bd910 Stavros Sachtouris
    def _run(self, option):
169 7493ccb6 Stavros Sachtouris
        section, sep, key = option.rpartition('.')
170 7493ccb6 Stavros Sachtouris
        section = section or 'global'
171 144b3551 Stavros Sachtouris
        prefix = 'cloud.'
172 844a6bdb Stavros Sachtouris
        if section.startswith(prefix):
173 144b3551 Stavros Sachtouris
            cloud = section[len(prefix):]
174 844a6bdb Stavros Sachtouris
            try:
175 144b3551 Stavros Sachtouris
                self.config.remove_from_cloud(cloud, key)
176 844a6bdb Stavros Sachtouris
            except KeyError:
177 844a6bdb Stavros Sachtouris
                raise CLIError('Field %s does not exist' % option)
178 844a6bdb Stavros Sachtouris
        else:
179 844a6bdb Stavros Sachtouris
            self.config.remove_option(section, key, self['default'])
180 7493ccb6 Stavros Sachtouris
        self.config.write()
181 8eb8c540 Stavros Sachtouris
        self.config.reload()
182 436bd910 Stavros Sachtouris
183 436bd910 Stavros Sachtouris
    def main(self, option):
184 436bd910 Stavros Sachtouris
        self._run(option)