def test_18_repo_select_0(): '''check if no repo is chosen if 0 is entered when adding a repo''' # for RHBZ#1305612 # upload the small cert and try entering 0 when the list of repos is displayed RHUIManagerEntitlements.upload_rh_certificate( RHUA, "/tmp/extra_rhui_files/rhcert_atomic.pem") RHUIManager.screen(RHUA, "repo") Expect.enter(RHUA, "a") Expect.expect(RHUA, "Enter value", 180) Expect.enter(RHUA, "3") Expect.expect(RHUA, "Enter value") Expect.enter(RHUA, "0") Expect.expect(RHUA, "Enter value") Expect.enter(RHUA, "c") Expect.expect(RHUA, "Proceed") Expect.enter(RHUA, "y") Expect.expect(RHUA, "Content") Expect.enter(RHUA, "q") # the RHUI repo list ought to be empty now; if not, delete the repo and fail repo_list = RHUIManagerRepo.list(RHUA) RHUIManager.remove_rh_certs(RHUA) if repo_list: RHUIManagerRepo.delete_all_repos(RHUA) raise AssertionError("The repo list is not empty: %s." % repo_list)
def add_cds(connection, clustername, cdsname, hostname="", displayname=""): ''' register (add) a new CDS instance ''' RHUIManager.screen(connection, "cds") RHUIManagerCds._add_cds_part1(connection, cdsname, hostname, displayname) state = Expect.expect_list(connection, [(re.compile(".*Enter a CDS cluster name:.*", re.DOTALL), 1), (re.compile(".*Select a CDS cluster or enter a new one:.*", re.DOTALL), 2)]) if state == 1: Expect.enter(connection, clustername) else: Expect.enter(connection, 'b') Expect.expect(connection, "rhui \(cds\) =>") RHUIManagerCds._add_cds_part1(connection, cdsname, hostname, displayname) RHUIManagerCds._select_cluster(connection, clustername) # We need to compare the output before proceeding checklist = ["Hostname: " + cdsname] if hostname != "": checklist.append("Client Hostname: " + hostname) else: checklist.append("Client Hostname: " + cdsname) if displayname != "": checklist.append("Name: " + displayname) else: checklist.append("Name: " + cdsname) checklist.append("Cluster: " + clustername) RHUIManager.proceed_with_check(connection, "The following CDS instance will be registered:", checklist) RHUIManager.quit(connection, "Successfully registered")
def remove_rh_certs(connection): ''' Remove all RH certificates from RHUI ''' Expect.enter(connection, "find /etc/pki/rhui/redhat/ -name '*.pem' -delete") Expect.expect(connection, "root@")
def add_cds(connection, clustername, cdsname, hostname="", displayname=""): """ register (add) a new CDS instance """ RHUIManager.screen(connection, "cds") RHUIManagerCds._add_cds_part1(connection, cdsname, hostname, displayname) state = Expect.expect_list( connection, [ (re.compile(".*Enter a CDS cluster name:.*", re.DOTALL), 1), (re.compile(".*Select a CDS cluster or enter a new one:.*", re.DOTALL), 2), ], ) if state == 1: Expect.enter(connection, clustername) else: Expect.enter(connection, "b") Expect.expect(connection, "rhui \(cds\) =>") RHUIManagerCds._add_cds_part1(connection, cdsname, hostname, displayname) RHUIManagerCds._select_cluster(connection, clustername) # We need to compare the output before proceeding checklist = ["Hostname: " + cdsname] if hostname != "": checklist.append("Client Hostname: " + hostname) else: checklist.append("Client Hostname: " + cdsname) if displayname != "": checklist.append("Name: " + displayname) else: checklist.append("Name: " + cdsname) checklist.append("Cluster: " + clustername) RHUIManager.proceed_with_check(connection, "The following CDS instance will be registered:", checklist) RHUIManager.quit(connection, "Successfully registered")
def upload_rh_certificate(connection, certificate_file="/tmp/extra_rhui_files/rhcert.pem"): ''' upload a new or updated Red Hat content certificate ''' bad_cert_msg = "The provided certificate is expired or invalid" incompatible_cert_msg = "does not contain any entitlements" RHUIManager.screen(connection, "entitlements") Expect.enter(connection, "u") Expect.expect(connection, "Full path to the new content certificate:") Expect.enter(connection, certificate_file) state = Expect.expect_list(connection, [(re.compile(".*The RHUI will be updated.*", re.DOTALL), 1), (re.compile(".*Cannot find file.*", re.DOTALL), 2)]) if state == 2: Expect.enter(connection, CTRL_C) RHUIManager.quit(connection) raise MissingCertificate("No such certificate file: %s" % certificate_file) Expect.enter(connection, "y") match = Expect.match(connection, re.compile("(.*)" + PROMPT, re.DOTALL)) matched_string = match[0].replace('l\r\n\r\nRed Hat Entitlements\r\n\r\n ' + '\x1b[92mValid\x1b[0m\r\n ', '', 1) if bad_cert_msg in matched_string: Expect.enter(connection, 'q') raise BadCertificate() if incompatible_cert_msg in matched_string: Expect.enter(connection, 'q') raise IncompatibleCertificate() entitlements_list = [] pattern = re.compile('(.*?\r\n.*?pem)', re.DOTALL) for entitlement in pattern.findall(matched_string): entitlements_list.append(entitlement.strip()) Expect.enter(connection, 'q') return entitlements_list
def create_conf_rpm(connection, dirname, certpath, certkey, rpmname, rpmversion="", unprotected_repos=None): ''' create a client configuration RPM from an entitlement certificate ''' RHUIManager.screen(connection, "client") Expect.enter(connection, "c") Expect.expect(connection, "Full path to local directory.*:") Expect.enter(connection, dirname) Expect.expect(connection, "Name of the RPM:") Expect.enter(connection, rpmname) Expect.expect(connection, "Version of the configuration RPM.*:") Expect.enter(connection, rpmversion) Expect.expect(connection, "Full path to the entitlement certificate.*:") Expect.enter(connection, certpath) Expect.expect( connection, "Full path to the private key for the above entitlement certificate:" ) Expect.enter(connection, certkey) if unprotected_repos: RHUIManager.select(connection, unprotected_repos) RHUIManager.quit(connection)
def generate_gpg_key(connection, keytype="DSA", keysize="1024", keyvalid="0", realname="Key Owner", email="*****@*****.**", comment="comment"): ''' Generate GPG keypair WARNING!!! It takes too long to wait for this operation to complete... use pre-created keys instead! ''' Expect.enter(connection, "cat > /tmp/gpgkey << EOF") Expect.enter(connection, "Key-Type: " + keytype) Expect.enter(connection, "Key-Length: " + keysize) Expect.enter(connection, "Subkey-Type: ELG-E") Expect.enter(connection, "Subkey-Length: " + keysize) Expect.enter(connection, "Name-Real: " + realname) Expect.enter(connection, "Name-Comment: " + comment) Expect.enter(connection, "Name-Email: " + email) Expect.enter(connection, "Expire-Date: " + keyvalid) Expect.enter(connection, "%commit") Expect.enter(connection, "%echo done") Expect.enter(connection, "EOF") Expect.expect(connection, "root@") Expect.enter(connection, "gpg --gen-key --no-random-seed-file --batch /tmp/gpgkey") for _ in xrange(1, 200): Expect.enter(connection, ''.join(random.choice(string.ascii_lowercase) for x in range(200))) time.sleep(1) try: Expect.expect(connection, "gpg: done") break except ExpectFailed: continue
def check_for_package(connection, reponame, package): ''' list packages in a repository ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "p") RHUIManager.select_one(connection, reponame) Expect.expect(connection, "\(blank line for no filter\):") Expect.enter(connection, package) pattern = re.compile('.*only\.\r\n(.*)\r\n-+\r\nrhui\s* \(repo\)\s* =>', re.DOTALL) ret = Expect.match(connection, pattern, grouplist=[1])[0] reslist = map(lambda x: x.strip(), ret.split("\r\n")) print reslist packagelist = [] for line in reslist: if line == '': continue if line == 'Packages:': continue if line == 'No packages found that match the given filter.': continue if line == 'No packages in the repository.': continue packagelist.append(line) Expect.enter(connection, 'q') return packagelist
def upload_content(connection, repolist, path): ''' upload content to a custom repository ''' # Temporarily quit rhui-manager and check whether "path" is a file or a directory. # If it is a directory, get a list of *.rpm files in it. Expect.enter(connection, 'q') Expect.enter(connection, "stat -c %F " + path) path_type = Expect.expect_list(connection, [(re.compile(".*regular file.*", re.DOTALL), 1), (re.compile(".*directory.*", re.DOTALL), 2)]) if path_type == 1: content = [basename(path)] elif path_type == 2: Expect.enter(connection, "echo " + path + "/*.rpm") output = Expect.match(connection, re.compile("(.*)", re.DOTALL))[0] rpm_files = output.splitlines()[1] content = [] for rpm_file in rpm_files.split(): content.append(basename(rpm_file)) else: # This should not happen. Getting here means that "path" is neither a file nor a directory. # Anyway, going on with no content, leaving it up to proceed_with_check() to handle this situation. content = [] # Start rhui-manager again and continue. RHUIManager.initial_run(connection) RHUIManager.screen(connection, "repo") Expect.enter(connection, "u") RHUIManager.select(connection, repolist) Expect.expect(connection, "will be uploaded:") Expect.enter(connection, path) RHUIManager.proceed_with_check(connection, "The following RPMs will be uploaded:", content) Expect.expect(connection, "rhui \(" + "repo" + "\) =>")
def check_for_package(connection, reponame, package): ''' list packages in a repository ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "p") RHUIManager.select_one(connection, reponame) Expect.expect(connection, r"\(blank line for no filter\):") Expect.enter(connection, package) pattern = re.compile(r'.*only\.\r\n(.*)\r\n-+\r\nrhui\s* \(repo\)\s* =>', re.DOTALL) ret = Expect.match(connection, pattern, grouplist=[1])[0] reslist = map(str.strip, str(ret).splitlines()) packagelist = [] for line in reslist: if line == '': continue if line == 'Packages:': continue if line == 'No packages found that match the given filter.': continue if line == 'No packages in the repository.': continue packagelist.append(line) Expect.enter(connection, 'q') return packagelist
def upload_content(connection, repolist, path): ''' upload content to a custom repository ''' # Temporarily quit rhui-manager and check whether "path" is a file or a directory. # If it is a directory, get a list of *.rpm files in it. Expect.enter(connection, 'q') Expect.enter(connection, "stat -c %F " + path) path_type = Expect.expect_list( connection, [(re.compile(".*regular file.*", re.DOTALL), 1), (re.compile(".*directory.*", re.DOTALL), 2)]) if path_type == 1: content = [basename(path)] elif path_type == 2: Expect.enter(connection, "echo " + path + "/*.rpm") output = Expect.match(connection, re.compile("(.*)", re.DOTALL))[0] rpm_files = output.splitlines()[1] content = [] for rpm_file in rpm_files.split(): content.append(basename(rpm_file)) else: # This should not happen. Getting here means that "path" is neither a file nor a directory. # Anyway, going on with no content, leaving it up to proceed_with_check() to handle this situation. content = [] # Start rhui-manager again and continue. RHUIManager.initial_run(connection) RHUIManager.screen(connection, "repo") Expect.enter(connection, "u") RHUIManager.select(connection, repolist) Expect.expect(connection, "will be uploaded:") Expect.enter(connection, path) RHUIManager.proceed_with_check(connection, "The following RPMs will be uploaded:", content) RHUIManager.quit(connection)
def upload_content(connection, repolist, path): ''' upload content to a custom repository ''' # Check whether "path" is a file or a directory. # If it is a directory, get a list of *.rpm files in it. path_type = Util.get_file_type(connection, path) if path_type == "regular file": content = [basename(path)] elif path_type == "directory": content = Util.get_rpms_in_dir(connection, path) else: # This should not happen. Getting here means that "path" is neither a file # nor a directory. # Anyway, going on with no content, # leaving it up to proceed_with_check() to handle this situation. content = [] # Continue in rhui-manager. RHUIManager.screen(connection, "repo") Expect.enter(connection, "u") RHUIManager.select(connection, repolist) Expect.expect(connection, "will be uploaded:") Expect.enter(connection, path) RHUIManager.proceed_with_check(connection, "The following RPMs will be uploaded:", content) RHUIManager.quit(connection, timeout=60)
def select_all(connection): ''' Select all items ''' Expect.expect(connection, "Enter value .*:") Expect.enter(connection, "a") Expect.expect(connection, "Enter value .*:") Expect.enter(connection, "c")
def remove_conf_rpm(connection): ''' Remove RHUI configuration rpm from instance (which owns /etc/yum/pluginconf.d/rhui-lb.conf file) ''' Expect.enter(connection, "") Expect.expect(connection, "root@") Expect.enter(connection, "([ ! -f /etc/yum/pluginconf.d/rhui-lb.conf ] && echo SUCCESS ) || (rpm -e `rpm -qf --queryformat %{NAME} /etc/yum/pluginconf.d/rhui-lb.conf` && echo SUCCESS)") Expect.expect(connection, "[^ ]SUCCESS.*root@", 60)
def logout(connection, prefix=""): ''' Logout from rhui-manager Use @param prefix to specify something to expect before exiting ''' Expect.expect(connection, prefix + ".*rhui \(.*\) =>") Expect.enter(connection, "logout")
def delete_repo(connection, repolist): ''' delete a repository from the RHUI ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "d") RHUIManager.select(connection, repolist) RHUIManager.proceed_without_check(connection) Expect.expect(connection, ".*rhui \(" + "repo" + "\) =>")
def delete_cdses(connection, *cdses): ''' unregister (delete) CDS instance from the RHUI ''' RHUIManager.screen(connection, "cds") Expect.enter(connection, "d") RHUIManager.select_items(connection, *cdses) Expect.enter(connection, "y") Expect.expect(connection, "Unregistered" + ".*rhui \(.*\) =>", 180)
def quit(connection, prefix="", timeout=10): ''' Quit from rhui-manager Use @param prefix to specify something to expect before exiting Use @param timeout to specify the timeout ''' Expect.expect(connection, prefix + ".*rhui \(.*\) =>", timeout) Expect.enter(connection, "q")
def delete_haps(connection, *hapes): ''' unregister (delete) HAP instance from the RHUI ''' RHUIManager.screen(connection, "loadbalancers") Expect.enter(connection, "d") RHUIManager.select_items(connection, *hapes) Expect.enter(connection, "y") Expect.expect(connection, "Unregistered" + ".*rhui \(.*\) =>", 180)
def add_rh_repo_all(connection): ''' add a new Red Hat content repository (All in Certificate) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 660) Expect.enter(connection, "1") RHUIManager.proceed_without_check(connection) RHUIManager.quit(connection, "", 180)
def add_rh_repo_all(connection): ''' add a new Red Hat content repository (All in Certificate) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 660) Expect.enter(connection, "1") RHUIManager.proceed_without_check(connection) Expect.expect(connection, ".*rhui \(" + "repo" + "\) =>", 180)
def add_rh_repo_all(connection): ''' add a new Red Hat content repository (All in Certificate) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 180) Expect.enter(connection, "1") RHUIManager.proceed_without_check(connection) RHUIManager.quit(connection, "Content will not be downloaded", 45)
def add_rh_repo_by_product(connection, productlist): ''' add a new Red Hat content repository (By Product) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 660) Expect.enter(connection, "2") RHUIManager.select(connection, productlist) RHUIManager.proceed_with_check(connection, "The following products will be deployed:", productlist) Expect.expect(connection, ".*rhui \(" + "repo" + "\) =>")
def upload_content(connection, repolist, path): ''' upload content to a custom repository ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "u") RHUIManager.select(connection, repolist) Expect.expect(connection, "will be uploaded:") Expect.enter(connection, path) RHUIManager.proceed_without_check(connection) RHUIManager.quit(connection)
def sync_repo(connection, repolist): ''' sync an individual repository immediately ''' RHUIManager.screen(connection, "sync") Expect.enter(connection, "sr") Expect.expect(connection, "Select one or more repositories.*for more commands:", 60) Expect.enter(connection, "l") RHUIManager.select(connection, repolist) RHUIManager.proceed_with_check(connection, "The following repositories will be scheduled for synchronization:", repolist) RHUIManager.quit(connection)
def remove_conf_rpm(connection): ''' Remove RHUI configuration rpm from instance (which owns /etc/yum/pluginconf.d/rhui-lb.conf file) ''' Expect.enter(connection, "") Expect.expect(connection, "root@") Expect.enter( connection, "([ ! -f /etc/yum/pluginconf.d/rhui-lb.conf ] && echo SUCCESS ) || (rpm -e `rpm -qf --queryformat %{NAME} /etc/yum/pluginconf.d/rhui-lb.conf` && echo SUCCESS)" ) Expect.expect(connection, "[^ ]SUCCESS.*root@", 60)
def add_rh_repo_by_product(connection, productlist): ''' add a new Red Hat content repository (By Product) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 180) Expect.enter(connection, "2") RHUIManager.select(connection, productlist) RHUIManager.proceed_with_check(connection, "The following products will be deployed:", productlist) RHUIManager.quit(connection, "Content will not be downloaded", 45)
def screen(connection, screen_name): ''' Open specified rhui-manager screen ''' if screen_name in ["repo", "cds", "loadbalancers", "sync", "identity", "users"]: key = screen_name[:1] elif screen_name == "client": key = "e" elif screen_name == "entitlements": key = "n" Expect.enter(connection, key) Expect.expect(connection, "rhui \(" + screen_name + "\) =>")
def _select_cluster(connection, clustername, create_new=True): ''' select cluster ''' try: select = Expect.match(connection, re.compile(".*\s+([0-9]+)\s*-\s*" + clustername + "\s*\r\n.*to abort:.*", re.DOTALL)) Expect.enter(connection, select[0]) except ExpectFailed as err: if not create_new: raise err Expect.enter(connection, "1") Expect.expect(connection, "Enter a[^\n]*CDS cluster name:") Expect.enter(connection, clustername)
def add_rh_repo_by_product(connection, productlist): ''' add a new Red Hat content repository (By Product) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 660) Expect.enter(connection, "2") RHUIManager.select(connection, productlist) RHUIManager.proceed_with_check(connection, "The following products will be deployed:", productlist) RHUIManager.quit(connection)
def delete_cds(connection, clustername, cdslist, force=False): ''' unregister (delete) a CDS instance from the RHUI ''' RHUIManager.screen(connection, "cds") Expect.enter(connection, "d") RHUIManager.select_one(connection, clustername) RHUIManager.select(connection, cdslist) RHUIManager.proceed_with_check(connection, "The following CDS instances from the %s cluster will be unregistered:" % clustername, cdslist) if force: Expect.expect(connection, "Forcibly remove these CDS instances", 60) Expect.enter(connection, "y") RHUIManager.quit(connection, timeout=30)
def test_14_verbose_reporting(): ''' check if a failure is reported properly (if puppet is verbose) ''' # for RHBZ#1751378 # choose a random CDS and open port 443 on it, which will later prevent Apache from starting cds = random.choice(CDS) Expect.enter(cds, "ncat -l 443 --keep-open") # try adding the CDS and check for the specific error message in the output error_msg = "change from stopped to running failed" RHUIManager.screen(RHUA, "cds") Expect.enter(RHUA, "a") Expect.expect(RHUA, "Hostname") Expect.enter(RHUA, cds.hostname) Expect.expect(RHUA, "Username") Expect.enter(RHUA, SUDO_USER_NAME) Expect.expect(RHUA, "Absolute") Expect.enter(RHUA, SUDO_USER_KEY) Expect.expect(RHUA, "Proceed") Expect.enter(RHUA, "y") state = Expect.expect_list(RHUA, [(re.compile(".*%s.*" % error_msg, re.DOTALL), 1), (re.compile(".*Aborting.*", re.DOTALL), 2), (re.compile(".*was successfully configured.*", re.DOTALL), 3)], timeout=180) # quit rhui-manager, clean up, fail if the error message didn't appear Expect.enter(RHUA, "q") Expect.enter(cds, CTRL_C) cds_list = RHUIManagerInstance.list(RHUA, "cds") if cds_list: RHUIManagerInstance.delete_all(RHUA, "cds") if state != 1: raise AssertionError("The expected error message was not seen in rhui-manager's output.")
def initial_run(connection, username="******", password="******"): ''' Run rhui-manager and make sure we're logged in, then quit it. ''' Expect.enter(connection, "rhui-manager") state = Expect.expect_list( connection, [(re.compile(".*RHUI Username:.*", re.DOTALL), 1), (re.compile(r".*rhui \(home\) =>.*", re.DOTALL), 2)]) if state == 1: Expect.enter(connection, username) Expect.expect(connection, "RHUI Password:"******".*Invalid login.*", re.DOTALL), 1), (re.compile(r".*rhui \(home\) =>.*", re.DOTALL), 2)]) if password_state == 1: initial_password = Util.get_initial_password(connection) if not initial_password: raise RuntimeError( "Could not get the initial rhui-manager password.") Expect.enter(connection, "rhui-manager") Expect.expect(connection, ".*RHUI Username:"******"RHUI Password:"******"rhui \(home\) =>") Expect.enter(connection, "q")
def upload_content_cert(connection, certpath): ''' upload a new or updated Red Hat content certificate ''' if certpath[:1] == '/': Expect.enter(connection, "mkdir -p `dirname " + certpath + "` && echo SUCCESS") Expect.expect(connection, "[^ ]SUCCESS") connection.sftp.put(certpath, certpath) RHUIManager.screen(connection, "entitlements") Expect.enter(connection, "u") Expect.expect(connection, "Full path to the new content certificate:") Expect.enter(connection, certpath) RHUIManager.proceed_with_check(connection, "The RHUI will be updated with the following certificate:", [certpath]) RHUIManager.quit(connection, "Red Hat Entitlements.*Valid.*------------------")
def screen(connection, screen_name): ''' Open specified rhui-manager screen ''' Expect.enter(connection, "rhui-manager") Expect.expect(connection, "rhui \(home\) =>") if screen_name in ["repo", "cds", "sync", "identity", "users"]: key = screen_name[:1] elif screen_name == "client": key = "e" elif screen_name == "entitlements": key = "n" Expect.enter(connection, key) Expect.expect(connection, "rhui \(" + screen_name + "\) =>")
def add_rh_repo_by_repo(connection, repolist): ''' add a new Red Hat content repository (By Repository) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 660) Expect.enter(connection, "3") RHUIManager.select(connection, repolist) repolist_mod = list(repolist) for repo in repolist: repolist_mod.append(re.sub(" \\\\\([a-zA-Z0-9_-]*\\\\\) \\\\\(Yum\\\\\)", "", repo)) RHUIManager.proceed_with_check(connection, "The following product repositories will be deployed:", repolist_mod) Expect.expect(connection, ".*rhui \(" + "repo" + "\) =>")
def initial_run(connection, username="******", password="******"): ''' Do rhui-manager initial run ''' Expect.enter(connection, "rhui-manager") state = Expect.expect_list( connection, [(re.compile(".*RHUI Username:.*", re.DOTALL), 1), (re.compile(".*rhui \(home\) =>.*", re.DOTALL), 2)]) if state == 1: Expect.enter(connection, username) Expect.expect(connection, "RHUI Password:"******".*Invalid login.*", re.DOTALL), 1), (re.compile(".*rhui \(home\) =>.*", re.DOTALL), 2)]) if password_state == 1: initial_password = Util.get_initial_password(connection) Expect.enter(connection, "rhui-manager") Expect.expect(connection, ".*RHUI Username:"******"RHUI Password:"******"rhui \(home\) =>") else: pass else: # initial step was already performed by someone pass
def sync_repo(connection, repolist): ''' sync an individual repository immediately ''' RHUIManager.screen(connection, "sync") Expect.enter(connection, "sr") Expect.expect(connection, "Select one or more repositories.*for more commands:", 60) Expect.enter(connection, "l") RHUIManager.select(connection, repolist) RHUIManager.proceed_with_check( connection, "The following repositories will be scheduled " + "for synchronization:", repolist) RHUIManager.quit(connection)
def _select_cluster(connection, clustername, create_new=True): """ select cluster """ try: select = Expect.match( connection, re.compile(".*\s+([0-9]+)\s*-\s*" + clustername + "\s*\r\n.*to abort:.*", re.DOTALL) ) Expect.enter(connection, select[0]) except ExpectFailed as err: if not create_new: raise err Expect.enter(connection, "1") Expect.expect(connection, "Enter a[^\n]*CDS cluster name:") Expect.enter(connection, clustername)
def test_17_missing_cert_handling(): '''check if rhui-manager can handle the loss of the RH cert''' # for RHBZ#1325390 RHUIManagerEntitlements.upload_rh_certificate(RHUA) # launch rhui-manager in one connection, delete the cert in the other RHUIManager.screen(RHUA, "repo") RHUIManager.remove_rh_certs(RHUA_2) Expect.enter(RHUA, "a") # a bit strange response to see in this context, but eh, no == all if you're a geek Expect.expect( RHUA, "All entitled products are currently deployed in the RHUI") Expect.enter(RHUA, "q") # an error message should be logged, though Expect.ping_pong(RHUA, "tail /root/.rhui/rhui.log", "The entitlement.*has no associated certificate")
def delete_all_repos(connection): ''' delete all repositories from the RHUI ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "d") Expect.expect(connection, "Enter value .*:", 360) Expect.enter(connection, "a") Expect.expect(connection, "Enter value .*:") Expect.enter(connection, "c") RHUIManager.proceed_without_check(connection) # Wait until all repos are deleted RHUIManager.quit(connection, "", 360) while len(RHUIManagerRepo.list(connection)) != 0: time.sleep(10)
def delete_cds(connection, clustername, cdslist, force=False): """ unregister (delete) a CDS instance from the RHUI """ RHUIManager.screen(connection, "cds") Expect.enter(connection, "d") RHUIManager.select_one(connection, clustername) RHUIManager.select(connection, cdslist) RHUIManager.proceed_with_check( connection, "The following CDS instances from the %s cluster will be unregistered:" % clustername, cdslist ) if force: Expect.expect(connection, "Forcibly remove these CDS instances", 60) Expect.enter(connection, "y") RHUIManager.quit(connection, timeout=30)
def create_docker_conf_rpm(connection, dirname, rpmname, rpmversion="", dockerport=""): """ create a client configuration RPM from an entitlement certificate """ RHUIManager.screen(connection, "client") Expect.enter(connection, "d") Expect.expect(connection, "Full path to local directory.*:") Expect.enter(connection, dirname) Expect.expect(connection, "Name of the RPM:") Expect.enter(connection, rpmname) Expect.expect(connection, "Version of the configuration RPM.*:") Expect.enter(connection, rpmversion) Expect.expect(connection, "Port to serve Docker content on .*:") Expect.enter(connection, dockerport) Expect.expect(connection, ".*rhui \(" + "client" + "\) =>")
def add_rh_repo_by_repo(connection, repolist): ''' add a new Red Hat content repository (By Repository) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 660) Expect.enter(connection, "3") RHUIManager.select(connection, repolist) repolist_mod = list(repolist) for repo in repolist: repolist_mod.append( re.sub(" \([a-zA-Z0-9_-]*\) \([a-zA-Z]*\)", "", repo)) RHUIManager.proceed_with_check( connection, "The following product repositories will be deployed:", repolist_mod) RHUIManager.quit(connection)
def add_rh_repo_by_repo(connection, repolist): ''' add a new Red Hat content repository (By Repository) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 180) Expect.enter(connection, "3") RHUIManager.select(connection, repolist) repocheck = list(repolist) for repo in repolist: #adding repo titles to check list repotitle = re.sub(" \\\\\([^\(]*\\\\\)$", "", repo) if not repotitle in repocheck: repocheck.append(repotitle) RHUIManager.proceed_with_check(connection, "The following product repositories will be deployed:", repocheck) RHUIManager.quit(connection, "Content will not be downloaded", 45)
def generate_gpg_key(connection, keydata="", ownerdata="", comment="comment"): ''' Generate GPG keypair WARNING!!! It takes too long to wait for this operation to complete... use pre-created keys instead! ''' # keydata = [type, length, expire date] # ownerdata = [real name, email] if len(keydata) != 3: keydata = ["DSA", "1024", "0"] if len(ownerdata) != 2: ownerdata = ["Key Owner", "kowner@%s" % DOMAIN] Expect.enter(connection, "cat > /tmp/gpgkey << EOF") Expect.enter(connection, "Key-Type: " + keydata[0]) Expect.enter(connection, "Key-Length: " + keydata[1]) Expect.enter(connection, "Subkey-Type: ELG-E") Expect.enter(connection, "Subkey-Length: " + keydata[1]) Expect.enter(connection, "Name-Real: " + ownerdata[0]) Expect.enter(connection, "Name-Comment: " + comment) Expect.enter(connection, "Name-Email: " + ownerdata[1]) Expect.enter(connection, "Expire-Date: " + keydata[2]) Expect.enter(connection, "%commit") Expect.enter(connection, "%echo done") Expect.enter(connection, "EOF") Expect.expect(connection, "root@") Expect.enter( connection, "gpg --gen-key --no-random-seed-file --batch /tmp/gpgkey") for _ in range(1, 200): Expect.enter( connection, ''.join( random.choice(string.ascii_lowercase) for x in range(200))) time.sleep(1) try: Expect.expect(connection, "gpg: done") break except ExpectFailed: continue
def upload_content_cert(connection, certpath): ''' upload a new or updated Red Hat content certificate ''' if certpath[:1] == '/': Expect.enter(connection, "mkdir -p `dirname " + certpath + "` && echo SUCCESS") Expect.expect(connection, "[^ ]SUCCESS") connection.sftp.put(certpath, certpath) RHUIManager.screen(connection, "entitlements") Expect.enter(connection, "u") Expect.expect(connection, "Full path to the new content certificate:") Expect.enter(connection, certpath) RHUIManager.proceed_with_check( connection, "The RHUI will be updated with the following certificate:", [certpath]) RHUIManager.quit(connection, "Red Hat Entitlements.*Valid.*------------------")
def add_rh_repo_by_repo(connection, repolist): ''' add a new Red Hat content repository (By Repository) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 180) Expect.enter(connection, "3") RHUIManager.select(connection, repolist) repocheck = list(repolist) for repo in repolist: #adding repo titles to check list repotitle = re.sub(" \\\\\([^\(]*\\\\\)$", "", repo) if not repotitle in repocheck: repocheck.append(repotitle) RHUIManager.proceed_with_check( connection, "The following product repositories will be deployed:", repocheck) RHUIManager.quit(connection, "Content will not be downloaded", 45)
def add_rh_repo_by_repo(connection, repolist): ''' add a new Red Hat content repository (By Repository) ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "a") Expect.expect(connection, "Import Repositories:.*to abort:", 660) Expect.enter(connection, "3") RHUIManager.select(connection, repolist) repolist_mod = list(repolist) for repo in repolist: # strip " (kind)" repo_stripped = re.sub(r" \([a-zA-Z]*\)$", "", repo) # strip " (version)" if present (if "(RPMs)" isn't there instead) repo_stripped = re.sub(r" \((?!RPMs)[a-zA-Z0-9_-]*\)$", "", repo_stripped) repolist_mod.append(repo_stripped) RHUIManager.proceed_with_check(connection, "The following product repositories will be deployed:", repolist_mod) RHUIManager.quit(connection)
def screen(connection, screen_name): ''' Open specified rhui-manager screen ''' if screen_name in [ "repo", "cds", "loadbalancers", "sync", "identity", "users" ]: key = screen_name[:1] elif screen_name == "client": key = "e" elif screen_name == "entitlements": key = "n" elif screen_name == "subscriptions": key = "sm" else: raise ValueError("Unsupported screen name: " + screen_name) Expect.enter(connection, "rhui-manager") Expect.expect(connection, "rhui \(home\) =>") Expect.enter(connection, key) Expect.expect(connection, "rhui \(" + screen_name + "\) =>")
def create_conf_rpm(connection, clustername, primary_cds, dirname, certpath, certkey, rpmname, rpmversion="", unprotected_repos=None): ''' create a client configuration RPM from an entitlement certificate ''' RHUIManager.screen(connection, "client") Expect.enter(connection, "c") Expect.expect(connection, "Full path to local directory.*:") Expect.enter(connection, dirname) Expect.expect(connection, "Name of the RPM:") Expect.enter(connection, rpmname) Expect.expect(connection, "Version of the configuration RPM.*:") Expect.enter(connection, rpmversion) Expect.expect(connection, "Full path to the entitlement certificate.*:") Expect.enter(connection, certpath) Expect.expect(connection, "Full path to the private key for the above entitlement certificate:") Expect.enter(connection, certkey) RHUIManager.select_one(connection, clustername) RHUIManager.select_one(connection, primary_cds) if unprotected_repos: RHUIManager.select(connection, unprotected_repos) RHUIManager.quit(connection, "Successfully created client configuration RPM")
def generate_ent_cert(connection, repolist, certname, dirname, validity_days="", cert_pw=None): """ generate an entitlement certificate """ RHUIManager.screen(connection, "client") Expect.enter(connection, "e") RHUIManager.select(connection, repolist) Expect.expect(connection, "Name of the certificate.*contained with it:") Expect.enter(connection, certname) Expect.expect(connection, "Local directory in which to save the generated certificate.*:") Expect.enter(connection, dirname) Expect.expect(connection, "Number of days the certificate should be valid.*:") Expect.enter(connection, validity_days) RHUIManager.proceed_without_check(connection) Expect.expect(connection, ".*rhui \(" + "client" + "\) =>")
def upload_rh_certificate(connection): ''' upload a new or updated Red Hat content certificate ''' certificate_file = '/tmp/extra_rhui_files/rhcert.pem' if connection.recv_exit_status("ls -la %s" % certificate_file)!=0: raise ExpectFailed("Missing certificate file: %s" % certificate_file) RHUIManager.screen(connection, "entitlements") Expect.enter(connection, "u") Expect.expect(connection, "Full path to the new content certificate:") Expect.enter(connection, certificate_file) Expect.expect(connection, "The RHUI will be updated with the following certificate:") Expect.enter(connection, "y") match = Expect.match(connection, re.compile("(.*)" + RHUIManagerEntitlements.prompt, re.DOTALL)) matched_string = match[0].replace('l\r\n\r\nRed Hat Entitlements\r\n\r\n \x1b[92mValid\x1b[0m\r\n ', '', 1) entitlements_list = [] pattern = re.compile('(.*?\r\n.*?pem)', re.DOTALL) for entitlement in pattern.findall(matched_string): entitlements_list.append(entitlement.strip()) return entitlements_list
def add_docker_container(connection, containername, containerid="", displayname=""): ''' add a new Red Hat docker container ''' RHUIManager.screen(connection, "repo") Expect.enter(connection, "ad") Expect.expect(connection, "Name of the container in the registry:") Expect.enter(connection, containername) Expect.expect(connection, "Unique ID for the container .*]", 60) Expect.enter(connection, containerid) Expect.expect(connection, "Display name for the container.*]:") Expect.enter(connection, displayname) RHUIManager.proceed_with_check(connection, "The following container will be added:", ["Container Id: " + containername.replace("/","_").replace(".","_"), "Display Name: " + displayname, "Upstream Container Name: " + containername]) Expect.expect(connection, ".*rhui \(" + "repo" + "\) =>")
def change_user_password(connection, password='******'): ''' Change the password of rhui-manager user ''' Expect.enter(connection, "p") Expect.expect(connection, "Username:"******"New Password:"******"Re-enter Password:") Expect.enter(connection, password)
def _add_cds_part1(connection, cdsname, hostname="", displayname=""): """ service function for add_cds """ Expect.enter(connection, "a") Expect.expect(connection, "Hostname of the CDS to register:") Expect.enter(connection, cdsname) Expect.expect(connection, "Client hostname \(hostname clients will use to connect to the CDS\).*:") Expect.enter(connection, hostname) Expect.expect(connection, "Display name for the CDS.*:") Expect.enter(connection, displayname)