root / kamaki / cli / commands / config.py @ 466636c9
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 | 844a6bdb | Stavros Sachtouris | from sys import stdout |
35 | 844a6bdb | Stavros Sachtouris | |
36 | 234954d1 | Stavros Sachtouris | from kamaki.cli import command |
37 | 3fe56be4 | Stavros Sachtouris | from kamaki.cli.argument import FlagArgument |
38 | 436bd910 | Stavros Sachtouris | from kamaki.cli.commands import _command_init, errors |
39 | d486baec | Stavros Sachtouris | from kamaki.cli.command_tree import CommandTree |
40 | 844a6bdb | Stavros Sachtouris | from kamaki.cli.errors import CLIError, CLISyntaxError |
41 | 234954d1 | Stavros Sachtouris | |
42 | a29d2f88 | Stavros Sachtouris | config_cmds = CommandTree('config', 'Kamaki configurations') |
43 | e9a92550 | Stavros Sachtouris | _commands = [config_cmds] |
44 | 234954d1 | Stavros Sachtouris | |
45 | 3fe56be4 | Stavros Sachtouris | about_options = '\nAbout options:\
|
46 | 439826ec | Stavros Sachtouris | \n. syntax: [group.]option\
|
47 | c626151a | Stavros Sachtouris | \n. example: global.log_file\
|
48 | 439826ec | Stavros Sachtouris | \n. special case: <option> is equivalent to global.<option>\
|
49 | 439826ec | Stavros Sachtouris | \n. configuration file syntax:\
|
50 | 439826ec | Stavros Sachtouris | \n. [group]\
|
51 | 439826ec | Stavros Sachtouris | \n. option=value\
|
52 | c626151a | Stavros Sachtouris | \n. (more options can be set per group)\
|
53 | 144b3551 | Stavros Sachtouris | \n. special case: named clouds.\
|
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 | c626151a | Stavros Sachtouris | \n. token = <auth_token_from_demo_site>\
|
58 | 144b3551 | Stavros Sachtouris | \n. which are referenced as cloud.demo.url , cloud.demo.token'
|
59 | 3fe56be4 | Stavros Sachtouris | |
60 | 234954d1 | Stavros Sachtouris | |
61 | d486baec | Stavros Sachtouris | @command(config_cmds)
|
62 | 5eae854d | Stavros Sachtouris | class config_list(_command_init): |
63 | 3fe56be4 | Stavros Sachtouris | """List all configuration options
|
64 | 3fe56be4 | Stavros Sachtouris | FAQ:
|
65 | 3fe56be4 | Stavros Sachtouris | Q: I haven't set any options!
|
66 | 3fe56be4 | Stavros Sachtouris | A: Defaults are used (override with /config set )
|
67 | 3fe56be4 | Stavros Sachtouris | Q: There are more options than I have set
|
68 | 3fe56be4 | Stavros Sachtouris | A: Default options remain if not explicitly replaced or deleted
|
69 | 3fe56be4 | Stavros Sachtouris | """
|
70 | 7493ccb6 | Stavros Sachtouris | |
71 | 436bd910 | Stavros Sachtouris | @errors.generic.all
|
72 | 436bd910 | Stavros Sachtouris | def _run(self): |
73 | 7493ccb6 | Stavros Sachtouris | for section in sorted(self.config.sections()): |
74 | 57ecec97 | Stavros Sachtouris | items = self.config.items(section)
|
75 | 7493ccb6 | Stavros Sachtouris | for key, val in sorted(items): |
76 | 144b3551 | Stavros Sachtouris | if section in ('cloud',): |
77 | 844a6bdb | Stavros Sachtouris | prefix = '%s.%s' % (section, key)
|
78 | 844a6bdb | Stavros Sachtouris | for k, v in val.items(): |
79 | fa382f9e | Stavros Sachtouris | print('%s.%s = %s' % (prefix, k, v))
|
80 | 844a6bdb | Stavros Sachtouris | else:
|
81 | 844a6bdb | Stavros Sachtouris | print('%s.%s = %s' % (section, key, val))
|
82 | 7493ccb6 | Stavros Sachtouris | |
83 | 436bd910 | Stavros Sachtouris | def main(self): |
84 | 436bd910 | Stavros Sachtouris | self._run()
|
85 | 436bd910 | Stavros Sachtouris | |
86 | 234954d1 | Stavros Sachtouris | |
87 | d486baec | Stavros Sachtouris | @command(config_cmds)
|
88 | 5eae854d | Stavros Sachtouris | class config_get(_command_init): |
89 | 436bd910 | Stavros Sachtouris | """Show a configuration option"""
|
90 | 3fe56be4 | Stavros Sachtouris | |
91 | 3fe56be4 | Stavros Sachtouris | __doc__ += about_options |
92 | 7493ccb6 | Stavros Sachtouris | |
93 | 436bd910 | Stavros Sachtouris | @errors.generic.all
|
94 | 436bd910 | Stavros Sachtouris | def _run(self, option): |
95 | 7493ccb6 | Stavros Sachtouris | section, sep, key = option.rpartition('.')
|
96 | 844a6bdb | Stavros Sachtouris | if not sep: |
97 | 844a6bdb | Stavros Sachtouris | match = False
|
98 | 844a6bdb | Stavros Sachtouris | for k in self.config.keys(key): |
99 | 844a6bdb | Stavros Sachtouris | match = True
|
100 | 144b3551 | Stavros Sachtouris | if option != 'cloud': |
101 | 844a6bdb | Stavros Sachtouris | stdout.write('%s.%s =' % (option, k))
|
102 | 844a6bdb | Stavros Sachtouris | self._run('%s.%s' % (option, k)) |
103 | 844a6bdb | Stavros Sachtouris | if match:
|
104 | 844a6bdb | Stavros Sachtouris | return
|
105 | 844a6bdb | Stavros Sachtouris | section = 'global'
|
106 | 144b3551 | Stavros Sachtouris | prefix = 'cloud.'
|
107 | 844a6bdb | Stavros Sachtouris | get, section = ( |
108 | 144b3551 | Stavros Sachtouris | self.config.get_cloud, section[len(prefix):]) if ( |
109 | 844a6bdb | Stavros Sachtouris | section.startswith(prefix)) else (self.config.get, section) |
110 | 844a6bdb | Stavros Sachtouris | value = get(section, key) |
111 | 844a6bdb | Stavros Sachtouris | if isinstance(value, dict): |
112 | 844a6bdb | Stavros Sachtouris | for k, v in value.items(): |
113 | 844a6bdb | Stavros Sachtouris | print('%s.%s.%s = %s' % (section, key, k, v))
|
114 | 844a6bdb | Stavros Sachtouris | elif value:
|
115 | 7493ccb6 | Stavros Sachtouris | print(value) |
116 | 7493ccb6 | Stavros Sachtouris | |
117 | 436bd910 | Stavros Sachtouris | def main(self, option): |
118 | 436bd910 | Stavros Sachtouris | self._run(option)
|
119 | 436bd910 | Stavros Sachtouris | |
120 | 234954d1 | Stavros Sachtouris | |
121 | d486baec | Stavros Sachtouris | @command(config_cmds)
|
122 | 5eae854d | Stavros Sachtouris | class config_set(_command_init): |
123 | 7493ccb6 | Stavros Sachtouris | """Set a configuration option"""
|
124 | 7493ccb6 | Stavros Sachtouris | |
125 | 3fe56be4 | Stavros Sachtouris | __doc__ += about_options |
126 | 3fe56be4 | Stavros Sachtouris | |
127 | 436bd910 | Stavros Sachtouris | @errors.generic.all
|
128 | 436bd910 | Stavros Sachtouris | def _run(self, option, value): |
129 | 7493ccb6 | Stavros Sachtouris | section, sep, key = option.rpartition('.')
|
130 | 144b3551 | Stavros Sachtouris | prefix = 'cloud.'
|
131 | 844a6bdb | Stavros Sachtouris | if section.startswith(prefix):
|
132 | 466636c9 | Stavros Sachtouris | cloudname = section[len(prefix):]
|
133 | 466636c9 | Stavros Sachtouris | if cloudname:
|
134 | 466636c9 | Stavros Sachtouris | self.config.set_cloud(cloudname, key, value)
|
135 | 466636c9 | Stavros Sachtouris | else:
|
136 | 466636c9 | Stavros Sachtouris | raise CLISyntaxError(
|
137 | 466636c9 | Stavros Sachtouris | 'Empty cloud alias (%s)' % option, importance=2) |
138 | 144b3551 | Stavros Sachtouris | elif section in ('cloud',): |
139 | 844a6bdb | Stavros Sachtouris | raise CLISyntaxError(
|
140 | 844a6bdb | Stavros Sachtouris | 'Invalid syntax for cloud definition', importance=2, details=[ |
141 | 144b3551 | Stavros Sachtouris | 'To define a cloud "%s"' % key,
|
142 | 844a6bdb | Stavros Sachtouris | 'set the cloud\'s authentication url and token:',
|
143 | 144b3551 | Stavros Sachtouris | ' /config set cloud.%s.url <URL>' % key,
|
144 | 144b3551 | Stavros Sachtouris | ' /config set cloud.%s.token <t0k3n>' % key])
|
145 | 844a6bdb | Stavros Sachtouris | else:
|
146 | 844a6bdb | Stavros Sachtouris | section = section or 'global' |
147 | 844a6bdb | Stavros Sachtouris | self.config.set(section, key, value)
|
148 | 7493ccb6 | Stavros Sachtouris | self.config.write()
|
149 | 8eb8c540 | Stavros Sachtouris | self.config.reload()
|
150 | 7493ccb6 | Stavros Sachtouris | |
151 | 436bd910 | Stavros Sachtouris | def main(self, option, value): |
152 | 436bd910 | Stavros Sachtouris | self._run(option, value)
|
153 | 436bd910 | Stavros Sachtouris | |
154 | 234954d1 | Stavros Sachtouris | |
155 | d486baec | Stavros Sachtouris | @command(config_cmds)
|
156 | 5eae854d | Stavros Sachtouris | class config_delete(_command_init): |
157 | 3fe56be4 | Stavros Sachtouris | """Delete a configuration option
|
158 | 3fe56be4 | Stavros Sachtouris | Default values are not removed by default. To alter this behavior in a
|
159 | 3fe56be4 | Stavros Sachtouris | session, use --default.
|
160 | 3fe56be4 | Stavros Sachtouris | """
|
161 | 3fe56be4 | Stavros Sachtouris | |
162 | 3fe56be4 | Stavros Sachtouris | arguments = dict(
|
163 | 3fe56be4 | Stavros Sachtouris | default=FlagArgument( |
164 | 436bd910 | Stavros Sachtouris | 'Remove default value as well (persists until end of session)',
|
165 | 3fe56be4 | Stavros Sachtouris | '--default')
|
166 | 3fe56be4 | Stavros Sachtouris | ) |
167 | 7493ccb6 | Stavros Sachtouris | |
168 | 436bd910 | Stavros Sachtouris | @errors.generic.all
|
169 | 436bd910 | Stavros Sachtouris | def _run(self, option): |
170 | 7493ccb6 | Stavros Sachtouris | section, sep, key = option.rpartition('.')
|
171 | 7493ccb6 | Stavros Sachtouris | section = section or 'global' |
172 | 144b3551 | Stavros Sachtouris | prefix = 'cloud.'
|
173 | 844a6bdb | Stavros Sachtouris | if section.startswith(prefix):
|
174 | 144b3551 | Stavros Sachtouris | cloud = section[len(prefix):]
|
175 | 844a6bdb | Stavros Sachtouris | try:
|
176 | 144b3551 | Stavros Sachtouris | self.config.remove_from_cloud(cloud, key)
|
177 | 844a6bdb | Stavros Sachtouris | except KeyError: |
178 | 844a6bdb | Stavros Sachtouris | raise CLIError('Field %s does not exist' % option) |
179 | 844a6bdb | Stavros Sachtouris | else:
|
180 | 844a6bdb | Stavros Sachtouris | self.config.remove_option(section, key, self['default']) |
181 | 7493ccb6 | Stavros Sachtouris | self.config.write()
|
182 | 8eb8c540 | Stavros Sachtouris | self.config.reload()
|
183 | 436bd910 | Stavros Sachtouris | |
184 | 436bd910 | Stavros Sachtouris | def main(self, option): |
185 | 436bd910 | Stavros Sachtouris | self._run(option) |