Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
hubboss / boss / commands.py
Size: Mime:
import sys
import subprocess

from . import decorators
from . import config
from . import git_utils
from . import utils


def push(app, repository, tag):
    host = app.config.hosts.get('local')[0]
    return host.push(repository=repository, tag=tag)


def logs(app, host, container_id, follow=False):
    """
    docker --tls -H tcp://rs4.dckr.io:4243 logs eeebd95139d5
    """
    hostname = host
    host = utils.get_host(hostname, utils.flatten(app.config.hosts.itervalues()))

    if host is None:
        raise Exception("Could not find host: `{}`".format(hostname))

    command_line = "docker {host_config} logs {follow_opt} {container_id} ".format(
        host_config=host.get_cli_config(),
        container_id=container_id,
        follow_opt="-f" if follow else ""
    )

    # we want to exit immediatly after the subprocess is done and not return
    return subprocess.call(command_line, shell=True)


@decorators.parallel(utils.list_containers)
def ps(all=False, grep=None, show_restart=False, show_stopped=None, quiet=False):
    return dict(grep=grep, show_all=all, show_restart=show_restart, quiet=quiet)


@decorators.parallel(utils.rm_containers)
def rm(container_ids, force=False, **kw):
    return dict(force=force, container_ids=container_ids.split(","))


def split_container_ids(container_ids, **kw):
    return dict(container_ids=container_ids.split(","))

start = decorators.parallel(utils.start_containers)(split_container_ids)
stop = decorators.parallel(utils.stop_containers)(split_container_ids)
restart = decorators.parallel(utils.restart_containers)(split_container_ids)

@decorators.parallel(utils.get_images)
def images(name, **kw):
    return name


def run(app, hosts, image, container_type, number_containers, skip_pass, skip_tag_checkout, wait=False):
    # this function
    if container_type is None:
        raise Exception('`container_type` parameter is required')
    skip_pass = skip_pass
    skip_tag_checkout = skip_tag_checkout

    if not skip_pass:
        # update pass secrets
        utils.update_pass()

    if not skip_tag_checkout:
        # checkout git tag
        # TODO: checkout_tag should probably be in git_utils?
        config.checkout_tag(image)

    # run containers
    return _run(
        app=app,
        hosts=hosts,
        image=image,
        container_type=container_type,
        number_containers=number_containers,
        wait=wait
    )


@decorators.parallel(utils.run_containers)
def _run(image, container_type, number_containers, wait=False, **kw):
    return dict(
        image=image,
        container_type=container_type,
        number_containers=number_containers,
        wait=wait
    )


def container_exec(app, host, container_id, command):
    """
    docker --tls -H tcp://rs4.dckr.io:4243 exec -it eeebd95139d5 command
    docker --tls -H tcp://rs4.dckr.io:4243 exec -it 19ead861a047 bash
    """
    hostname = host
    host = utils.get_host(hostname, utils.flatten(app.config.hosts.itervalues()))

    if host is None:
        raise Exception("Could not find host: `{}`".format(hostname))

    command_line = "docker {host_config} exec -it {container_id} {command}".format(
        host_config=host.get_cli_config(),
        container_id=container_id,
        command=command
    )

    return subprocess.call(command_line, shell=True)


def publish(app, repository, bump):
    image_name = repository
    git_url = config.get_git_url(image_name)
    # TODO: config should come from app.config
    repo = git_utils.get_working_repo(git_url, config.config.home_dir)
    # we are now at "master"
    # bump verion
    bump_data = subprocess.check_output(['bumpversion {} --list'.format(bump)], cwd=repo.working_dir, shell=True)
    bump_data = bump_data.split("\n")
    current_version = bump_data[0].split("=")[1]
    new_version = bump_data[-2].split("=")[1]
    print("Bumping version from {} -> {}".format(current_version, new_version))
    origin = repo.remote()
    origin.push(tags=True)
    origin.push()

    # run docker build
    print("\n\nBuilding image {}:{}... ".format(image_name, new_version))
    process = subprocess.Popen(['docker build -t {}:{} .'.format(image_name, new_version)], cwd=repo.working_dir, stdout=subprocess.PIPE, shell=True)
    for c in iter(lambda: process.stdout.read(1), ''):
        sys.stdout.write(c)
    # run docker push
    print("\n\nPushing {}:{} to docker hub... \n".format(image_name, new_version))
    process = subprocess.Popen(['docker push {}:{}'.format(image_name, new_version)], cwd=repo.working_dir, stdout=subprocess.PIPE, shell=True)
    for c in iter(lambda: process.stdout.read(1), ''):
        sys.stdout.write(c)

    return ''