~openstack-ubuntu-testing/charms/precise/glance/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/context.py

  • Committer: Adam Gandelman
  • Date: 2013-10-17 21:39:32 UTC
  • mfrom: (13.18.7 glance)
  • Revision ID: adamg@canonical.com-20131017213932-07uwux3uu2ndxmzn
MergeĀ upstreamĀ charm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import json
1
2
import os
2
3
 
3
4
from base64 import b64decode
21
22
    related_units,
22
23
    unit_get,
23
24
    unit_private_ip,
 
25
    ERROR,
24
26
    WARNING,
25
27
)
26
28
 
370
372
        return None
371
373
 
372
374
    def _ensure_packages(self):
373
 
        ensure_packages(self.packages)
 
375
        [ensure_packages(pkgs) for pkgs in self.packages]
374
376
 
375
377
    def _save_flag_file(self):
376
378
        if self.network_manager == 'quantum':
431
433
                flags[k.strip()] = v
432
434
            ctxt = {'user_config_flags': flags}
433
435
            return ctxt
 
436
 
 
437
 
 
438
class SubordinateConfigContext(OSContextGenerator):
 
439
    """
 
440
    Responsible for inspecting relations to subordinates that
 
441
    may be exporting required config via a json blob.
 
442
 
 
443
    The subordinate interface allows subordinates to export their
 
444
    configuration requirements to the principle for multiple config
 
445
    files and multiple serivces.  Ie, a subordinate that has interfaces
 
446
    to both glance and nova may export to following yaml blob as json:
 
447
 
 
448
        glance:
 
449
            /etc/glance/glance-api.conf:
 
450
                sections:
 
451
                    DEFAULT:
 
452
                        - [key1, value1]
 
453
            /etc/glance/glance-registry.conf:
 
454
                    MYSECTION:
 
455
                        - [key2, value2]
 
456
        nova:
 
457
            /etc/nova/nova.conf:
 
458
                sections:
 
459
                    DEFAULT:
 
460
                        - [key3, value3]
 
461
 
 
462
 
 
463
    It is then up to the principle charms to subscribe this context to
 
464
    the service+config file it is interestd in.  Configuration data will
 
465
    be available in the template context, in glance's case, as:
 
466
        ctxt = {
 
467
            ... other context ...
 
468
            'subordinate_config': {
 
469
                'DEFAULT': {
 
470
                    'key1': 'value1',
 
471
                },
 
472
                'MYSECTION': {
 
473
                    'key2': 'value2',
 
474
                },
 
475
            }
 
476
        }
 
477
 
 
478
    """
 
479
    def __init__(self, service, config_file, interface):
 
480
        """
 
481
        :param service     : Service name key to query in any subordinate
 
482
                             data found
 
483
        :param config_file : Service's config file to query sections
 
484
        :param interface   : Subordinate interface to inspect
 
485
        """
 
486
        self.service = service
 
487
        self.config_file = config_file
 
488
        self.interface = interface
 
489
 
 
490
    def __call__(self):
 
491
        ctxt = {}
 
492
        for rid in relation_ids(self.interface):
 
493
            for unit in related_units(rid):
 
494
                sub_config = relation_get('subordinate_configuration',
 
495
                                          rid=rid, unit=unit)
 
496
                if sub_config and sub_config != '':
 
497
                    try:
 
498
                        sub_config = json.loads(sub_config)
 
499
                    except:
 
500
                        log('Could not parse JSON from subordinate_config '
 
501
                            'setting from %s' % rid, level=ERROR)
 
502
                        continue
 
503
 
 
504
                    if self.service not in sub_config:
 
505
                        log('Found subordinate_config on %s but it contained'
 
506
                            'nothing for %s service' % (rid, self.service))
 
507
                        continue
 
508
 
 
509
                    sub_config = sub_config[self.service]
 
510
                    if self.config_file not in sub_config:
 
511
                        log('Found subordinate_config on %s but it contained'
 
512
                            'nothing for %s' % (rid, self.config_file))
 
513
                        continue
 
514
 
 
515
                    sub_config = sub_config[self.config_file]
 
516
                    for k, v in sub_config.iteritems():
 
517
                        ctxt[k] = v
 
518
 
 
519
        if not ctxt:
 
520
            ctxt['sections'] = {}
 
521
 
 
522
        return ctxt