Пример #1
0
def snapshots_purge():
    """
    Delete all snapshots
    """
    click.confirm('This will delete all snapshots from the local registry', abort=True)

    docker = docker_client()

    for image in docker.images():
        snapshots = [
            repotag
            for repotag in image.get('RepoTags', [])
            if repotag.startswith('art/')
        ]

        if not snapshots:
            continue

        click.echo('')

        for snapshot in snapshots:
            click.echo('Deleting %s ... ' % snapshot, nl=False)
            try:
                docker.remove_image(snapshot)
                click.secho('DONE', fg='green')
            except APIError as e:
                click.secho('FAILED [%s]' % e.explanation.decode('utf-8'), fr='red')
Пример #2
0
def snapshots_rm(image):
    """
    Remove a specific snapshot
    """
    docker = docker_client()

    image, image_name = _resolve_image(docker, image)

    click.echo('Deleting %s ... ' % image_name, nl=False)
    try:
        docker.remove_image(image_name)
        click.secho('DONE', fg='green')
    except APIError as e:
        click.secho('FAILED [%s]' % e.explanation.decode('utf-8'), fr='red')
Пример #3
0
def snapshots_view(image):
    """
    Display the output of the ansible play that was run on this snapshot
    """
    docker = docker_client()

    image, image_name = _resolve_image(docker, image)

    try:
        res = docker.inspect_image(image=image_name)
    except APIError as e:
        click.secho('error: %s' % e.explanation.decode('utf-8'), fg='red', err=True)
        sys.exit(1)

    try:
        play = json.loads(res.get('Comment'))
    except ValueError as e:
        click.secho('error: %s' % str(e), fr='red')
        sys.exit(1)

    repo, tag = image_name.split(':')
    repo = repo.split('.')
    host = repo.pop()

    TestFramework.print_header('PLAY [%s]' % host)

    for task in play['tasks']:
        TestFramework.print_header('TASK: [%s]' % task['name'])
        if task['state'] == 'ok':
            if 'changed' in task['res'] and task['res']['changed'] is True:
                click.secho('changed: [%s]' % host, fg='yellow')
            else:
                click.secho('ok: [%s]' % host, fg='green')
        elif task['state'] == 'skipped':
            click.secho('skipped: [%s]' % host, fg='cyan')
        elif task['state'] == 'failed':
            click.secho('failed: [%s]\n%s' % (host, task['res']), fg='red')

    TestFramework.print_header('PLAY RECAP [%s]' % image_name)

    click.secho('{host:<27s}: {ok} {changed} {unreachable} {failed}\n'.format(
        host=click.style(host, fg='yellow'),
        ok=click.style('ok=%-4d' % play['stats']['ok'], fg='green'),
        changed=click.style('changed=%-4d' % play['stats']['changed'], fg='yellow'),
        unreachable='unreachable=%-4d' % play['stats']['unreachable'],
        failed=click.style('failed=%-4d' % play['stats']['failed'], fg='red'),
    ))
Пример #4
0
def test(role,
         config,
         # path args
         roles_path, library_path, plugins_action_path,
         plugins_filter_path, plugins_lookup_path,
         # ansible-playbook args
         extra_vars, limit, skip_tags, tags, verbosity,
         # misc
         ansible_version, privileged, cache, save):
    """
    Run tests

    ROLE can be either be a local path, a git repository or an ansible-galaxy
    role name.
    """
    with ContainerManager(docker_client()) as docker:
        ansible_paths = {
            'roles': roles_path,
            'library': library_path,
            'plugins': {
                'action': plugins_action_path,
                'filter': plugins_filter_path,
                'lookup': plugins_lookup_path,
            }
        }

        _load_config(ansible_paths, config)

        framework = TestFramework(docker, role, ansible_paths,
                                  ansible_version)
        res = framework.run(
            extra_vars=extra_vars,
            limit=limit,
            skip_tags=skip_tags,
            tags=tags,
            verbosity=verbosity,
            privileged=privileged,
            cache=cache,
            save=save
        )

        if res != 0 and save != 'failed':
            click.secho('''
info: some of the tests have failed. If you wish to inspect the failed
      containers, rerun the command while adding the --save=failed flag
      to your command line.''', fg='blue')
    sys.exit(res)
Пример #5
0
def snapshots_list(filter):
    """
    List all snapshots and whether they failed or succeeded
    """
    docker = docker_client()

    output_fmt = '{role_name:<24s}{container:<20s}{status:<16s}{date:<24s}{image_name}'
    click.echo(output_fmt.format(
        role_name='ROLE NAME',
        container='CONTAINER',
        status='STATUS',
        date='DATE',
        image_name='IMAGE NAME'
    ))
    for image in docker.images():
        snapshots = [
            repotag[4:]
            for repotag in image.get('RepoTags', [])
            if repotag.startswith('art/')
        ]

        if not snapshots:
            continue

        for snapshot in snapshots:
            repo, tag = snapshot.split(':')
            role_name = repo.split('.')
            container = role_name.pop()
            role_name = '.'.join(role_name)
            state, date = tag.split('-')

            if filter and role_name != filter:
                continue

            click.echo(output_fmt.format(
                role_name=role_name,
                container=container,
                status=state,
                date=datetime.datetime.fromtimestamp(int(date)).isoformat(),
                image_name=snapshot
            ))