def get_execution_info(context):
    """Aggregate information about execution server


    :param reservation: context.reservation info
    :param api: cloudshell.api session

    :return: dict with aggregated info
    """

    import platform, socket

    reservation_info = {}
    hostname = socket.gethostname()
    reservation_info['Python version'] = platform.python_version()
    reservation_info['Operating System'] = platform.platform()
    reservation_info['Platform'] = platform.system()
    reservation_info['Hostname'] = hostname
    reservation_info['IP'] = socket.gethostbyname(hostname)

    try:
        reservation_info['ReservationID'] = get_reservation_context_attribute('reservation_id', context)
        reservation_info['Description'] = get_reservation_context_attribute('description', context)
        reservation_info['EnviromentName'] = get_reservation_context_attribute('environment_name', context)
        reservation_info['Username'] = get_reservation_context_attribute('owner_user', context)
    except:
        pass

    return reservation_info
예제 #2
0
    def get_execution_info(context):
        """Aggregate information about execution server.

        :param context: ResourceCommandContext
        :return: dict with aggregated info
        """
        reservation_info = {}
        hostname = socket.gethostname()
        reservation_info["Python version"] = platform.python_version()
        reservation_info["Operating System"] = platform.platform()
        reservation_info["Platform"] = platform.system()
        reservation_info["Hostname"] = hostname

        try:
            reservation_info["IP"] = socket.gethostbyname(hostname)
        except Exception:
            reservation_info["IP"] = "n/a"

        try:
            reservation_info[
                "ReservationID"] = get_reservation_context_attribute(
                    "reservation_id", context)
            reservation_info[
                "Description"] = get_reservation_context_attribute(
                    "description", context)
            reservation_info[
                "EnviromentName"] = get_reservation_context_attribute(
                    "environment_name", context)
            reservation_info["Username"] = get_reservation_context_attribute(
                "owner_user", context)
        except Exception:
            pass

        return reservation_info
예제 #3
0
def get_execution_info(context):
    """Aggregate information about execution server

    :param context: ResourceCommandContext
    :return: dict with aggregated info
    """

    reservation_info = {}
    hostname = socket.gethostname()
    reservation_info['Python version'] = platform.python_version()
    reservation_info['Operating System'] = platform.platform()
    reservation_info['Platform'] = platform.system()
    reservation_info['Hostname'] = hostname

    try:
        reservation_info['IP'] = socket.gethostbyname(hostname)
    except:
        reservation_info['IP'] = "n/a"

    try:
        reservation_info['ReservationID'] = get_reservation_context_attribute('reservation_id', context)
        reservation_info['Description'] = get_reservation_context_attribute('description', context)
        reservation_info['EnviromentName'] = get_reservation_context_attribute('environment_name', context)
        reservation_info['Username'] = get_reservation_context_attribute('owner_user', context)
    except:
        pass

    return reservation_info
def _open_new_api_connection(context):
    try:
        domain = get_reservation_context_attribute('domain', context)
    except:
        domain = 'Global'

    server_address = get_connectivity_context_attribute('server_address', context)
    api_port = get_connectivity_context_attribute('cloudshell_api_port', context)
    token = get_connectivity_context_attribute('admin_auth_token', context)

    api = CloudShellAPISession(server_address, port=api_port, token_id=token, domain=domain)
    return api
예제 #5
0
    def provision_vdb(self, target_db_name, souce_db_name, source_group_name,
                      target_group_name, vdb_type, timestamp, logger):

        host_name = "192.168.65.69"  # ENV to save <-- this needed to be recognized from the reservation !

        # api = get_api(context)
        from cloudshell.api.cloudshell_api import CloudShellAPISession
        from cloudshell.shell.core.context_utils import get_reservation_context_attribute
        reservation_id = get_reservation_context_attribute(
            'reservation_id', context)

        # todo: use api to decrypt password

        # find correct resource???
        # api.GetReservationDetails("992c9402-f2b2-4dfb-a7be-68ca01e9b8c0").ReservationDescription.Resources[0].LogicalResource.__dict__

        # import ipdb;ipdb.set_trace()

        engine = self._get_engine()

        all_groups = web.group.get_all(engine=engine)

        target_group = self._find_obj_by_name(collection=all_groups,
                                              name=target_group_name)
        source_group = self._find_obj_by_name(collection=all_groups,
                                              name=source_group_name)

        all_dbs = web.database.get_all(engine=engine,
                                       group=source_group.reference)
        source_database = self._find_obj_by_name(collection=all_dbs,
                                                 name=souce_db_name)

        all_envs = web.environment.get_all(engine=engine)
        env = self._find_obj_by_name(collection=all_envs, name=host_name)

        if vdb_type.lower() not in self._vdb_params_prepare_map:
            raise Exception(
                "Unknown VDB type {}. Supported types are {}".format(
                    vdb_type, self._vdb_params_prepare_map.keys()))

        prepare_vdb_params_handler = self._vdb_params_prepare_map[
            vdb_type.lower()]

        vdb_params = prepare_vdb_params_handler(
            engine=engine,
            env=env,
            group=target_group,
            db_name=target_db_name,
            source_database=source_database)

        # by default operation is a synchronous
        web.database.provision(engine, vdb_params)
    def configure_interface_mtu(self, context, interfaces):
        logger = inject.instance('logger')
        reservation_id = get_reservation_context_attribute('reservation_id', context)

        # No MTU configuration on NxOS
        if context.resource.model == 'Cisco NXOS Switch':
            logger.info('No Interface MTU Configuration for Nexus OS')
            return 'No Interface MTU Configuration for Nexus OS'

        # api = inject.instance('api')
        ports_list = interfaces.split(',')
        connectors = self.api.GetReservationDetails(reservation_id).ReservationDescription.Connectors

        for port in ports_list:
            # Generate port name
            resource_map = self.api.GetResourceDetails(context.resource.name)
            port_full_name = self._get_resource_full_name(port, resource_map)
            port_name = port_full_name.split('/')[-1].replace('-', '/')

            if 'channel' in port_name.lower():
                port_name = port_name.replace('/', '-')

            try:
                conn_port = self.api.GetResourceDetails(port_full_name).Connections.FullPath
            except AttributeError:
                conn_port = self.api.GetResourceDetails(port_full_name).Connections[0].FullPath

            # Get MTU from link attributes
            for connector in connectors:
                if connector.Source in conn_port or connector.Target in conn_port:
                    try:
                        mtu = [attr.Value for attr in connector.Attributes if attr.Name == 'Link MTU'][0]
                    except IndexError:
                        mtu = ''
                    break

            # Configure MTU if not already configured
            if not re.search('^mtu', (
            self.cli.send_command('show running interface {}'.format(port_name))).lower()) and mtu != '':
                self.cli.send_config_command('interface {}'.format(port_name))
                self.cli.send_config_command('mtu {}'.format(mtu))
            logger.info('Interface {0} was configured for MTU {1}'.format(port_name, mtu))

        return 'Interface MTU Configuration Completed'
예제 #7
0
    def provision_vdb(self, context, target_db_name, source_db_name,
                      source_group_name, target_group_name, timestamp,
                      vdb_type):
        logger = driver_helper.get_logger_with_thread_id(context)
        engine_config = parse_delphix_resource(context)
        cs_api = driver_helper.get_api(context)
        provision_operation = ProvisionVBDOperation(engine_conf=engine_config,
                                                    logger=logger)

        reservation_id = get_reservation_context_attribute(
            'reservation_id', context)
        provision_operation.run(target_db_name=target_db_name,
                                souce_db_name=source_db_name,
                                source_group_name=source_group_name,
                                target_group_name=target_group_name,
                                timestamp=timestamp,
                                vdb_type=vdb_type,
                                cloudshell_api=cs_api,
                                reservation_id=reservation_id)
    def configure_interface_speed(self, context, interfaces):
        logger = inject.instance('logger')
        reservation_id = get_reservation_context_attribute('reservation_id', context)

        # api = inject.instance('api')
        ports_list = interfaces.split(',')
        connectors = self.api.GetReservationDetails(reservation_id).ReservationDescription.Connectors

        for port in ports_list:
            # Generate port name
            resource_map = self.api.GetResourceDetails(context.resource.name)
            port_full_name = self._get_resource_full_name(port, resource_map)
            port_name = port_full_name.split('/')[-1].replace('-', '/')

            if 'channel' in port_name.lower():
                port_name = port_name.replace('/', '-')

            try:
                conn_port = self.api.GetResourceDetails(port_full_name).Connections.FullPath
            except AttributeError:
                conn_port = self.api.GetResourceDetails(port_full_name).Connections[0].FullPath

            # Get speed from link attributes
            for connector in connectors:
                if connector.Source in conn_port or connector.Target in conn_port:
                    try:
                        speed = [attr.Value for attr in connector.Attributes if attr.Name == 'Link Speed'][0]
                    except IndexError:
                        speed = ''
                    break

            # Configure speed if not already configured
            if not re.search('^speed', (
                    self.cli.send_command('show running interface {}'.format(port_name))).lower()) and speed != '':
                self.cli.send_config_command('interface {}'.format(port_name))
                self.cli.send_config_command('speed {}'.format(speed))
            logger.info('Interface {0} was configured for speed {1}'.format(port_name, speed))

        return 'Interface Speed Configuration Completed'
예제 #9
0
 def test_get_reservation_context_attribute_exception(self):
     domain = 'domain_name'
     self.context.reservation.domain = domain
     with self.assertRaises(Exception):
         get_reservation_context_attribute('domain', self.context)
예제 #10
0
 def test_get_reservation_context_attribute(self):
     domain = 'domain_name'
     self.context.reservation.domain = domain
     self.assertEqual(
         domain, get_reservation_context_attribute('domain', self.context))
    def create_port_channel(self, context, ports, stp_mode=''):
        # api = inject.instance('api')
        logger = inject.instance('logger')
        resource_details = context.resource
        reservation_id = get_reservation_context_attribute('reservation_id', context)
        existing_port_channels = list()
        port_channel_id = '0'
        dut_ports = ports.split(',')
        
        # Values for IOS
        max_port_chann = 65
        port_chann_str = 'Port-channel'

        # Values for NXOS
        if resource_details.model == 'Cisco NXOS Switch':
            max_port_chann = 4095
            port_chann_str = 'port-channel'

        # Get existing port-channels from switch
        port_channels = self.cli.send_command('show running-config | include {}'.format(port_chann_str))

        # Make a list of existing port channels on switch
        for line in port_channels.splitlines():
            if line.startswith('interface '):
                existing_port_channels.append(line.strip('interface {}'.format(port_chann_str)))

        # Find next available port channel on switch
        for port_chann in range(1, max_port_chann):
            if str(port_chann) not in existing_port_channels:
                port_channel_id = str(port_chann)
                break

        # Exit if all port channels are used up
        if port_channel_id == '0':
            logger.error('Could not find available port channel')
            raise Exception('could not find available port channel')

        # Create port channel on switch
        self.cli.send_config_command('interface port-channel {}'.format(port_channel_id))
        self.cli.send_config_command('switchport')
        if stp_mode.lower() == 'edge':
            self.cli.send_config_command('spanning-tree port type edge trunk')
        self.cli.send_config_command('description "{0}"'.format(reservation_id))
        logger.info('{0} was created'.format(port_channel_id))

        # Add interfaces to the new port channel
        exclude_list = list()
        for port in dut_ports:
            try:
                temp_port_name = self.api.GetResourceDetails(port).Connections.FullPath
            except AttributeError:
                try:
                    temp_port_name = self.api.GetResourceDetails(port).Connections[0].FullPath
                except:
                    exclude_list.append(port)
                    continue

            if '/' not in temp_port_name:
                logger.error('Interface was not found')
                raise Exception('Interface not found')

            port_name = temp_port_name.split('/')[-1].replace('-', '/')

            # If interface has a VLAN, cannot add to port-channel
            vlan_id = [line for line in
                       self.cli.send_command('show running interface {} | include vlan'.format(port_name)).splitlines()
                       if 'switchport' in line]

            if vlan_id:
                logger.info('Interface {0} has vlan, so cannot add to port-channel'.format(port_name))
                exclude_list.append(port_name)
                continue

            # Add ports to new port-channel
            self.cli.send_config_command('interface {}'.format(port_name))
            self.cli.send_config_command('no shutdown')
            self.cli.send_config_command('switchport')
            if resource_details.model == 'Cisco NXOS Switch':
                self.cli.send_config_command('channel-group {0} mode active'.format(port_channel_id))
            else:
                self.cli.send_config_command('channel-group {0} mode auto'.format(port_channel_id))
            if stp_mode == 'edge':
                self.cli.send_config_command('spanning-tree port type edge')
            logger.info('Interface {0} was added to channel-group {1}'.format(port_name, port_channel_id))

        logger.info('Port-Channel {} Configuration Completed, exiting create_port_channel'.format(port_channel_id))
        return 'Port-Channel {} Configuration Completed'.format(port_channel_id)