~nskaggs/juju-ci-tools/add-assess-terms

« back to all changes in this revision

Viewing changes to deploy_stack.py

  • Committer: Aaron Bentley
  • Date: 2014-09-20 00:05:37 UTC
  • mto: This revision was merged to the branch mainline in revision 669.
  • Revision ID: aaron.bentley@canonical.com-20140920000537-wek1ioeymqut6vhk
Port bootstrap_from_env to EnvJujuClient, add unit tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
import subprocess
15
15
import sys
16
16
from time import sleep
17
 
import yaml
18
17
 
19
18
from jujuconfig import (
20
 
    get_environments_path,
21
19
    get_jenv_path,
22
20
    get_juju_home,
23
21
)
24
22
from jujupy import (
 
23
    bootstrap_from_env,
25
24
    Environment,
 
25
    get_local_root,
26
26
)
27
27
from utility import (
28
28
    PortTimeoutError,
29
29
    scoped_environ,
30
 
    temp_dir,
31
30
    until_timeout,
32
31
    wait_for_port,
33
32
)
216
215
    dump_euca_console(host_id, directory)
217
216
 
218
217
 
219
 
def get_local_root(juju_home, env):
220
 
    return os.path.join(juju_home, env.environment)
221
 
 
222
 
 
223
218
def copy_local_logs(directory, env):
224
219
    local = get_local_root(get_juju_home(), env)
225
220
    log_names = [os.path.join(local, 'cloud-init-output.log')]
299
294
    return (machine_id for machine_id, name in description)
300
295
 
301
296
 
302
 
def check_free_disk_space(path, required, purpose):
303
 
    df_result = subprocess.check_output(["df", "-k", path])
304
 
    df_result = df_result.split('\n')[1]
305
 
    df_result = re.split(' +', df_result)
306
 
    available = int(df_result[3])
307
 
    if available < required:
308
 
        message = (
309
 
            "Warning: Probably not enough disk space available for\n"
310
 
            "%(purpose)s in directory %(path)s,\n"
311
 
            "mount point %(mount)s\n"
312
 
            "required: %(required)skB, available: %(available)skB."
313
 
            )
314
 
        print(message % {
315
 
            'path': path, 'mount': df_result[5], 'required': required,
316
 
            'available': available, 'purpose': purpose
317
 
            })
318
 
 
319
 
 
320
 
def bootstrap_from_env(juju_home, env):
321
 
    # Always bootstrap a matching environment.
322
 
    env.config['agent-version'] = env.get_matching_agent_version()
323
 
    if env.config['type'] == 'local':
324
 
        env.config.setdefault('root-dir', get_local_root(juju_home, env))
325
 
    new_config = {'environments': {env.environment: env.config}}
326
 
    jenv_path = get_jenv_path(juju_home, env.environment)
327
 
    with temp_dir(juju_home) as temp_juju_home:
328
 
        if os.path.lexists(jenv_path):
329
 
            raise Exception('%s already exists!' % jenv_path)
330
 
        new_jenv_path = get_jenv_path(temp_juju_home, env.environment)
331
 
        if env.local:
332
 
            # MongoDB requires a lot of free disk space, and the only
333
 
            # visible error message is from "juju bootstrap":
334
 
            # "cannot initiate replication set" if disk space is low.
335
 
            # What "low" exactly means, is unclear, but 8GB should be
336
 
            # enough.
337
 
            check_free_disk_space(temp_juju_home, 8000000, "MongoDB files")
338
 
            if env.kvm:
339
 
                check_free_disk_space(
340
 
                    "/var/lib/uvtool/libvirt/images", 2000000,
341
 
                    "KVM disk files")
342
 
            else:
343
 
                check_free_disk_space(
344
 
                    "/var/lib/lxc", 2000000, "LXC containers")
345
 
        # Create a symlink to allow access while bootstrapping, and to reduce
346
 
        # races.  Can't use a hard link because jenv doesn't exist until
347
 
        # partway through bootstrap.
348
 
        try:
349
 
            os.mkdir(os.path.join(juju_home, 'environments'))
350
 
        except OSError as e:
351
 
            if e.errno != errno.EEXIST:
352
 
                raise
353
 
        os.symlink(new_jenv_path, jenv_path)
354
 
        try:
355
 
            temp_environments = get_environments_path(temp_juju_home)
356
 
            with open(temp_environments, 'w') as config_file:
357
 
                yaml.safe_dump(new_config, config_file)
358
 
            with scoped_environ():
359
 
                os.environ['JUJU_HOME'] = temp_juju_home
360
 
                env.bootstrap()
361
 
            # replace symlink with file before deleting temp home.
362
 
            os.rename(new_jenv_path, jenv_path)
363
 
        except:
364
 
            os.unlink(jenv_path)
365
 
 
366
 
 
367
297
def deploy_job():
368
298
    from argparse import ArgumentParser
369
299
    parser = ArgumentParser('deploy_job')
459
389
                if e.errno != errno.ENOENT:
460
390
                    raise
461
391
            try:
462
 
                bootstrap_from_env(juju_home, env)
 
392
                bootstrap_from_env(juju_home, env.client.get_env_client(env))
463
393
            except:
464
394
                if host is not None:
465
395
                    dump_logs(env, host, log_dir, bootstrap_id)