Esempio n. 1
0
def handle_updates(vlab_api, vlab_config, skip_update_check):
    """Check for an updated CLI, and prompt the user to download if available.

    :Returns: None

    :param vlab_api: An established HTTP/S connect to the vLab server
    :type vlab_api: vlab_cli.lib.api.vLabApi

    :param vlab_config: The user's config
    :type vlab_config: configparser.ConfigParser

    :param skip_update_check: A chicken switch to avoid SPAMing power users
    :type skip_update_check: Boolean
    """
    if skip_update_check:
        return
    page = vlab_api.get('https://vlab.emc.com/getting_started.html').content
    soup = BeautifulSoup(page, features="html.parser")
    site_version = ''
    for a in soup.find_all('a', href=True):
        url = a['href']
        package = os.path.basename(url)
        if package.startswith('vlab-cli'):
            # example package name: vlab-cli-2020.5.21-amd64.msi
            site_version = package.split('-')[2]
            break

    current_version = version.__version__
    if site_version and site_version != current_version:
        question = "A new version of vLab CLI is available. Download now? [Y/n]"
        answer = prompt(question, boolean=True, boolean_default=True)
        if answer:
            download_url = build_url(vlab_api.server, url)
            conn = Connectorizer(vlab_config, gateway_ip='n/a')
            conn.https(port=443, url=download_url)
Esempio n. 2
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)
Esempio n. 3
0
def router(ctx, name, protocol):
    """Connect to the console of a network router"""
    # Router only supports console access
    if protocol == 'console':
        info = consume_task(ctx.obj.vlab_api,
                            endpoint='/api/2/inf/router',
                            message='Looking up connection info for {}'.format(name),
                            method='GET').json()
        if not info['content'].get(name, None):
            error = 'No Router 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:
        error = 'Unexpected connection protocol supplied'
        raise click.ClickException(error)
Esempio n. 4
0
def winserver(ctx, name, protocol, user, password):
    """Connect to a Microsoft Server instance"""
    if protocol == 'console':
        info = consume_task(
            ctx.obj.vlab_api,
            endpoint='/api/2/inf/winserver',
            message='Looking up connection info for {}'.format(name),
            method='GET').json()
        if not info['content'].get(name, None):
            error = 'No Windows 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('winserver', 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)
        conn.rdp(port=conn_port)
Esempio n. 5
0
def dd(ctx, name, protocol, user, password):
    """Connect to an Data Domain server"""
    if protocol == 'console':
        info = consume_task(
            ctx.obj.vlab_api,
            endpoint='/api/2/inf/data-domain',
            message='Looking up connection info for {}'.format(name),
            method='GET').json()
        if not info['content'].get(name, None):
            error = 'No Data Domain 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('datadomain', 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 == 'https':
            conn.https(port=conn_port)
        elif protocol == 'scp':
            conn.scp(port=conn_port)
        else:
            error = 'Unexpected protocol requested: {}'.format(protocol)
            raise RuntimeError(error)
Esempio n. 6
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)
Esempio n. 7
0
def claritynow(ctx, name, protocol):
    """Connect to a ClarityNow instance"""
    if protocol == 'console':
        info = consume_task(ctx.obj.vlab_api,
                            endpoint='/api/2/inf/claritynow',
                            message='Looking up connection info for {}'.format(name),
                            method='GET').json()
        if not info['content'].get(name, None):
            error = 'No ClarityNow 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('claritynow', 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'])
        if protocol == 'ssh':
            conn.ssh(port=conn_port)
        elif protocol == 'https':
            conn.https(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)