~ubuntuone-pqm-team/juju-deployer/trunk

« back to all changes in this revision

Viewing changes to utils.py

  • Committer: Adam Gandelman
  • Date: 2013-02-08 00:09:58 UTC
  • mfrom: (58.3.7 juju-deployer-webops)
  • Revision ID: adamg@canonical.com-20130208000958-jshyuihkae91oxfj
Merge mew's multi-config file overriding.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import time
8
8
import os
9
9
from contextlib import contextmanager
 
10
from copy import deepcopy
10
11
 
11
12
 
12
13
log = logging.getLogger("juju-deployer")
23
24
        os.chdir(cwd)
24
25
 
25
26
 
 
27
def dict_merge(onto, source):
 
28
    target = deepcopy(onto)
 
29
    for (key, value) in source.items():
 
30
        if key in target and isinstance(target[key], dict) and isinstance(value, dict):
 
31
            target[key] = dict_merge(target[key], value)
 
32
        else:
 
33
            target[key] = value
 
34
    return target
 
35
 
 
36
 
26
37
def init_logging(filename, debug):
27
38
    """Set up logging handlers to write messages to stdout and a file"""
28
39
    log.setLevel(logging.DEBUG)
450
461
    reactor.run()
451
462
 
452
463
def load_deployment(config, deployment):
453
 
    relations = {}
454
 
    series = None
455
 
    charms = {}
456
 
    overrides = {}
457
464
    if deployment not in config.keys():
458
465
        log.error("deployment %s not found.", deployment)
459
466
        display_deploys(config)
460
467
        exit(1)
461
468
 
462
 
    if 'series' in config[deployment]:
463
 
        series = config[deployment]['series']
464
 
 
465
 
    if 'inherits' in config[deployment]:
 
469
    deploy_config = config[deployment]
 
470
    if 'inherits' in deploy_config:
466
471
        cur = config[deployment]
467
472
        configs = []
468
473
 
480
485
        configs.insert(0, base)
481
486
        configs.append(config[deployment])
482
487
 
483
 
        for config in configs:
484
 
            if 'series' in config:
485
 
                series = config['series']
486
 
            if 'overrides' in config:
487
 
                for k, v in config['overrides'].iteritems():
488
 
                    overrides[k] = v
489
 
            if 'services' in config:
490
 
                for k, v in config['services'].iteritems():
491
 
                    if k not in charms:
492
 
                        charms[k] = v
493
 
                    else:
494
 
                        if 'branch' in v:
495
 
                            charms[k]['branch'] = v['branch']
496
 
                        if 'options' in v:
497
 
                            for opt, value in v['options'].iteritems():
498
 
                                charms[k]['options'][opt] = value
499
 
            if 'relations' in config:
500
 
                rels = relations_json_to_tuples(config['relations'])
501
 
                for w, rs in rels.iteritems():
502
 
                    if w in relations:
503
 
                        [relations[w].append(r) for r in rs]
504
 
                    else:
505
 
                        relations[w] = rs
 
488
        deploy_config = reduce(dict_merge, configs)
 
489
 
 
490
    series = deploy_config.get('series')
 
491
    charms = deploy_config.get('services',{})
 
492
    overrides = deploy_config.get('overrides',{})
 
493
    if 'relations' in deploy_config:
 
494
        relations = relations_json_to_tuples(deploy_config["relations"])
506
495
    else:
507
 
        series = config[deployment]['series']
508
 
        charms = config[deployment]['services']
509
 
        if 'relations' in config[deployment]:
510
 
            relations = relations_json_to_tuples(config[deployment]["relations"])
 
496
        relations = {}
511
497
 
512
498
    return (series, charms, relations, overrides)