示例#1
0
def push(ctx, tag):
    """
    Push image to remote registry.
    """
    config = ctx.obj['config']
    service = config['service']

    branch, commit, clean = utils.get_git_info()

    build_tag = utils.get_build_tag(service=service,
                                    branch=branch,
                                    commit=commit,
                                    tag=tag)

    remote = '{gcloud_registry}/{gcloud_project}'.format(
        gcloud_registry=config['gcloud']['registry'],
        gcloud_project=config['gcloud']['project'])

    click.secho("Pushing '{build_tag}' to '{remote}'".format(
        build_tag=build_tag, remote=remote),
                fg='cyan')
    click.confirm('Do you want to continue?', abort=True)

    # Add remote tag to local image
    remote_tag = '{remote}/{build_tag}'.format(remote=remote,
                                               build_tag=build_tag)
    cmd = 'docker tag {build_tag} {remote_tag}'.format(build_tag=build_tag,
                                                       remote_tag=remote_tag)
    utils.run_local(cmd, verbose=True)

    # Push image to remote server
    cmd = 'gcloud docker -- push {remote_tag}'.format(remote_tag=remote_tag)
    utils.run_local(cmd, verbose=True)

    click.echo('Done!')
示例#2
0
def build(ctx, force, tag):
    """
    Build image from current branch.
    """
    config = ctx.obj['config']
    verbose = ctx.obj['verbose']
    service = config['service']

    branch, commit, clean = utils.get_git_info()
    build_tag = utils.get_build_tag(service=service,
                                    branch=branch,
                                    commit=commit,
                                    tag=tag)

    # Repository needs to be clean to build. Otherwise the image can contain
    # uncommited changes.
    if not clean and not force:
        click.secho(
            "Repository is not clean. Clean up or use `--force` "
            "if you know what you're doing.",
            fg='yellow')
        ctx.abort()

    click.secho("Building image '{build_tag}'".format(build_tag=build_tag),
                fg='cyan')
    click.confirm('Do you want to continue?', abort=True)

    # Build and tag local image
    cmd = "docker build --tag {build_tag} source {quiet}".format(
        build_tag=build_tag, quiet='--quiet=true' if not verbose else '')
    utils.run_local(cmd, verbose=True)

    click.echo('Done!')
示例#3
0
def set_credentials(ctx):
    """
    Setting the right gcloud cluster credentials for kubectl.
    """
    config = ctx.obj['config']
    gcloud_project = config['gcloud']['project']
    gcloud_cluster = config['gcloud']['cluster']
    cmd = ('gcloud container clusters get-credentials {gcloud_cluster} '
           '--project={gcloud_project}').format(gcloud_cluster=gcloud_cluster,
                                                gcloud_project=gcloud_project)
    utils.run_local(cmd)
示例#4
0
def config(ctx, environment):
    """
    Opens config map edit from kubectl
    """
    config = ctx.obj['config']
    service = config['service']
    ctx.invoke(set_credentials)

    config_name = utils.get_config_name(service=service)
    cmd = 'kubectl edit configmap {name} --namespace={environment}'.format(
        name=config_name, environment=environment)
    utils.run_local(cmd, output=False, verbose=True)
示例#5
0
def latest(ctx, branch):
    config = ctx.obj['config']
    repo = config['repository']
    cmd = 'git ls-remote {repo} {branch}'.format(repo=repo, branch=branch)
    commit = utils.run_local(cmd, verbose=True)
    if commit:
        short = commit[:7]
        click.secho(
            "Latest commit on '{branch}' available for deploy is '{commit}'".
            format(branch=branch, commit=short))
        return short
    return None
示例#6
0
def init(ctx):
    """
    Login to gcloud and set project.

    TODO (silvan): how to handle service account for e.g. CI?
    """
    config = ctx.obj['config']
    gcloud_project = config['gcloud']['project']

    if click.confirm(
            'Do you want to (re)login to gcloud too? (will open browser)'):
        utils.run_local('gcloud auth login')
        utils.run_local('gcloud auth application-default login')

    utils.run_local('gcloud config set project {}'.format(gcloud_project))

    click.echo('Done!')
示例#7
0
def apply(ctx, environment, dry_run):
    """
    Create a new environment.
    """
    clean, config_keys, secret_keys = ctx.invoke(inspect_deployments)
    if not clean:
        ctx.abort()

    config = ctx.obj['config']
    service = config['service']
    ctx.invoke(set_credentials)

    # Create namespace if not exists
    if not type_exists(type='namespace', name=environment):
        utils.run_local('kubectl create namespace {}'.format(environment),
                        verbose=True,
                        execute=not dry_run)

    # Create configmap if not exists
    config_name = utils.get_config_name(service=service)
    if not type_exists(
            type='configmap', name=config_name, namespace=environment):
        cmd = 'kubectl create configmap {name} --namespace={environment}'.format(
            name=config_name, environment=environment)
        utils.run_local(cmd, verbose=True, execute=not dry_run)

    # Apply k8s files
    deployments = config['deployments']
    for deployment in deployments:
        for to_apply in deployment['apply']:
            cmd = 'kubectl apply -f {to_apply} --namespace={environment}'.format(
                environment=environment, to_apply=to_apply)
            utils.run_local(cmd, verbose=True, execute=not dry_run)

    if len(config_keys) > 0:
        click.secho("Don't forget to update config with "
                    "`ueli config {environment}`: \n\n{keys}\n".format(
                        environment=environment, keys='\n'.join(config_keys)),
                    fg='cyan')

    if len(secret_keys) > 0:
        click.secho("Don't forget to update secrets: \n\n{keys}\n".format(
            keys='\n'.join(secret_keys)),
                    fg='cyan')

    click.echo('Done!')
示例#8
0
def list_type(type, namespace=None):
    namespace_option = '--namespace={}'.format(namespace) if namespace else ''
    cmd = 'kubectl get {type} -o name {namespace_option}'.format(
        type=type, namespace_option=namespace_option)
    return utils.run_local(cmd).split('\n')
示例#9
0
def delete_images():
    """
    Delete all local docker images.
    """
    utils.run_local('docker rmi -f $(docker images -q)', verbose=True)