예제 #1
0
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
예제 #2
0
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
예제 #3
0
    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.")
예제 #4
0
    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)
예제 #5
0
    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)
예제 #6
0
    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()
예제 #7
0
    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()
예제 #8
0
    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.")
예제 #9
0
 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))
예제 #10
0
 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)
예제 #11
0
    """
    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.