Example #1
0
    def deploy(self, compose_definition):
        '''
        Calls docker-compose with the contents of a compose file as input.
        '''

        # save definition in file
        fs_util.create_dir_dont_complain(self.compose_path)
        definition_file = os.path.join(self.compose_path, 'docker-cluster.yml')
        with open(definition_file, 'w') as df:
            df.write(compose_definition)

        # call docker-compose command, should pick up the created file
        # note: apparently, using docker-compose.yml and removing '-f' fails to
        # to acknowledge the --force-recreate option
        #
        cmd = 'docker-compose --no-ansi -f docker-cluster.yml up -d --force-recreate'
        run = runit.execute(cmd, cwd=self.compose_path, env=os.environ)

        # always show the output of the docker-compose call
        print(run[1])
        print(run[0])

        if run[2] or run[0]:
            # return code is different than 0, something went wrong
            raise ComposeFailure('docker-compose command failed, check output')
Example #2
0
def ensure_docker_compose():
    '''
    Looks for docker_compose in PATH. If the executable is not found, it will try to download
    it from the docker-compose repository. Exits normally if successful, raises ValueError if
    the executable cannot be downloaded.
    '''
    log = logger.logger_for_me(ensure_docker_package)

    # check for docker-compose in PATH
    docker_compose_path = distutils.spawn.find_executable('docker-compose')

    if not docker_compose_path:
        # prepare to download it at user HOME
        server_url = docker_compose_url()
        output_dir = os.path.expanduser('~/.dcluster/bin')
        fs.create_dir_dont_complain(output_dir)  # ensure download dir
        docker_compose_path = os.path.join(output_dir, 'docker-compose')

        # download if this fails then give up
        download.download_to_file_strict(server_url, docker_compose_path)

        # make it executable
        st = os.stat(docker_compose_path)
        os.chmod(docker_compose_path, st.st_mode | stat.S_IEXEC)

    # check for docker-compose version
    cmd = 'docker-compose -v'
    stdout, stderr, rc = runit.execute(cmd, env=os.environ)

    if 'docker-compose' not in stdout or rc != 0:
        err_msg = 'Not able to use docker-compose (rc={}):\n{}\n{}'
        raise ValueError(err_msg.format(rc, stdout, stderr))

    log.info('Found docker-compose at: {}'.format(docker_compose_path))
Example #3
0
def create_inventory(cluster_specs, inventory_workpath):
    fs_util.create_dir_dont_complain(inventory_workpath)
    ansible_inventory = AnsibleInventory(cluster_specs)

    inventory_file = os.path.join(inventory_workpath, 'inventory.yml')
    ansible_inventory.to_yaml(inventory_file)
    return (ansible_inventory, inventory_file)
Example #4
0
def run_playbook(cluster_name, playbook_name, inventory_file, extra_vars=None):

    # find our playbook: it is inside a directory <playbook_name> in some paths, see dansible_config
    playbook_path = dansible_config.find_playbook_path(playbook_name)

    # 'install' the playbook in dcluster working directory, oif it wasn't there already
    dansible_home = dansible_config.installed_playbook_path()
    playbook_target = os.path.join(dansible_home, playbook_name)

    if playbook_path != playbook_target:
        fs_util.create_dir_dont_complain(playbook_target)
        fs_util.copytree(playbook_path, playbook_target)

    # always run from working directory
    playbook_filename = 'playbook.yml'
    playbook_file = os.path.join(playbook_target, playbook_filename)
    execute_playbook(playbook_file, inventory_file, extra_vars)
Example #5
0
def get_workpath(args):
    '''
    Set the work path of dcluster. By default, it is set by the configuration (paths:work),
    but it can be overridden by the user using the --workpath optional variable.
    '''
    log = logger.logger_for_me(get_workpath)
    if args.workpath is not None:
        workpath = args.workpath
    else:
        # work_path_with_shell_variables = main_config.paths('work')
        # log.debug('workpath shell %s' % work_path_with_shell_variables)
        # workpath = fs_util.evaluate_shell_path(work_path_with_shell_variables)
        workpath = main_config.paths('work')

    # create the directory, it may not exist but we need it now
    log.debug('workpath %s' % workpath)
    fs_util.create_dir_dont_complain(workpath)
    return workpath
Example #6
0
def install_production_config(sandbox_dir):
    '''
    Production environment is obtained by reading both config/common.yml and config/prod.yml
    '''

    # create from sources
    production_config = main_config.create_prod_config()

    (config_deploy_dir, config_deploy_filename) = os.path.split(CONFIG_FILE)

    # save to production config file, note the buildroot
    # join will not work, CONFIG_DIR is abs path
    sandboxed_config_dir = sandbox_dir + config_deploy_dir
    fs_util.create_dir_dont_complain(sandboxed_config_dir)

    # save YAML fle
    sandboxed_config_file = os.path.join(sandboxed_config_dir, config_deploy_filename)
    with open(sandboxed_config_file, 'w') as scf:
        # https://stackoverflow.com/a/47940875
        yaml.dump(production_config, scf, default_flow_style=False)

    return sandboxed_config_file