def connect(self, host, userid, password=None, ssh_key_string=None): _method_ = "MLNXOSPlugin.connect" self.host = host self.userid = userid try: self.client = paramiko.SSHClient() self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) if ssh_key_string: private_key = paramiko.RSAKey.from_private_key( StringIO(ssh_key_string), password) self.client.connect(host, username=userid, pkey=private_key, timeout=30, allow_agent=False) else: self.client.connect(host, username=userid, password=password, timeout=30, allow_agent=False) if not self._is_mellanox_switch(): raise exceptions.InvalidDeviceException( "Device is not a Mellanox Switch") except paramiko.AuthenticationException: logging.error( "%s::userid/password combination not valid. host=%s userid=%s", _method_, host, userid) raise exceptions.AuthenticationException( "userid/password combination not valid") except (paramiko.ssh_exception.SSHException, OSError, socket.timeout) as e: logging.exception(e) logging.error("%s::Connection timed out. host=%s", _method_, host) raise exceptions.ConnectionException("Unable to ssh to the device")
def list_systems(self): """ Method to query Cobbler and list all systems defined within its database """ _method_ = "CobblerDiscoveryPlugin.list_systems" systems = [] if self.shell is None: self.shell = spur.LocalShell() with self.shell: try: process = self.shell.spawn(["sh", "-c", "sudo -S %s" % COBBLER_CMD_SYSTEM_LIST]) process.stdin_write(SUDO_PASSWORD + "\n") result = process.wait_for_result() # iterate through output if command executed successfully if result.return_code == 0: for line in result.output.decode('ascii').splitlines(): system = line.strip() systems.append(system) logging.info("%s::Found system to be imported from Cobbler::%s", _method_, system) else: logging.error("%s::Failed to retrieve systems from Cobbler", _method_) except (spur.ssh.NoSuchCommandError, spur.ssh.ConnectionError) as e: logging.exception(e) logging.error("%s::Connection error - host = %s", _method_, self.shell._hostname) raise exceptions.ConnectionException(e) return systems
def connect(self, host, userid, password=None, ssh_key_string=None): """connect to the BMC and store the mtm and serial number """ _method_ = "PowerNodePlugin.connect" self.host = host self.userid = userid self.password = password if ssh_key_string is not None: raise exceptions.AuthenticationException( "SSH Key Authentication " "is not supported for PowerNode devices") cmd_parms = [ self.IPMI_TOOL, "-I", "lanplus", "-H", host, "-U", userid, "-P", password, "fru", "print" ] (_rc, stdout, stderr) = execute_command(" ".join(cmd_parms)) logging.warning("%s::ipmi query standard error output %s", _method_, stderr) for line in stderr: if "Unable to establish IPMI" in line: raise exceptions.ConnectionException( "Unable to connect to the device using IPMI") for line in stdout: if "Chassis Part Number" in line: self.machine_type_model = line.split(":")[1].strip() elif "Chassis Serial" in line: self.serial_number = line.split(":")[1].strip()
def discover_resources(self, resource_label='*', offline=False, add=False): """ Method to query all data from all systems defined within Cobbler's database """ _method_ = "CobblerDiscoveryPlugin.import_resources" systems = self.list_systems() if self.shell is None: self.shell = spur.LocalShell() for system in systems: if resource_label != '*' and resource_label != system: continue with self.shell: try: process = self.shell.spawn(["sh", "-c", "sudo -S %s %s" % (COBBLER_CMD_SYSTEM_DUMPVARS, system)]) process.stdin_write(SUDO_PASSWORD + "\n") result = process.wait_for_result() resource_ipv4 = None resource_breed = None # iterate through output if command executed successfully if result.return_code == 0: for line in result.output.decode('ascii').splitlines(): # Parse the output to extract the resource data if COBBLER_TAG_ADDRESS in line: resource_ipv4 = line.split(':')[1].strip() elif COBBLER_TAG_BREED in line: resource_breed = line.split(':')[1].strip() else: logging.error("%s::Failed to import data from Cobbler for system::%s", _method_, system) # Figure out if there is a valid resource type for this system resource_type = COBBLER_DIC_TYPES[resource_breed] resource = self.format_resource(system, resource_type, resource_ipv4, DEFAULT_RESOURCE_USER) if resource_type is None: print("Warning:: Cannot find a suitable resource type for %s" % resource) else: print("Discovered from %s: %s" % (self.get_type(), resource)) rc1, message = validate_label(system) if rc1 != 0: print("Warning:: %s" % message) rc2, message = validate_address(resource_ipv4) if rc2 != 0: print("Warning:: %s" % message) if add: if rc1 == 0 and rc2 == 0: rc, message = add_resource(system, resource_type, resource_ipv4, DEFAULT_RESOURCE_USER, DEFAULT_RESOURCE_PWD, offline=offline) if rc == 0: print("Resource imported: %s" % system) else: print("Import error: %s" % message) else: print("Cannot import: %s - conflicting resource exists" % system) except (spur.ssh.NoSuchCommandError, spur.ssh.ConnectionError) as e: logging.exception(e) logging.error("%s::Connection error - host = %s", _method_, self.shell._hostname) raise exceptions.ConnectionException(e)
def connect(self, host, user, pwd, pkey=None): """ Method to optionally connecting to a remote host prior to interacting with Cobbler """ _method_ = "connect" if pkey: self.shell = spur.SshShell(hostname=host, username=user, private_key_file=pkey, missing_host_key=spur.ssh.MissingHostKey.accept) else: self.shell = spur.SshShell(hostname=host, username=user, password=pwd) with self.shell: try: self.shell.run(["echo", "-n", "ping"]) except (spur.ssh.NoSuchCommandError, spur.ssh.ConnectionError) as e: logging.exception(e) logging.error("%s::Connection error - host = %s", _method_, host) raise exceptions.ConnectionException(e)