def set_password(password, user): admin = libuser.admin() root = admin.lookupUserByName(user) passwd = cryptPassword(password) _functions.unmount_config("/etc/shadow") admin.setpassUser(root, passwd, "is_crypted") _functions.ovirt_store_config("/etc/shadow") return True
def kdump_apply(self, obj): try: from ocsetup import ocs except ImportError: pass else: page = ocs.page_kernel_dump buttons = page.nfs_ssh_restore_custom.btns val = None for i in range(3): # i == 0 is NFS, i == 1 is SSH, i == 2 is RESTORE if buttons[i].get_active() and i == 0: val = page.nfs_location_custom.entry.get_text() elif buttons[i].get_active() and i == 1: val = page.ssh_location_custom.entry.get_text() elif buttons[i].get_active() and i == 2: restore_kdump_config() if val: write_kdump_config(val) pb = ProgressBar() pb.make_progress(0.1) if '@' in val: if os.path.exists("/usr/bin/kdumpctl"): kdump_prop_cmd = "kdumpctl propagate" else: kdump_prop_cmd = "service kdump propagate" from sshcmd import runcmd runcmd(kdump_prop_cmd) pb.make_progress(0.2) if runcmd: # SSH LOGIN SUCCESS. ovirt_store_config("/root/.ssh/kdump_id_rsa.pub") ovirt_store_config("/root/.ssh/kdump_id_rsa") ovirt_store_config("/root/.ssh/known_hosts") ovirt_store_config("/root/.ssh/config") pb.make_progress(0.5) system('servic kdump restart &') while True: res = check_output('service kdump status') if 'starting' not in res: break pb.make_progress(0.5) time.sleep(3) pb.make_progress(0.8) if 'Kdump is not operational' in res: # restart kdump Failed. unmount_config("/etc/kdump.conf") if os.path.exists("/etc/kdump.conf"): os.remove("/etc/kdump.conf") pb.make_progress(0, 'FAILED!') pb.progress_label.set_label('FAILED!') elif 'Kdump is operational' in res: ovirt_store_config("/etc/kdump.conf") pb.make_progress(1, 'successful!') else: log('kdump start Failed:' + res) pb.make_progress(0, 'FAILED!') pb.progress_label.set_label('FAILED!')
def set_nfsv4_domain(domain): idmap_conf = "/etc/idmapd.conf" current_domain = get_current_nfsv4_domain() _functions.unmount_config(idmap_conf) if current_domain.startswith("#"): current_domain = "#Domain = %s" % current_domain.replace("# ","") _functions.system("sed -i 's/%s/Domain = %s/g' %s" \ % (current_domain, domain, idmap_conf)) else: _functions.system("sed -i '/^Domain/ s/%s/%s/g' %s" \ % (current_domain, domain, idmap_conf)) if _functions.ovirt_store_config(idmap_conf): logger.info("NFSv4 domain set as: " + domain) else: logger.warning("Setting nfsv4 domain failed") _functions.system_closefds("service rpcidmapd restart") _functions.system_closefds("nfsidmap -c &>/dev/null")
def set_nfsv4_domain(domain): idmap_conf = "/etc/idmapd.conf" current_domain = get_current_nfsv4_domain() _functions.unmount_config(idmap_conf) if current_domain.startswith("#"): current_domain = "#Domain = %s" % current_domain.replace("# ", "") _functions.system("sed -i 's/%s/Domain = %s/g' %s" \ % (current_domain, domain, idmap_conf)) else: _functions.system("sed -i 's/%s/%s/g' %s" \ % (current_domain, domain, idmap_conf)) if _functions.ovirt_store_config(idmap_conf): logger.info("NFSv4 domain set as: " + domain) else: logger.warning("Setting nfsv4 domain failed") _functions.system_closefds("service rpcidmapd restart") _functions.system_closefds("nfsidmap -c &>/dev/null")
def commit(self): from ovirtnode.ovirtfunctions import unmount_config, \ ovirt_store_config try: utils.process.check_call("service kdump restart") except utils.process.CalledProcessError as e: self.logger.info("Failure while restarting kdump: %s" % e) unmount_config("/etc/kdump.conf") self.backups.restore("/etc/kdump.conf") utils.process.call("service kdump restart") raise RuntimeError("KDump configuration failed, " + "location unreachable. Previous " + "configuration was restored.") ovirt_store_config("/etc/kdump.conf") self.backups.remove()
def __update_profile(self, rng_num_bytes, disable_aes): import ovirtnode.ovirtfunctions as ofunc additional_lines = [] ofunc.unmount_config("/etc/profile") process.check_call("sed -i '/OPENSSL_DISABLE_AES_NI/d' /etc/profile") if disable_aes: additional_lines += ["export OPENSSL_DISABLE_AES_NI=1"] process.check_call("sed -i '/SSH_USE_STRONG_RNG/d' /etc/profile") if rng_num_bytes: additional_lines += ["export SSH_USE_STRONG_RNG=%s" % rng_num_bytes] if additional_lines: self.logger.debug("Updating /etc/profile") lines = "\n" + "\n".join(additional_lines) File("/etc/profile").write(lines, "a") ofunc.ovirt_store_config("/etc/profile") self.restart()
def __update_profile(self, rng_num_bytes, disable_aes): import ovirtnode.ovirtfunctions as ofunc additional_lines = [] ofunc.unmount_config("/etc/profile") process.check_call("sed -i '/OPENSSL_DISABLE_AES_NI/d' /etc/profile") if disable_aes: additional_lines += ["export OPENSSL_DISABLE_AES_NI=1"] process.check_call("sed -i '/SSH_USE_STRONG_RNG/d' /etc/profile") if rng_num_bytes: additional_lines += [ "export SSH_USE_STRONG_RNG=%s" % rng_num_bytes ] if additional_lines: self.logger.debug("Updating /etc/profile") lines = "\n" + "\n".join(additional_lines) File("/etc/profile").write(lines, "a") ofunc.ovirt_store_config("/etc/profile") self.restart()
def perform_partitioning(self): if self.HOSTVGDRIVE is None and not _functions.is_iscsi_install(): logger.error("\nNo storage device selected.") return False if self.BOOTDRIVE is None and _functions.is_iscsi_install(): logger.error("No storage device selected.") return False if not self.cross_check_host_app(): logger.error("Skip disk partitioning, AppVG overlaps with HostVG") return False if _functions.has_fakeraid(self.HOSTVGDRIVE): if not handle_fakeraid(self.HOSTVGDRIVE): return False if _functions.has_fakeraid(self.ROOTDRIVE): if not handle_fakeraid(self.ROOTDRIVE): return False logger.info("Saving parameters") _functions.unmount_config("/etc/default/ovirt") if not self.check_partition_sizes(): return False # Check for still remaining HostVGs this can be the case when # Node was installed on a disk not given in storage_init # rhbz#872114 existing_vgs = str(_functions.passthrough("vgs")) for vg in existing_vgs.split("\n"): vg = vg.strip() if "HostVG" in str(vg): logger.error("An existing installation was found or not " + "all VGs could be removed. " + "Please manually cleanup the storage using " + "standard disk tools.") return False logger.info("Removing old LVM partitions") # HostVG must not exist at this point # we wipe only foreign LVM here logger.info("Wiping LVM on HOSTVGDRIVE %s" % self.HOSTVGDRIVE) if not self.wipe_lvm_on_disk(self.HOSTVGDRIVE): logger.error("Wiping LVM on %s Failed" % self.HOSTVGDRIVE) return False logger.info("Wiping LVM on ROOTDRIVE %s" % self.ROOTDRIVE) if not self.wipe_lvm_on_disk(self.ROOTDRIVE): logger.error("Wiping LVM on %s Failed" % self.ROOTDRIVE) return False logger.info("Wiping LVM on BOOTDRIVE %s" % self.BOOTDRIVE) if not self.wipe_lvm_on_disk(self.BOOTDRIVE): logger.error("Wiping LVM on %s Failed" % self.BOOTDRIVE) return False logger.debug("Old LVM partitions should be gone.") logger.debug(_functions.passthrough("vgdisplay -v")) self.boot_size_si = self.BOOT_SIZE * (1024 * 1024) / (1000 * 1000) if _functions.is_iscsi_install(): if "OVIRT_ISCSI_NAME" in _functions.OVIRT_VARS: iscsi_name = _functions.OVIRT_VARS["OVIRT_ISCSI_NAME"] set_iscsi_initiator(iscsi_name) # login to target and setup disk get_targets = ("iscsiadm -m discovery -p %s:%s -t sendtargets" % (_functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_HOST"], _functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_PORT"])) _functions.system(get_targets) before_login_drvs = self.get_dev_name() logger.debug(before_login_drvs) login_cmd = ("iscsiadm -m node -T %s -p %s:%s -l" % (_functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_NAME"], _functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_HOST"], _functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_PORT"])) _functions.system(login_cmd) _functions.system("multipath -r") after_login_drvs = self.get_dev_name() logger.debug(after_login_drvs) logger.info("iSCSI enabled, partitioning boot drive: %s" % self.BOOTDRIVE) _functions.wipe_partitions(self.BOOTDRIVE) self.reread_partitions(self.BOOTDRIVE) logger.info("Creating boot partition") parted_cmd = "parted %s -s \"mklabel %s\"" % (self.BOOTDRIVE, self.LABEL_TYPE) _functions.system(parted_cmd) self.create_efi_partition() boot_end_mb = self.EFI_SIZE + self.BOOT_SIZE parted_cmd = ("parted \"%s\" -s \"mkpart primary ext2 %sM %sM\"" % (self.BOOTDRIVE, self.EFI_SIZE, boot_end_mb)) _functions.system(parted_cmd) parted_cmd = ("parted \"%s\" -s \"mkpart primary ext2 %sM %sM\"" % (self.BOOTDRIVE , boot_end_mb, boot_end_mb + self.BOOT_SIZE)) _functions.system(parted_cmd) parted_cmd = ("parted \"" + self.BOOTDRIVE + "\" -s \"set 1 " + "boot on\"") _functions.system(parted_cmd) self.reread_partitions(self.BOOTDRIVE) partboot = self.BOOTDRIVE + "2" partbootbackup = self.BOOTDRIVE + "3" if not os.path.exists(partboot): logger.debug("%s does not exist" % partboot) partboot = self.BOOTDRIVE + "p2" partbootbackup = self.BOOTDRIVE + "p3" # sleep to ensure filesystems are created before continuing _functions.system("udevadm settle") _functions.system("mke2fs \"" + str(partboot) + "\" -L Boot") _functions.system("tune2fs -c 0 -i 0 \"" + str(partboot) + "\"") _functions.system("ln -snf \"" + partboot + \ "\" /dev/disk/by-label/Boot") _functions.system("mke2fs \"" + str(partbootbackup) + \ "\" -L BootBackup") _functions.system("tune2fs -c 0 -i 0 \"" + \ str(partbootbackup) + "\"") _functions.system("ln -snf \"" + partbootbackup + "\" /dev/disk/by-label/BootBackup") self.ISCSIDRIVE = _functions.translate_multipath_device( _functions.OVIRT_VARS["OVIRT_ISCSI_INIT"]) logger.debug(self.ISCSIDRIVE) if self.create_iscsiroot(): logger.info("iSCSI Root Partitions Created") if self.create_hostvg(): if len(self.APPVGDRIVE) > 0: self.create_appvg() logger.info("Completed!") return True if ("OVIRT_ROOT_INSTALL" in _functions.OVIRT_VARS and _functions.OVIRT_VARS["OVIRT_ROOT_INSTALL"] == "y" and not \ _functions.is_iscsi_install()): logger.info("Partitioning root drive: " + self.ROOTDRIVE) _functions.wipe_partitions(self.ROOTDRIVE) self.reread_partitions(self.ROOTDRIVE) logger.info("Labeling Drive: " + self.ROOTDRIVE) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mklabel " + self.LABEL_TYPE + "\"") _functions.passthrough(parted_cmd, logger.debug) logger.debug("Creating Root and RootBackup Partitions") if _functions.is_efi_boot(): self.create_efi_partition() else: # create partition labeled bios_grub parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mkpart primary 1M " + str(self.EFI_SIZE) + "M\"") _functions.passthrough(parted_cmd, logger.debug) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"set 1 bios_grub on\"") _functions.passthrough(parted_cmd, logger.debug) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mkpart primary ext2 " + str(self.EFI_SIZE) + "M " + str(self.Root_end) + "M\"") _functions.passthrough(parted_cmd, logger.debug) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mkpart primary ext2 " + str(self.Root_end) + "M " + str(self.RootBackup_end) + "M\"") logger.debug(parted_cmd) _functions.system(parted_cmd) _functions.system("sync ; udevadm settle ; partprobe") parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"set 2 boot on\"") logger.debug(parted_cmd) _functions.system(parted_cmd) # force reload some cciss devices will fail to mkfs _functions.system("multipath -r &>/dev/null") self.reread_partitions(self.ROOTDRIVE) partroot = self.ROOTDRIVE + "2" partrootbackup = self.ROOTDRIVE + "3" if not os.path.exists(partroot): partroot = self.ROOTDRIVE + "p2" partrootbackup = self.ROOTDRIVE + "p3" _functions.system("mke2fs \"" + partroot + "\" -L Root") _functions.system("tune2fs -c 0 -i 0 \"" + partroot + "\"") _functions.system("ln -snf \"" + partrootbackup + "\" /dev/disk/by-label/RootBackup") _functions.system("mke2fs \"" + partrootbackup + \ "\" -L RootBackup") _functions.system("tune2fs -c 0 -i 0 \"" + partrootbackup + "\"") hostvg1 = self.HOSTVGDRIVE.split(",")[0] self.reread_partitions(self.ROOTDRIVE) if self.ROOTDRIVE != hostvg1: _functions.system("parted \"" + hostvg1 + "\" -s \"mklabel " + self.LABEL_TYPE + "\"") if self.create_hostvg(): if len(self.APPVGDRIVE) > 0: self.create_appvg() else: return False if ("OVIRT_CRYPT_SWAP2" in _functions.OVIRT_VARS or "OVIRT_CRYPT_SWAP" in _functions.OVIRT_VARS): _functions.ovirt_store_config("/etc/ovirt-crypttab") return True
def perform_partitioning(self): if self.HOSTVGDRIVE is None and not _functions.is_iscsi_install(): logger.error("\nNo storage device selected.") return False if self.BOOTDRIVE is None and _functions.is_iscsi_install(): logger.error("No storage device selected.") return False if _functions.has_fakeraid(self.HOSTVGDRIVE): if not handle_fakeraid(self.HOSTVGDRIVE): return False if _functions.has_fakeraid(self.ROOTDRIVE): if not handle_fakeraid(self.ROOTDRIVE): return False logger.info("Saving parameters") _functions.unmount_config("/etc/default/ovirt") if not self.check_partition_sizes(): return False logger.info("Removing old LVM partitions") # HostVG must not exist at this point # we wipe only foreign LVM here logger.info("Wiping LVM on HOSTVGDRIVE %s" % self.HOSTVGDRIVE) if not self.wipe_lvm_on_disk(self.HOSTVGDRIVE): logger.error("Wiping LVM on %s Failed" % self.HOSTVGDRIVE) return False logger.info("Wiping LVM on ROOTDRIVE %s" % self.ROOTDRIVE) if not self.wipe_lvm_on_disk(self.ROOTDRIVE): logger.error("Wiping LVM on %s Failed" % self.ROOTDRIVE) return False logger.info("Wiping LVM on BOOTDRIVE %s" % self.BOOTDRIVE) if not self.wipe_lvm_on_disk(self.BOOTDRIVE): logger.error("Wiping LVM on %s Failed" % self.BOOTDRIVE) return False logger.debug("Old LVM partitions should be gone.") logger.debug(_functions.passthrough("vgdisplay -v")) self.boot_size_si = self.BOOT_SIZE * (1024 * 1024) / (1000 * 1000) if _functions.is_iscsi_install(): # login to target and setup disk" get_targets = ("iscsiadm -m discovery -p %s:%s -t sendtargets" % (_functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_HOST"], _functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_PORT"])) _functions.system(get_targets) before_login_drvs = self.get_dev_name() logger.debug(before_login_drvs) login_cmd = ("iscsiadm -m node -T %s -p %s:%s -l" % (_functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_NAME"], _functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_HOST"], _functions.OVIRT_VARS["OVIRT_ISCSI_TARGET_PORT"])) _functions.system(login_cmd) _functions.system("multipath -r") after_login_drvs = self.get_dev_name() logger.debug(after_login_drvs) logger.info("iSCSI enabled, partitioning boot drive: %s" % self.BOOTDRIVE) _functions.wipe_partitions(self.BOOTDRIVE) self.reread_partitions(self.BOOTDRIVE) logger.info("Creating boot partition") parted_cmd = "parted %s -s \"mklabel %s\"" % (self.BOOTDRIVE, self.LABEL_TYPE) _functions.system(parted_cmd) parted_cmd = ("parted \"%s\" -s \"mkpart primary ext2 1M 256M\"" % self.BOOTDRIVE) _functions.system(parted_cmd) parted_cmd = ("parted \"%s\" -s \"mkpart primary ext2 256M " + "512M\"") % self.BOOTDRIVE _functions.system(parted_cmd) parted_cmd = ("parted \"" + self.BOOTDRIVE + "\" -s \"set 1 " + "boot on\"") _functions.system(parted_cmd) self.reread_partitions(self.BOOTDRIVE) partboot = self.BOOTDRIVE + "1" if not os.path.exists(partboot): logger.debug("%s does not exist" % partboot) partboot = self.BOOTDRIVE + "p1" partbootbackup = self.BOOTDRIVE + "2" if not os.path.exists(partbootbackup): logger.debug("%s does not exist" % partbootbackup) partbootbackup = self.BOOTDRIVE + "p2" # sleep to ensure filesystems are created before continuing _functions.system("udevadm settle") time.sleep(10) _functions.system("mke2fs \"" + str(partboot) + "\" -L Boot") _functions.system("tune2fs -c 0 -i 0 \"" + str(partboot) + "\"") _functions.system("ln -snf \"" + partboot + \ "\" /dev/disk/by-label/Boot") _functions.system("mke2fs \"" + str(partbootbackup) + \ "\" -L BootBackup") _functions.system("tune2fs -c 0 -i 0 \"" + \ str(partbootbackup) + "\"") _functions.system("ln -snf \"" + partbootbackup + "\" /dev/disk/by-label/BootBackup") self.ISCSIDRIVE = _functions.translate_multipath_device( _functions.OVIRT_VARS["OVIRT_ISCSI_INIT"]) logger.debug(self.ISCSIDRIVE) if self.create_iscsiroot(): logger.info("iSCSI Root Partitions Created") if self.create_hostvg(): logger.info("Completed!") return True if ("OVIRT_ROOT_INSTALL" in _functions.OVIRT_VARS and _functions.OVIRT_VARS["OVIRT_ROOT_INSTALL"] == "y"): logger.info("Partitioning root drive: " + self.ROOTDRIVE) _functions.wipe_partitions(self.ROOTDRIVE) self.reread_partitions(self.ROOTDRIVE) logger.info("Labeling Drive: " + self.ROOTDRIVE) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mklabel " + self.LABEL_TYPE + "\"") _functions.passthrough(parted_cmd, logger.debug) logger.debug("Creating Root and RootBackup Partitions") # efi partition should at 0M if _functions.is_efi_boot(): parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mkpart EFI 1M " + str(self.EFI_SIZE) + "M\"") _functions.passthrough(parted_cmd, logger.debug) else: # create partition labeled bios_grub parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mkpart primary 1M " + str(self.EFI_SIZE) + "M\"") _functions.passthrough(parted_cmd, logger.debug) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"set 1 bios_grub on\"") _functions.passthrough(parted_cmd, logger.debug) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mkpart primary ext2 " + str(self.EFI_SIZE) + "M " + str(self.Root_end) + "M\"") _functions.passthrough(parted_cmd, logger.debug) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"mkpart primary ext2 " + str(self.Root_end) + "M " + str(self.RootBackup_end) + "M\"") logger.debug(parted_cmd) _functions.system(parted_cmd) parted_cmd = ("parted \"" + self.ROOTDRIVE + "\" -s \"set 2 boot on\"") logger.debug(parted_cmd) _functions.system(parted_cmd) # sleep to ensure filesystems are created before continuing time.sleep(5) # force reload some cciss devices will fail to mkfs _functions.system("multipath -r &>/dev/null") self.reread_partitions(self.ROOTDRIVE) partefi = self.ROOTDRIVE + "1" partroot = self.ROOTDRIVE + "2" partrootbackup = self.ROOTDRIVE + "3" if not os.path.exists(partroot): partefi = self.ROOTDRIVE + "p1" partroot = self.ROOTDRIVE + "p2" partrootbackup = self.ROOTDRIVE + "p3" if _functions.is_efi_boot(): _functions.system("ln -snf \"" + partefi + \ "\" /dev/disk/by-label/EFI") _functions.system("mkfs.vfat \"" + partefi + "\"") _functions.system("ln -snf \"" + partroot + \ "\" /dev/disk/by-label/Root") _functions.system("mke2fs \"" + partroot + "\" -L Root") _functions.system("tune2fs -c 0 -i 0 \"" + partroot + "\"") _functions.system("ln -snf \"" + partrootbackup + "\" /dev/disk/by-label/RootBackup") _functions.system("mke2fs \"" + partrootbackup + \ "\" -L RootBackup") _functions.system("tune2fs -c 0 -i 0 \"" + partrootbackup + "\"") hostvg1 = self.HOSTVGDRIVE.split(",")[0] self.reread_partitions(self.ROOTDRIVE) if self.ROOTDRIVE != hostvg1: _functions.system("parted \"" + hostvg1 + "\" -s \"mklabel " + self.LABEL_TYPE + "\"") if self.create_hostvg(): if len(self.APPVGDRIVE) > 0: self.create_appvg() else: return False if ("OVIRT_CRYPT_SWAP2" in _functions.OVIRT_VARS or "OVIRT_CRYPT_SWAP" in _functions.OVIRT_VARS): _functions.ovirt_store_config("/etc/ovirt-crypttab") return True
def commit(self): cfg = RHN().retrieve() self.logger.debug(cfg) # rhntype = cfg["rhntype"] org = cfg["org"] serverurl = cfg["url"] cacert = cfg["ca_cert"] activationkey = cfg["activationkey"] username = cfg["username"] profilename = cfg["profile"] proxy = cfg["proxy"] proxyuser = cfg["proxyuser"] if os.path.exists("/etc/sysconfig/rhn/systemid"): remove_config("/etc/sysconfig/rhn/systemid") extra_args = ['--force'] if not activationkey: extra_args.append("--autosubscribe") sm = ['/usr/sbin/subscription-manager'] args = list(sm) args.append('register') if activationkey and org: args.append('--activationkey') args.append(activationkey) args.append('--org') args.append(org) elif username: args.append('--username') args.append(username) if password: args.append('--password') args.append(password) else: # skip RHN registration when neither activationkey # nor username/password is supplied # return success for AUTO w/o rhn_* parameters return if serverurl: (host, port) = parse_host_port(serverurl) parsed_url = urlparse(serverurl) prefix = parsed_url.path if port == 0: port = "443" else: port = str(port) else: prefix = "/subscription" host = "subscription.rhn.redhat.com" port = "443" location = "/etc/rhsm/ca/candlepin-local.pem" if cacert: if not os.path.exists(cacert): self.logger.info("Downloading CA cert.....") RHN().retrieveCert(cacert, location) if os.path.isfile(location): if os.stat(location).st_size > 0: ovirt_store_config(location) else: raise RuntimeError("Error Downloading CA cert!") smconf = list(sm) smconf.append('config') smconf.append('--server.hostname') smconf.append(host) smconf.append('--server.port') smconf.append(port) smconf.append('--server.prefix') smconf.append(prefix) if cacert: smconf.append('--rhsm.repo_ca_cert') smconf.append('/etc/rhsm/ca/candlepin-local.pem') try: subprocess.check_call(smconf) ovirt_store_config("/etc/rhsm/rhsm.conf") except: raise RuntimeError("Error updating subscription manager \ configuration") if profilename: args.append('--name') args.append(profilename) if proxy: try: (host, port) = proxy.split(":") process.check_call(["subscription-manager", "config", "--server.proxy_hostname", host]) process.check_call(["subscription-manager", "config", "--server.proxy_port", port]) if proxyuser: args.append('--proxyuser') args.append(proxyuser) cmd = ["subscription-manager", "config", "--server.proxy_user", proxyuser] process.check_call(cmd) if proxypass: args.append('--proxypassword') args.append(proxypass) cmd = ["subscription-manager", "config", "--server.proxy_password", proxypass] logged_args = list(cmd) remove_values_from_args = [ "--server.proxy_password"] for idx, arg in enumerate(cmd): if arg in remove_values_from_args: logged_args[idx+1] = "XXXXXXX" logged_args = str(logged_args) self.logger.info(logged_args) subprocess.check_call(cmd) except: raise RuntimeError("Error updating subscription \ manager proxy configuration") args.extend(extra_args) self.logger.info("Registering to RHN account.....") rhsm_configs = (["/var/lib/rhsm/cache/installed_products.json", "/var/lib/rhsm/facts/facts.json"]) unmount_config(rhsm_configs) unmount_config(glob.glob("/etc/pki/consumer/*pem")) def unlink_if_exists(f): if os.path.exists(f): os.unlink(f) for f in rhsm_configs: unlink_if_exists(f) logged_args = list(args) remove_values_from_args = ["--password", "--proxypassword"] for idx, arg in enumerate(logged_args): if arg in remove_values_from_args: logged_args[idx+1] = "XXXXXXX" logged_args = str(logged_args) self.logger.info(logged_args) smreg_output = process.pipe(args) self.logger.debug(smreg_output) if "been registered" not in smreg_output: if "Invalid credentials" in smreg_output: raise RuntimeError("Invalid Username / Password") elif "already been taken" in smreg_output: raise RuntimeError("Hostname is already " + "registered") else: raise RuntimeError("Registration Failed") else: ovirt_store_config(rhsm_configs) ovirt_store_config("/etc/pki/consumer/key.pem") ovirt_store_config("/etc/pki/consumer/cert.pem") self.logger.info("System %s sucessfully registered \ to %s" % (profilename, serverurl))
def commit(self): cfg = RHN().retrieve() self.logger.debug(cfg) # rhntype = cfg["rhntype"] org = cfg["org"] serverurl = cfg["url"] cacert = cfg["ca_cert"] activationkey = cfg["activationkey"] username = cfg["username"] profilename = cfg["profile"] proxy = cfg["proxy"] proxyuser = cfg["proxyuser"] if os.path.exists("/etc/sysconfig/rhn/systemid"): remove_config("/etc/sysconfig/rhn/systemid") extra_args = ['--force'] if not activationkey: extra_args.append("--autosubscribe") sm = ['/usr/sbin/subscription-manager'] args = list(sm) args.append('register') if activationkey and org: args.append('--activationkey') args.append(activationkey) args.append('--org') args.append(org) elif username: args.append('--username') args.append(username) if password: args.append('--password') args.append(password) else: # skip RHN registration when neither activationkey # nor username/password is supplied # return success for AUTO w/o rhn_* parameters return if serverurl: (host, port) = parse_host_port(serverurl) parsed_url = urlparse(serverurl) prefix = parsed_url.path if port == 0: port = "443" else: port = str(port) else: prefix = "/subscription" host = "subscription.rhn.redhat.com" port = "443" location = "/etc/rhsm/ca/candlepin-local.pem" if cacert: if not os.path.exists(cacert): self.logger.info("Downloading CA cert.....") RHN().retrieveCert(cacert, location) if os.path.isfile(location): if os.stat(location).st_size > 0: ovirt_store_config(location) else: raise RuntimeError("Error Downloading CA cert!") smconf = list(sm) smconf.append('config') smconf.append('--server.hostname') smconf.append(host) smconf.append('--server.port') smconf.append(port) smconf.append('--server.prefix') smconf.append(prefix) if cacert: smconf.append('--rhsm.repo_ca_cert') smconf.append('/etc/rhsm/ca/candlepin-local.pem') try: subprocess.check_call(smconf) ovirt_store_config("/etc/rhsm/rhsm.conf") except: raise RuntimeError("Error updating subscription manager \ configuration") if profilename: args.append('--name') args.append(profilename) if proxy: try: (host, port) = proxy.split(":") process.check_call([ "subscription-manager", "config", "--server.proxy_hostname", host ]) process.check_call([ "subscription-manager", "config", "--server.proxy_port", port ]) if proxyuser: args.append('--proxyuser') args.append(proxyuser) cmd = [ "subscription-manager", "config", "--server.proxy_user", proxyuser ] process.check_call(cmd) if proxypass: args.append('--proxypassword') args.append(proxypass) cmd = [ "subscription-manager", "config", "--server.proxy_password", proxypass ] logged_args = list(cmd) remove_values_from_args = [ "--server.proxy_password" ] for idx, arg in enumerate(cmd): if arg in remove_values_from_args: logged_args[idx + 1] = "XXXXXXX" logged_args = str(logged_args) self.logger.info(logged_args) subprocess.check_call(cmd) except: raise RuntimeError("Error updating subscription \ manager proxy configuration") args.extend(extra_args) self.logger.info("Registering to RHN account.....") rhsm_configs = ([ "/var/lib/rhsm/cache/installed_products.json", "/var/lib/rhsm/facts/facts.json" ]) unmount_config(rhsm_configs) unmount_config(glob.glob("/etc/pki/consumer/*pem")) def unlink_if_exists(f): if os.path.exists(f): os.unlink(f) for f in rhsm_configs: unlink_if_exists(f) logged_args = list(args) remove_values_from_args = ["--password", "--proxypassword"] for idx, arg in enumerate(logged_args): if arg in remove_values_from_args: logged_args[idx + 1] = "XXXXXXX" logged_args = str(logged_args) self.logger.info(logged_args) smreg_output = process.pipe(args) self.logger.debug(smreg_output) if "been registered" not in smreg_output: if "Invalid credentials" in smreg_output: raise RuntimeError("Invalid Username / Password") elif "already been taken" in smreg_output: raise RuntimeError("Hostname is already " + "registered") else: raise RuntimeError("Registration Failed") else: ovirt_store_config(rhsm_configs) ovirt_store_config("/etc/pki/consumer/key.pem") ovirt_store_config("/etc/pki/consumer/cert.pem") self.logger.info("System %s sucessfully registered \ to %s" % (profilename, serverurl))