def _do_iscsiadm_attach(iqn, targets, user=None, passwd=None, iscsi_portal_ip=None): """ Attach an iSCSI device. Parameters ---------- iqn: str The iSCSI qualified name. targets: dict The targets, user: str The iscsiadm username. passwd: str The iscsiadm user password. iscsi_portal_ip: str portal IP Returns ------- None Raise ----- Exception in case of error """ if not iscsi_portal_ip: portal_ip = None if targets is None: raise Exception( "ocid must be running to determine the portal IP address for this device" ) for ipaddr in list(targets.keys()): if iqn in targets[ipaddr]: portal_ip = ipaddr if portal_ip is None: # this shouldn't really happen, but just in case raise Exception("Can't find portal IP address") else: portal_ip = iscsi_portal_ip retval = iscsiadm.attach(portal_ip, 3260, iqn, user, passwd, auto_startup=True) _logger.info("Result: %s", iscsiadm.error_message_from_code(retval)) if retval != 0: raise Exception('iSCSI attachment failed: %s' % iscsiadm.error_message_from_code(retval))
def display_detached_iscsi_device(iqn, targets, attach_failed=()): """ Display the iSCSI devices Parameters ---------- iqn: str The iSCSI qualified name. targets: dict The targets. attach_failed: dict The devices for which attachment failed. """ devicePrinter = get_row_printer_impl('table')(title="Target %s" % iqn, text_truncate=False, columns=(['Portal', 12, 'portal'], ['State', 12, 'state'])) devicePrinter.printHeader() _item = {} for ipaddr in list(targets.keys()): _item['portal'] = "%s:3260" % ipaddr if iqn in attach_failed: _item['state'] = iscsiadm.error_message_from_code(attach_failed[iqn]) else: _item['state'] = "Detached" devicePrinter.printRow(_item) devicePrinter.rowBreak() devicePrinter.printFooter() devicePrinter.finish()
def do_attach(oci_sess, iqn, targets, user=None, passwd=None): """ Attach an iSCSI device. Parameters ---------- oci_sess: OCISession The OCISession. iqn: str The iSCSI qualified name. targets: dict The targets, user: str The iscsiadm username. passwd: str The iscsiadm user password. Returns ------- int The iscsiadm attach return value on successful start, 99 otherwise. """ if oci_sess is not None: oci_vols = oci_sess.find_volumes(iqn=iqn) if len(oci_vols) == 1: user = oci_vols[0].get_user() passwd = oci_vols[0].get_password() portal_ip = None if targets is None: print "ocid must be running to determine the portal IP address " \ "for this device" return 99 for ipaddr in targets.keys(): if iqn in targets[ipaddr]: portal_ip = ipaddr if portal_ip is None: # this shouldn't really happen, but just in case print "Can't find portal IP address" return 99 retval = iscsiadm.attach(portal_ip, 3260, iqn, user, passwd, auto_startup=True) print "Result: %s" \ % iscsiadm.error_message_from_code(retval) return retval
def display_detached_iscsi_device(iqn, targets, attach_failed=()): """ Display the iSCSI devices Parameters ---------- iqn: str The iSCSI qualified name. targets: dict The targets. attach_failed: dict The devices for which attachment failed. """ print("Target %s" % iqn) for ipaddr in list(targets.keys()): if iqn in targets[ipaddr]: print(" Portal: %s:%s" % (ipaddr, 3260)) if iqn in attach_failed: print(" State: %s" % iscsiadm.error_message_from_code(attach_failed[iqn])) else: print(" State: Detached")
def display_attach_failed_device(iqn, targets, attach_failed): """ Display the devices which could not attached automatically. Parameters ---------- iqn: str The iSCSI qualified name. targets: dict The targets. attach_failed: dict The devices for which attachment failed. Returns ------- No return value. """ print print "Target %s" % iqn for ipaddr in targets.keys(): if iqn in targets[ipaddr]: print " Portal: %s:%s" % (ipaddr, 3260) print " State: %s" % \ iscsiadm.error_message_from_code(attach_failed[iqn])
def do_create_volume(sess, size, display_name, attach_it): """ Create a new OCI volume and attach it to this instance. Parameters ---------- sess: OCISession The OCISession instance. size: int The volume size in GB. display_name: str The volume display name. attach_it: boolean Do we attach the newly created volume Returns ------- nothing Raises ------ Exception if something went wrong """ try: _logger.info("Creating a new %d GB volume", size) inst = sess.this_instance() if inst is None: raise Exception("OCI SDK error: couldn't get instance info") vol = sess.create_volume(inst.get_compartment_id(), inst.get_availability_domain_name(), size=size, display_name=display_name, wait=True) except Exception as e: _logger.debug("Failed to create volume", exc_info=True) raise Exception("Failed to create volume") from e _logger.info("Volume %s created", vol.get_display_name()) if not attach_it: return _logger.info("Attaching the volume to this instance") try: vol = vol.attach_to(instance_id=inst.get_ocid()) except Exception as e: _logger.debug('cannot attach BV', exc_info=True) vol.destroy() raise Exception('cannot attach BV') from e # # attach using iscsiadm commands _logger.info("Attaching iSCSI device") retval = iscsiadm.attach(ipaddr=vol.get_portal_ip(), port=vol.get_portal_port(), iqn=vol.get_iqn(), username=vol.get_user(), password=vol.get_password(), auto_startup=True) _logger.info("iscsiadm attach Result: %s", iscsiadm.error_message_from_code(retval)) if retval == 0: _logger.debug('Creation succesful') return # here because of error case try: _logger.debug('destroying the volume') vol.destroy() except Exception as e: _logger.debug("Failed to destroy volume", exc_info=True) _logger.error("Failed to destroy volume: %s", str(e)) raise Exception('Failed to attach created volume: %s' % iscsiadm.error_message_from_code(retval))
def do_create_volume(sess, size, display_name, use_chap=False): """ Create a new OCI volume and attach it to this instance. Parameters ---------- sess: OCISession The OCISession instance. size: int The volume size in GB. display_name: str The volume display name. use_chap: bool Flag, use chap secret when set. # --GT-- not used yet, left in to avoid breaking function call. Returns ------- bool True on success, False otherwise. """ if not USE_OCI_SDK or sess is None: _logger.error("Need OCI Service to create volume.\n" "Make sure to install and configure " "OCI Python SDK (python-oci-sdk)\n") if oci_sdk_error is not None: _logger.error("OCI SDK error: %s\n" % oci_sdk_error) return False if size < 50: _logger.error("Volume size must be at least 50GBs\n") return False # FIXME: use_chap, but not used yet # vol = None # inst = None try: print "Creating a new %d GB volume" % size inst = sess.this_instance() if inst is None: _logger.error("OCI SDK error: couldn't get instance info.") return False vol = inst.create_volume(size=size, display_name=display_name) except Exception as e: _logger.debug("Failed to create volume", exc_info=True) _logger.error("Failed to create volume: %s" % e) return False print "Volume %s created" % vol.get_display_name() # attach using iscsiadm commands state = vol.get_attachment_state() if OCI_ATTACHMENT_STATE[state] in ( OCI_ATTACHMENT_STATE.ATTACHED, OCI_ATTACHMENT_STATE.ATTACHING): print "Volume %s is %s" % (vol.get_display_name(), state) return True print "Attaching iSCSI device" retval = iscsiadm.attach(ipaddr=vol.get_portal_ip(), port=vol.get_portal_port(), iqn=vol.get_iqn(), username=vol.get_user(), password=vol.get_password(), auto_startup=True) print "Result: %s" % iscsiadm.error_message_from_code(retval) if retval == 0: return True try: vol.destroy() except Exception as e: _logger.debug("Failed to destroy volume", exc_info=True) _logger.error("Failed to destroy volume: %s" % e) return False
def do_attach_ocid(sess, ocid): """ Make API calls to attach a volume with the given OCID to this instance. Parameters ---------- sess : OCISession An OCISession instance ocid : str The volume OCID Returns ------- bool True on success, False otherwise. """ global _user_euid if not USE_OCI_SDK or sess is None: _logger.error("Need OCI Service to create volume.\n" "Make sure to install and configure " "OCI Python SDK (python-oci-sdk)\n") if oci_sdk_error is not None: _logger.error("OCI SDK error: %s\n" % oci_sdk_error) return False vol = sess.get_volume(ocid) if vol is None: _logger.error("Volume %s not found.\n" % ocid) return False if vol.is_attached(): if vol.get_instance().get_ocid() == sess.this_instance().get_ocid(): # attached to this instance already print "Volume %s already attached to this instance." % \ ocid return True else: _logger.error("Volume %s\nis currently attached to " "instance %s (%s)\n" % (ocid, vol.get_instance().get_display_name(), vol.get_instance().get_public_ip())) return False print "Attaching OCI Volume to this instance." vol = vol.attach_to(instance_id=sess.this_instance().get_ocid(), wait=True) if _user_euid != 0: if vol.get_user() is not None: # requires CHAP auth user/password _logger.error("Run oci-iscsi-config with root privileges " "to attach this device.\n") return False else: # ocid will attach it automatically return True # attach using iscsiadm commands print "Attaching iSCSI device" retval = iscsiadm.attach(ipaddr=vol.get_portal_ip(), port=vol.get_portal_port(), iqn=vol.get_iqn(), username=vol.get_user(), password=vol.get_password(), auto_startup=True) print "Result: %s" \ % iscsiadm.error_message_from_code(retval) if retval == 0: return True return False