Пример #1
0
def icap(ctx, name, protocol):
    """Connect to an ICAP server"""
    if protocol == 'console':
        info = consume_task(ctx.obj.vlab_api,
                            endpoint='/api/2/inf/icap',
                            message='Looking up connection info for {}'.format(name),
                            method='GET').json()
        if not info['content'].get(name, None):
            error = 'No ICAP VM named {} found'.format(name)
            raise click.ClickException(error)
        else:
            vm_moid = info['content'][name].get('moid', 'n/a')
        conn = Connectorizer(ctx.obj.vlab_config, gateway_ip='n/a')
        conn.console(vm_moid)
    else:
        target_port = get_protocol_port('icap', protocol)
        with Spinner('Lookin up connection information for {}'.format(name)):
            resp = ctx.obj.vlab_api.get('/api/1/ipam/portmap', params={'name' : name, 'target_port' : target_port}).json()
            try:
                conn_port = list(resp['content']['ports'].keys())[0]
            except Exception as doh:
                ctx.obj.log.debug(doh, exc_info=True)
                conn_port = None
        if not conn_port:
            error = 'No mapping rule for {} to {} exists'.format(protocol, name)
            raise click.ClickException(error)

        conn = Connectorizer(ctx.obj.vlab_config, resp['content']['gateway_ip'])
        conn.rdp(port=conn_port)
Пример #2
0
def deployment(ctx, name, protocol, user, password):
    """Connect to a deployed machine"""
    if protocol == 'console':
        info = consume_task(
            ctx.obj.vlab_api,
            endpoint='/api/2/inf/deployment',
            message='Looking up connection info for {}'.format(name),
            method='GET').json()
        if not info['content'].get(name, None):
            error = 'No Deployment VM named {} found'.format(name)
            raise click.ClickException(error)
        else:
            vm_moid = info['content'][name].get('moid', 'n/a')
        conn = Connectorizer(ctx.obj.vlab_config, gateway_ip='n/a')
        conn.console(vm_moid)
    else:
        with Spinner('Looking up connection information for {}'.format(name)):
            data = ctx.obj.vlab_api.get('/api/1/ipam/portmap',
                                        params={
                                            'name': name
                                        }).json()['content']
            ports = data['ports']
            port_map = {ports[x]['target_port']: x for x in ports.keys()}
            try:
                conn_port = determine_port(protocol, port_map)
            except Exception as doh:
                ctx.obj.log.debug(doh, exc_info=True)
                conn_port = None
        if not conn_port:
            error = 'No mapping rule for {} to {} exists'.format(
                protocol, name)
            raise click.ClickException(error)

        if password:
            password_value = getpass.getpass('Password for {}: '.format(user))
            conn = Connectorizer(ctx.obj.vlab_config,
                                 data['gateway_ip'],
                                 user=user,
                                 password=password_value)
        else:
            conn = Connectorizer(ctx.obj.vlab_config,
                                 data['gateway_ip'],
                                 user=user)
        if protocol == 'ssh':
            conn.ssh(port=conn_port)
        elif protocol == 'scp':
            conn.scp(port=conn_port)
        elif protocol == 'rdp':
            conn.rdp(port=conn_port)
        elif protocol == 'https':
            conn.https(port=conn_port)
        else:
            error = 'Unexpected protocol requested: {}'.format(protocol)
            raise RuntimeError(error)
Пример #3
0
def dns(ctx, name, protocol, user, password):
    """Connect to a DNS server"""
    if protocol == 'console':
        info = consume_task(
            ctx.obj.vlab_api,
            endpoint='/api/2/inf/dns',
            message='Looking up connection info for {}'.format(name),
            method='GET').json()
        if not info['content'].get(name, None):
            error = 'No DNS server named {} found'.format(name)
            raise click.ClickException(error)
        else:
            vm_moid = info['content'][name].get('moid', 'n/a')
        conn = Connectorizer(ctx.obj.vlab_config, gateway_ip='n/a')
        conn.console(vm_moid)
    else:
        target_port = get_protocol_port('dns', protocol)
        with Spinner('Lookin up connection information for {}'.format(name)):
            resp = ctx.obj.vlab_api.get('/api/1/ipam/portmap',
                                        params={
                                            'name': name,
                                            'target_port': target_port
                                        }).json()
            try:
                conn_port = list(resp['content']['ports'].keys())[0]
            except Exception as doh:
                ctx.obj.log.debug(doh, exc_info=True)
                conn_port = None
        if not conn_port:
            error = 'No mapping rule for {} to {} exists'.format(
                protocol, name)
            raise click.ClickException(error)

        if password:
            password_value = getpass.getpass('Password for {}: '.format(user))
            conn = Connectorizer(ctx.obj.vlab_config,
                                 resp['content']['gateway_ip'],
                                 user=user,
                                 password=password_value)
        else:
            conn = Connectorizer(ctx.obj.vlab_config,
                                 resp['content']['gateway_ip'],
                                 user=user)
        if protocol == 'ssh':
            conn.ssh(port=conn_port)
        elif protocol == 'scp':
            conn.scp(port=conn_port)
        elif protocol == 'rdp':
            conn.rdp(port=conn_port)
        else:
            error = 'Unexpected protocol requested: {}'.format(protocol)
            raise RuntimeError(error)