Exemplo n.º 1
0
def shell(ctx, id):
    CalledProcessError = subprocess.CalledProcessError
    sh = '/bin/bash'
    env = {
        'SHELL': sh,
    }

    def _envargs():
        pairs = ["%s='%s'" % (k, env[k]) for k in env.keys()]
        return ' '.join(pairs)

    cmdlist = [
        "env {envargs} sudo -n -p 'Password for %p' login -f {user}",
        "su -c 'login -f {user}'",
        "env {envargs} sudo -p 'Password for %u' -u {user} -",
    ]
    session = utils.Session()
    try:
        bunit = session.query(BuildUnit).get(id)
        user = bunit.build_user
        for c in cmdlist:
            r = 0
            cmd = shlex.split(c.format(user=user, envargs=_envargs()))
            r = subprocess.call(cmd)
            if r == 0:
                break
        if r != 0:
            msg = ('Could not spawn shell:'
                   ' "{cmd}" returned with non-zero status {ret}'.format(
                       cmd=' '.join(cmd), ret=r))
            click.echo(msg, err=True)
    except sa.exc.SQLAlchemyError:
        click.echo("No such unit with id '%s'" % id, err=True)
Exemplo n.º 2
0
def show(ctx, format, id):
    def _get_script_dir(bunit):
        bdir = Path(bunit.build_dir)
        return (bdir / 'chef-bcpc' / 'bootstrap/vagrant_scripts').as_posix()

    session = utils.Session()
    try:
        bunit = session.query(BuildUnit).get(id)
        if not bunit:
            raise NotFoundError(id)
        sdir = _get_script_dir(bunit)
        out = subprocess.check_output('./dump_config.sh',
                                      cwd=sdir,
                                      universal_newlines=True)
        out = '\n'.join(filter(lambda x: x.strip(), out.split('\n')))

        def _mk_json(out):
            lines = sorted(out.split('\n'))
            try:
                d = {}
                for l in lines:
                    parts = l.split('=')
                    key = parts[0]
                    value = ''.join(parts[1:])
                    d[key] = value
                return json.dumps(d, indent=2, sort_keys=True)
            except IndexError:
                raise click.ClickException('Malformed configuration.')

        if format == 'json':
            out = _mk_json(out)
        click.echo(out)
    except sa.exc.SQLAlchemyError as e:
        click.echo('Something went wrong: %s' % e, err=True)
        raise click.Abort
Exemplo n.º 3
0
def show(ctx, id, format):
    formatters = {
        'json': BuildUnitShowJSONFormat,
        'table': BuildUnitShowTableFormat,
    }
    session = utils.Session()
    try:
        bunit = session.query(BuildUnit).get(id)
        if bunit is None:
            msg = "No such unit with id '%s'" % id
            raise click.ClickException(msg)
        formatter = formatters[format]
        click.echo(formatter.format(bunit))
    except sa.exc.SQLAlchemyError as e:
        raise click.ClickException(e)
    except KeyError:
        raise NotImplementedError('%s format' % format)
Exemplo n.º 4
0
def list(ctx, format, failed, long, build_user, build_state):
    import builtins
    formatters = {
        'json': BuildUnitListingJSONFormat,
        'table': BuildUnitListingTableFormat,
        'value': BuildUnitListingValueFormat,
    }
    session = utils.Session()
    try:
        builds = session.query(BuildUnit)
        if build_user is not None:
            builds = builds.filter(BuildUnit.build_user == build_user)

        # Check for any "failed" states
        if failed:
            def failure_state(estate):
                m = re.match('^failed(:[^ ]+)?$', estate.value)
                return m

            failed_states = filter(failure_state,
                                   BuildStateEnum.__members__.values())
            q = [BuildUnit.build_state == state for state in failed_states]
            builds = builds.filter(sa.or_(*q))
        elif build_state is not None:
            builds = builds.filter(
                BuildUnit.build_state == BuildStateEnum(build_state)
            )

        if not builtins.list(builds):
            return
        formatter = formatters[format]
        click.echo(formatter.format(sorted(builds), long=long))
    except sa.exc.SQLAlchemyError as e:
        click.echo('Something went wrong: %s' % e, err=True)
        raise click.Abort
    except KeyError:
        raise NotImplementedError('%s format' % format)
Exemplo n.º 5
0
from bcpc_build.db import utils
from bcpc_build.build_unit import BuildUnit

id = 'i'
session = utils.Session()
x = session.query(BuildUnit).get(id)
print(x)