root / kamaki / cli / config / __init__.py @ 58602137
History | View | Annotate | Download (14.6 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 | 7493ccb6 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 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 | 7493ccb6 | Stavros Sachtouris | import os |
35 | 3f0eae61 | Stavros Sachtouris | from logging import getLogger |
36 | c5997814 | Stavros Sachtouris | from sys import stdout, stderr |
37 | 7493ccb6 | Stavros Sachtouris | |
38 | 7493ccb6 | Stavros Sachtouris | from collections import defaultdict |
39 | 29be4062 | Stavros Sachtouris | from ConfigParser import RawConfigParser, NoOptionError, NoSectionError, Error |
40 | 3f0eae61 | Stavros Sachtouris | from re import match |
41 | 7493ccb6 | Stavros Sachtouris | |
42 | ce9ccb40 | Stavros Sachtouris | from kamaki.cli.errors import CLISyntaxError |
43 | 144b3551 | Stavros Sachtouris | from kamaki import __version__ |
44 | ce9ccb40 | Stavros Sachtouris | |
45 | 7493ccb6 | Stavros Sachtouris | try:
|
46 | 7493ccb6 | Stavros Sachtouris | from collections import OrderedDict |
47 | 7493ccb6 | Stavros Sachtouris | except ImportError: |
48 | 1792ed1d | Stavros Sachtouris | from kamaki.clients.utils.ordereddict import OrderedDict |
49 | 7493ccb6 | Stavros Sachtouris | |
50 | 7493ccb6 | Stavros Sachtouris | |
51 | 29be4062 | Stavros Sachtouris | class InvalidCloudNameError(Error): |
52 | c5997814 | Stavros Sachtouris | """A valid cloud name must pass through this regex: ([~@#$:.-\w]+)"""
|
53 | 29be4062 | Stavros Sachtouris | |
54 | 29be4062 | Stavros Sachtouris | |
55 | 3f0eae61 | Stavros Sachtouris | log = getLogger(__name__) |
56 | 3f0eae61 | Stavros Sachtouris | |
57 | 7493ccb6 | Stavros Sachtouris | # Path to the file that stores the configuration
|
58 | 7493ccb6 | Stavros Sachtouris | CONFIG_PATH = os.path.expanduser('~/.kamakirc')
|
59 | 9f1d1fcf | Stavros Sachtouris | HISTORY_PATH = os.path.expanduser('~/.kamaki.history')
|
60 | 6144f3d4 | Stavros Sachtouris | CLOUD_PREFIX = 'cloud'
|
61 | 7493ccb6 | Stavros Sachtouris | |
62 | 7493ccb6 | Stavros Sachtouris | # Name of a shell variable to bypass the CONFIG_PATH value
|
63 | 7493ccb6 | Stavros Sachtouris | CONFIG_ENV = 'KAMAKI_CONFIG'
|
64 | 7493ccb6 | Stavros Sachtouris | |
65 | 144b3551 | Stavros Sachtouris | version = ''
|
66 | 144b3551 | Stavros Sachtouris | for c in '%s' % __version__: |
67 | 144b3551 | Stavros Sachtouris | if c not in '0.123456789': |
68 | 144b3551 | Stavros Sachtouris | break
|
69 | 144b3551 | Stavros Sachtouris | version += c |
70 | c825ddc9 | Stavros Sachtouris | HEADER = '# Kamaki configuration file v%s\n' % version
|
71 | 7493ccb6 | Stavros Sachtouris | |
72 | 7493ccb6 | Stavros Sachtouris | DEFAULTS = { |
73 | 7493ccb6 | Stavros Sachtouris | 'global': {
|
74 | 144b3551 | Stavros Sachtouris | 'default_cloud': '', |
75 | 4f6a21f6 | Stavros Sachtouris | 'colors': 'off', |
76 | edf00ab3 | Stavros Sachtouris | 'log_file': os.path.expanduser('~/.kamaki.log'), |
77 | c5b9380c | Stavros Sachtouris | 'log_token': 'off', |
78 | c5b9380c | Stavros Sachtouris | 'log_data': 'off', |
79 | 64a3c0de | Stavros Sachtouris | 'log_pid': 'off', |
80 | 05e144e2 | Stavros Sachtouris | 'max_threads': 7, |
81 | 3f0eae61 | Stavros Sachtouris | 'history_file': HISTORY_PATH,
|
82 | 3f0eae61 | Stavros Sachtouris | 'user_cli': 'astakos', |
83 | c308d73f | Stavros Sachtouris | 'admin_cli': 'astakos', |
84 | c308d73f | Stavros Sachtouris | 'project_cli': 'astakos', |
85 | 3f0eae61 | Stavros Sachtouris | 'file_cli': 'pithos', |
86 | 3f0eae61 | Stavros Sachtouris | 'server_cli': 'cyclades', |
87 | 3f0eae61 | Stavros Sachtouris | 'flavor_cli': 'cyclades', |
88 | d18e6439 | Stavros Sachtouris | 'network_cli': 'networking', |
89 | d18e6439 | Stavros Sachtouris | 'subnet_cli': 'networking', |
90 | d18e6439 | Stavros Sachtouris | 'port_cli': 'networking', |
91 | 6489c38b | Stavros Sachtouris | 'ip_cli': 'cyclades', |
92 | 3f0eae61 | Stavros Sachtouris | 'image_cli': 'image', |
93 | 3f0eae61 | Stavros Sachtouris | 'config_cli': 'config', |
94 | 3f0eae61 | Stavros Sachtouris | 'history_cli': 'history' |
95 | f5c28bfa | Stavros Sachtouris | # Optional command specs:
|
96 | ce9ccb40 | Stavros Sachtouris | # 'livetest_cli': 'livetest',
|
97 | 7493ccb6 | Stavros Sachtouris | }, |
98 | 9f5cbafe | Stavros Sachtouris | CLOUD_PREFIX: { |
99 | 144b3551 | Stavros Sachtouris | #'default': {
|
100 | 144b3551 | Stavros Sachtouris | # 'url': '',
|
101 | 144b3551 | Stavros Sachtouris | # 'token': ''
|
102 | 5e4a0bb9 | Stavros Sachtouris | # 'pithos_container': 'THIS IS DANGEROUS'
|
103 | 144b3551 | Stavros Sachtouris | # 'pithos_type': 'object-store',
|
104 | 144b3551 | Stavros Sachtouris | # 'pithos_version': 'v1',
|
105 | 144b3551 | Stavros Sachtouris | # 'cyclades_type': 'compute',
|
106 | 144b3551 | Stavros Sachtouris | # 'cyclades_version': 'v2.0',
|
107 | 144b3551 | Stavros Sachtouris | # 'plankton_type': 'image',
|
108 | 144b3551 | Stavros Sachtouris | # 'plankton_version': '',
|
109 | 144b3551 | Stavros Sachtouris | # 'astakos_type': 'identity',
|
110 | 144b3551 | Stavros Sachtouris | # 'astakos_version': 'v2.0'
|
111 | 144b3551 | Stavros Sachtouris | #}
|
112 | 7493ccb6 | Stavros Sachtouris | } |
113 | 7493ccb6 | Stavros Sachtouris | } |
114 | 7493ccb6 | Stavros Sachtouris | |
115 | 7493ccb6 | Stavros Sachtouris | |
116 | 7493ccb6 | Stavros Sachtouris | class Config(RawConfigParser): |
117 | c5997814 | Stavros Sachtouris | |
118 | 3f0eae61 | Stavros Sachtouris | def __init__(self, path=None, with_defaults=True): |
119 | 7493ccb6 | Stavros Sachtouris | RawConfigParser.__init__(self, dict_type=OrderedDict)
|
120 | 7493ccb6 | Stavros Sachtouris | self.path = path or os.environ.get(CONFIG_ENV, CONFIG_PATH) |
121 | 7493ccb6 | Stavros Sachtouris | self._overrides = defaultdict(dict) |
122 | 3f0eae61 | Stavros Sachtouris | if with_defaults:
|
123 | 3f0eae61 | Stavros Sachtouris | self._load_defaults()
|
124 | 7493ccb6 | Stavros Sachtouris | self.read(self.path) |
125 | 7493ccb6 | Stavros Sachtouris | |
126 | ce9ccb40 | Stavros Sachtouris | for section in self.sections(): |
127 | 144b3551 | Stavros Sachtouris | r = self._cloud_name(section)
|
128 | ce9ccb40 | Stavros Sachtouris | if r:
|
129 | ce9ccb40 | Stavros Sachtouris | for k, v in self.items(section): |
130 | 144b3551 | Stavros Sachtouris | self.set_cloud(r, k, v)
|
131 | ce9ccb40 | Stavros Sachtouris | self.remove_section(section)
|
132 | 14b9a185 | Stavros Sachtouris | |
133 | 3f0eae61 | Stavros Sachtouris | @staticmethod
|
134 | 144b3551 | Stavros Sachtouris | def _cloud_name(full_section_name): |
135 | 29be4062 | Stavros Sachtouris | if not full_section_name.startswith(CLOUD_PREFIX + ' '): |
136 | 29be4062 | Stavros Sachtouris | return None |
137 | c5997814 | Stavros Sachtouris | matcher = match(CLOUD_PREFIX + ' "([~@#$.:\-\w]+)"', full_section_name)
|
138 | 29be4062 | Stavros Sachtouris | if matcher:
|
139 | 29be4062 | Stavros Sachtouris | return matcher.groups()[0] |
140 | 29be4062 | Stavros Sachtouris | else:
|
141 | 29be4062 | Stavros Sachtouris | icn = full_section_name[len(CLOUD_PREFIX) + 1:] |
142 | 29be4062 | Stavros Sachtouris | raise InvalidCloudNameError('Invalid Cloud Name %s' % icn) |
143 | 3f0eae61 | Stavros Sachtouris | |
144 | c5997814 | Stavros Sachtouris | def rescue_old_file(self, err=stderr): |
145 | ce9ccb40 | Stavros Sachtouris | lost_terms = [] |
146 | ce9ccb40 | Stavros Sachtouris | global_terms = DEFAULTS['global'].keys()
|
147 | ce9ccb40 | Stavros Sachtouris | translations = dict(
|
148 | ce9ccb40 | Stavros Sachtouris | config=dict(serv='', cmd='config'), |
149 | ce9ccb40 | Stavros Sachtouris | history=dict(serv='', cmd='history'), |
150 | ce9ccb40 | Stavros Sachtouris | pithos=dict(serv='pithos', cmd='file'), |
151 | ce9ccb40 | Stavros Sachtouris | file=dict(serv='pithos', cmd='file'), |
152 | ce9ccb40 | Stavros Sachtouris | store=dict(serv='pithos', cmd='file'), |
153 | ce9ccb40 | Stavros Sachtouris | storage=dict(serv='pithos', cmd='file'), |
154 | ce9ccb40 | Stavros Sachtouris | image=dict(serv='plankton', cmd='image'), |
155 | ce9ccb40 | Stavros Sachtouris | plankton=dict(serv='plankton', cmd='image'), |
156 | ce9ccb40 | Stavros Sachtouris | compute=dict(serv='compute', cmd=''), |
157 | ce9ccb40 | Stavros Sachtouris | cyclades=dict(serv='compute', cmd='server'), |
158 | ce9ccb40 | Stavros Sachtouris | server=dict(serv='compute', cmd='server'), |
159 | ce9ccb40 | Stavros Sachtouris | flavor=dict(serv='compute', cmd='flavor'), |
160 | ce9ccb40 | Stavros Sachtouris | network=dict(serv='compute', cmd='network'), |
161 | ce9ccb40 | Stavros Sachtouris | astakos=dict(serv='astakos', cmd='user'), |
162 | ce9ccb40 | Stavros Sachtouris | user=dict(serv='astakos', cmd='user'), |
163 | ce9ccb40 | Stavros Sachtouris | ) |
164 | ce9ccb40 | Stavros Sachtouris | |
165 | 6144f3d4 | Stavros Sachtouris | self.set('global', 'default_' + CLOUD_PREFIX, 'default') |
166 | ce9ccb40 | Stavros Sachtouris | for s in self.sections(): |
167 | c5997814 | Stavros Sachtouris | if s in ('global', ): |
168 | ce9ccb40 | Stavros Sachtouris | # global.url, global.token -->
|
169 | 144b3551 | Stavros Sachtouris | # cloud.default.url, cloud.default.token
|
170 | ce9ccb40 | Stavros Sachtouris | for term in set(self.keys(s)).difference(global_terms): |
171 | ce9ccb40 | Stavros Sachtouris | if term not in ('url', 'token'): |
172 | ce9ccb40 | Stavros Sachtouris | lost_terms.append('%s.%s = %s' % (
|
173 | ce9ccb40 | Stavros Sachtouris | s, term, self.get(s, term)))
|
174 | ce9ccb40 | Stavros Sachtouris | self.remove_option(s, term)
|
175 | ce9ccb40 | Stavros Sachtouris | continue
|
176 | ce9ccb40 | Stavros Sachtouris | gval = self.get(s, term)
|
177 | c5997814 | Stavros Sachtouris | default_cloud = self.get(
|
178 | c5997814 | Stavros Sachtouris | 'global', 'default_cloud') or 'default' |
179 | c825ddc9 | Stavros Sachtouris | try:
|
180 | c5997814 | Stavros Sachtouris | cval = self.get_cloud(default_cloud, term)
|
181 | c825ddc9 | Stavros Sachtouris | except KeyError: |
182 | c825ddc9 | Stavros Sachtouris | cval = ''
|
183 | ce9ccb40 | Stavros Sachtouris | if gval and cval and ( |
184 | ce9ccb40 | Stavros Sachtouris | gval.lower().strip('/') != cval.lower().strip('/')): |
185 | ce9ccb40 | Stavros Sachtouris | raise CLISyntaxError(
|
186 | c5997814 | Stavros Sachtouris | 'Conflicting values for default %s' % (
|
187 | c5997814 | Stavros Sachtouris | term), |
188 | ce9ccb40 | Stavros Sachtouris | importance=2, details=[
|
189 | ce9ccb40 | Stavros Sachtouris | ' global.%s: %s' % (term, gval),
|
190 | c5997814 | Stavros Sachtouris | ' %s.%s.%s: %s' % (
|
191 | c5997814 | Stavros Sachtouris | CLOUD_PREFIX, |
192 | c5997814 | Stavros Sachtouris | default_cloud, |
193 | c5997814 | Stavros Sachtouris | term, |
194 | c5997814 | Stavros Sachtouris | cval), |
195 | ce9ccb40 | Stavros Sachtouris | 'Please remove one of them manually:',
|
196 | ce9ccb40 | Stavros Sachtouris | ' /config delete global.%s' % term,
|
197 | ce9ccb40 | Stavros Sachtouris | ' or'
|
198 | c5997814 | Stavros Sachtouris | ' /config delete %s.%s.%s' % (
|
199 | c5997814 | Stavros Sachtouris | CLOUD_PREFIX, default_cloud, term), |
200 | ce9ccb40 | Stavros Sachtouris | 'and try again'])
|
201 | ce9ccb40 | Stavros Sachtouris | elif gval:
|
202 | c5997814 | Stavros Sachtouris | err.write(u'... rescue %s.%s => %s.%s.%s\n' % (
|
203 | c5997814 | Stavros Sachtouris | s, term, CLOUD_PREFIX, default_cloud, term)) |
204 | c5997814 | Stavros Sachtouris | err.flush() |
205 | 144b3551 | Stavros Sachtouris | self.set_cloud('default', term, gval) |
206 | ce9ccb40 | Stavros Sachtouris | self.remove_option(s, term)
|
207 | ce9ccb40 | Stavros Sachtouris | # translation for <service> or <command> settings
|
208 | ce9ccb40 | Stavros Sachtouris | # <service> or <command group> settings --> translation --> global
|
209 | ce9ccb40 | Stavros Sachtouris | elif s in translations: |
210 | ce9ccb40 | Stavros Sachtouris | |
211 | ce9ccb40 | Stavros Sachtouris | if s in ('history',): |
212 | ce9ccb40 | Stavros Sachtouris | k = 'file'
|
213 | ce9ccb40 | Stavros Sachtouris | v = self.get(s, k)
|
214 | ce9ccb40 | Stavros Sachtouris | if v:
|
215 | c5997814 | Stavros Sachtouris | err.write(u'... rescue %s.%s => global.%s_%s\n' % (
|
216 | ce9ccb40 | Stavros Sachtouris | s, k, s, k)) |
217 | c5997814 | Stavros Sachtouris | err.flush() |
218 | ce9ccb40 | Stavros Sachtouris | self.set('global', '%s_%s' % (s, k), v) |
219 | ce9ccb40 | Stavros Sachtouris | self.remove_option(s, k)
|
220 | ce9ccb40 | Stavros Sachtouris | |
221 | ce9ccb40 | Stavros Sachtouris | trn = translations[s] |
222 | ce9ccb40 | Stavros Sachtouris | for k, v in self.items(s, False): |
223 | ce9ccb40 | Stavros Sachtouris | if v and k in ('cli',): |
224 | c5997814 | Stavros Sachtouris | err.write(u'... rescue %s.%s => global.%s_cli\n' % (
|
225 | ce9ccb40 | Stavros Sachtouris | s, k, trn['cmd']))
|
226 | c5997814 | Stavros Sachtouris | err.flush() |
227 | 144b3551 | Stavros Sachtouris | self.set('global', '%s_cli' % trn['cmd'], v) |
228 | 144b3551 | Stavros Sachtouris | elif k in ('container',) and trn['serv'] in ('pithos',): |
229 | c5997814 | Stavros Sachtouris | err.write( |
230 | c5997814 | Stavros Sachtouris | u'... rescue %s.%s => %s.default.pithos_%s\n' % (
|
231 | c5997814 | Stavros Sachtouris | s, k, CLOUD_PREFIX, k)) |
232 | c5997814 | Stavros Sachtouris | err.flush() |
233 | 144b3551 | Stavros Sachtouris | self.set_cloud('default', 'pithos_%s' % k, v) |
234 | 144b3551 | Stavros Sachtouris | else:
|
235 | ce9ccb40 | Stavros Sachtouris | lost_terms.append('%s.%s = %s' % (s, k, v))
|
236 | ce9ccb40 | Stavros Sachtouris | self.remove_section(s)
|
237 | ce9ccb40 | Stavros Sachtouris | # self.pretty_print()
|
238 | ce9ccb40 | Stavros Sachtouris | return lost_terms
|
239 | ce9ccb40 | Stavros Sachtouris | |
240 | c5997814 | Stavros Sachtouris | def pretty_print(self, out=stdout): |
241 | ce9ccb40 | Stavros Sachtouris | for s in self.sections(): |
242 | c5997814 | Stavros Sachtouris | out.write(s) |
243 | c5997814 | Stavros Sachtouris | out.flush() |
244 | ce9ccb40 | Stavros Sachtouris | for k, v in self.items(s): |
245 | ce9ccb40 | Stavros Sachtouris | if isinstance(v, dict): |
246 | c5997814 | Stavros Sachtouris | out.write(u'\t%s => {\n' % k)
|
247 | c5997814 | Stavros Sachtouris | out.flush() |
248 | ce9ccb40 | Stavros Sachtouris | for ki, vi in v.items(): |
249 | c5997814 | Stavros Sachtouris | out.write(u'\t\t%s => %s\n' % (ki, vi))
|
250 | c5997814 | Stavros Sachtouris | out.flush() |
251 | c5997814 | Stavros Sachtouris | out.write(u'\t}\n')
|
252 | ce9ccb40 | Stavros Sachtouris | else:
|
253 | c5997814 | Stavros Sachtouris | out.write(u'\t %s => %s\n' % (k, v))
|
254 | c5997814 | Stavros Sachtouris | out.flush() |
255 | 14b9a185 | Stavros Sachtouris | |
256 | 3f0eae61 | Stavros Sachtouris | def guess_version(self): |
257 | 144b3551 | Stavros Sachtouris | """
|
258 | 3564be49 | Stavros Sachtouris | :returns: (float) version of the config file or 0.9 if unrecognized
|
259 | 144b3551 | Stavros Sachtouris | """
|
260 | 3f0eae61 | Stavros Sachtouris | checker = Config(self.path, with_defaults=False) |
261 | 3f0eae61 | Stavros Sachtouris | sections = checker.sections() |
262 | f1d06d74 | Stavros Sachtouris | log.debug('Config file heuristic 1: old global section ?')
|
263 | 3f0eae61 | Stavros Sachtouris | if 'global' in sections: |
264 | 3f0eae61 | Stavros Sachtouris | if checker.get('global', 'url') or checker.get('global', 'token'): |
265 | f1d06d74 | Stavros Sachtouris | log.debug('..... config file has an old global section')
|
266 | c825ddc9 | Stavros Sachtouris | return 0.8 |
267 | f1d06d74 | Stavros Sachtouris | log.debug('........ nope')
|
268 | f1d06d74 | Stavros Sachtouris | log.debug('Config file heuristic 2: Any cloud sections ?')
|
269 | 6144f3d4 | Stavros Sachtouris | if CLOUD_PREFIX in sections: |
270 | 6144f3d4 | Stavros Sachtouris | for r in self.keys(CLOUD_PREFIX): |
271 | f1d06d74 | Stavros Sachtouris | log.debug('... found cloud "%s"' % r)
|
272 | c825ddc9 | Stavros Sachtouris | return 0.9 |
273 | f1d06d74 | Stavros Sachtouris | log.debug('........ nope')
|
274 | f1d06d74 | Stavros Sachtouris | log.debug('All heuristics failed, cannot decide')
|
275 | df0045d8 | Stavros Sachtouris | return 0.9 |
276 | 3f0eae61 | Stavros Sachtouris | |
277 | 144b3551 | Stavros Sachtouris | def get_cloud(self, cloud, option): |
278 | ce9ccb40 | Stavros Sachtouris | """
|
279 | 144b3551 | Stavros Sachtouris | :param cloud: (str) cloud alias
|
280 | ce9ccb40 | Stavros Sachtouris |
|
281 | 144b3551 | Stavros Sachtouris | :param option: (str) option in cloud section
|
282 | ce9ccb40 | Stavros Sachtouris |
|
283 | ce9ccb40 | Stavros Sachtouris | :returns: (str) the value assigned on this option
|
284 | ce9ccb40 | Stavros Sachtouris |
|
285 | 144b3551 | Stavros Sachtouris | :raises KeyError: if cloud or cloud's option does not exist
|
286 | ce9ccb40 | Stavros Sachtouris | """
|
287 | 6144f3d4 | Stavros Sachtouris | r = self.get(CLOUD_PREFIX, cloud)
|
288 | ce9ccb40 | Stavros Sachtouris | if not r: |
289 | 144b3551 | Stavros Sachtouris | raise KeyError('Cloud "%s" does not exist' % cloud) |
290 | ce9ccb40 | Stavros Sachtouris | return r[option]
|
291 | ce9ccb40 | Stavros Sachtouris | |
292 | 144b3551 | Stavros Sachtouris | def set_cloud(self, cloud, option, value): |
293 | ce9ccb40 | Stavros Sachtouris | try:
|
294 | 6144f3d4 | Stavros Sachtouris | d = self.get(CLOUD_PREFIX, cloud) or dict() |
295 | ce9ccb40 | Stavros Sachtouris | except KeyError: |
296 | 844a6bdb | Stavros Sachtouris | d = dict()
|
297 | ce9ccb40 | Stavros Sachtouris | d[option] = value |
298 | 6144f3d4 | Stavros Sachtouris | self.set(CLOUD_PREFIX, cloud, d)
|
299 | 362adf50 | Stavros Sachtouris | |
300 | 7493ccb6 | Stavros Sachtouris | def _load_defaults(self): |
301 | 7493ccb6 | Stavros Sachtouris | for section, options in DEFAULTS.items(): |
302 | 7493ccb6 | Stavros Sachtouris | for option, val in options.items(): |
303 | 7493ccb6 | Stavros Sachtouris | self.set(section, option, val)
|
304 | 7493ccb6 | Stavros Sachtouris | |
305 | f724cd35 | Stavros Sachtouris | def _get_dict(self, section, include_defaults=True): |
306 | f724cd35 | Stavros Sachtouris | try:
|
307 | f724cd35 | Stavros Sachtouris | d = dict(DEFAULTS[section]) if include_defaults else {} |
308 | f724cd35 | Stavros Sachtouris | except KeyError: |
309 | f724cd35 | Stavros Sachtouris | d = {} |
310 | f724cd35 | Stavros Sachtouris | try:
|
311 | f724cd35 | Stavros Sachtouris | d.update(RawConfigParser.items(self, section))
|
312 | f724cd35 | Stavros Sachtouris | except NoSectionError:
|
313 | f724cd35 | Stavros Sachtouris | pass
|
314 | f724cd35 | Stavros Sachtouris | return d
|
315 | f724cd35 | Stavros Sachtouris | |
316 | 8eb8c540 | Stavros Sachtouris | def reload(self): |
317 | 8eb8c540 | Stavros Sachtouris | self = self.__init__(self.path) |
318 | 8eb8c540 | Stavros Sachtouris | |
319 | 7493ccb6 | Stavros Sachtouris | def get(self, section, option): |
320 | 114e19da | Stavros Sachtouris | """
|
321 | 144b3551 | Stavros Sachtouris | :param section: (str) HINT: for clouds, use cloud.<section>
|
322 | 114e19da | Stavros Sachtouris |
|
323 | 114e19da | Stavros Sachtouris | :param option: (str)
|
324 | 114e19da | Stavros Sachtouris |
|
325 | 114e19da | Stavros Sachtouris | :returns: (str) the value stored at section: {option: value}
|
326 | 114e19da | Stavros Sachtouris | """
|
327 | 7493ccb6 | Stavros Sachtouris | value = self._overrides.get(section, {}).get(option)
|
328 | 7493ccb6 | Stavros Sachtouris | if value is not None: |
329 | 7493ccb6 | Stavros Sachtouris | return value
|
330 | 6144f3d4 | Stavros Sachtouris | prefix = CLOUD_PREFIX + '.'
|
331 | 144b3551 | Stavros Sachtouris | if section.startswith(prefix): |
332 | 144b3551 | Stavros Sachtouris | return self.get_cloud(section[len(prefix):], option) |
333 | 7493ccb6 | Stavros Sachtouris | try:
|
334 | 7493ccb6 | Stavros Sachtouris | return RawConfigParser.get(self, section, option) |
335 | 7493ccb6 | Stavros Sachtouris | except (NoSectionError, NoOptionError):
|
336 | 7493ccb6 | Stavros Sachtouris | return DEFAULTS.get(section, {}).get(option)
|
337 | 7493ccb6 | Stavros Sachtouris | |
338 | 7493ccb6 | Stavros Sachtouris | def set(self, section, option, value): |
339 | 114e19da | Stavros Sachtouris | """
|
340 | 144b3551 | Stavros Sachtouris | :param section: (str) HINT: for remotes use cloud.<section>
|
341 | 114e19da | Stavros Sachtouris |
|
342 | 114e19da | Stavros Sachtouris | :param option: (str)
|
343 | 114e19da | Stavros Sachtouris |
|
344 | 114e19da | Stavros Sachtouris | :param value: str
|
345 | 114e19da | Stavros Sachtouris | """
|
346 | 6144f3d4 | Stavros Sachtouris | prefix = CLOUD_PREFIX + '.'
|
347 | 144b3551 | Stavros Sachtouris | if section.startswith(prefix): |
348 | 29be4062 | Stavros Sachtouris | cloud = self._cloud_name(
|
349 | 29be4062 | Stavros Sachtouris | CLOUD_PREFIX + ' "' + section[len(prefix):] + '"') |
350 | 29be4062 | Stavros Sachtouris | return self.set_cloud(cloud, option, value) |
351 | 7493ccb6 | Stavros Sachtouris | if section not in RawConfigParser.sections(self): |
352 | 7493ccb6 | Stavros Sachtouris | self.add_section(section)
|
353 | 8556d269 | Stavros Sachtouris | return RawConfigParser.set(self, section, option, value) |
354 | 7493ccb6 | Stavros Sachtouris | |
355 | 3fe56be4 | Stavros Sachtouris | def remove_option(self, section, option, also_remove_default=False): |
356 | 7493ccb6 | Stavros Sachtouris | try:
|
357 | 3fe56be4 | Stavros Sachtouris | if also_remove_default:
|
358 | 3fe56be4 | Stavros Sachtouris | DEFAULTS[section].pop(option) |
359 | 7493ccb6 | Stavros Sachtouris | RawConfigParser.remove_option(self, section, option)
|
360 | 8556d269 | Stavros Sachtouris | except (NoSectionError, KeyError): |
361 | 7493ccb6 | Stavros Sachtouris | pass
|
362 | 7493ccb6 | Stavros Sachtouris | |
363 | a7aacf12 | Stavros Sachtouris | def remove_from_cloud(self, cloud, option): |
364 | 6144f3d4 | Stavros Sachtouris | d = self.get(CLOUD_PREFIX, cloud)
|
365 | 844a6bdb | Stavros Sachtouris | if isinstance(d, dict): |
366 | 844a6bdb | Stavros Sachtouris | d.pop(option) |
367 | 844a6bdb | Stavros Sachtouris | |
368 | f724cd35 | Stavros Sachtouris | def keys(self, section, include_defaults=True): |
369 | f724cd35 | Stavros Sachtouris | d = self._get_dict(section, include_defaults)
|
370 | f724cd35 | Stavros Sachtouris | return d.keys()
|
371 | f724cd35 | Stavros Sachtouris | |
372 | 57ecec97 | Stavros Sachtouris | def items(self, section, include_defaults=True): |
373 | f724cd35 | Stavros Sachtouris | d = self._get_dict(section, include_defaults)
|
374 | 7493ccb6 | Stavros Sachtouris | return d.items()
|
375 | 7493ccb6 | Stavros Sachtouris | |
376 | 7493ccb6 | Stavros Sachtouris | def override(self, section, option, value): |
377 | 7493ccb6 | Stavros Sachtouris | self._overrides[section][option] = value
|
378 | 7493ccb6 | Stavros Sachtouris | |
379 | 7493ccb6 | Stavros Sachtouris | def write(self): |
380 | 35d47a6c | Stavros Sachtouris | cld_bu = self._get_dict(CLOUD_PREFIX)
|
381 | 6144f3d4 | Stavros Sachtouris | try:
|
382 | 6144f3d4 | Stavros Sachtouris | for r, d in self.items(CLOUD_PREFIX): |
383 | 6144f3d4 | Stavros Sachtouris | for k, v in d.items(): |
384 | 6144f3d4 | Stavros Sachtouris | self.set(CLOUD_PREFIX + ' "%s"' % r, k, v) |
385 | 6144f3d4 | Stavros Sachtouris | self.remove_section(CLOUD_PREFIX)
|
386 | 6144f3d4 | Stavros Sachtouris | |
387 | 6144f3d4 | Stavros Sachtouris | with open(self.path, 'w') as f: |
388 | 6144f3d4 | Stavros Sachtouris | os.chmod(self.path, 0600) |
389 | 6144f3d4 | Stavros Sachtouris | f.write(HEADER.lstrip()) |
390 | 6144f3d4 | Stavros Sachtouris | f.flush() |
391 | 6144f3d4 | Stavros Sachtouris | RawConfigParser.write(self, f)
|
392 | 6144f3d4 | Stavros Sachtouris | finally:
|
393 | 6144f3d4 | Stavros Sachtouris | if CLOUD_PREFIX not in self.sections(): |
394 | 6144f3d4 | Stavros Sachtouris | self.add_section(CLOUD_PREFIX)
|
395 | 35d47a6c | Stavros Sachtouris | for cloud, d in cld_bu.items(): |
396 | 35d47a6c | Stavros Sachtouris | self.set(CLOUD_PREFIX, cloud, d) |