def _create_remote_dirs(self): """Create mandatory directories in remote host.""" cmd = self.cfg.remote_mkdir + [self._remote_testplan_path] execute_cmd(self.cfg.ssh_cmd(self.ssh_cfg, ' '.join([str(a) for a in cmd])), label='create remote dirs', logger=self.logger)
def _execute_cmd_remote(self, cmd, label=None, check=True): """ Execute a command on the remote host. :param cmd: Remote command to execute - list of parameters. :param label: Optional label for debugging. :param check: Whether to check command return-code - defaults to True. See self._execute_cmd for more detail. """ execute_cmd(self.cfg.ssh_cmd(self.ssh_cfg, ' '.join([str(a) for a in cmd])), label=label, check=check, logger=self.logger)
def _mkdir_remote(self, remote_dir, label=None): """ Create a directory path on the remote host. :param remote_dir: Path to create. :param label: Optional debug label. """ if not label: label = 'remote mkdir' cmd = self.cfg.remote_mkdir + [remote_dir] execute_cmd(self.cfg.ssh_cmd(self.ssh_cfg, ' '.join([str(a) for a in cmd])), label=label, logger=self.logger)
def starting(self): """Starts the Zookeeper instance.""" super(ZookeeperStandalone, self).starting() start_cmd = [self.cfg.binary, "start", self.config] self.std = StdFiles(self.runpath) execute_cmd( start_cmd, label=self.uid(), check=True, stdout=self.std.out, stderr=self.std.err, logger=self.logger, env=self.env, )
def stopping(self): """Stops the Zookeeper instance.""" stop_cmd = [self.cfg.binary, "stop", self.config] try: execute_cmd( stop_cmd, label=self.uid(), check=True, stdout=self.std.out, stderr=self.std.err, logger=self.logger, env=self.env, ) finally: self.std.close()
def test_pool_basic(mockplan, remote_pool_type): """Basic test scheduling.""" workspace, schedule_path = setup_workspace() try: # Make sure our current working directory is within the workspace - # testplan requires this. orig_dir = os.getcwd() os.chdir(workspace) schedule_tests_to_pool( mockplan, RemotePool, hosts={REMOTE_HOST: 2}, workspace=workspace, pool_type=remote_pool_type, schedule_path=schedule_path, restart_count=0, clean_remote=True, ) finally: assert 0 == execute_cmd( ssh_cmd({"host": REMOTE_HOST}, f"test -L {workspace}"), label="workspace imitated on remote", check=False, ) os.chdir(orig_dir) shutil.rmtree(workspace)
def _copy_testplan_package(self): """Make testplan package available on remote host""" module_path = os.path.dirname(module_abspath(testplan)) self._testplan_import_path.local = os.path.abspath( os.path.join(module_path, '..')) if self.cfg.testplan_path: self._testplan_import_path.remote = self.cfg.testplan_path return # test if testplan package is available on remote host cmd = remote_filepath_exists(self.cfg.ssh_cmd, self.ssh_cfg, self._testplan_import_path.local) if 0 == execute_cmd(cmd, label='testplan package availability check', check=False, logger=self.logger): # exists on remote self._testplan_import_path.remote = self._testplan_import_path.local else: # copy to remote self._testplan_import_path.remote = os.path.join( self._remote_testplan_path, 'testplan_lib') # as module_path has no trailing / # this will copy the entire testplan subdir to testplan_lib self._transfer_data(source=module_path, target=self._testplan_import_path.remote, remote_target=True, deref_links=True)
def _copy_testplan_package(self): """Make testplan package available on remote host""" self._testplan_import_path.local = self._get_testplan_import_path() if self.cfg.testplan_path: self._testplan_import_path.remote = self.cfg.testplan_path return # test if testplan package is available on remote host cmd = remote_filepath_exists(self.cfg.ssh_cmd, self.ssh_cfg, self._testplan_import_path.local) if 0 == execute_cmd( cmd, label="testplan package availability check", check=False, logger=self.logger, ): # exists on remote self._testplan_import_path.remote = ( self._testplan_import_path.local) else: # copy to remote self._testplan_import_path.remote = os.path.join( self._remote_testplan_path, "testplan_lib") # add trailing / to _testplan_import_path.local # this will copy everything under import path to to testplan_lib self._transfer_data( source=os.path.join(self._testplan_import_path.local, ""), target=self._testplan_import_path.remote, remote_target=True, deref_links=True, )
def _copy_workspace(self): """Make the local workspace available on remote host.""" self._workspace_paths.local = fix_home_prefix(self.cfg.workspace) self._workspace_paths.remote = '{}/{}'.format( self._remote_testplan_path, self._workspace_paths.local.split(os.sep)[-1]) if self.cfg.remote_workspace: # User defined the remote workspace to be used # Make a soft link and return execute_cmd(self.cfg.ssh_cmd( self.ssh_cfg, ' '.join( self.cfg.link_cmd(path=fix_home_prefix( self.cfg.remote_workspace), link=self._workspace_paths.remote))), label='linking to remote workspace (1).', logger=self.logger) return copy = True # flag to make a copy of workspace to remote if self.cfg.copy_workspace_check: cmd = self.cfg.copy_workspace_check(self.cfg.ssh_cmd, self.ssh_cfg, self._workspace_paths.local) copy = execute_cmd(cmd, label='workspace availability check', check=False, logger=self.logger) != 0 if copy: # Workspace should be copied to remote. self._transfer_data(source=self._workspace_paths.local, target=self._remote_testplan_path, remote_target=True, exclude=self.cfg.workspace_exclude) # Mark that workspace pushed is safe to delete. Not some NFS. self.setup_metadata.workspace_pushed = True else: # Make a soft link instead of copying workspace. execute_cmd(self.cfg.ssh_cmd( self.ssh_cfg, ' '.join( self.cfg.link_cmd(path=self._workspace_paths.local, link=self._workspace_paths.remote))), label='linking to remote workspace (2).', logger=self.logger)
def _transfer_data(self, source, target, remote_source=False, remote_target=False, **copy_args): if remote_source: source = self._remote_copy_path(source) if remote_target: target = self._remote_copy_path(target) self.logger.debug('Copying %(source)s to %(target)s', locals()) cmd = self.cfg.copy_cmd(source, target, **copy_args) with open(os.devnull, 'w') as devnull: execute_cmd(cmd, 'transfer data [..{}]'.format( os.path.basename(target)), stdout=devnull, logger=self.logger)
def _transfer_data( self, source, target, remote_source=False, remote_target=False, **copy_args, ): if remote_source: source = self._remote_copy_path(source) if remote_target: target = self._remote_copy_path(target) self.logger.debug("Copying %(source)s to %(target)s", locals()) cmd = self.cfg.copy_cmd(source, target, port=self.ssh_cfg["port"], **copy_args) with open(os.devnull, "w") as devnull: execute_cmd( cmd, "transfer data [..{}]".format(os.path.basename(target)), stdout=devnull, logger=self.logger, )
def stopping(self): """Stops the Zookeeper instance.""" stop_cmd = [self.cfg.binary, "stop", self.config] execute_cmd(stop_cmd, check=True, logger=self.logger)