def run_health(api): config = scaffold.get_config() try: exe('ping -c 1 -w 1 {}'.format(config['mgmt_ip'])) except EnvironmentError: print('Could not ping mgmt_ip:', config['mgmt_ip']) return False try: api.app_instances.list() except Exception as e: print("Could not connect to cluster", e) return False npass = True av = api.system.network.access_vip.get() for np in av['network_paths']: ip = np.get('ip') if ip: try: exe('ping -c 1 -w 1 {}'.format(ip)) except EnvironmentError: print('Could not ping: {} {}'.format(np.get('name'), ip)) npass = False if not npass: return False print("Health Check Completed Successfully") return True
def _login(iqn, portals, multipath, lun): retries = 10 if not multipath: portals = [portals[0]] if lun == 0: for portal in portals: while True: dprint("Trying to log into target:", portal) try: exe("sudo iscsiadm -m discovery -t st -p {}:3260".format( portal)) exe("sudo iscsiadm -m node -T {iqn} -p {ip}:3260 " "--login".format(iqn=iqn, ip=portal)) break except EnvironmentError as e: if 'returned non-zero exit status 15' in str(e): break retries -= 1 if not retries: dprint("Could not log into portal before end of " "polling period") raise dprint("Failed to login to portal, retrying") time.sleep(2) _set_noop_scheduler(portals, iqn, lun) path = DEV_TEMPLATE.format(ip=portals[0], iqn=iqn, lun=lun) if multipath: dprint('Sleeping to allow for multipath devices to finish linking') time.sleep(2) dpath = _get_multipath_disk(path) else: dpath = path return dpath
def _unmount(ai_name, si_name, vol_name, directory): folder = get_dirname(directory, ai_name, si_name, vol_name) try: exe("sudo umount {}".format(folder)) except EnvironmentError as e: dprint(e) return exe("sudo rmdir {}".format(folder))
def find_mount(si, lun, multipath): ip = si.access['ips'][0] iqn = si.access['iqn'] path = DEV_TEMPLATE.format(ip=ip, iqn=iqn, lun=lun) if multipath: path = _get_multipath_disk(path) out = exe("ls -l {} | awk '{{print $NF}}'".format(path)) device = out.split("/")[-1].strip() if not device: return None, path, device mount = exe( "cat /proc/mounts | grep {} | awk '{{print $2}}'".format(device)) return mount.strip(), path, device
def _set_noop_scheduler(portals, iqn, lun): for portal in portals: path = DEV_TEMPLATE.format(ip=portal, iqn=iqn, lun=lun) device = None while True: out = exe("ls -l {} | awk '{{print $NF}}'".format(path)) device = out.split("/")[-1].strip() if device: break dprint("Waiting for device to be ready:", path) time.sleep(1) dprint("Setting noop scheduler for device:", device) exe("echo 'noop' | sudo tee /sys/block/{}/queue/scheduler".format( device))
def _logout(iqn, portals): for portal in portals: exe("sudo iscsiadm -m node -T {iqn} -p {ip}:3260 --logout".format( iqn=iqn, ip=portal), fail_ok=True) exe("sudo iscsiadm -m node -T {iqn} -p {ip}:3260 --op delete".format( iqn=iqn, ip=portal), fail_ok=True) exe("sudo iscsiadm -m discoverydb -p {ip}:3260 --op delete".format( ip=portal), fail_ok=True) exe("sudo iscsiadm -m session --rescan", fail_ok=True) exe("sudo multipath -F", fail_ok=True) dprint("Sleeping to wait for logout") time.sleep(2) dprint("Logout complete")
def find_from_mount(api, mount, t): device = exe("df -P {} | tail -1 | cut -d' ' -f 1".format(mount)).strip() if not device: print("No device found for mount:", mount) if t == 'ai': return find_ai_from_device_path(api, device) else: return find_from_device_path(api, device)
def _format_mount_device(path, fs, fsargs, folder): timeout = 5 while True: try: exe("sudo mkfs.{} {} {} ".format(fs, fsargs, path)) break except EnvironmentError: dprint("Checking for existing filesystem on:", path) try: out = exe( "sudo blkid {} | grep -Eo '(TYPE=\".*\")'".format(path)) parts = out.split("=") if len(parts) == 2: found_fs = parts[-1].lower().strip().strip('"') if found_fs == fs.lower(): dprint("Found existing filesystem, continuing") break except EnvironmentError: pass dprint("Failed to format {}. Waiting for device to be " "ready".format(path)) if not timeout: raise time.sleep(1) timeout -= 1 exe("sudo mkdir -p /{}".format(folder.strip("/"))) exe("sudo mount {} {}".format(path, folder)) print("Volume mount:", folder)
def _get_initiator(): file_path = '/etc/iscsi/initiatorname.iscsi' try: out = exe('sudo cat {}'.format(file_path)) for line in out.splitlines(): if line.startswith('InitiatorName='): return line.split("=", 1)[-1].strip() except EnvironmentError: dprint("Could not find the iSCSI Initiator File %s", file_path) raise
def iqn_lun_from_device(device): links = exe( "udevadm info --query=symlink --name={}".format(device)).split() links = filter(lambda x: 'by-path' in x, links) if len(links) == 0: if re.search('mapper', device): dmpath = exe("udevadm info --query=path --name={}".format(device)) dmdev = dmpath.split("/")[-1].strip() elif re.search('dm', device): dmdev = device.split("/")[-1] else: print("No /dev/disk/by-path link found for device:", device) return None, None slaves = exe("ls /sys/block/{}/slaves".format(dmdev)) slave = slaves.split()[1] links = exe( "udevadm info --query=symlink --name=/dev/{}".format(slave)).split() links = filter(lambda x: 'by-path' in x, links) link = links[0] match = IQN_RE.search(link) if not match: print("No iqn found in link:", link) return None, None return match.group('iqn'), int(match.group('lun'))
def _setup_initiator(api): initiator = _get_initiator() host = exe('hostname').strip() initiator_obj = None try: initiator_obj = api.initiators.get(initiator) # Handle case where initiator exists in parent tenant # We want to create a new initiator in the case tenant = api.context.tenant if not tenant: tenant = '/root' if initiator_obj.tenant != tenant: raise dat_exceptions.ApiNotFoundError() except dat_exceptions.ApiNotFoundError: initiator_obj = api.initiators.create(name=host, id=initiator) return initiator_obj