Revision d2d0dbdb

b/kamaki/cli/config.py
52 52
# Path to the file that stores the configuration
53 53
CONFIG_PATH = os.path.expanduser('~/.kamakirc')
54 54
HISTORY_PATH = os.path.expanduser('~/.kamaki.history')
55
CLOUD_PREFIX = 'cloud'
55 56

  
56 57
# Name of a shell variable to bypass the CONFIG_PATH value
57 58
CONFIG_ENV = 'KAMAKI_CONFIG'
......
84 85
        #  'livetest_cli': 'livetest',
85 86
        #  'astakos_cli': 'snf-astakos'
86 87
    },
87
    'cloud':
88
    CLOUD_PREFIX:
88 89
    {
89 90
        #'default': {
90 91
        #    'url': '',
......
120 121

  
121 122
    @staticmethod
122 123
    def _cloud_name(full_section_name):
123
        matcher = match('cloud "(\w+)"', full_section_name)
124
        matcher = match(CLOUD_PREFIX + ' "(\w+)"', full_section_name)
124 125
        return matcher.groups()[0] if matcher else None
125 126

  
126 127
    def rescue_old_file(self):
......
144 145
            user=dict(serv='astakos', cmd='user'),
145 146
        )
146 147

  
147
        self.set('global', 'default_cloud', 'default')
148
        self.set('global', 'default_' + CLOUD_PREFIX, 'default')
148 149
        for s in self.sections():
149 150
            if s in ('global'):
150 151
                # global.url, global.token -->
......
166 167
                                'Conflicting values for default %s' % term,
167 168
                                importance=2, details=[
168 169
                                    ' global.%s:  %s' % (term, gval),
169
                                    ' cloud.default.%s:  %s' % (term, cval),
170
                                    ' %s.default.%s:  %s' % (
171
                                        CLOUD_PREFIX, term, cval),
170 172
                                    'Please remove one of them manually:',
171 173
                                    ' /config delete global.%s' % term,
172 174
                                    ' or'
173
                                    ' /config delete cloud.default.%s' % term,
175
                                    ' /config delete %s.default.%s' % (
176
                                        CLOUD_PREFIX, term),
174 177
                                    'and try again'])
175 178
                    elif gval:
176
                        print('... rescue %s.%s => cloud.default.%s' % (
177
                            s, term, term))
179
                        print('... rescue %s.%s => %s.default.%s' % (
180
                            s, term, CLOUD_PREFIX, term))
178 181
                        self.set_cloud('default', term, gval)
179 182
                    self.remove_option(s, term)
180 183
            # translation for <service> or <command> settings
......
197 200
                            s, k, trn['cmd']))
198 201
                        self.set('global', '%s_cli' % trn['cmd'], v)
199 202
                    elif k in ('container',) and trn['serv'] in ('pithos',):
200
                        print('... rescue %s.%s => cloud.default.pithos_%s' % (
201
                                    s, k, k))
203
                        print('... rescue %s.%s => %s.default.pithos_%s' % (
204
                                    s, k, CLOUD_PREFIX, k))
202 205
                        self.set_cloud('default', 'pithos_%s' % k, v)
203 206
                    else:
204 207
                        lost_terms.append('%s.%s = %s' % (s, k, v))
......
231 234
                return 0.8
232 235
        log.warning('........ nope')
233 236
        log.warning('Config file heuristic 2: Any cloud sections ?')
234
        if 'cloud' in sections:
235
            for r in self.keys('cloud'):
237
        if CLOUD_PREFIX in sections:
238
            for r in self.keys(CLOUD_PREFIX):
236 239
                log.warning('... found cloud "%s"' % r)
237 240
                return 0.9
238 241
        log.warning('........ nope')
......
249 252

  
250 253
        :raises KeyError: if cloud or cloud's option does not exist
251 254
        """
252
        r = self.get('cloud', cloud)
255
        r = self.get(CLOUD_PREFIX, cloud)
253 256
        if not r:
254 257
            raise KeyError('Cloud "%s" does not exist' % cloud)
255 258
        return r[option]
......
259 262

  
260 263
    def set_cloud(self, cloud, option, value):
261 264
        try:
262
            d = self.get('cloud', cloud) or dict()
265
            d = self.get(CLOUD_PREFIX, cloud) or dict()
263 266
        except KeyError:
264 267
            d = dict()
265 268
        d[option] = value
266
        self.set('cloud', cloud, d)
269
        self.set(CLOUD_PREFIX, cloud, d)
267 270

  
268 271
    def set_global(self, option, value):
269 272
        self.set('global', option, value)
......
298 301
        value = self._overrides.get(section, {}).get(option)
299 302
        if value is not None:
300 303
            return value
301
        prefix = 'cloud.'
304
        prefix = CLOUD_PREFIX + '.'
302 305
        if section.startswith(prefix):
303 306
            return self.get_cloud(section[len(prefix):], option)
304 307
        try:
......
314 317

  
315 318
        :param value: str
316 319
        """
317
        prefix = 'cloud.'
320
        prefix = CLOUD_PREFIX + '.'
318 321
        if section.startswith(prefix):
319 322
            return self.set_cloud(section[len(prefix)], option, value)
320 323
        if section not in RawConfigParser.sections(self):
......
330 333
            pass
331 334

  
332 335
    def remote_from_cloud(self, cloud, option):
333
        d = self.get('cloud', cloud)
336
        d = self.get(CLOUD_PREFIX, cloud)
334 337
        if isinstance(d, dict):
335 338
            d.pop(option)
336 339

  
......
346 349
        self._overrides[section][option] = value
347 350

  
348 351
    def write(self):
349
        for r, d in self.items('cloud'):
350
            for k, v in d.items():
351
                self.set('cloud "%s"' % r, k, v)
352
        self.remove_section('cloud')
353

  
354
        with open(self.path, 'w') as f:
355
            os.chmod(self.path, 0600)
356
            f.write(HEADER.lstrip())
357
            f.flush()
358
            RawConfigParser.write(self, f)
352
        cld_bu = dict(self._get_dict(CLOUD_PREFIX))
353
        try:
354
            for r, d in self.items(CLOUD_PREFIX):
355
                for k, v in d.items():
356
                    self.set(CLOUD_PREFIX + ' "%s"' % r, k, v)
357
            self.remove_section(CLOUD_PREFIX)
358

  
359
            with open(self.path, 'w') as f:
360
                os.chmod(self.path, 0600)
361
                f.write(HEADER.lstrip())
362
                f.flush()
363
                RawConfigParser.write(self, f)
364
        finally:
365
            if CLOUD_PREFIX not in self.sections():
366
                self.add_section(CLOUD_PREFIX)
367
            self._get_dict(CLOUD_PREFIX).update(cld_bu)

Also available in: Unified diff