"""glusterutils module contains wrappers for gluster commands. *) glusterd_start *) glusterd_start_allservers *) glusterd_stop *) glusterd_stop_allservers *) glusterd_restart *) glusterd_remove_dir *) glusterd_remove_dir_allservers *) glusterd_remove_logs_allservers *) volume_delete *) volume_create *) volume_start *) volume_stop *) volume_addbrick *) volume_replacebrick *) volume_set *) volume_reset *) peer_probe *) create_brick *) mount_exportdir *) umount_exportdir """ import re import atfutils import hostutils from atfglobals import GlobalObj def glusterd_start(serverkey, force=False): """ """ logger = GlobalObj.getLoggerObj() env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() commands_to_execute = ["which glusterd", "ps -e | grep glusterd"] gluster_version = env.getServer(serverkey).glusterversion host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 """ Check if gluster is already running. If already Running and force=True, restart glusterd process""" command = commands_to_execute.pop() output = host_connection.executecommand(command) return_status = atfutils.assert_success(**output) if not return_status: if force: return_status = glusterd_restart(serverkey) return return_status else: return return_status command = commands_to_execute.pop() output = host_connection.executecommand(command) if output["exitstatus"]: logger.error("Unable to start glusterd") return_status = atfutils.assert_success(**output) return return_status else: if output["stdoutdata"]: gluster_path = None gluster_path = output["stdoutdata"][0].strip("\n") else: logger.error("Unable to find gluster path") return_status = atfutils.assert_success(**output) return return_status if gluster_path: command = gluster_path + " --version" output = host_connection.executecommand(command) if not output["stdoutdata"] == None: if re.search(gluster_version, str(output["stdoutdata"])): logger.debug('%s: Executing Command: %s' %(serverkey, gluster_path)) output = host_connection.executecommand(gluster_path) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status else: logger.error("Unable to start glusterd") return 1 else: logger.error("Unable to start glusterd") return 1 def glusterd_start_allservers(force=False): """ """ env = GlobalObj.getTestenvObj() all_servers = env.getServers() for serverkey in all_servers.keys(): return_status = glusterd_start(serverkey) if return_status: return return_status return 0 def glusterd_stop(serverkey): """ """ logger = GlobalObj.getLoggerObj() base_command = "kill -KILL " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 gluster_pid_list = [] output = host_connection.executecommand("pidof glusterd") return_status = atfutils.assert_failure(**output) if not return_status: return return_status else: if output["stdoutdata"]: for output in output["stdoutdata"]: gluster_pid_list.append(output) for pid in gluster_pid_list: command = base_command + pid logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) if return_status: break return return_status def glusterd_stop_allservers(): """ """ env = GlobalObj.getTestenvObj() all_servers = env.getServers() for serverkey in all_servers.keys(): return_status = glusterd_stop(serverkey) if return_status: return return_status return 0 def glusterd_restart(serverkey): """ """ return_status = glusterd_stop(serverkey) if return_status: return return_status else: return_status = glusterd_start(serverkey) return return_status def glusterd_remove_dir(serverkey): """ """ logger = GlobalObj.getLoggerObj() base_command = "rm -rf" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() glusterd_dir = GlobalObj.glusterd_dir server_obj = env.getServer(serverkey) if not server_obj: logger.error("Invalid Host. %s not defined in TestEnvironment" % serverkey) return 1 server_connection = cm.getConnection(serverkey) if not server_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 command = ' '.join([base_command, glusterd_dir]) logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return 0 def glusterd_remove_dir_allservers(): """ """ env = GlobalObj.getTestenvObj() all_servers = env.getServers() for serverkey in all_servers.keys(): return_status = glusterd_remove_dir(serverkey) return 0 def glusterd_remove_logs(serverkey): """ """ logger = GlobalObj.getLoggerObj() base_command = "rm -rf" log_paths = GlobalObj.glusterd_log_paths absolute_path_list = [] prefix_path = '' env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() server_obj = env.getServer(serverkey) if not server_obj: logger.error("Invalid Host. %s not defined in TestEnvironment" % serverkey) return 1 server_connection = cm.getConnection(serverkey) if not server_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 if server_obj.installpath: prefix_path = server_obj.installpath for path in log_paths: absolute_path_list.append(prefix_path + path) for path in absolute_path_list: command = ' '.join([base_command, path]) logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return 0 def glusterd_remove_logs_allservers(): """ """ env = GlobalObj.getTestenvObj() all_servers = env.getServers() for serverkey in all_servers.keys(): return_status = glusterd_remove_logs(serverkey) return 0 def volume_delete(serverkey): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume delete " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: logger.error("Invalid Volume.ActiveVolume not defined" + "for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def volume_create(serverkey): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume create" command = [base_command] env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: logger.error("ActiveVolume not defined for the TestEnvironment") return 1 command.extend([active_volume.volumename]) if active_volume.replica: command.extend(["replica", active_volume.replica]) if active_volume.stripe: command.extend(["stripe", active_volume.stripe]) if active_volume.transporttype: command.extend(["transport", active_volume.transporttype]) command = ' '.join(command) for brick_obj in active_volume.bricks: brick_value = brick_obj.hostname + ":" + brick_obj.path command = ' '.join([command, brick_value]) host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if return_status: if str(output["stdoutdata"]).rfind("already exists"): return_status = 0 atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def volume_start(serverkey, force=False): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume start " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename if force: command = command + " force" host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if return_status: if str(output["stdoutdata"]).rfind("already started"): return_status = 0 atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def volume_stop(serverkey, force=False): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume stop " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename if force: command = command + " force" host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def volume_addbrick(serverkey, *bricks): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume add-brick" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() command = [base_command] if not active_volume: logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command.extend([volumename]) for brick in bricks: brick_obj = env.getBrick(brick) if not brick_obj: logger.error("Invalid Brick. Brick Not defined in TestEnvironment") return 1 brick_value = brick_obj.hostname + ":" + brick_obj.path command.extend([brick_value]) command = ' '.join(command) host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if not return_status: if env.addBricksToVolume(*bricks): return 1 atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def volume_replacebrick(serverkey, replacebrick_key, tobrick_key): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume replace-brick " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() command = [base_command] active_volume = env.getActiveVolume() if not active_volume: logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename replace_brick = env.getbrick(replacebrick_key) to_brick = env.getbrick(tobrick_key) if not (to_brick and replace_brick): logger.error("Invalid Brick. Brick Not defined in TestEnvironment") return 1 command.extend([volumename, replace_brick, to_brick]) command = ' '.join(command) host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) if not return_status: if env.replaceBrickInVolume(replacebrick_key, tobrick_key): return 1 atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def volume_set(serverkey, key, value): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume set" env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() command = [base_command] active_volume = env.getActiveVolume() if not active_volume: logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command.extend([volumename, key, value]) command = ' '.join(command) host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def volume_reset(serverkey): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster volume reset " env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() active_volume = env.getActiveVolume() if not active_volume: logger.error("ActiveVolume not defined for the TestEnvironment") return 1 volumename = active_volume.volumename command = base_command + volumename host_connection = cm.getConnection(serverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = host_connection.executecommand(command, commandInput="y\n") return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def peer_probe(fromserverkey): """ """ logger = GlobalObj.getLoggerObj() base_command = "gluster peer probe " command = [base_command] all_servers = {} env = GlobalObj.getTestenvObj() cm = GlobalObj.getConnectionsManagerObj() all_servers = env.getServers() all_servers.pop(fromserverkey) for key in all_servers.keys(): if key is fromserverkey: continue else: server_obj = all_servers[key] command.extend([server_obj.hostname]) command = ' '.join(command) host_connection = cm.getConnection(fromserverkey) if not host_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 logger.debug('%s: Executing Command: %s' % (fromserverkey, command)) output = host_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def create_brick(brickkey): """ """ logger = GlobalObj.getLoggerObj() return_status = 1 env = GlobalObj.getTestenvObj() brick_obj = env.getRawBrick(brickkey) hostname_value = brick_obj.hostname serverkey = re.split("\.", hostname_value, maxsplit=1)[0] exportdir = brick_obj.path device = fstype = None """If the exportdir is not a mount point of a device: 1) Remove the existing exportdir 2) Create new exportdir""" if re.match("^\/", exportdir): dirpath = exportdir else: export_obj = env.getExportdir(exportdir) dirpath = export_obj.dir device = export_obj.device fstype = export_obj.fstype options = export_obj.options logger.debug('%s: Executing Command: %s'% (serverkey, 'create_brick')) if device: if umount_device(serverkey, device): return return_status if hostutils.mkfs(serverkey, device, fstype): return return_status if mount_exportdir(serverkey, device, fstype, options, dirpath): return return_status return 0 else: if hostutils.rmdir(serverkey, dirpath): return return_status if hostutils.mkdir(serverkey, dirpath): return return_status return 0 def umount_device(serverkey, device): """ """ logger = GlobalObj.getLoggerObj() base_command = "umount " cm = GlobalObj.getConnectionsManagerObj() server_connection = cm.getConnection(serverkey) if not server_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 mountpoints = hostutils.find_mountpoints(serverkey, device) for mountpoint in mountpoints: command = base_command + mountpoint logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) if return_status: stdoutdata = str(output["stdoutdata"]) if ((stdoutdata.rfind("not found")) or (stdoutdata.rfind("not mount"))): return_status = 0 else: return return_status return 0 def mount_exportdir(serverkey, device, fstype, options, dirpath): """ """ logger = GlobalObj.getLoggerObj() base_command = "mount " cm = GlobalObj.getConnectionsManagerObj() command = [base_command] server_connection = cm.getConnection(serverkey) if not server_connection: logger.error("SSH connection to host '%s' has not been established" % serverkey) return 1 if fstype is None: fstype = "xfs" command.extend(["-t", fstype]) if options: command.extend([options]) command.extend([device, dirpath]) command = ' '.join(command) logger.debug('%s: Executing Command: %s' % (serverkey, command)) output = server_connection.executecommand(command) return_status = atfutils.assert_success(**output) atfutils.print_stdout(output['stdoutdata']) atfutils.print_stderr(output['stderrdata']) return return_status def create_brick_allservers(): """ """ env = GlobalObj.getTestenvObj() brick_keys = env.getBrickKeys() for brickkey in brick_keys: return_status = create_brick(brickkey) if return_status: return return_status return 0 __all__ = ['glusterd_start', 'glusterd_start_allservers', 'glusterd_stop', 'glusterd_stop_allservers', 'glusterd_restart', 'glusterd_remove_dir', 'glusterd_remove_dir_allservers', 'glusterd_remove_logs_allservers', 'volume_delete', 'volume_create', 'volume_start', 'volume_stop', 'volume_addbrick', 'volume_replacebrick', 'volume_set', 'volume_reset', 'peer_probe', 'create_brick', 'create_brick_allservers', 'mount_exportdir', 'umount_device']