def read_kickstart(path): """Parse a kickstart file and return a KickstartParser instance. This is a simple utility function which takes a path to a kickstart file, parses it and returns a pykickstart KickstartParser instance which can be then passed to an ImageCreator constructor. If an error occurs, a CreatorError exception is thrown. """ version = ksversion.makeVersion() ks = ksparser.KickstartParser(version) try: if "://" not in path: path = "file://%s" % (urllib.request.pathname2url( os.path.abspath(path))) ksdata = urllib.request.urlopen(path).read().decode("utf-8") ks.readKickstartFromString(ksdata, reset=False) # Fallback to e.args[0] is a workaround for bugs in urlgragger and pykickstart. except IOError as e: raise errors.KickstartError("Failed to read kickstart file " "'%s' : %s" % (path, e.strerror or e.args[0])) except kserrors.KickstartError as e: raise errors.KickstartError("Failed to parse kickstart file " "'%s' : %s" % (path, e)) return ks
def read_kickstart(path): """Parse a kickstart file and return a KickstartParser instance. This is a simple utility function which takes a path to a kickstart file, parses it and returns a pykickstart KickstartParser instance which can be then passed to an ImageCreator constructor. If an error occurs, a CreatorError exception is thrown. """ version = ksversion.makeVersion() ks = ksparser.KickstartParser(version) try: ksfile = urlgrabber.urlgrab(path) ks.readKickstart(ksfile) # Fallback to e.args[0] is a workaround for bugs in urlgragger and pykickstart. except IOError as e: raise errors.KickstartError("Failed to read kickstart file " "'%s' : %s" % (path, e.strerror or e.args[0])) except kserrors.KickstartError as e: raise errors.KickstartError("Failed to parse kickstart file " "'%s' : %s" % (path, e)) return ks
def relabel(self, ksselinux): # touch some files which get unhappy if they're not labeled correctly for fn in ("/etc/resolv.conf", ): path = self.path(fn) if not os.path.islink(path): f = open(path, "a") os.chmod(path, 0o644) f.close() if ksselinux.selinux == ksconstants.SELINUX_DISABLED: return try: rc = subprocess.call([ 'setfiles', '-p', '-e', '/proc', '-e', '/sys', '-e', '/dev', selinux.selinux_file_context_path(), '/' ], preexec_fn=self.chroot) except OSError as e: if e.errno == errno.ENOENT: logging.info('The setfiles command is not available.') return if rc: if ksselinux.selinux == ksconstants.SELINUX_ENFORCING: raise errors.KickstartError("SELinux relabel failed.") else: logging.error("SELinux relabel failed.")
def apply(self, ksnet): path = self.path("/etc/network/interfaces") fs.makedirs(path) f = file(path, "w+") c = "auto lo\n" c += "iface lo inet loopback\n" f.write(c) useipv6 = False nodns = False hostname = None gateway = None nameservers = None for network in ksnet.network: if not network.device: raise errors.KickstartError("No --device specified with " "network kickstart command") if (network.onboot and network.bootProto.lower() != "dhcp" and not (network.ip and network.netmask)): raise errors.KickstartError("No IP address and/or netmask " "specified with static " "configuration for '%s'" % network.device) # self.write_ifcfg(network) # self.write_wepkey(network) if network.ipv6: useipv6 = True if network.nodns: nodns = True if network.hostname: hostname = network.hostname if network.gateway: gateway = network.gateway if network.nameserver: nameservers = network.nameserver.split(",") # self.write_sysconfig(useipv6, hostname, gateway) self.write_hosts(hostname) self.write_resolv(nodns, nameservers)
def apply(self, ksnet): fs.makedirs(self.path("/etc/sysconfig/network-scripts")) useipv6 = False nodns = False hostname = None gateway = None nameservers = None for network in ksnet.network: if not network.device: raise errors.KickstartError("No --device specified with " "network kickstart command") if (network.onboot and network.bootProto.lower() != "dhcp" and not (network.ip and network.netmask)): raise errors.KickstartError("No IP address and/or netmask " "specified with static " "configuration for '%s'" % network.device) self.write_ifcfg(network) self.write_wepkey(network) if network.ipv6: useipv6 = True if network.nodns: nodns = True if network.hostname: hostname = network.hostname if network.gateway: gateway = network.gateway if network.nameserver: nameservers = network.nameserver.split(",") self.write_sysconfig(useipv6, hostname, gateway) self.write_hosts(hostname) self.write_hostname(hostname) self.write_resolv(nodns, nameservers)
def set_unencrypted(self, password): try: p1 = subprocess.Popen(["echo", password], stdout=subprocess.PIPE, preexec_fn=self.chroot) except OSError as e: if e.errno == errno.ENOENT: raise errors.KickstartError("Unable to set unencrypted " "password due to lack of 'echo'.") try: p2 = subprocess.Popen(["passwd", "--stdin", "root"], stdin=p1.stdout, stdout=subprocess.PIPE, preexec_fn=self.chroot) except OSError as e: if e.errno == errno.ENOENT: raise errors.KickstartError("Unable to set unencrypted " "password due to lack of 'passwd'.") p2.communicate()
def set_unencrypted(self, password): for p in ("/bin/echo", "/usr/bin/passwd"): if not os.path.exists("%s/%s" %(self.instroot, p)): raise errors.KickstartError("Unable to set unencrypted password due to lack of %s" % p) p1 = subprocess.Popen(["/bin/echo", password], stdout = subprocess.PIPE, preexec_fn = self.chroot) p2 = subprocess.Popen(["/usr/bin/passwd", "--stdin", "root"], stdin = p1.stdout, stdout = subprocess.PIPE, preexec_fn = self.chroot) p2.communicate()
def relabel(self, ksselinux): # touch some files which get unhappy if they're not labeled correctly for fn in ("/etc/resolv.conf",): path = self.path(fn) if not os.path.islink(path): f = file(path, "a") os.chmod(path, 0644) f.close() if ksselinux.selinux == ksconstants.SELINUX_DISABLED: return if not os.path.exists(self.path("/sbin/setfiles")): return rc = self.call(["/sbin/setfiles", "-p", "-e", "/proc", "-e", "/sys", "-e", "/dev", selinux.selinux_file_context_path(), "/"]) if rc: if ksselinux.selinux == ksconstants.SELINUX_ENFORCING: raise errors.KickstartError("SELinux relabel failed.") else: logging.error("SELinux relabel failed.")
def call(self, args): try: return subprocess.call(args, preexec_fn=self.chroot) except OSError as e: if e.errno == errno.ENOENT: raise errors.KickstartError("Unable to run %s!" % (args))
def call(self, args): if not os.path.exists("%s/%s" % (self.instroot, args[0])): raise errors.KickstartError("Unable to run %s!" % (args)) return subprocess.call(args, preexec_fn=self.chroot)
""" version = ksversion.makeVersion() ks = ksparser.KickstartParser(version) try: ksfile = urlgrabber.urlgrab(path) ks.readKickstart(ksfile) # Fallback to e.args[0] is a workaround for bugs in urlgragger and pykickstart. except IOError, e: raise errors.KickstartError("Failed to read kickstart file " "'%s' : %s" % (path, e.strerror or e.args[0])) except kserrors.KickstartError, e: raise errors.KickstartError("Failed to parse kickstart file " "'%s' : %s" % (path, e)) return ks def build_name(kscfg, prefix=None, suffix=None, maxlen=None): """Construct and return an image name string. This is a utility function to help create sensible name and fslabel strings. The name is constructed using the sans-prefix-and-extension kickstart filename and the supplied prefix and suffix. If the name exceeds the maxlen length supplied, the prefix is first dropped and then the kickstart filename portion is reduced until it fits. In other words, the suffix takes precedence over the kickstart portion and the kickstart portion takes precedence over the prefix.