~andrewjbeach/juju-ci-tools/make-local-patcher

« back to all changes in this revision

Viewing changes to assess_spaces_subnets.py

  • Committer: Aaron Bentley
  • Date: 2015-12-15 16:03:14 UTC
  • mto: This revision was merged to the branch mainline in revision 1197.
  • Revision ID: aaron.bentley@canonical.com-20151215160314-zsy2kn45fefnqlht
Switch FakeJujuClient to raise on accessing juju_home.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
from __future__ import print_function
3
 
from argparse import ArgumentParser
 
3
import logging
4
4
import re
5
 
import sys
 
5
import yaml
6
6
from textwrap import dedent
 
7
from argparse import ArgumentParser
7
8
 
 
9
from jujuconfig import (
 
10
    get_juju_home,
 
11
)
 
12
from jujupy import (
 
13
    parse_new_state_server_from_error,
 
14
    temp_bootstrap_env,
 
15
)
8
16
from utility import (
 
17
    print_now,
9
18
    add_basic_testing_arguments,
10
 
    configure_logging,
11
19
)
12
20
from deploy_stack import (
13
 
    BootstrapManager,
 
21
    get_machine_dns_name,
 
22
    dump_env_logs
14
23
)
15
24
from assess_container_networking import (
16
 
    cleaned_bootstrap_context,
 
25
    clean_environment,
17
26
    ssh,
 
27
    get_client,
18
28
)
19
29
 
20
30
__metaclass__ = type
82
92
    :return: None. Raises exception on failure.
83
93
    """
84
94
    for space in sorted(network_config.keys()):
85
 
        client.add_space(space)
 
95
        client.juju('space create', space)
86
96
        for subnet in network_config[space]:
87
 
            client.add_subnet(subnet, space)
 
97
            client.juju('subnet add', (subnet, space))
88
98
 
89
99
    for name in sorted(charms_to_space.keys()):
90
100
        if 'charm' not in charms_to_space[name]:
100
110
    client.juju('add-unit', 'mediawiki')
101
111
    status = client.wait_for_started()
102
112
 
103
 
    spaces = client.list_space()
 
113
    spaces = yaml.load(client.get_juju_output('space list'))
104
114
 
105
115
    unit_priv_address = {}
106
116
    units_found = 0
153
163
    return (ipv4 & mask) == subnet
154
164
 
155
165
 
156
 
def main(argv=None):
157
 
    args = parse_args(argv)
158
 
    configure_logging(args.verbose)
159
 
    bs_manager = BootstrapManager.from_args(args)
160
 
    bs_manager.client.enable_feature('address-allocation')
161
 
    with cleaned_bootstrap_context(bs_manager, args) as ctx:
162
 
        assess_spaces_subnets(bs_manager.client)
163
 
    return ctx.return_code
 
166
def main():
 
167
    args = parse_args()
 
168
    client = get_client(args)
 
169
    juju_home = get_juju_home()
 
170
    bootstrap_host = None
 
171
    try:
 
172
        if args.clean_environment:
 
173
            try:
 
174
                if not clean_environment(client):
 
175
                    with temp_bootstrap_env(juju_home, client):
 
176
                        client.bootstrap(args.upload_tools)
 
177
            except Exception as e:
 
178
                logging.exception(e)
 
179
                client.destroy_environment()
 
180
                client = get_client(args)
 
181
                with temp_bootstrap_env(juju_home, client):
 
182
                    client.bootstrap(args.upload_tools)
 
183
        else:
 
184
            client.destroy_environment()
 
185
            client = get_client(args)
 
186
            with temp_bootstrap_env(juju_home, client):
 
187
                client.bootstrap(args.upload_tools)
 
188
 
 
189
        logging.info('Waiting for the bootstrap machine agent to start.')
 
190
        client.wait_for_started()
 
191
        bootstrap_host = get_machine_dns_name(client, 0)
 
192
 
 
193
        assess_spaces_subnets(client)
 
194
 
 
195
    except Exception as e:
 
196
        logging.exception(e)
 
197
        try:
 
198
            if bootstrap_host is None:
 
199
                bootstrap_host = parse_new_state_server_from_error(e)
 
200
        except Exception as e:
 
201
            print_now('exception while dumping logs:\n')
 
202
            logging.exception(e)
 
203
        exit(1)
 
204
    finally:
 
205
        if bootstrap_host is not None:
 
206
            dump_env_logs(client, bootstrap_host, args.logs)
 
207
        if not args.keep_env:
 
208
            if args.clean_environment:
 
209
                clean_environment(client)
 
210
            else:
 
211
                client.destroy_environment()
164
212
 
165
213
 
166
214
if __name__ == '__main__':
167
 
    sys.exit(main())
 
215
    main()