~pedronis/adt-continuous-deployer/ols-tweak

« back to all changes in this revision

Viewing changes to ci_automation/juju.py

  • Committer: Ubuntu CI Bot
  • Author(s): Thomi Richards
  • Date: 2015-05-27 02:26:18 UTC
  • mfrom: (52.1.7 trunk-make-reaper-nicer)
  • Revision ID: ubuntu_ci_bot-20150527022618-a8wd07xtse5ek2hb
Make reaper.py destroy services before destroying the environment. [r=Paul Larson]

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
from . import nova
27
27
 
28
28
 
 
29
def destroy_all_services(name, base):
 
30
    """Destroy all services for a particular juju environment.
 
31
 
 
32
    This is necessary since destroying the environment does not shut down
 
33
    services cleanly. This can result in (for example) stale rabbit
 
34
    connections.
 
35
    """
 
36
    # This can take a while, so let's limit the damage to 5 minutes:
 
37
    timeout = 60 * 5
 
38
    env_dir = os.path.join(base, name)
 
39
    env = os.environ.copy()
 
40
    env['JUJU_HOME'] = env_dir
 
41
    try:
 
42
        for service in get_active_services(name, base):
 
43
            subprocess.call(
 
44
                ['juju', 'destroy-service', service],
 
45
                env=env
 
46
            )
 
47
        start_time = time.time()
 
48
        while time.time() - start_time < timeout:
 
49
            services = get_active_services(name, base)
 
50
            if len(services) == 0:
 
51
                return
 
52
            time.sleep(29)
 
53
    except subprocess.CalledProcessError as e:
 
54
        print("juju status failed: %s" % e)
 
55
        return
 
56
 
 
57
 
 
58
def get_active_services(name, base):
 
59
    """Get a list of the services currently active.
 
60
 
 
61
    Ignores ksplice, landscape and basenode.
 
62
 
 
63
    """
 
64
    ignored_services = (
 
65
        'ksplice',
 
66
        'landscape-client',
 
67
        'ubuntu-basenode',
 
68
    )
 
69
    env_dir = os.path.join(base, name)
 
70
    env = os.environ.copy()
 
71
    env['JUJU_HOME'] = env_dir
 
72
 
 
73
    args = ['juju', 'status']
 
74
    juju_status = yaml.load(
 
75
        subprocess.check_output(args, env=env)
 
76
    )
 
77
    return [s for s in juju_status['services'] if s not in ignored_services]
 
78
 
 
79
 
29
80
def destroy_environment(name, base):
30
81
    # We can safely destroy the existing environment, as the services are
31
82
    # promised to be idempotent.