Ejemplo n.º 1
0
            raise UserException('No PXE IP Address in Inventory for client '
                                '\'%s\'' % hostname)
        if ip_list != '':
            ip = ',' + ip
        ip_list += ip

    return ip_list


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--deployer', action='store_true')
    args = parser.parse_args()

    config_pointer_file = get_python_path() + '/config_pointer_file'
    if os.path.isfile(config_pointer_file):
        with open(config_pointer_file) as f:
            config_path = f.read()
    else:
        config_path = None

    inv = Inventory(config_path)
    cfg = Config(config_path)

    ip_list = _get_pxe_ips(inv)

    if args.deployer:
        ip_list += "," + cfg.get_depl_netw_cont_ip()

    print(ip_list)
Ejemplo n.º 2
0
class GetSwitchInfoAssignClass(object):
    """Get switch model information and assign class.

    This class is responsible for collection switch model information
    and assign the corresponding switch class.
    """

    supported_mgmt_switches = (('G8052', 'Lenovo'), )

    supported_data_switches = (('MLNX-OS', 'Mellanox'), )

    MGMT_SWITCH_TYPE = 'mgmt'
    DATA_SWITCH_TYPE = 'data'

    ENABLE_REMOTE_CONFIG_MGMT = 'enable;configure terminal; %s'
    SHOW_VERSION_MTM = 'show version | include ^MTM'
    MODEL = 'Model'
    MTM_VALUE = 'MTM Value'

    ENABLE_REMOTE_CONFIG_DATA = 'cli enable "configure terminal" "%s"'
    SHOW_VERSION_PRODUCT = 'show version | include ^Product'
    PRODUCT_NAME = 'Product name'
    SHOW_INVENTORY_CHASSIS = 'show inventory | include ^CHASSIS'
    CHASSIS = 'CHASSIS'

    def __init__(self, log, inv_file):
        self.info_list = []
        self.class_list = []
        self.info_dict = AttrDict()
        self.class_dict = AttrDict()
        self.ipv4 = None
        self.userid = None
        self.password = None
        self.enable_remote = None

        self.inv = Inventory(log, inv_file)
        self.log = log

    def update_mgmt_switch_info(self):
        """Update management switch model information and assign class."""

        self.enable_remote = self.ENABLE_REMOTE_CONFIG_MGMT
        self.info_list = []
        self.class_list = []
        for switch in self.inv.yield_switches(self.inv.SwitchType.MGMT):
            self.info_dict = AttrDict()
            self.class_dict = AttrDict()
            self.ipv4 = switch.ip_addr
            self.userid = switch.userid
            self.password = switch.password
            switch_valid = False

            output = self._send_cmd(self.SHOW_VERSION_MTM, 'Query MTM', False)

            switch_valid |= self._set_switch_info_class(
                r'\s+(\S+)\(config\)#', self.MODEL, output,
                self.supported_mgmt_switches)

            switch_valid |= self._set_switch_info_class(
                r'%s:\s+(\S+)\s+' % self.MTM_VALUE, self.MTM_VALUE, output,
                self.supported_mgmt_switches)

            if not switch_valid:
                if self.info_list:
                    self.log.error('Unsupported management switch: %s' %
                                   self.info_dict)
                else:
                    self.log.error('Management switch could not be identified')
                sys.exit(1)

        if self.info_list:
            self.inv.update_switch_model_info(self.inv.SwitchType.MGMT,
                                              self.info_list)
            self.inv.update_switch_class(self.inv.SwitchType.MGMT,
                                         self.class_list)

    def update_data_switch_info(self):
        """Update data switch model information and assign class."""

        self.enable_remote = self.ENABLE_REMOTE_CONFIG_DATA
        self.info_list = []
        self.class_list = []
        for switch in self.inv.yield_switches(self.inv.SwitchType.DATA):
            self.info_dict = AttrDict()
            self.class_dict = AttrDict()
            self.ipv4 = switch.ip_addr
            self.userid = switch.userid
            self.password = switch.password
            switch_valid = False

            output = self._send_cmd(self.SHOW_VERSION_PRODUCT,
                                    'Query Product Name', False)

            switch_valid |= self._set_switch_info_class(
                r'%s:\s+(\S+)\s+' % self.PRODUCT_NAME, self.PRODUCT_NAME,
                output, self.supported_data_switches)

            output = self._send_cmd(self.SHOW_INVENTORY_CHASSIS,
                                    'Query CHASSIS', False)

            switch_valid |= self._set_switch_info_class(
                r'%s\s+(\S+)\s+' % self.CHASSIS, self.CHASSIS, output,
                self.supported_data_switches)

            if not switch_valid:
                if self.info_list:
                    self.log.error('Unsupported data switch: %s' %
                                   self.info_dict)
                else:
                    self.log.error('Data switch could not be identified')
                sys.exit(1)

        if self.info_list:
            self.inv.update_switch_model_info(self.inv.SwitchType.DATA,
                                              self.info_list)
            self.inv.update_switch_class(self.inv.SwitchType.DATA,
                                         self.class_list)

    def _set_switch_info_class(self, pattern, attr, output,
                               supported_switches):
        """Add model and class information to switch structure.

        Check whether switch is supported.

        Args:
            pattern (string): Command response pattern.
            attr (string): Attribute key.
            output (string): Command output.
            supported_switches (tuple of tuples): Supported switches.

        Returns:
            (boolean): Whether switch is supported based on given attribute.
        """

        pat = re.compile(pattern, re.MULTILINE)
        match = pat.search(output)
        if match:
            switch_attr = match.group(1)
            self.info_dict[attr] = switch_attr
            self.info_list.append(self.info_dict)
            attr_list = [sublist[0] for sublist in supported_switches]
            class_list = [sublist[1] for sublist in supported_switches]
            self.log.info(attr + ': ' + switch_attr + ' on ' + self.ipv4)
            if switch_attr in attr_list:
                index = attr_list.index(switch_attr)
                self.class_dict = class_list[index]
                self.class_list.append(self.class_dict)
                return True
        return False

    def _send_cmd(self, cmd, msg, status_check=True):
        """Send command to switch.

        Args:
            cmd (string): Switch command.
            msg (string): Description for log file.
            status_check (boolean): Whether to check for SSH error.

        Returns:
            (string): Command output from switch.
        """

        ssh = SSH(self.log)
        self.log.debug(cmd + ' on ' + self.ipv4)
        status, stdout_, _ = ssh.exec_cmd(self.ipv4, self.userid,
                                          self.password,
                                          self.enable_remote % cmd)
        if status:
            if status_check:
                self.log.error('Failed: ' + msg + ' on ' + self.ipv4 +
                               ' - Error: ' +
                               stdout_.replace('\n', ' ').replace('\r', ''))
                sys.exit(1)
            else:
                self.log.info(msg + ' on ' + self.ipv4 + ' - Error: ' +
                              stdout_.replace('\n', ' ').replace('\r', ''))
        else:
            self.log.info(msg + ' on ' + self.ipv4)
        return stdout_
Ejemplo n.º 3
0
class ConfigureDataSwitch(object):
    ENABLE_REMOTE_CONFIG = 'cli enable "configure terminal" "%s"'
    FORCE = 'force'
    SET_VLAN = 'vlan %d'
    SET_MTU = 'mtu %d'
    INTERFACE_ETHERNET = 'interface ethernet 1/%s'
    SWITCHPORT_MODE_HYBRID = 'switchport mode hybrid'
    SWITCHPORT_HYBRID_ALLOWED_VLAN = \
        'switchport hybrid allowed-vlan add %d'
    SHUTDOWN = 'shutdown'
    NO_SHUTDOWN = 'no shutdown'
    IP_ROUTING = 'ip routing'
    ENABLE_LACP = 'lacp'
    QOS_ENABLE = 'dcb priority-flow-control enable force'
    MLAG = 'protocol mlag'
    LAG_PORT_CHANNEL = 'interface port-channel %d'
    LACP = 'channel-group %d mode active'
    IPL = 'ipl 1'
    QOS_ON = 'dcb priority-flow-control mode on force'
    INTERFACE_VLAN = 'interface vlan %d'
    IP_CIDR = 'ip address %s'
    PEER_ADDR = 'peer-address %s'
    MLAG_VIP = 'mlag-vip my-mlag-vip-domain ip %s force'
    ENABLE_MLAG = 'no mlag shutdown'
    MLAG_PORT_CHANNEL = 'interface mlag-port-channel %d'
    STP_PORT_TYPE_EDGE = 'spanning-tree port type edge'
    STP_BPDUFILTER_ENABLE = 'spanning-tree bpdufilter enable'
    MLAG_ACTIVE = 'mlag-channel-group %d mode active'
    NO_CHANNEL_GROUP = 'no channel-group'

    def __init__(self, log, inv_file):
        self.inv = Inventory(log, inv_file)
        self.log = log
        self.userid = self.inv.get_userid_data_switch()
        self.password = self.inv.get_password_data_switch()
        self.switch_dict = {}
        self.switch_name = self.inv.get_data_switch_name()

        for ipv4 in self.inv.yield_data_switch_ip():
            self.switch_dict[ipv4] = self.switch = SwitchFactory.factory(
                log,
                self.switch_name,
                ipv4,
                self.userid,
                self.password,
                mode='active')

        for self.ipv4, userid, password, vlans in self.inv.yield_data_vlans(self.userid, self.password):
            for vlan in vlans:
                self.switch_dict[self.ipv4].create_vlan(vlan)

        switch_index = 0
        for self.ipv4, port_vlans, port_mtu, port_bonds \
                in self.inv.yield_data_switch_ports(self.userid, self.password):
            if port_bonds:
                for ports in port_bonds.values():
                    for port in ports:
                        # Set port mode and add VLANs
                        if port in port_vlans:
                            self.switch_dict[self.ipv4].add_vlans_to_port(port, port_vlans[port])
                        # Specify MTU
                        if port in port_mtu:
                            self.switch_dict[self.ipv4].set_mtu_for_port(port, port_mtu[port])
            else:
                for port, vlans in port_vlans.items():
                    # Set port mode and add VLANs
                    self.switch_dict[self.ipv4].set_switchport_mode('trunk', port)
                    self.switch_dict[self.ipv4].add_vlans_to_port(port, vlans)
                for port, mtu in port_mtu.items():
                    # Specify MTU
                    self.switch_dict[self.ipv4].set_mtu_for_port(port, mtu)

            if port_bonds:
                # Enable LACP
                self.switch_dict[self.ipv4].enable_lacp()
                # Configure port for MLAG
                if self.inv.is_mlag():
                    vlan = self.inv.get_mlag_vlan()
                    port_channel = self.inv.get_mlag_port_channel()
                    cidr_mlag_ipl = self.inv.get_cidr_mlag_ipl(switch_index)
                    ipaddr_mlag_ipl_peer = self.inv.get_ipaddr_mlag_ipl_peer(switch_index)
                    ipaddr_mlag_vip = self.inv.get_ipaddr_mlag_vip()
                    mlag_ports = self.inv.get_mlag_ports(switch_index)
                    self.switch_dict[self.ipv4].configure_mlag(
                        vlan,
                        port_channel,
                        cidr_mlag_ipl,
                        ipaddr_mlag_ipl_peer,
                        ipaddr_mlag_vip,
                        mlag_ports)
                    for port_channel, ports in port_bonds.items():
                        # Remove any channel-group from port
                        self.switch_dict[self.ipv4].remove_channel_group(ports[0])
                        self.switch_dict[self.ipv4].create_mlag_interface(port_channel)
                        if ports[0] in port_vlans:
                            self.switch_dict[self.ipv4].add_vlans_to_mlag_port_channel(
                                port_channel, port_vlans[ports[0]])
                        if ports[0] in port_mtu:
                            self.switch_dict[self.ipv4].set_mtu_for_mlag_port_channel(
                                port_channel, port_mtu[ports[0]])
                        self.switch_dict[self.ipv4].bind_port_to_mlag_interface(port_channel)
                    # Enable MLAG
                    self.switch_dict[self.ipv4].enable_mlag()
                # Configure port for LAG
                else:
                    for port_channel, ports in port_bonds.items():
                        for port in ports:
                            self.switch_dict[self.ipv4].remove_channel_group(port)
                        if ports[0] in port_vlans:
                            self.switch_dict[self.ipv4].add_vlans_to_lag_port_channel(
                                port_channel, port_vlans[ports[0]])
                        if ports[0] in port_mtu:
                            self.switch_dict[self.ipv4].set_mtu_for_lag_port_channel(
                                port_channel, port_mtu[ports[0]])
                        self.switch_dict[self.ipv4].create_lag(port_channel)
                        self.switch_dict[self.ipv4].activate_lag(port_channel, ports)

            switch_index += 1

        if self.inv.is_write_switch_memory():
            switch = WriteSwitchMemory(LOG, INV_FILE)
            switch.write_data_switch_memory()
Ejemplo n.º 4
0
    def __init__(self, log, inv_file):
        self.inv = Inventory(log, inv_file)
        self.log = log
        self.userid = self.inv.get_userid_data_switch()
        self.password = self.inv.get_password_data_switch()
        self.switch_dict = {}
        self.switch_name = self.inv.get_data_switch_name()

        for ipv4 in self.inv.yield_data_switch_ip():
            self.switch_dict[ipv4] = self.switch = SwitchFactory.factory(
                log,
                self.switch_name,
                ipv4,
                self.userid,
                self.password,
                mode='active')

        for self.ipv4, userid, password, vlans in self.inv.yield_data_vlans(self.userid, self.password):
            for vlan in vlans:
                self.switch_dict[self.ipv4].create_vlan(vlan)

        switch_index = 0
        for self.ipv4, port_vlans, port_mtu, port_bonds \
                in self.inv.yield_data_switch_ports(self.userid, self.password):
            if port_bonds:
                for ports in port_bonds.values():
                    for port in ports:
                        # Set port mode and add VLANs
                        if port in port_vlans:
                            self.switch_dict[self.ipv4].add_vlans_to_port(port, port_vlans[port])
                        # Specify MTU
                        if port in port_mtu:
                            self.switch_dict[self.ipv4].set_mtu_for_port(port, port_mtu[port])
            else:
                for port, vlans in port_vlans.items():
                    # Set port mode and add VLANs
                    self.switch_dict[self.ipv4].set_switchport_mode('trunk', port)
                    self.switch_dict[self.ipv4].add_vlans_to_port(port, vlans)
                for port, mtu in port_mtu.items():
                    # Specify MTU
                    self.switch_dict[self.ipv4].set_mtu_for_port(port, mtu)

            if port_bonds:
                # Enable LACP
                self.switch_dict[self.ipv4].enable_lacp()
                # Configure port for MLAG
                if self.inv.is_mlag():
                    vlan = self.inv.get_mlag_vlan()
                    port_channel = self.inv.get_mlag_port_channel()
                    cidr_mlag_ipl = self.inv.get_cidr_mlag_ipl(switch_index)
                    ipaddr_mlag_ipl_peer = self.inv.get_ipaddr_mlag_ipl_peer(switch_index)
                    ipaddr_mlag_vip = self.inv.get_ipaddr_mlag_vip()
                    mlag_ports = self.inv.get_mlag_ports(switch_index)
                    self.switch_dict[self.ipv4].configure_mlag(
                        vlan,
                        port_channel,
                        cidr_mlag_ipl,
                        ipaddr_mlag_ipl_peer,
                        ipaddr_mlag_vip,
                        mlag_ports)
                    for port_channel, ports in port_bonds.items():
                        # Remove any channel-group from port
                        self.switch_dict[self.ipv4].remove_channel_group(ports[0])
                        self.switch_dict[self.ipv4].create_mlag_interface(port_channel)
                        if ports[0] in port_vlans:
                            self.switch_dict[self.ipv4].add_vlans_to_mlag_port_channel(
                                port_channel, port_vlans[ports[0]])
                        if ports[0] in port_mtu:
                            self.switch_dict[self.ipv4].set_mtu_for_mlag_port_channel(
                                port_channel, port_mtu[ports[0]])
                        self.switch_dict[self.ipv4].bind_port_to_mlag_interface(port_channel)
                    # Enable MLAG
                    self.switch_dict[self.ipv4].enable_mlag()
                # Configure port for LAG
                else:
                    for port_channel, ports in port_bonds.items():
                        for port in ports:
                            self.switch_dict[self.ipv4].remove_channel_group(port)
                        if ports[0] in port_vlans:
                            self.switch_dict[self.ipv4].add_vlans_to_lag_port_channel(
                                port_channel, port_vlans[ports[0]])
                        if ports[0] in port_mtu:
                            self.switch_dict[self.ipv4].set_mtu_for_lag_port_channel(
                                port_channel, port_mtu[ports[0]])
                        self.switch_dict[self.ipv4].create_lag(port_channel)
                        self.switch_dict[self.ipv4].activate_lag(port_channel, ports)

            switch_index += 1

        if self.inv.is_write_switch_memory():
            switch = WriteSwitchMemory(LOG, INV_FILE)
            switch.write_data_switch_memory()
Ejemplo n.º 5
0
    def __init__(self, inv_path=None, cfg_path=None):
        self.log = logger.getlogger()

        self.cfg_path = cfg_path
        self.inv = Inventory(cfg_path, inv_path)
Ejemplo n.º 6
0
def cobbler_add_systems(cfg_file=None):
    LOG = logger.getlogger()

    cobbler_user = gen.get_cobbler_user()
    cobbler_pass = gen.get_cobbler_pass()
    cobbler_server = xmlrpc.client.Server("http://127.0.0.1/cobbler_api")
    token = cobbler_server.login(cobbler_user, cobbler_pass)

    inv = Inventory(cfg_file=cfg_file)

    for index, hostname in enumerate(inv.yield_nodes_hostname()):
        ipv4_ipmi = inv.get_nodes_ipmi_ipaddr(0, index)
        userid_ipmi = inv.get_nodes_ipmi_userid(index)
        password_ipmi = inv.get_nodes_ipmi_password(index)
        ipv4_pxe = inv.get_nodes_pxe_ipaddr(0, index)
        mac_pxe = inv.get_nodes_pxe_mac(0, index)
        cobbler_profile = gen.check_os_profile(
            re.sub("[.]iso", "", inv.get_nodes_os_profile(index)))
        raid1_enabled = False

        new_system_create = cobbler_server.new_system(token)

        cobbler_server.modify_system(new_system_create, "name", hostname,
                                     token)
        cobbler_server.modify_system(new_system_create, "hostname", hostname,
                                     token)
        cobbler_server.modify_system(new_system_create, "power_address",
                                     ipv4_ipmi, token)
        cobbler_server.modify_system(new_system_create, "power_user",
                                     userid_ipmi, token)
        cobbler_server.modify_system(new_system_create, "power_pass",
                                     password_ipmi, token)
        cobbler_server.modify_system(new_system_create, "power_type",
                                     "ipmilan", token)
        cobbler_server.modify_system(new_system_create, "profile",
                                     cobbler_profile, token)
        cobbler_server.modify_system(
            new_system_create, 'modify_interface', {
                "macaddress-eth0": mac_pxe,
                "ipaddress-eth0": ipv4_pxe,
                "dnsname-eth0": hostname
            }, token)
        ks_meta = ""
        disks = inv.get_nodes_os_install_device(index)
        if disks is not None:
            if isinstance(disks, str):
                ks_meta += 'install_disk=%s ' % disks
            elif isinstance(disks, list) and len(disks) == 2:
                ks_meta += ('install_disk=%s install_disk_2=%s ' %
                            (disks[0], disks[1]))
                raid1_enabled = True
            else:
                LOG.error('%s: Invalid install_device value: %s '
                          'Must be string or two item list.' %
                          (hostname, disks))
        if raid1_enabled:
            ks_meta += 'raid1_enabled=true '
        users = inv.get_nodes_os_users(index)
        if users is not None:
            for user in users:
                if 'name' in user and user['name'] != 'root':
                    ks_meta += 'default_user=%s ' % user['name']
                    LOG.debug("%s: Using \'%s\' as default user" %
                              (hostname, user['name']))
                    if 'password' in user:
                        ks_meta += ('passwd=%s passwdcrypted=true ' %
                                    user['password'])
                    break
            else:
                LOG.debug("%s: No default user found" % hostname)
        else:
            LOG.debug("%s: No users defined" % hostname)
        if ks_meta != "":
            cobbler_server.modify_system(new_system_create, "ks_meta", ks_meta,
                                         token)
        kernel_options = inv.get_nodes_os_kernel_options(index)
        if 'ubuntu-18.04' in cobbler_profile.lower():
            if kernel_options is None:
                kernel_options = ''
            if 'netcfg/do_not_use_netplan=true' not in kernel_options:
                kernel_options += ' netcfg/do_not_use_netplan=true'
        if kernel_options is not None:
            cobbler_server.modify_system(new_system_create, "kernel_options",
                                         kernel_options, token)
            cobbler_server.modify_system(new_system_create,
                                         "kernel_options_post", kernel_options,
                                         token)
        comment = ""
        cobbler_server.modify_system(new_system_create, "comment", comment,
                                     token)

        cobbler_server.save_system(new_system_create, token)

        LOG.info("Cobbler Add System: name=%s, profile=%s" %
                 (hostname, cobbler_profile))

    cobbler_server.sync(token)
    LOG.info("Running Cobbler sync")
Ejemplo n.º 7
0
class InventoryAddPorts(object):
    """Class instance for adding port information to the Cluster Genesis
    inventory file.
    Args:
        dhcp_leases_file (str): file path for the dnsmasq.leases file.
        port_type (str): 'ipmi' or 'pxe'
    """
    def __init__(self, dhcp_leases_file, port_type, config_path=None):
        self.log = logger.getlogger()
        self.cfg = Config(config_path)
        self.dhcp_leases_file = dhcp_leases_file
        self.port_type = port_type
        self.inv = Inventory(cfg_file=config_path)
        self.log.debug('Add ports, port type: {}'.format(self.port_type))

    def get_ports(self):
        dhcp_leases = GetDhcpLeases(self.dhcp_leases_file)
        dhcp_mac_ip = dhcp_leases.get_mac_ip()
        self.log.debug('DHCP leases: {}'.format(dhcp_mac_ip))

        mgmt_sw_cfg_mac_lists = AttrDict()

        if self.cfg.is_passive_mgmt_switches():
            self.log.debug('passive mode')
            for switch_label in self.cfg.yield_sw_mgmt_label():
                file_path = os.path.join(GEN_PASSIVE_PATH, switch_label)
                mac_info = {}
                try:
                    with open(file_path, 'r') as f:
                        mac_info = f.read()

                except IOError as error:
                    self.log.error(
                        'Passive switch MAC address table file not found {}'
                        .format(error))
                    raise
                mgmt_sw_cfg_mac_lists[switch_label] = \
                    SwitchCommon.get_port_to_mac(mac_info, self.log)
        else:
            for sw_ai in self.cfg.yield_sw_mgmt_access_info():
                self.log.debug('switch ai: {}'.format(sw_ai))
                sw = SwitchFactory.factory(*sw_ai[1:])
                label = sw_ai[0]
                mgmt_sw_cfg_mac_lists[label] = \
                    sw.show_mac_address_table(format='std')

        self.log.debug('Management switches MAC address tables: {}'.format(
            mgmt_sw_cfg_mac_lists))

        # Remove all the mac address table entries which do not have a matching
        # MAC address in the DHCP leases table, then remove any MAC addresses
        # which do not have a  DHCP table entry.
        for switch in mgmt_sw_cfg_mac_lists.keys():
            for port in mgmt_sw_cfg_mac_lists[switch]:
                port_macs = mgmt_sw_cfg_mac_lists[switch][port]
                found_mac = False
                for mac in dhcp_mac_ip.keys():
                    if mac in port_macs:
                        found_mac = True
                        # keep only the mac which has a dhcp address
                        mgmt_sw_cfg_mac_lists[switch][port] = [mac]
                if not found_mac:
                    del mgmt_sw_cfg_mac_lists[switch][port]
        self.log.debug('Management switches MAC address table of ports with'
                       'dhcp leases: {}'.format(mgmt_sw_cfg_mac_lists))

        if self.port_type == "ipmi":
            self.inv.add_macs_ipmi(mgmt_sw_cfg_mac_lists)
            self.inv.add_ipaddrs_ipmi(dhcp_mac_ip)
        elif self.port_type == "pxe":
            self.inv.add_macs_pxe(mgmt_sw_cfg_mac_lists)
            self.inv.add_ipaddrs_pxe(dhcp_mac_ip)

        if self.port_type == 'ipmi':
            self.node_table, self.ports_found, self.ports_total = \
                self._build_node_table_ipmi(self.cfg, dhcp_mac_ip, mgmt_sw_cfg_mac_lists)

        if self.port_type == 'pxe':
            self.node_table, self.ports_found, self.ports_total = \
                self._build_node_table_pxe(self.cfg, dhcp_mac_ip, mgmt_sw_cfg_mac_lists)

    def _build_node_table_ipmi(self, cfg, dhcp_list, mac_lists):
        # Tabulate results by Node type (template)
        node_table = {}
        ports_total = 0
        ports_found = 0
        for idx_ntmplt in self.cfg.yield_ntmpl_ind():
            node_label = self.cfg.get_ntmpl_label(idx_ntmplt)
            self.log.debug('node label: {}'.format(node_label))
            ports_list = []

            for idx_ipmi in self.cfg.yield_ntmpl_phyintf_ipmi_ind(idx_ntmplt):
                switch_label = self.cfg.get_ntmpl_phyintf_ipmi_switch(
                    idx_ntmplt, idx_ipmi)
                ports_total += self.cfg.get_ntmpl_phyintf_ipmi_pt_cnt(
                    idx_ntmplt, idx_ipmi)

                for idx_port in self.cfg.yield_ntmpl_phyintf_ipmi_pt_ind(
                        idx_ntmplt, idx_ipmi):
                    port = self.cfg.get_ntmpl_phyintf_ipmi_ports(
                        idx_ntmplt, idx_ipmi, idx_port)
                    result = self.inv.get_port_mac_ip(switch_label, port)
                    if None not in result:
                        ports_found += 1
                        ports_list.append(
                            [True, switch_label, port, result[0], result[1], 'ipmi'])
                        self.log.debug(
                            'Node Port Defined in Inventory - Template: {}'
                            'Switch: {} Port: {} MAC: {} IP: {}'.format(
                                node_label, switch_label, port, result[0],
                                result[1], 'ipmi'))
                    elif str(port) in mac_lists[switch_label]:
                        for mac in mac_lists[switch_label][str(port)]:
                            if mac in dhcp_list:
                                ipaddr = dhcp_list[mac]
                                ports_list.append(
                                    [False, switch_label, port, mac,
                                     ipaddr, 'ipmi'])
                                self.log.debug(
                                    'Node Port MAC/IP NOT Defined in Inventory - '
                                    'Template: {} Switch: {} Port: {} '
                                    'MAC: {} IP: {}'.format(
                                        node_label, switch_label, port, mac, ipaddr))
                            else:
                                ports_list.append(
                                    [False, switch_label, port, mac, '-', 'ipmi'])
                                self.log.debug(
                                    'No DHCP Lease Found for Port MAC Address - '
                                    'Template: {} Switch: {} Port: {} '
                                    'MAC: {}'.format(
                                        node_label, switch_label, port, mac))
                    else:
                        ports_list.append(
                            [False, switch_label, port, '-', '-', 'ipmi'])
                        self.log.debug(
                            'No Entries Found in MGMT Switch MAC Address Table - '
                            'Template: {} Switch: {} Port: {}'.format(
                                node_label, switch_label, port))
            node_table[node_label] = ports_list
        self.log.debug('node table: {}'.format(node_table))
        return node_table, ports_found, ports_total

    def _build_node_table_pxe(self, cfg, dhcp_list, mac_lists):
        # Tabulate results by Node type (template)
        node_table = {}
        ports_total = 0
        ports_found = 0
        for idx_ntmplt in cfg.yield_ntmpl_ind():
            node_label = cfg.get_ntmpl_label(idx_ntmplt)
            self.log.debug('node label: {}'.format(node_label))
            ports_list = []

            for idx_pxe in cfg.yield_ntmpl_phyintf_pxe_ind(idx_ntmplt):
                switch_label = cfg.get_ntmpl_phyintf_pxe_switch(idx_ntmplt, idx_pxe)
                ports_total += cfg.get_ntmpl_phyintf_pxe_pt_cnt(idx_ntmplt, idx_pxe)

                for idx_port in cfg.yield_ntmpl_phyintf_pxe_pt_ind(
                        idx_ntmplt, idx_pxe):
                    port = cfg.get_ntmpl_phyintf_pxe_ports(
                        idx_ntmplt, idx_pxe, idx_port)
                    result = self.inv.get_port_mac_ip(switch_label, port)
                    if None not in result:
                        ports_found += 1
                        ports_list.append(
                            [True, switch_label, port, result[0], result[1], 'pxe'])
                        self.log.debug(
                            'Node Port Defined in Inventory - Template: {}'
                            'Switch: {} Port: {} MAC: {} IP: {}'.format(
                                node_label, switch_label, port, result[0],
                                result[1], 'pxe'))
                    elif str(port) in mac_lists[switch_label]:
                        for mac in mac_lists[switch_label][str(port)]:
                            if mac in dhcp_list:
                                ipaddr = dhcp_list[mac]
                                ports_list.append(
                                    [False, switch_label, port, mac,
                                     ipaddr, 'pxe'])
                                self.log.debug(
                                    'Node Port MAC/IP NOT Defined in Inventory - '
                                    'Template: {} Switch: {} Port: {} '
                                    'MAC: {} IP: {}'.format(
                                        node_label, switch_label, port, mac, ipaddr))
                            else:
                                ports_list.append(
                                    [False, switch_label, port, mac, '-', 'pxe'])
                                self.log.debug(
                                    'No DHCP Lease Found for Port MAC Address - '
                                    'Template: {} Switch: {} Port: {} '
                                    'MAC: {}'.format(
                                        node_label, switch_label, port, mac))
                    else:
                        ports_list.append(
                            [False, switch_label, port, '-', '-', 'pxe'])
                        self.log.debug(
                            'No Entries Found in MGMT Switch MAC Address Table - '
                            'Template: {} Switch: {} Port: {}'.format(
                                node_label, switch_label, port))
            node_table[node_label] = ports_list
        self.log.debug('node table: {}'.format(node_table))
        return node_table, ports_found, ports_total

    def get_table(self):
        return self.node_table

    def get_table_pretty(self):
        """Generates a list of node template names and pretty tables. To
        print, simply print the list items. ie;
        for item in add_ports.get_table_pretty():
            print item
        """
        blue = '\033[94m'
        endc = '\033[0m'
        table_pretty = []
        table_header = ['In-Inventory', 'Switch', 'Port',
                        'MAC', 'IP', 'Port type']
        for template in self.node_table.keys():
            table_pretty.append('\n{}Node Type: {}:{}'.format(blue, template, endc))
            table_pretty.append(tabulate(self.node_table[template], table_header))

        return table_pretty

    def get_status(self):
        return (self.ports_found, self.ports_total)
Ejemplo n.º 8
0
Archivo: player.py Proyecto: pjht/game
class Player(Character):
    def __init__(self, x, y, map, screen, uname, type, *groups):
        super().__init__(x, y, type, map, screen, *groups)
        self.inv = Inventory()
        self.uname = uname

    def facingTile(self):
        x = self.x
        y = self.y
        if self.dir == "up":
            y -= 1
        elif self.dir == "down":
            y += 1
        elif self.dir == "left":
            x -= 1
        elif self.dir == "right":
            x += 1
        if x > constants.MAPWIDTH - 1:
            return False
        if x < 0:
            return False
        if y > constants.MAPHEIGHT - 1:
            return False
        if y < 0:
            return False
        return (x, y)

    def interact(self):
        coords = self.facingTile()
        if coords == False:
            return
        tile = self.map.tileAt(coords[0], coords[1])
        name = tile.unlocalisedName
        if name == "grass":
            to_place = self.inv.selected
            if to_place == "":
                return
            self.map.tiles[(coords[0], coords[1])] = None
            self.map.addTile(to_place, coords[0], coords[1])
            self.inv.remove(to_place)
        else:
            tile.interact(self.inv)

    def attack(self):
        coords = self.facingTile()
        if coords == False:
            return
        tile = self.map.tileAt(coords[0], coords[1])
        name = tile.unlocalisedName
        if name == "grass":
            return
        self.map.tiles[(coords[0], coords[1])] = None
        self.map.addTile("grass", coords[0], coords[1])
        if tile.drops == False:
            self.inv.addTile(name, 1)
        else:
            drop = tile.drops[0]
            amount = tile.drops[1]
            self.inv.addTile(drop, amount)

    def draw(self):
        oldx = self.x
        oldy = self.y
        self.x = constants.CENTERX
        self.y = constants.CENTERY
        super().draw()
        self.x = oldx
        self.y = oldy
Ejemplo n.º 9
0
 def __init__(self, log, inv_file):
     inv = Inventory(log, inv_file)
     inv.add_switches()
Ejemplo n.º 10
0
def ipmi_set_power(state, config_path=None, client_list=None, max_attempts=5,
                   wait=6):
    """Set power on or off
    Args:
        state (str) : 'on' or 'off'
        client_list (list of str): list of IP addresses
    """
    log = logger.getlogger()
    inv = Inventory(config_path)
    wait = float(wait)
    max_attempts = int(max_attempts)

    if not client_list:
        log.debug('Retrieving IPMI address list from inventory')
        client_list = inv.get_nodes_ipmi_ipaddr(0)

    clients_left = client_list[:]
    attempt = 0

    none_cnt = 0
    for client in client_list:
        if client is None:
            none_cnt += 1
            log.warning('client node ip address is "None"')
            clients_left.remove(None)

    clients_left.sort()
    while clients_left and attempt < max_attempts:
        nodes = {}
        attempt += 1
        if attempt > 1:
            print('Retrying set power {}. Attempt {} of {}'
                  .format(state, attempt, max_attempts))
            print('Clients remaining: {}'.format(clients_left))
        clients_set = []
        bmc_dict = {}
        for index, hostname in enumerate(inv.yield_nodes_hostname()):
            ipv4 = inv.get_nodes_ipmi_ipaddr(0, index)
            if ipv4 is None or ipv4 not in clients_left:
                continue
            rack_id = inv.get_nodes_rack_id(index)
            userid = inv.get_nodes_ipmi_userid(index)
            password = inv.get_nodes_ipmi_password(index)
            nodes[ipv4] = [rack_id, ipv4]
            for i in range(2):
                try:
                    bmc_dict[ipv4] = ipmi_command.Command(
                        bmc=ipv4,
                        userid=userid,
                        password=password)
                except pyghmi_exception.IpmiException as error:
                    log.error('IPMI login attempt {}, address {}\nIPMI error'
                              'message: {}'.format(i, ipv4, error.message))
                    time.sleep(1)
                else:
                    break

        for client in clients_left:
            if client in bmc_dict:
                try:
                    log.debug('Setting power state to {}. Device: {}'
                              .format(state, client))
                    status = bmc_dict[client].set_power(state, wait)
                    if attempt in [2, 4, 8]:
                        print('{} - {}'.format(client, status))
                except pyghmi_exception.IpmiException as error:
                    msg = ('set_power failed Rack: %s - IP: %s, \n%s' %
                           (nodes[client][0], nodes[client][1], str(error)))
                    log.error(msg)
                else:
                    # Allow delay between turn on to limit power surge
                    if state == 'on':
                        time.sleep(0.5)
                finally:
                    if 'error' in status:
                        log.error(status)

        time.sleep(wait + attempt)

        for client in clients_left:
            if client in bmc_dict:
                try:
                    status = bmc_dict[client].get_power()
                    if attempt in [2, 4, 8]:
                        print('{} - {}'.format(client, status))
                except pyghmi_exception.IpmiException as error:
                    msg = ('get_power failed - Rack: %s - IP: %s, %s' %
                           (rack_id, ipv4, str(error)))
                    log.error(msg)
                else:
                    if status['powerstate'] == state:
                        log.debug('set_power successful Rack: %s - IP: %s' %
                                  (nodes[client][0], nodes[client][1]))
                        clients_set += [client]
                finally:
                    if 'error' in status:
                        log.error(status)
                bmc_dict[client].ipmi_session.logout()

        for client in clients_set:
            clients_left.remove(client)

        if attempt == max_attempts and clients_left:
            log.error('Failed to power {} some clients'.format(state))
            log.error(clients_left)

        del bmc_dict

    log.info('Powered {} {} of {} client devices.'
             .format(state, len(client_list) - (len(clients_left) + none_cnt),
                     len(client_list)))

    if state == 'off':
        print('Pausing 60 sec for client power off')
        time.sleep(60)

    if clients_left:
        return False
    return True
Ejemplo n.º 11
0
def generate_dynamic_inventory():
    config_pointer_file = gen.get_python_path() + '/config_pointer_file'
    if os.path.isfile(config_pointer_file):
        with open(config_pointer_file) as f:
            config_path = f.read()
    else:
        config_path = None

    inv = Inventory(config_path)
    cfg = Config(config_path)

    # Initialize the empty inventory
    dynamic_inventory = INVENTORY_INIT

    meta_hostvars = dynamic_inventory['_meta']['hostvars']

    # Add 'env_variables' to 'all' 'vars'
    dynamic_inventory['all']['vars']['env_variables'] = (
        cfg.get_globals_env_variables())

    # Add 'localhost' to inventory
    meta_hostvars['localhost'] = {}
    meta_hostvars['localhost']['ansible_connection'] = 'local'

    # Add 'deployer' (container) to inventory
    meta_hostvars['deployer'] = {}
    meta_hostvars['deployer']['ansible_host'] = cfg.get_depl_netw_cont_ip()
    meta_hostvars['deployer']['ansible_user'] = SSH_USER
    meta_hostvars['deployer']['ansible_ssh_private_key_file'] = SSH_PRIVATE_KEY

    # Add 'software_bootstrap' list to 'client_nodes' 'vars' if not empty
    software_bootstrap = cfg.get_software_bootstrap()
    if len(software_bootstrap) > 0:
        dynamic_inventory['client_nodes']['vars']['software_bootstrap'] = (
            software_bootstrap)

    # Add client nodes to inventory
    for index, hostname in enumerate(inv.yield_nodes_hostname()):
        # Add node to top-level group (node-template label & 'client-nodes')
        label = _sanitize(inv.get_nodes_label(index))
        if label not in dynamic_inventory:
            dynamic_inventory[label] = {'hosts': []}
            dynamic_inventory['client_nodes']['children'].append(label)
        dynamic_inventory[label]['hosts'].append(hostname)

        # Add node hostvars in '_meta' dictionary
        meta_hostvars[hostname] = inv.get_node_dict(index)
        meta_hostvars[hostname]['ansible_host'] = (inv.get_nodes_pxe_ipaddr(
            0, index))
        meta_hostvars[hostname]['ansible_user'] = SSH_USER
        meta_hostvars[hostname]['ansible_ssh_private_key_file'] = (
            SSH_PRIVATE_KEY)

        # Add node to any additional groups ('roles')
        roles = inv.get_nodes_roles(index)
        if roles is not None:
            for role in roles:
                _role = _sanitize(role)
                if _role not in dynamic_inventory:
                    dynamic_inventory[_role] = {'hosts': []}
                dynamic_inventory[_role]['hosts'].append(hostname)

    return dynamic_inventory
Ejemplo n.º 12
0
def ipmi_set_bootdev(bootdev,
                     persist=False,
                     config_path=None,
                     client_list=None,
                     max_attempts=5):
    log = logger.getlogger()
    inv = Inventory(cfg_file=config_path)

    if type(persist) is not bool:
        persist = (persist == 'True')

    # if client list passed, it is assumed to be pxe addresses.
    # otherwise use the entire ipmi inventory list. This allows a
    # subset of nodes to have their bootdev updated during install
    if not client_list:
        client_list = inv.get_nodes_ipmi_ipaddr(0)
        clients_left = client_list[:]
    else:
        # Get corresponing ipmi addresses
        clients_left = []
        for index, hostname in enumerate(inv.yield_nodes_hostname()):
            ipv4_ipmi = inv.get_nodes_ipmi_ipaddr(0, index)
            ipv4_pxe = inv.get_nodes_pxe_ipaddr(0, index)
            if ipv4_pxe is not None and ipv4_pxe in client_list:
                clients_left.append(ipv4_ipmi)

    attempt = 0
    clients_left.sort()
    while clients_left and attempt < max_attempts:
        nodes = {}
        attempt += 1
        if attempt > 1:
            print('Retrying set bootdev. Attempt {} of {}'.format(
                attempt, max_attempts))
            print('Clients remaining: {}'.format(clients_left))
        clients_set = []
        bmc_dict = {}
        for index, hostname in enumerate(inv.yield_nodes_hostname()):
            ipv4 = inv.get_nodes_ipmi_ipaddr(0, index)
            if ipv4 is None or ipv4 not in clients_left:
                continue
            rack_id = inv.get_nodes_rack_id(index)
            userid = inv.get_nodes_ipmi_userid(index)
            password = inv.get_nodes_ipmi_password(index)
            nodes[ipv4] = [rack_id, ipv4]
            for i in range(2):
                try:
                    bmc_dict[ipv4] = ipmi_command.Command(bmc=ipv4,
                                                          userid=userid,
                                                          password=password)
                except pyghmi_exception.IpmiException as error:
                    log.error('IPMI login try {}, address {} - {}'.format(
                        i, ipv4, error.message))
                    time.sleep(1)
                else:
                    break

        for client in clients_left:
            if client in bmc_dict:
                try:
                    status = bmc_dict[client].set_bootdev(bootdev, persist)
                    if attempt in [2, 4, 8]:
                        print('Client node: {} - bootdev status: {}'.format(
                            client, status))
                except pyghmi_exception.IpmiException as error:
                    msg = ('set_bootdev failed (device=%s persist=%s), '
                           'Rack: %s - IP: %s, %s' %
                           (bootdev, persist, nodes[client][0],
                            nodes[client][1], str(error)))
                    log.warning(msg)
                finally:
                    if 'error' in status:
                        log.error(status)

        time.sleep(1 + attempt)

        for client in clients_left:
            if client in bmc_dict:
                try:
                    status = bmc_dict[client].get_bootdev()
                    if attempt in [2, 4, 8]:
                        print('{} - {}'.format(client, status))
                except pyghmi_exception.IpmiException as error:
                    msg = ('get_bootdev failed - '
                           'Rack: %s - IP: %s, %s' %
                           (rack_id, ipv4, str(error)))
                    log.error(msg)
                else:
                    if status['bootdev'] == bootdev and str(status['persistent']) \
                            == str(persist):
                        log.debug(
                            'set_bootdev successful (device=%s persist=%s) - '
                            'Rack: %s - IP: %s' %
                            (bootdev, persist, nodes[client][0],
                             nodes[client][1]))
                        clients_set += [client]
                finally:
                    if 'error' in status:
                        log.error(status)
                bmc_dict[client].ipmi_session.logout()

        for client in clients_set:
            clients_left.remove(client)

        if attempt == max_attempts and clients_left:
            log.error('Failed to set boot device for some clients')
            log.debug(clients_left)

        del bmc_dict
    log.info('Set boot device to {} on {} of {} client devices.'.format(
        bootdev,
        len(client_list) - len(clients_left), len(client_list)))
Ejemplo n.º 13
0
                       "sccm_collection")

(options, args) = parser.parse_args()

config_parser = ConfigParser()
config = 'monitoring.ini'
config_parser.read(config)

json_file = config_parser.get('Monitoring', 'source_json')
logger.info("Loading json data from %s" % json_file)

if options.extra and not options.csv:
    logger.critical("CSV extra options passed but the CSV flag was not set.")
    sys.exit(-1)

inv = Inventory(json_file=json_file)
con = Constitution()
con.load_legislation(config_parser)
pol = Police(con, inv, logger)

if options.print_data:
    print inv

elif options.show_detail:
    print con

elif options.dry_run:
    pol.dry_run()

elif options.csv:
    additional_fields = []
Ejemplo n.º 14
0
    """
    Arg1: inventory file
    Arg2: switch type
    Arg3: log level
    """
    LOG = Logger(__file__)

    if len(sys.argv) != 4:
        try:
            raise Exception()
        except:
            LOG.error('Invalid argument count')
            sys.exit(1)

    INV_FILE = sys.argv[1]
    SWITCH_TYPE = sys.argv[2]
    LOG.set_level(sys.argv[3])

    INV = Inventory(LOG, INV_FILE)

    if SWITCH_TYPE == 'mgmt':
        result = validate_mgmt_mac_table_files(LOG, INV)
    elif SWITCH_TYPE == 'data':
        result = validate_data_mac_table_files(LOG, INV)
    else:
        LOG.error('Invalid "switch type" argument')
        sys.exit(1)

    if not result:
        sys.exit(1)
Ejemplo n.º 15
0
def mock_inventory(cfg_file, inv_file, log):

    # install.yml - include: lxc-update.yml
    copy2(cfg_file, inv_file)
    os.chmod(inv_file, 0o644)
    for line in fileinput.input(inv_file, inplace=1):
        print(line, end='')
        if line.startswith('password-default'):
            print('ansible_user: root')
            print('ansible_ssh_private_key_file: '
                  '/home/ubuntu/.ssh/id_rsa_ansible-generated')

    # install.yml - include: container/inv_add_ports.yml port_type=ipmi
    inv = Inventory(log, inv_file)
    (dhcp_mac_ip, mgmt_sw_cfg) = get_port_mac_ip(inv)
    inv.create_nodes(dhcp_mac_ip, mgmt_sw_cfg)

    # install.yml - include: container/inv_add_switches.yml
    InventoryAddSwitches(log, inv_file)

    # install.yml - include: container/inv_add_ports.yml port_type=pxe
    inv = Inventory(log, inv_file)
    inv.add_pxe(dhcp_mac_ip, mgmt_sw_cfg)

    # install.yml - include: container/inv_add_ipmi_data.yml
    inv = Inventory(log, inv_file)
    add_ipmi_data(inv)

    # install.yml - include: container/allocate_ip_addresses.yml
    inv = Inventory(log, inv_file)
    allocate_ips(inv_file)

    # gather_mac_addresses.yml
    inv = Inventory(log, inv_file)
    switch_ip_to_port_to_macs = get_switch_ip_to_mac_map(inv)
    inv.add_data_switch_port_macs(switch_ip_to_port_to_macs)
Ejemplo n.º 16
0
def main(log, inv_file):
    inv = Inventory(log, inv_file)
    print()
    mgmt_network_ext_cidr = None
    key_addr = None
    try:
        mgmt_network_port = inv.get_port_mgmt_network()
        userid = inv.get_userid_mgmt_switch()
        password = inv.get_password_mgmt_switch()
        mgmt_network_gen = inv.get_ipaddr_mgmt_network()
        mgmt_network_ext = inv.get_mgmt_switch_external_dev_ip()
        mgmt_network_ext_prefix = inv.get_mgmt_switch_external_prefix()
        mgmt_network_ext = mgmt_network_ext + '/' + mgmt_network_ext_prefix
        mgmt_network_ext = netaddr.IPNetwork(mgmt_network_ext)
        mgmt_network_ext_cidr = str(mgmt_network_ext.cidr)
    except KeyError:
        print('Switch access information not present in: {}'.format(inv_file))
        print('Cluster Genesis may be in "passive" mode')
    else:
        output = subprocess.check_output(['bash', '-c',
                                          'ip route']).decode("utf-8")
        if mgmt_network_ext_cidr in output:
            key_addr = 'addr_ext'
        elif mgmt_network_gen in output:
            key_addr = 'addr_gen'
        else:
            print('No route found using config file addresses')

    if key_addr:
        print('==============================')
        print('Defined switches: ')
        index = 1
        switches_m = {}
        for rack, ipv4 in inv.yield_mgmt_rack_ipv4():
            switches_m[index] = {'rack': rack, 'addr_gen': ipv4}
            index += 1

        index = 1
        for ipv4 in inv.yield_mgmt_switch_external_switch_ip():
            switches_m[index]['addr_ext'] = ipv4
            print(' ' + str(index) + ')  rack: ' + switches_m[index]['rack'] +
                  ',  external address: ' + switches_m[index]['addr_ext'] +
                  ',  Genesis address: ' + switches_m[index]['addr_gen'])
            index += 1
        if not (len(switches_m) == 1):
            sw = get_int_input("\n\nSelect a switch: ", 1, len(switches_m))
        else:
            sw = 1
        addr = switches_m[sw][key_addr]
        print()

    else:
        # output = subprocess.check_output(['bash', '-c', 'ip route'])
        addr = '192.168.32.20/24'
        userid = 'admin'
        password = '******'
        while 1:
            addr = rlinput("Enter an address for the management switch: ",
                           addr)
            mgmt_network_ext = netaddr.IPNetwork(addr)
            mgmt_network_ext_cidr = str(mgmt_network_ext.cidr)
            userid = rlinput("Enter a userid for the management switch: ",
                             userid)
            password = rlinput(
                "Enter a password for the management switch (last char = '.' to terminate): ",
                password)
            if password[-1:] == '.':
                sys.exit(0)
            output = subprocess.check_output(['bash', '-c',
                                              'ip route']).decode("utf-8")
            if mgmt_network_ext_cidr in output:
                addr = addr[:addr.find('/')]
                break


# the G8052 misbehaves & closes it's SSH connection
# after every paramiko 'exec_command', so using SSH
# with commands strung together

    ssh = SSH(log)
    cmd = 'show interface ip;show vlan;show interface port %s;' \
        % (str(mgmt_network_port))
    try:
        _, switch_info, _ = ssh.exec_cmd(addr,
                                         userid,
                                         password,
                                         cmd,
                                         ssh_log=ssh_log,
                                         look_for_keys=False)
    except SSH_Exception as exc:
        print('Failed to SSH to switch at {} using userid {} and password {}'.
              format(addr, userid, password))
        print(exc)
        sys.exit(1)
    print_lines(switch_info, ['Interface information:', 'IP4'])
    print('\n\nVLAN information: ')
    print_lines(switch_info, ['-  ------  -', ' VLAN '])
    print()
    print('Deployer port: ')
    print_lines(switch_info, ['Current port', 'VLANs'])
    print()
Ejemplo n.º 17
0
def set_bootdev_clients(bootdev, persist=False, config_path=None, clients=None,
                        max_attempts=5):
    """Set boot device for multiple clients. If a list of ip addresses
    are given they are assumed to be PXE addresses. Corresponding BMC addresses
    are looked up in inventory file corresponding to the config file given in
    the config_path. Similarly if no client list is given, all BMCs enumerated in
    the inventory file corresponding to the config file specified in config_path
    will be acted on.  If clients is a dictionary, then the credentials are
    taken from the dictionary values.

    Args:
        state (str) : 'on' or 'off'
        config_path (str): path to a config file
        clients (dict or list of str): list of IP addresses or
        dict of ip addresses with values of credentials as tuple
        ie {'192.168.1.2': ('user', 'password', 'bmc_type')}
    """
    log = logger.getlogger()
    if config_path:
        inv = Inventory(cfg_file=config_path)

    if type(persist) is not bool:
        persist = (persist == 'True')

    def _get_cred_list(client_list=None):
        """Returns dict with values of tuples or list.  Each tuple/list
         has the credentials for a node (userid, password, bmc_type).
        If no client list is passed, all nodes are returned
        Args:
            client_list (list of str): each list item is an ipv4 address
        """
        cred_list = {}
        for index, hostname in enumerate(inv.yield_nodes_hostname()):
            ipv4 = inv.get_nodes_ipmi_ipaddr(0, index)
            if client_list and ipv4 not in client_list:
                continue
            userid = inv.get_nodes_ipmi_userid(index)
            password = inv.get_nodes_ipmi_password(index)
            bmc_type = inv.get_nodes_bmc_type(index)
            cred_list[ipv4] = (userid, password, bmc_type)
        return cred_list

    # if client list passed, it is assumed to be pxe addresses which
    # are used to look up the associated bmc addresses for the node.
    # otherwise use the entire ipmi inventory list. This allows a
    # subset of nodes to have their bootdev updated during install
    if isinstance(clients, list):
        # Get corresponing ipmi addresses
        _clients = []
        for index, hostname in enumerate(inv.yield_nodes_hostname()):
            ipv4_ipmi = inv.get_nodes_ipmi_ipaddr(0, index)
            ipv4_pxe = inv.get_nodes_pxe_ipaddr(0, index)
            if ipv4_pxe is not None and ipv4_pxe in clients:
                _clients.append(ipv4_ipmi)
    if not clients:
        log.debug('Retrieving IPMI address list from inventory')
        clients = inv.get_nodes_ipmi_ipaddr(0)
        _clients = clients[:]

    if isinstance(clients, list):
        log.debug('Retrieving client credentials from inventory')
        cred_list = _get_cred_list(_clients)
    else:
        # insure cred info in tuple
        cred_list = {}
        for client in clients:
            cred_list[client] = tuple(clients[client])

    clients_left = list(cred_list.keys())
    attempt = 0
    clients_left.sort()
    while clients_left and attempt < max_attempts:
        attempt += 1
        if attempt > 1:
            log.info('Retrying set bootdev. Attempt {} of {}'.format(attempt, max_attempts))
            log.info('Clients remaining: {}'.format(clients_left))
        clients_set = []
        bmc_dict = {}
        for client in clients_left:
            for i in range(3):
                tmp = _bmc.Bmc(client, *cred_list[client])
                if tmp.is_connected():
                    bmc_dict[client] = tmp
                    break
                else:
                    log.debug(f'Failed BMC login attempt {i + 1} BMC: {client}')
                    if i > 0:
                        log.info(f'BMC login attempt {i + 1} BMC: {client}')
                    if attempt == max_attempts and i == 2:
                        log.error(f'Failed BMC login. BMC: {client}')
                    time.sleep(1)
                    del tmp

        for client in clients_left:
            if client in bmc_dict:
                log.debug(f'Setting boot device to {bootdev}. '
                          f'Device: {client}')
                if bootdev in ('setup'):
                    status = bmc_dict[client].host_boot_mode(bootdev)
                else:
                    status = bmc_dict[client].host_boot_source(bootdev)
                    log.debug(f'status1 from set bootdev: {status}')

                if status:
                    if attempt in [2, 4, 8]:
                        log.info(f'{client} - Boot source: {status} Required source: '
                                 f'{bootdev}')
                elif attempt == max_attempts:
                    log.error(f'Failed attempt {attempt} set boot source {bootdev} '
                              f'for node {client}')

        time.sleep(1 + attempt)

        for client in clients_left:
            if client in bmc_dict:
                if bootdev in ('setup'):
                    status = bmc_dict[client].host_boot_mode()
                else:
                    status = bmc_dict[client].host_boot_source()
                    log.debug(f'status2 from set bootdev: {status}')

                if status:
                    if attempt in [2, 4, 8]:
                        log.info(f'{client} - Boot source: {bootdev}')
                    if status == bootdev:
                        log.debug(f'Successfully set boot source to {bootdev} for '
                                  f'node {client}')
                        clients_set += [client]
                elif attempt == max_attempts:
                    log.error(f'Failed attempt {attempt} set host boot source to'
                              f'{bootdev} for node {client}')

                bmc_dict[client].logout()

        for client in clients_set:
            clients_left.remove(client)

        if attempt == max_attempts and clients_left:
            log.error('Failed to set boot device for some clients')
            log.debug(clients_left)

        del bmc_dict
    log.info('Set boot device to {} on {} of {} client devices.'
             .format(bootdev, len(cred_list) - len(clients_left),
                     len(cred_list)))
Ejemplo n.º 18
0
Archivo: player.py Proyecto: pjht/game
 def __init__(self, x, y, map, screen, uname, type, *groups):
     super().__init__(x, y, type, map, screen, *groups)
     self.inv = Inventory()
     self.uname = uname
Ejemplo n.º 19
0
class InventoryNodes(object):
    SWITCH_NOT_FOUND = \
        "Node template '%s' did not have corresponding management switch '%s'"

    def __init__(self, inv_path=None, cfg_path=None):
        self.log = logger.getlogger()

        self.cfg_path = cfg_path
        self.inv = Inventory(cfg_path, inv_path)

    def __del__(self):
        self.inv.update_nodes()

    def create_nodes(self):
        cfg = Config(self.cfg_path)
        interface_ip_lists = _gen_interface_ip_lists(cfg)
        # Iterate over node templates
        for index_ntmplt in cfg.yield_ntmpl_ind():
            # Get Label
            label = cfg.get_ntmpl_label(index_ntmplt)
            # Get Hostname
            hostname = cfg.get_ntmpl_os_hostname_prefix(index_ntmplt)
            if hostname is None:
                hostname = label
            # Get bmc_type
            bmc_type = cfg.get_ntmpl_bmc_type(index_ntmplt)
            # Get Rack
            switch_label = cfg.get_ntmpl_phyintf_ipmi_switch(index_ntmplt, 0)
            switch_index = cfg.get_sw_mgmt_index_by_label(switch_label)
            if switch_index is None:
                node_template = cfg.get_ntmpl_label(index_ntmplt)
                self.log.error(self.SWITCH_NOT_FOUND %
                               (node_template, switch_label))
            # Iterate over client systems
            index_host = 0
            for index_port in cfg.yield_ntmpl_phyintf_ipmi_pt_ind(
                    index_ntmplt, 0):
                # Set Label
                self.inv.add_nodes_label(label)
                # Set Hostname
                self.inv.add_nodes_hostname(hostname + '-' +
                                            str(index_host + 1))
                # Set bmc_type
                self.inv.add_nodes_bmc_type(bmc_type)
                # Set Rack ID
                switch_label = cfg.get_ntmpl_phyintf_ipmi_switch(
                    index_ntmplt, 0)
                switch_index = cfg.get_sw_mgmt_index_by_label(switch_label)
                self.inv.add_nodes_rack_id(
                    cfg.get_sw_mgmt_rack_id(switch_index))
                # Copy OS settings dictionary
                self.inv.add_nodes_os_dict(cfg.get_ntmpl_os_dict(index_ntmplt))
                # Copy roles
                self.inv.add_nodes_roles(cfg.get_ntmpl_roles(index_ntmplt))

                ports_ipmi = []
                ports_pxe = []
                switches_ipmi = []
                switches_pxe = []
                macs_ipmi = []
                macs_pxe = []
                ipaddrs_ipmi = []
                ipaddrs_pxe = []
                devices_pxe = []
                rename_pxe = []
                # Iterate over IPMI members
                for index_ipmi in cfg.yield_ntmpl_phyintf_ipmi_ind(
                        index_ntmplt):
                    # Create client system IPMI switch list
                    switches_ipmi.append(
                        cfg.get_ntmpl_phyintf_ipmi_switch(
                            index_ntmplt, index_ipmi))
                    # Create client system PXE switch list
                    switches_pxe.append(
                        cfg.get_ntmpl_phyintf_pxe_switch(
                            index_ntmplt, index_ipmi))
                    # Create client system IPMI port list
                    ports_ipmi.append(
                        cfg.get_ntmpl_phyintf_ipmi_ports(
                            index_ntmplt, index_ipmi, index_port))
                    # Create client system PXE port list
                    ports_pxe.append(
                        cfg.get_ntmpl_phyintf_pxe_ports(
                            index_ntmplt, index_ipmi, index_port))
                    # Create client system IPMI mac list
                    macs_ipmi.append(None)
                    # Create client system PXE mac list
                    macs_pxe.append(None)
                    # Create client system IPMI ipaddrs list
                    ipaddrs_ipmi.append(None)
                    # Create client system PXE ipaddrs list
                    ipaddrs_pxe.append(None)
                    # Create client PXE network device list
                    devices_pxe.append(
                        cfg.get_ntmpl_phyintf_pxe_dev(index_ntmplt))
                    # Create client PXE device rename list
                    rename_pxe.append(
                        cfg.get_ntmpl_phyintf_pxe_rename(index_ntmplt))
                # Set client system IPMI switches
                self.inv.add_nodes_switches_ipmi(switches_ipmi)
                # Set client system PXE switches
                self.inv.add_nodes_switches_pxe(switches_pxe)
                # Set client system IPMI ports
                self.inv.add_nodes_ports_ipmi(ports_ipmi)
                # Set client system PXE ports
                self.inv.add_nodes_ports_pxe(ports_pxe)

                # Set client system IPMI macs
                self.inv.add_nodes_macs_ipmi(macs_ipmi)
                # Set client system PXE macs
                self.inv.add_nodes_macs_pxe(macs_pxe)
                # Set client system IPMI apaddrs
                self.inv.add_nodes_ipaddrs_ipmi(ipaddrs_ipmi)
                # Set client system PXE ipaddrs
                self.inv.add_nodes_ipaddrs_pxe(ipaddrs_pxe)
                # Set client system PXE rename
                self.inv.add_nodes_rename_pxe(rename_pxe)

                # Set client system IPMI userids
                self.inv.add_nodes_userid_ipmi(
                    cfg.get_ntmpl_ipmi_userid(index_ntmplt))
                # Set client system IPMI passwords
                self.inv.add_nodes_password_ipmi(
                    cfg.get_ntmpl_ipmi_password(index_ntmplt))
                # Set PXE network device
                self.inv.add_nodes_devices_pxe(devices_pxe)

                ports_data = []
                switches_data = []
                macs_data = []
                devices_data = []
                rename_data = []
                # Iterate over data members
                for index_data in cfg.yield_ntmpl_phyintf_data_ind(
                        index_ntmplt):
                    # Create client system data switch list
                    switches_data.append(
                        cfg.get_ntmpl_phyintf_data_switch(
                            index_ntmplt, index_data))
                    # Create client system data port list
                    ports_data.append(
                        cfg.get_ntmpl_phyintf_data_ports(
                            index_ntmplt, index_data, index_port))
                    # Create client system data mac list
                    macs_data.append(None)
                    # Create client data network device list
                    devices_data.append(
                        cfg.get_ntmpl_phyintf_data_dev(index_ntmplt,
                                                       index_data))
                    # Create client data device rename list
                    rename_data.append(
                        cfg.get_ntmpl_phyintf_data_rename(
                            index_ntmplt, index_data))

                # Set client system data switches
                self.inv.add_nodes_switches_data(switches_data)
                # Set client system data ports
                self.inv.add_nodes_ports_data(ports_data)
                # Set client system data macs
                self.inv.add_nodes_macs_data(macs_data)
                # Set client system data devices
                self.inv.add_nodes_devices_data(devices_data)
                # Set client system data rename
                self.inv.add_nodes_rename_data(rename_data)
                index_host += 1

                interfaces = cfg.get_ntmpl_interfaces(index_ntmplt)
                interfaces, interface_ip_lists = _assign_interface_ips(
                    interfaces, interface_ip_lists)
                self.inv.add_nodes_interfaces(interfaces)
        self.log.info('Successfully created inventory file')