def prepare_config_file(self): ''' Prepare DRDB resource config file ''' context = [ rf'resource {self.res_name} {{', rf'\ \ \ \ on maxluntarget {{', rf'\ \ \ \ \ \ \ \ device /dev/{self.drbd_device_name}\;', rf'\ \ \ \ \ \ \ \ disk {self.blk_dev_name}\;', rf'\ \ \ \ \ \ \ \ address 10.203.1.199:7789\;', rf'\ \ \ \ \ \ \ \ node-id 0\;', rf'\ \ \ \ \ \ \ \ meta-disk internal\;', r'\ \ \ \}', r'}' ] # for echo_command in context: # echo_result = self.ssh.excute_command( # f'echo {echo_command} >> /etc/drbd.d/{self.res_name}.res') # if echo_result is True: # continue # else: # s.pe('fail to prepare drbd config file..') for i in range(len(context)): if i == 0: echo_result = self.ssh.excute_command( f'echo {context[i]} > /etc/drbd.d/{self.res_name}.res') else: echo_result = self.ssh.excute_command( f'echo {context[i]} >> /etc/drbd.d/{self.res_name}.res') if echo_result is True: continue else: s.pe('fail to prepare drbd config file..') print(f'Create DRBD config file "{self.res_name}.res" done')
def start_test(self): if not self.find_session: self.iscsi_login() dev_name = self.explore_disk() mount_status = self.format_mount(dev_name) if mount_status: self.get_test_perf() else: s.pe(f'Device {dev_name} mount failed')
def _setting_order(self): ''' Setting up iSCSILogicalUnit resources of order ''' order_cmd = f'crm conf order {self.order_name} {self.target_name} {self.lu_name}' set_order = self.ssh.excute_command(order_cmd) if set_order is True: print('setting order succeed') return True else: s.pe('setting order failed')
def _setting_col(self): ''' Setting up iSCSILogicalUnit resources of colocation ''' col_cmd = f'crm conf colocation {self.colocation_name} inf: {self.lu_name} {self.target_name}' set_col = self.ssh.excute_command(col_cmd) if set_col is True: print('setting colocation successful') return True else: s.pe('setting colocation failed')
def _drbd_primary(self): ''' Complete initial synchronization of resources ''' primary_cmd = f'drbdadm primary --force {self.res_name}' drbd_primary = self.ssh.excute_command(primary_cmd) if drbd_primary is True: print(f'{self.res_name} primary success') return True else: s.pe(f'drbd resource {self.res_name} primary failed')
def _drbd_up(self): ''' Start DRBD resource ''' up_cmd = f'drbdadm up {self.res_name}' drbd_up = self.ssh.excute_command(up_cmd) if drbd_up is True: print(f'{self.res_name} up success') return True else: s.pe(f'drbd resource {self.res_name} up failed')
def _connect(self): try: self.telnet.open(self._host, self._port) self.telnet.read_until(b'Username:'******'\n') self.telnet.read_until(b'Password:'******'\n') except Exception as e: s.pe(f'Connect to {self._host} failed with error: {e}')
def _crm_start(self): ''' start the iSCSILogicalUnit resource ''' crm_start_cmd = f'crm res start {self.lu_name}' crm_start = self.ssh.excute_command(crm_start_cmd) if crm_start is True: print('iscsi lun start successful') return True else: s.pe('iscsi lun start failed')
def _connect(self): try: objSSHClient = paramiko.SSHClient() objSSHClient.set_missing_host_key_policy(paramiko.AutoAddPolicy()) objSSHClient.connect(self._host, port=self._port, username=self._username, password=self._password, timeout=self._timeout) self.SSHConnection = objSSHClient except Exception as e: s.pe(f'Connect to {self._host} failed with error: {e}')
def _get_dd_perf(self, arg_str): ''' Use re to get the speed of test ''' re_performance = re.compile(r'.*s, ([0-9.]* [A-Z]B/s)') string = arg_str.decode('utf-8') re_result = re_performance.findall(string) perf = re_result if perf: return perf[0] else: s.pe('Can not get test result')
def explore_disk(self): ''' Scan and get the device path from VersaPLX ''' if self.ssh.excute_command('/usr/bin/rescan-scsi-bus.sh'): time.sleep(0.5) lsscsi_result = self.ssh.excute_command('lsscsi') else: s.pe(f'Scan new LUN failed on VersaPLX') re_find_id_dev = r'\:(\d*)\].*LIO-ORG[ 0-9a-zA-Z._]*(/dev/sd[a-z]{1,3})' return s.GetDiskPath(self.id, re_find_id_dev, lsscsi_result, 'VersaPLX').explore_disk()
def discover_new_lun(self): ''' Scan and find the disk from NetApp ''' if self.ssh.excute_command('/usr/bin/rescan-scsi-bus.sh'): lsscsi_result = self.ssh.excute_command('lsscsi') else: s.pe(f'Scan new LUN failed on NetApp') re_find_id_dev = r'\:(\d*)\].*NETAPP[ 0-9a-zA-Z._]*(/dev/sd[a-z]{1,3})' self.blk_dev_name = s.GetDiskPath(self.id, re_find_id_dev, lsscsi_result, 'NetApp').explore_disk() print(f'Find device {self.blk_dev_name} for LUN id {self.id}')
def iscsi_login(self): ''' Discover iSCSI and login to session ''' login_cmd = f'iscsiadm -m discovery -t st -p {vplx_ip} -l' login_result = self.ssh.excute_command(login_cmd) if login_result: login_result = login_result.decode('utf-8') re_login = re.compile(f'Login to.*portal: ({vplx_ip}).*successful') re_result = re_login.findall(login_result) if re_result: return True else: s.pe(f'iscsi login to {vplx_ip} failed')
def _crm_create(self): ''' Create iSCSILogicalUnit resource ''' crm_create_cmd = f'crm conf primitive {self.lu_name} \ iSCSILogicalUnit params target_iqn="{self.target_iqn}" \ implementation=lio-t lun={self.id} path="/dev/{self.drbd_device_name}"\ allowed_initiators="{self.initiator_iqn}" op start timeout=40 interval=0 op stop timeout=40 interval=0 op monitor timeout=40 interval=50 meta target-role=Stopped' if self.ssh.excute_command(crm_create_cmd) is True: print('iscisi lun_create success') return True else: s.pe('iscisi lun_create failed')
def format_mount(self, dev_name): ''' Format disk and mount disk ''' format_cmd = f'mkfs.ext4 {dev_name} -F' cmd_result = self.ssh.excute_command(format_cmd) if self._judge_format(cmd_result): mount_cmd = f'mount {dev_name} {mount_point}' if self.ssh.excute_command(mount_cmd) == True: return True else: s.pe(f"mount {dev_name} to {mount_point} failed") else: s.pe("format disk %s failed" % dev_name)
def drbd_status_verify(self): ''' Check DRBD resource status and confirm the status is UpToDate ''' verify_cmd = f'drbdadm status {self.res_name}' result = self.ssh.excute_command(verify_cmd) if result: result = result.decode('utf-8') re_display = re.compile(r'''disk:(\w*)''') re_result = re_display.findall(result) if re_result: status = re_result[0] if status == 'UpToDate': print(f'{self.res_name} DRBD check successful') return True else: s.pe(f'{self.res_name} DRBD verification failed') else: s.pe(f'{self.res_name} DRBD does not exist')
def _drbd_init(self): ''' Initiakize DRBD resource ''' init_cmd = f'drbdadm create-md {self.res_name}' drbd_init = self.ssh.excute_command(init_cmd) if drbd_init: drbd_init = drbd_init.decode('utf-8') re_drbd = re.compile( 'New drbd meta data block successfully created') re_init = re_drbd.findall(drbd_init) if re_init: print(f'{self.res_name} initialize success') return True else: s.pe(f'drbd resource {self.res_name} initialize failed') else: s.pe(f'drbd resource {self.res_name} initialize failed')