def repo_add_updateinfo(name, updateinfo_url=None, hostname=None): """Modify repo with contents of updateinfo.xml file. :param str name: repository name :param str optional updateinfo_url: URL to download updateinfo.xml file from. If not specified - updateinfo.xml from repository folder will be used instead :param str optional hostname: hostname or IP address of the remote host. If ``None`` the hostname will be get from ``main.server.hostname`` config. :return: result of executing `modifyrepo` command """ updatefile = 'updateinfo.xml' repo_path = f'{PULP_PUBLISHED_YUM_REPOS_PATH}/{name}' updatefile_path = f'{repo_path}/{updatefile}' if updateinfo_url: result = ssh.command(f'find {updatefile_path}', hostname=hostname) if result.return_code == 0 and updatefile in result.stdout[0]: result = ssh.command( f'mv -f {updatefile_path} {updatefile_path}.bak', hostname=hostname) if result.return_code != 0: raise CLIReturnCodeError( result.return_code, result.stderr, f'Unable to backup existing {updatefile}', ) result = ssh.command(f'wget -O {updatefile_path} {updateinfo_url}', hostname=hostname) if result.return_code != 0: raise CLIReturnCodeError(result.return_code, result.stderr, f'Unable to download {updateinfo_url}') result = ssh.command(f'modifyrepo {updatefile_path} {repo_path}/repodata/') return result
def create_repo(name, repo_fetch_url=None, packages=None, wipe_repodata=False, hostname=None): """Creates a repository from given packages and publishes it into pulp's directory for web access. :param str name: repository name - name of a directory with packages :param str repo_fetch_url: URL to fetch packages from :param packages: list of packages to fetch (with extension) :param wipe_repodata: whether to recursively delete repodata folder :param str optional hostname: hostname or IP address of the remote host. If ``None`` the hostname will be get from ``main.server.hostname`` config. :return: URL where the repository can be accessed :rtype: str """ repo_path = f'{PULP_PUBLISHED_YUM_REPOS_PATH}/{name}' result = ssh.command(f'sudo -u apache mkdir -p {repo_path}', hostname=hostname) if result.return_code != 0: raise CLIReturnCodeError(result.return_code, result.stderr, 'Unable to create repo dir') if repo_fetch_url: # Add trailing slash if it's not there already if not repo_fetch_url.endswith('/'): repo_fetch_url += '/' for package in packages: result = ssh.command( f'wget -P {repo_path} {urljoin(repo_fetch_url, package)}', hostname=hostname, ) if result.return_code != 0: raise CLIReturnCodeError( result.return_code, result.stderr, f'Unable to download package {package}', ) if wipe_repodata: result = ssh.command(f'rm -rf {repo_path}/repodata/', hostname=hostname) if result.return_code != 0: raise CLIReturnCodeError(result.return_code, result.stderr, 'Unable to delete repodata folder') result = ssh.command(f'createrepo {repo_path}', hostname=hostname) if result.return_code != 0: raise CLIReturnCodeError( result.return_code, result.stderr, f'Unable to create repository. stderr contains following info:\n{result.stderr}', ) published_url = 'http://{}{}/pulp/repos/{}/'.format( settings.server.hostname, f':{settings.server.port}' if settings.server.port else '', name, ) return published_url
def enroll_idm_and_configure_external_auth(default_sat): """Enroll the Satellite6 Server to an IDM Server.""" ipa_host = ContentHost(settings.ipa.hostname) default_sat.execute( 'yum -y --disableplugin=foreman-protector install ipa-client ipa-admintools' ) ipa_host.execute(f'echo {settings.ipa.password} | kinit admin') output = default_sat.execute(f'ipa host-find {default_sat.hostname}') if output.status != 0: result = ipa_host.execute( f'ipa host-add --random {default_sat.hostname}') for line in result.stdout.splitlines(): if 'Random password' in line: _, password = line.split(': ', 2) break ipa_host.execute(f'ipa service-add HTTP/{default_sat.hostname}') _, domain = settings.ipa.hostname.split('.', 1) result = default_sat.execute( f"ipa-client-install --password '{password}' " f'--domain {domain} ' f'--server {settings.ipa.hostname} ' f'--realm {domain.upper()} -U') if result.status not in [0, 3]: CLIReturnCodeError( result.status, result.stderr, )
def test_init(self): """Check properties initialization""" error = CLIReturnCodeError(1, u'stderr', u'msg') self.assertEqual(error.return_code, 1) self.assertEqual(error.stderr, u'stderr') self.assertEqual(error.msg, u'msg') self.assertEqual(error.message, error.msg)
def get_repo_files(repo_path, extension='rpm', hostname=None): """Returns a list of repo files (for example rpms) in specific repository directory. :param str repo_path: unix path to the repo, e.g. '/var/lib/pulp/fooRepo/' :param str extension: extension of searched files. Defaults to 'rpm' :param str optional hostname: hostname or IP address of the remote host. If ``None`` the hostname will be get from ``main.server.hostname`` config. :return: list representing rpm package names :rtype: list """ if not repo_path.endswith('/'): repo_path += '/' result = ssh.command( "find {} -name '*.{}' | awk -F/ '{{print $NF}}'" .format(repo_path, extension), hostname=hostname, ) if result.return_code != 0: raise CLIReturnCodeError( result.return_code, result.stderr, 'No .{} found'.format(extension) ) # strip empty lines and sort alphabetically (as order may be wrong because # of different paths) return sorted([repo_file for repo_file in result.stdout if repo_file])
def get_repomd_revision(repo_path, hostname=None): """Fetches a revision of repository. :param str repo_path: unix path to the repo, e.g. '/var/lib/pulp/fooRepo' :param str optional hostname: hostname or IP address of the remote host. If ``None`` the hostname will be get from ``main.server.hostname`` config. :return: string containing repository revision :rtype: str """ repomd_path = 'repodata/repomd.xml' result = ssh.command( "grep -oP '(?<=<revision>).*?(?=</revision>)' {}/{}" .format(repo_path, repomd_path), hostname=hostname, ) # strip empty lines stdout = [line for line in result.stdout if line] if result.return_code != 0 or len(stdout) != 1: raise CLIReturnCodeError( result.return_code, result.stderr, 'Unable to fetch revision for {}. Please double check your ' 'hostname, path and contents of repomd.xml'.format(repo_path) ) return stdout[0]
def run_command(cmd, hostname=satellite, timeout=None): """helper function for ssh command and avoiding the return code check in called function""" if timeout: result = ssh.command(cmd=cmd, hostname=hostname, timeout=timeout) else: result = ssh.command(cmd=cmd, hostname=hostname) if result.return_code != 0: raise CLIReturnCodeError( result.return_code, result.stderr, f"Failed to run the command : {cmd}", ) else: return result.stdout
def fake_128_return_code(): """Fake CLI response with 128 return_code""" # flake8:noqa (line-too-long) pylint:disable=C0301 raise CLIReturnCodeError( 128, u"""[ERROR 2017-03-01 05:58:50 API] 404 Resource Not Found [ERROR 2017-03-01 05:58:50 Exception] Resource medium not found by id \\'1\\' Resource medium not found by id \\'1\\' [ERROR 2017-03-01 05:58:50 Exception] RestClient::ResourceNotFound (404 Resource Not Found):""", u"""Command "medium info" finished with return_code 128 stderr contains following message: [ERROR 2017-03-01 05:58:50 API] 404 Resource Not Found [ERROR 2017-03-01 05:58:50 Exception] Resource medium not found by id \\'1\\' Resource medium not found by id \\'1\\' [ERROR 2017-03-01 05:58:50 Exception] RestClient::ResourceNotFound (404 Resource Not Found):""")
def test_str(self): """Check __str__ returns message attribute""" error = CLIReturnCodeError(1, u'stderr', u'msg') self.assertEqual(error.message, str(error))
def encrypt_virt_who_password(password, server=None): result = ssh.command('virt-who-password -p {}'.format(password), hostname=server) if result.return_code != 0: raise CLIReturnCodeError(result.return_code, result.stderr, "Error running virt-who-password") return result.stdout[0]