Esempio n. 1
0
def main():
    opts = parse_args()
    if not opts.kscfg:
        print(_("Need to specify a config to flatten"), file=sys.stderr)
        sys.exit(1)

    ksversion = makeVersion(opts.version)
    ksparser = pykickstart.parser.KickstartParser(ksversion)
    try:
        ksparser.readKickstart(opts.kscfg)
    except IOError as msg:
        print(_("Failed to read kickstart file '%(filename)s' : %(error_msg)s") % {"filename": opts.kscfg, "error_msg": msg}, file=sys.stderr)
        sys.exit(1)
    except pykickstart.errors.KickstartError as e:
        print(_("Failed to parse kickstart file '%(filename)s' : %(error_msg)s") % {"filename": opts.kscfg, "error_msg": e}, file=sys.stderr)
        sys.exit(1)

    if opts.output:
        try:
            f = open(opts.output, 'w')
        except IOError as msg:
            print(_("Failed to open output file '%(filename)s' : %(error_msg)s") % {"filename": opts.output, "error_msg": msg}, file=sys.stderr)
            sys.exit(1)
    else:
        f = sys.stdout

    f.write("%s" % ksparser.handler)
    f.close()
Esempio n. 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:
        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
Esempio n. 3
0
def main(argv=None):
    opts = parse_args(argv)
    if not opts.kscfg:
        return (1, _("Need to specify a config to flatten"))

    try:
        ksversion = makeVersion(opts.version)
    except KickstartVersionError:
        print(_("The version %s is not supported by pykickstart") % opts.version)
        sys.exit(1)

    ksparser = pykickstart.parser.KickstartParser(ksversion)
    try:
        ksparser.readKickstart(opts.kscfg)
    except IOError as msg:
        return (1, _("Failed to read kickstart file '%(filename)s' : %(error_msg)s") % {"filename": opts.kscfg, "error_msg": msg})
    except pykickstart.errors.KickstartError as e:
        return (1, _("Failed to parse kickstart file '%(filename)s' : %(error_msg)s") % {"filename": opts.kscfg, "error_msg": e})

    if opts.output:
        try:
            f = open(opts.output, 'w')
        except IOError as msg:
            return (1, _("Failed to open output file '%(filename)s' : %(error_msg)s") % {"filename": opts.output, "error_msg": msg})
    else:
        f = sys.stdout

    f.write("%s" % ksparser.handler)

    if opts.output:
        f.close()

    return (0, '')
Esempio n. 4
0
def kickstart(ks, path=settings.KS_DIR):
    """
    return parsed pykickstart object
    """
    ks = "%s%s" % (path, ks)
    ksparser = KickstartParser(makeVersion())
    ksparser.readKickstart(ks)
    return ksparser
Esempio n. 5
0
def kickstart(ks, uploaded, path=settings.KS_DIR):
    """
    return parsed pykickstart object
    """
    if not uploaded:
        ks = "%s%s" % (path, ks)
    ksparser = DecoratedKickstartParser(makeVersion())
    ksparser.readKickstart(ks)
    
    return ksparser
Esempio n. 6
0
def kickstart(ks, path=settings.KS_DIR):
    """
    return parsed pykickstart object
    """
    ks = "%s%s" % (path, ks)
    ksparser = KickstartParser(makeVersion())
    if isinstance(ks, unicode):
        ks = ks.encode('utf-8')
    ksparser.readKickstart(ks)
    return ksparser
def process_kickstart(ksfile):
    # pykickstart refs
    # https://jlaska.fedorapeople.org/pykickstart-doc/pykickstart.commands.html
    ksparser = KickstartParser(makeVersion())
    try:
        ksparser.readKickstart(ksfile)
    except KickstartError as e:
        sys.stderr.write(str(e))
        sys.exit(1)
    user_data = '#!/bin/bash'
    # repo
    for repo in ksparser.handler.repo.repoList:
        if repo.mirrorlist:
            repo_url = 'metalink=%s' % repo.mirrorlist
        else:
            repo_url = 'baseurl=%s' % repo.baseurl
        user_data += """
cat <<"EOF" >/etc/yum.repos.d/%s.repo
[%s]
name=%s
%s
enabled=1
gpgcheck=0
EOF
""" % (repo.name,
       repo.name,
       repo.name,
       repo_url)  
    # rootpw
    if ksparser.handler.rootpw.isCrypted:
        user_data += 'echo "root:%s" | chpasswd -e\n' % ksparser.handler.rootpw.password
    else:
        user_data += 'echo "root:%s" | chpasswd\n' % ksparser.handler.rootpw.password
    # selinux
    if ksparser.handler.selinux.selinux is 0:
        selinux_status = 'disabled'
    elif ksparser.handler.selinux.selinux is 2:
        selinux_status = 'enforcing'
    else:
        selinux_status = 'enforcing'
    user_data += "sed -i 's/SELINUX=.*/SELINUX=%s/' /etc/selinux/config\n" % selinux_status
    # %packages
    packages = []
    for group in ksparser.handler.packages.groupList:
        packages.append("@%s" % group.name)
    for package in ksparser.handler.packages.packageList:
        packages.append(package)
    if packages:
        user_data += "yum -y install %s\n" % ' '.join(packages)
    # skip %prep
    # %post
    user_data += ksparser.handler.scripts[1].script
    # remove cloud-init package and reboot
    user_data += 'yum -y remove cloud-init\nreboot'
    print user_data
Esempio n. 8
0
	def __init__(self, main_window, kickstart=False):
		
		if kickstart:
			self.ksparser = pykickstart.parser.KickstartParser(makeVersion())
			self.storage = Blivet(ksdata=self.ksparser.handler)
		else:
			self.storage = Blivet()
			
		self.storage.reset()
		
		self.main_window = main_window
Esempio n. 9
0
def kickstart(ks, path=settings.KS_DIR):
    """
    return parsed pykickstart object
    """
    ks = "%s%s" % (path, ks)
    ksparser = DecoratedKickstartParser(makeVersion())
    ksparser.readKickstart(ks)
    
    # add the RIT stuff
    ritPostContents = get_rit_post()
    ksparser.addScript(ritPostContents, constants.KS_SCRIPT_POST)
    return ksparser
Esempio n. 10
0
    def __init__(self, ks_path, blacklist=None):
        self.ks_path = ks_path
        if blacklist is None:
            self.blacklist = []
        else:
            self.blacklist = blacklist

        # create a kickstart parser from the latest version
        self.ksparser = KickstartParser(makeVersion())
        self.yb = yum.YumBase()

        self._repo_problems = False
Esempio n. 11
0
 def split(self, path):
     """Split the kickstart given by path into elements."""
     self._elements = None
     self._kickstart_path = path
     handler = makeVersion()
     ksparser = SplitKickstartParser(handler, valid_sections=VALID_SECTIONS_ANACONDA)
     try:
         result = ksparser.split(path)
     except KickstartParseError as e:
         raise SplitKickstartSectionParsingError(e)
     except KickstartError as e:
         raise SplitKickstartMissingIncludeError(e)
     self._elements = result
Esempio n. 12
0
    def __init__(self, kickstart=False):

        if kickstart:
            self.ksparser = pykickstart.parser.KickstartParser(makeVersion())
            self.storage = blivet.Blivet(ksdata=self.ksparser.handler)
        else:
            self.storage = blivet.Blivet()

        blivet.formats.fs.NTFS._formattable = True

        self.storage.reset()
        self.storage.devicetree.populate()
        self.storage.devicetree.getActiveMounts()
        self.update_min_sizes_info()
Esempio n. 13
0
 def load_or_default(system_ks_path, ks_template):
     """ load system ks or default ks """
     ksparser = KickstartParser(makeVersion())
     try:
         ksparser.readKickstart(system_ks_path)
     except (KickstartError, IOError):
         log_message("Cannot read the system Kickstart at %s." % system_ks_path)
         try:
             ksparser.readKickstart(ks_template)
         except AttributeError:
             log_message("There is no KS_POSTSCRIPT_TEMPLATE specified in settings.py.", level=logging.DEBUG)
         except IOError:
             log_message("Cannot read the Kickstart template %s." % ks_template)
             return None
     return ksparser
Esempio n. 14
0
 def load_or_default(system_ks_path):
     """load system ks or default ks"""
     ksparser = KickstartParser(makeVersion())
     try:
         ksparser.readKickstart(system_ks_path)
     except (KickstartError, IOError):
         log_message("Can't read system kickstart at {0}".format(system_ks_path))
         try:
             ksparser.readKickstart(settings.KS_TEMPLATE)
         except AttributeError:
             log_message("There is no KS_TEMPLATE_POSTSCRIPT specified in settings.py")
         except IOError:
             log_message("Can't read kickstart template {0}".format(settings.KS_TEMPLATE))
             return None
     return ksparser
Esempio n. 15
0
    def runTest(self):
        for version, command_map in control.commandMap.items():

            handler = makeVersion(version)
            parser = KickstartParser(handler)

            for command_name, command_class in command_map.items():
                if not issubclass(command_class, DeprecatedCommand):
                    continue

                with warnings.catch_warnings(record=True):
                    # The deprecated commands should be ignored with
                    # a warning when they are parsed. Make sure that
                    # they will not cause any errors.
                    parser.readKickstartFromString(command_name)
Esempio n. 16
0
    def test_biosboot_bootloader_in_kickstart(self, mock_mountpoints, mock_devices, dbus):
        """Test that a biosboot bootloader shows up in the ks data."""
        # set up biosboot partition
        biosboot_device_obj = PartitionDevice("biosboot_partition_device")
        biosboot_device_obj.size = Size('1MiB')
        biosboot_device_obj.format = formats.get_format("biosboot")

        # mountpoints must exist for updateKSData to run
        mock_devices.return_value = [biosboot_device_obj]
        mock_mountpoints.values.return_value = []

        # initialize ksdata
        biosboot_blivet_obj = InstallerStorage(makeVersion())
        biosboot_blivet_obj.update_ksdata()

        self.assertIn("part biosboot", str(biosboot_blivet_obj.ksdata))
    def invalid_command_test(self):
        """Test invalid command or option in kickstart.

        Invalid command or command option in kickstart does not raise
        KickstartParseError because commands are not parsed in the filter.
        """
        ks_content = """
network --device=ens3 --activate
netork --device=ens5 --activate
network --device=ens7 --activate
network --devce=ens9 --activate
""".strip()

        handler = makeVersion()
        ksparser = SplitKickstartParser(handler)
        result = ksparser.split_from_string(ks_content)
        self.assertEqual(len(result.all_elements), 4)
Esempio n. 18
0
 def test_shutdown_virt(self):
     """Test a kickstart with reboot instead of shutdown"""
     opts = DataHolder(no_virt=False,
                       make_fsimage=True,
                       make_pxe_live=False)
     ks_version = makeVersion()
     ks = KickstartParser(ks_version,
                          errorsAreFatal=False,
                          missingIncludeIsFatal=False)
     ks.readKickstartFromString(
         "url --url=http://dl.fedoraproject.com\n"
         "network --bootproto=dhcp --activate\n"
         "repo --name=other --baseurl=http://dl.fedoraproject.com\n"
         "part / --size=4096\n"
         "reboot\n")
     errors = check_kickstart(ks, opts)
     self.assertTrue("must include shutdown when using virt" in errors[0])
Esempio n. 19
0
    def test_split_from_string_filename(self):
        """Test splitting kickstart supplied by string."""
        valid_sections = VALID_SECTIONS_ANACONDA
        handler = makeVersion()
        ksparser = SplitKickstartParser(handler, valid_sections)

        kickstart_files, _output = self._kickstart_flat
        filename, content = kickstart_files[0]

        # Kickstart from string has "<MAIN>" as filename
        result = ksparser.split_from_string(content)
        for element in result.all_elements:
            assert element.filename == SplitKickstartParser.unknown_filename
        # Or the value supplied by filename optional argument
        result = ksparser.split_from_string(content, filename=filename)
        for element in result.all_elements:
            assert element.filename == filename
Esempio n. 20
0
    def test_biosboot_bootloader_in_kickstart(self, mock_mountpoints, mock_devices, dbus):
        """Test that a biosboot bootloader shows up in the ks data."""
        # set up biosboot partition
        biosboot_device_obj = PartitionDevice("biosboot_partition_device")
        biosboot_device_obj.size = Size('1MiB')
        biosboot_device_obj.format = formats.get_format("biosboot")

        # mountpoints must exist for updateKSData to run
        mock_devices.return_value = [biosboot_device_obj]
        mock_mountpoints.values.return_value = []

        # initialize ksdata
        ksdata = makeVersion()
        biosboot_blivet_obj = InstallerStorage()
        update_storage_ksdata(biosboot_blivet_obj, ksdata)

        self.assertIn("part biosboot", str(ksdata))
Esempio n. 21
0
    def setUp(self):
        self.setupModules(["_isys", "logging"])
        
        import pyanaconda
        pyanaconda.anaconda_log = mock.Mock()

        from pykickstart.version import makeVersion
        from pyanaconda.flags import flags

        # set some things specially since we're just testing
        flags.testing = True

        # set up ksdata
        self.ksdata = makeVersion()

        from pyanaconda.packaging import Payload
        self.payload = Payload(self.ksdata)
Esempio n. 22
0
    def test_prepboot_bootloader_in_kickstart(self, mock_mountpoints,
                                              mock_bootloader_device, dbus):
        """Test that a prepboot bootloader shows up in the ks data."""
        # set up prepboot partition
        bootloader_device_obj = PartitionDevice("test_partition_device")
        bootloader_device_obj.size = Size('5 MiB')
        bootloader_device_obj.format = formats.get_format("prepboot")

        # mountpoints must exist for update_ksdata to run
        mock_bootloader_device.return_value = bootloader_device_obj
        mock_mountpoints.values.return_value = []

        # initialize ksdata
        prepboot_blivet_obj = InstallerStorage(makeVersion())
        prepboot_blivet_obj.update_ksdata()

        self.assertIn("part prepboot", str(prepboot_blivet_obj.ksdata))
Esempio n. 23
0
    def setUp(self):
        self.setupModules(["_isys", "logging"])

        import pyanaconda
        pyanaconda.anaconda_log = mock.Mock()

        from pykickstart.version import makeVersion
        from pyanaconda.flags import flags

        # set some things specially since we're just testing
        flags.testing = True

        # set up ksdata
        self.ksdata = makeVersion()

        from pyanaconda.packaging import Payload
        self.payload = Payload(self.ksdata)
Esempio n. 24
0
 def disk_size_simple_test(self):
     """Test calculating the disk size with a simple / partition"""
     opts = DataHolder(no_virt=True,
                       make_fsimage=False,
                       make_iso=False,
                       make_pxe_live=False)
     ks_version = makeVersion()
     ks = KickstartParser(ks_version,
                          errorsAreFatal=False,
                          missingIncludeIsFatal=False)
     ks.readKickstartFromString(
         "url --url=http://dl.fedoraproject.com\n"
         "network --bootproto=dhcp --activate\n"
         "repo --name=other --baseurl=http://dl.fedoraproject.com\n"
         "part / --size=4096\n"
         "shutdown\n")
     self.assertEqual(calculate_disk_size(opts, ks), 4098)
Esempio n. 25
0
 def test_nomethod_novirt(self):
     """Test a kickstart with repo and no url"""
     opts = DataHolder(no_virt=True,
                       make_fsimage=False,
                       make_pxe_live=False)
     ks_version = makeVersion()
     ks = KickstartParser(ks_version,
                          errorsAreFatal=False,
                          missingIncludeIsFatal=False)
     ks.readKickstartFromString(
         "network --bootproto=dhcp --activate\n"
         "repo --name=other --baseurl=http://dl.fedoraproject.com\n"
         "part / --size=4096\n"
         "shutdown\n")
     errors = check_kickstart(ks, opts)
     self.assertTrue("Only url, nfs and ostreesetup" in errors[0])
     self.assertTrue("repo can only be used with the url" in errors[1])
Esempio n. 26
0
def main():
    opts = parse_args()
    if not opts.kscfg:
        print(_("Need to specify a config to flatten"), file=sys.stderr)
        sys.exit(1)

    ksversion = makeVersion(opts.version)
    ksparser = pykickstart.parser.KickstartParser(ksversion)
    try:
        ksparser.readKickstart(opts.kscfg)
    except IOError as msg:
        print(
            _("Failed to read kickstart file '%(filename)s' : %(error_msg)s") %
            {
                "filename": opts.kscfg,
                "error_msg": msg
            },
            file=sys.stderr)
        sys.exit(1)
    except pykickstart.errors.KickstartError as e:
        print(
            _("Failed to parse kickstart file '%(filename)s' : %(error_msg)s")
            % {
                "filename": opts.kscfg,
                "error_msg": e
            },
            file=sys.stderr)
        sys.exit(1)

    if opts.output:
        try:
            f = open(opts.output, 'w')
        except IOError as msg:
            print(
                _("Failed to open output file '%(filename)s' : %(error_msg)s")
                % {
                    "filename": opts.output,
                    "error_msg": msg
                },
                file=sys.stderr)
            sys.exit(1)
    else:
        f = sys.stdout

    f.write("%s" % ksparser.handler)
    f.close()
Esempio n. 27
0
 def displaymode_test(self):
     """Test a kickstart with displaymode set"""
     opts = DataHolder(no_virt=True,
                       make_fsimage=False,
                       make_pxe_live=False)
     ks_version = makeVersion()
     ks = KickstartParser(ks_version,
                          errorsAreFatal=False,
                          missingIncludeIsFatal=False)
     ks.readKickstartFromString(
         "url --url=http://dl.fedoraproject.com\n"
         "network --bootproto=dhcp --activate\n"
         "repo --name=other --baseurl=http://dl.fedoraproject.com\n"
         "part / --size=4096\n"
         "shutdown\n"
         "graphical\n")
     errors = check_kickstart(ks, opts)
     self.assertTrue("must not set a display mode" in errors[0])
Esempio n. 28
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, e:
        raise errors.KickstartError("Failed to read kickstart file " "'%s' : %s" % (path, e.strerror or e.args[0]))
Esempio n. 29
0
    def test_valid_sections(self):
        """Test setting of valid sections for the parser."""
        valid_sections = VALID_SECTIONS_ANACONDA
        handler = makeVersion()
        ksparser = SplitKickstartParser(handler, valid_sections)
        kickstart_files, _output = self._kickstart_flat
        _filename, content = kickstart_files[0]

        returned_valid_sections = ksparser.valid_sections
        # valid_sections returns new list, not a reference to the internal object
        returned_valid_sections.append("%test")
        self.assertNotEqual(returned_valid_sections, ksparser.valid_sections)

        ksparser.valid_sections = ["%packages"]
        # Invalid section raises exception
        self.assertRaises(KickstartParseError, ksparser.split_from_string, content)
        # setting valid sections back to the original, the exception is gone
        ksparser.valid_sections = valid_sections
        ksparser.split_from_string(content)
Esempio n. 30
0
 def load_or_default(system_ks_path, ks_template):
     """ load system ks or default ks """
     ksparser = KickstartParser(makeVersion())
     try:
         ksparser.readKickstart(system_ks_path)
     except (KickstartError, IOError):
         log_message("Cannot read the system Kickstart at %s." %
                     system_ks_path)
         try:
             ksparser.readKickstart(ks_template)
         except AttributeError:
             log_message(
                 "There is no KS_POSTSCRIPT_TEMPLATE specified in settings.py.",
                 level=logging.DEBUG)
         except IOError:
             log_message("Cannot read the Kickstart template %s." %
                         ks_template)
             return None
     return ksparser
Esempio n. 31
0
    def add_repo(self, ksfile, siderepo):
        """ Add a repository to an existing KS file """

        # read
        ksparser = KickstartParser(makeVersion())
        ksparser.readKickstart(ksfile)
        
        #obtain the handler dump
        kshandlers = ksparser.handler

        # add a repository
        kshandlers.repo.repoList.extend(['repo --name="siderepo" --baseurl={0:s}\n'.format(siderepo)])

        # Write a new ks file
        outfile = open(ksfile, 'w')
        outfile.write(kshandlers.__str__())
        outfile.close()

        return
Esempio n. 32
0
 def load_or_default(system_ks_path):
     """load system ks or default ks"""
     ksparser = KickstartParser(makeVersion())
     try:
         ksparser.readKickstart(system_ks_path)
     except (KickstartError, IOError):
         log_message(
             "Can't read system kickstart at {0}".format(system_ks_path))
         try:
             ksparser.readKickstart(settings.KS_TEMPLATE)
         except AttributeError:
             log_message(
                 "There is no KS_TEMPLATE_POSTSCRIPT specified in settings.py"
             )
         except IOError:
             log_message("Can't read kickstart template {0}".format(
                 settings.KS_TEMPLATE))
             return None
     return ksparser
Esempio n. 33
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, e:
        raise errors.KickstartError("Failed to read kickstart file "
                                    "'%s' : %s" % (path, e.strerror or
                                    e.args[0]))
Esempio n. 34
0
    def test_prepboot_bootloader_in_kickstart(self, mock_mountpoints, dbus):
        """Test that a prepboot bootloader shows up in the ks data."""
        # disable other partitioning modules
        dbus.return_value.Enabled = False

        # set up prepboot partition
        bootloader_device_obj = PartitionDevice("test_partition_device")
        bootloader_device_obj.size = Size('5 MiB')
        bootloader_device_obj.format = formats.get_format("prepboot")

        # mountpoints must exist for update_ksdata to run
        mock_mountpoints.values.return_value = []

        # set up the storage
        prepboot_blivet_obj = InstallerStorage()
        prepboot_blivet_obj.bootloader.stage1_device = bootloader_device_obj

        # initialize ksdata
        ksdata = makeVersion()
        update_storage_ksdata(prepboot_blivet_obj, ksdata)

        self.assertIn("part prepboot", str(ksdata))
Esempio n. 35
0
    def test_prepboot_bootloader_in_kickstart(self, mock_mountpoints, dbus):
        """Test that a prepboot bootloader shows up in the ks data."""
        # disable other partitioning modules
        dbus.return_value.Enabled = False

        # set up prepboot partition
        bootloader_device_obj = PartitionDevice("test_partition_device")
        bootloader_device_obj.size = Size('5 MiB')
        bootloader_device_obj.format = formats.get_format("prepboot")

        # mountpoints must exist for update_ksdata to run
        mock_mountpoints.values.return_value = []

        # set up the storage
        prepboot_blivet_obj = InstallerStorage()
        prepboot_blivet_obj.bootloader.stage1_device = bootloader_device_obj

        # initialize ksdata
        ksdata = makeVersion()
        update_storage_ksdata(prepboot_blivet_obj, ksdata)

        self.assertIn("part prepboot", str(ksdata))
    def test_conflicting_commands(self):
        """Test conflicting commands in kickstart.

        Conflicting commands in kickstart do not raise KickstartParseError
        because commands are not parsed in the filter.
        """
        ks_content = """
# Partitioning conflicts with autopart
part /boot --fstype=xfs --onpart=vda1
part pv.100000 --size=18436 --ondisk=vda
volgroup Vol00 --pesize=4096 pv.100000
logvol / --fstype=xfs --name=lv_root --vgname=Vol00 --size=15360
logvol /home --fstype=xfs --name=lv_home --vgname=Vol00 --size=1024
logvol swap --fstype=swap --name=lv_swap --vgname=Vol00 --size=2048

autopart --encrypted --passphrase=starost --type=lvm
""".strip()

        handler = makeVersion()
        ksparser = SplitKickstartParser(handler)
        result = ksparser.split_from_string(ks_content)
        assert len(result.all_elements) == 7
Esempio n. 37
0
    def runTest(self):
        kshandler = makeVersion(DEVEL)
        self.assertIsNotNone(kshandler)

        # Did it add the commands, and is there at least one (part) that should be present?
        ksc = ksshell.KickstartCompleter(kshandler, {})
        self.assertTrue(len(ksc.commands) > 0)
        self.assertIn("part", ksc.commands)

        # Test tab completion on 'part [TAB]'
        # Initialize the matches with a mocked input line
        ksc._init_matches("part ", 5, 5)
        self.assertEqual(ksc.complete("", 1), "--fstype")

        # Test tab completion on 'auth[TAB]'
        ksc._init_matches("auth", 0, 5)

        # Python 3.5 returns things in a different order, just make sure they are there and different
        self.assertIn(ksc.complete("", 1),
                      ["auth", "authconfig", "authselect"])
        self.assertIn(ksc.complete("", 2),
                      ["auth", "authconfig", "authselect"])
        self.assertNotEqual(ksc.complete("", 1), ksc.complete("", 2))
Esempio n. 38
0
    def runTest(self):
        for version, command_map in control.commandMap.items():

            handler = makeVersion(version)
            parser = KickstartParser(handler)

            for command_name, command_class in command_map.items():
                if not issubclass(command_class, DeprecatedCommand):
                    continue
                if issubclass(command_class, RemovedCommand):
                    continue

                with warnings.catch_warnings(record=True):
                    # The deprecated commands should be ignored with
                    # a warning when they are parsed. Make sure that
                    # they will not cause any errors.
                    with self.assertWarns(KickstartDeprecationWarning) as cm:
                        parser.readKickstartFromString(command_name)

                    # Check the warning message.
                    expected = " {} command has been deprecated ".format(
                        command_name)
                    self.assertIn(expected, str(cm.warning))
Esempio n. 39
0
def bootloader_append(line, kernel_append):
    """ Insert the kernel_append string into the --append argument

    :param line: The bootloader ... line
    :type line: str
    :param kernel_append: The arguments to append to the --append section
    :type kernel_append: str

    Using pykickstart to process the line is the best way to make sure it
    is parsed correctly, and re-assembled for inclusion into the final kickstart
    """
    ks_version = makeVersion()
    ks = KickstartParser(ks_version,
                         errorsAreFatal=False,
                         missingIncludeIsFatal=False)
    ks.readKickstartFromString(line)

    if ks.handler.bootloader.appendLine:
        ks.handler.bootloader.appendLine += " %s" % kernel_append
    else:
        ks.handler.bootloader.appendLine = kernel_append

    # Converting back to a string includes a comment, return just the bootloader line
    return str(ks.handler.bootloader).splitlines()[-1]
Esempio n. 40
0
def main():

    ##
    ## OPTION PROCESSING
    ##

    op = argparse.ArgumentParser()
    op.add_argument("-i", "--input", dest="input",
                    help=_("a basis file to use for seeding the kickstart data (optional)"))
    op.add_argument("-o", "--output", dest="output",
                    help=_("the location to write the finished kickstart file, or stdout if not given"))
    op.add_argument("-v", "--version", dest="version", default=DEVEL,
                    help=_("version of kickstart syntax to validate against"))

    opts = op.parse_args(sys.argv[1:])

    ##
    ## SETTING UP PYKICKSTART
    ##

    try:
        kshandler = makeVersion(opts.version)
    except KickstartVersionError:
        print(_("The version %s is not supported by pykickstart") % opts.version)
        sys.exit(1)

    ksparser = KickstartParser(kshandler, followIncludes=True, errorsAreFatal=False)

    if opts.input:
        try:
            processedFile = preprocessKickstart(opts.input)
            ksparser.readKickstart(processedFile)
            os.remove(processedFile)
        except KickstartError as e:
            # Errors should just dump you to the prompt anyway.
            print(_("Warning:  The following error occurred when processing the input file:\n%s\n") % e)

    internalCommands = {".clear": ClearCommand(),
                        ".show": ShowCommand(),
                        ".quit": QuitCommand()}

    ##
    ## SETTING UP READLINE
    ##

    readline.parse_and_bind("tab: complete")
    readline.set_completer(KickstartCompleter(kshandler, internalCommands).complete)

    # Since everything in kickstart looks like a command line arg, we need to
    # remove '-' from the delimiter string.
    delims = readline.get_completer_delims()
    readline.set_completer_delims(delims.replace('-', ''))

    ##
    ## REPL
    ##

    print("Press ^D to exit.")

    while True:
        try:
            line = six.moves.input("ks> ")  # pylint: disable=no-member
        except EOFError:
            # ^D was hit, time to quit.
            break
        except KeyboardInterrupt:
            # ^C was hit, time to quit.  Don't be like other programs.
            break

        # All internal commands start with a ., so if that's the beginning of the
        # line, we need to dispatch ourselves.
        if line.startswith("."):
            words = line.split()
            if words[0] in internalCommands:
                try:
                    internalCommands[words[0]].execute(ksparser)
                except EOFError:
                    # ".quit" was typed, time to quit.
                    break
            else:
                print(_("Internal command %s not recognized.") % words[0])

            continue

        # Now process the line of input as if it were a kickstart file - just an
        # extremely short one.
        try:
            ksparser.readKickstartFromString(line)
        except KickstartError as e:
            print(e)

    # And finally, print the output kickstart file.
    if opts.output:
        with open(opts.output, "w") as fd:
            fd.write(str(ksparser.handler))
    else:
        print("\n" + str(ksparser.handler))
Esempio n. 41
0
#!/usr/bin/python

import sys
from pykickstart.parser import *
from pykickstart.version import makeVersion
ksparser = KickstartParser(makeVersion('RHEL7'),followIncludes=False)
ksparser.readKickstart(sys.argv[1])
ksgroups = ksparser.handler.packages.groupList
ksgroupnames = [group.name for group in ksgroups]
for n in ksgroupnames:
  print n
Esempio n. 42
0
    def run(self):
        self.ksHandler = makeVersion()

        if self.file:
            self.parser = KickstartParser(self.ksHandler)

            msg = None

            try:
                self.parser.readKickstart(self.file)
            except (KickstartParseError, KickstartValueError) as e:
                msg = _("The following error was found while parsing your "
                        "kickstart configuration:\n\n%s" % e)
            except KickstartError:
                msg = _(
                    "The kickstart file %s could not be opened.") % self.file

            if msg:
                dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR,
                                        gtk.BUTTONS_OK, msg)
                dlg.set_title(_("Error Parsing Kickstart Config"))
                dlg.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
                dlg.set_modal(True)
                dlg.run()
                dlg.destroy()
                sys.exit(0)

        self.xml = xml
        name_tag = (_("Kickstart"))
        comment_tag = (_("Create a kickstart file"))

        self.toplevel = xml.get_widget("main_window")
        self.toplevel.connect("destroy", self.destroy)
        self.toplevel.set_icon(iconPixbuf)

        #bring in widgets from glade file
        self.options_notebook = xml.get_widget("options_notebook")
        self.install_radiobutton = xml.get_widget("install_radiobutton")
        self.category_clist = xml.get_widget("category_clist")
        self.open_menu = xml.get_widget("open_menu")
        self.preview_menu = xml.get_widget("preview_menu")
        self.save_menu = xml.get_widget("save_menu")
        self.quit_menu = xml.get_widget("quit_menu")
        self.about_menu = xml.get_widget("about_menu")

        #populate category list
        self.category_view = xml.get_widget("list_view")
        self.category_store = gtk.ListStore(gobject.TYPE_STRING)
        self.category_view.set_model(self.category_store)

        col = gtk.TreeViewColumn(_("Subsection"),
                                 gtk.CellRendererText(),
                                 text=0)
        col.set_sort_column_id(0)
        self.category_view.append_column(col)

        self.category_list = [(_("Basic Configuration")),
                              (_("Installation Method")),
                              (_("Boot Loader Options")),
                              (_("Partition Information")),
                              (_("Network Configuration")),
                              (_("Authentication")),
                              (_("Firewall Configuration")),
                              (_("Display Configuration")),
                              (_("Package Selection")),
                              (_("Pre-Installation Script")),
                              (_("Post-Installation Script"))]

        for item in self.category_list:
            iter = self.category_store.append()
            self.category_store.set_value(iter, 0, item)

#bring in basic functions
        self.basic_class = basic.basic(self, xml, self.options_notebook,
                                       self.ksHandler)

        # Now that we've loaded the UI elements for the first active thing in the notebook,
        # draw it so we can display a progress bar when yum starts doing stuff.
        self.toplevel.show()
        while gtk.events_pending():
            gtk.main_iteration()

        self.bootloader_class = bootloader.bootloader(xml,
                                                      self.options_notebook,
                                                      self.ksHandler)
        self.install_class = install.install(self, xml, self.category_store,
                                             self.category_view,
                                             self.options_notebook,
                                             self.ksHandler)
        self.partition_class = partition.partition(xml, self.ksHandler)
        self.network_class = network.network(xml, self.ksHandler)
        self.auth_class = auth.auth(xml, self.ksHandler)
        self.firewall_class = firewall.Firewall(xml, self.ksHandler)
        self.X_class = xconfig.xconfig(xml, self.ksHandler)
        self.progress_window = progressWindow.ProgressWindow(self.toplevel)
        self.packages_class = packages.Packages(xml, self.ksHandler,
                                                self.progress_window)
        self.scripts_class = scripts.scripts(xml, self.ksHandler)

        self.open_menu.connect("activate", self.on_activate_open)
        self.preview_menu.connect("activate", self.on_activate_preview_options)
        self.save_menu.connect("activate", self.on_activate_save_options)
        self.quit_menu.connect("activate", gtk.main_quit)
        self.about_menu.connect("activate", self.on_about_activate)
        self.category_view.connect("cursor_changed",
                                   self.on_list_view_row_activated)
        self.options_notebook.connect("switch-page", self.on_notebook_changed)

        #show gui
        self.applyKickstart()
        self.toplevel.show()

        gtk.main()
Esempio n. 43
0
def start_build(cfg,
                dnflock,
                gitlock,
                branch,
                recipe_name,
                compose_type,
                test_mode=0):
    """ Start the build

    :param cfg: Configuration object
    :type cfg: ComposerConfig
    :param dnflock: Lock and YumBase for depsolving
    :type dnflock: YumLock
    :param recipe: The recipe to build
    :type recipe: str
    :param compose_type: The type of output to create from the recipe
    :type compose_type: str
    :returns: Unique ID for the build that can be used to track its status
    :rtype: str
    """
    share_dir = cfg.get("composer", "share_dir")
    lib_dir = cfg.get("composer", "lib_dir")

    # Make sure compose_type is valid
    if compose_type not in compose_types(share_dir):
        raise RuntimeError("Invalid compose type (%s), must be one of %s" %
                           (compose_type, compose_types(share_dir)))

    # Some image types (live-iso) need extra packages for composer to execute the output template
    with dnflock.lock:
        extra_pkgs = get_extra_pkgs(dnflock.dbo, share_dir, compose_type)
    log.debug("Extra packages needed for %s: %s", compose_type, extra_pkgs)

    with gitlock.lock:
        (commit_id, recipe) = read_recipe_and_id(gitlock.repo, branch,
                                                 recipe_name)

    # Combine modules and packages and depsolve the list
    module_nver = recipe.module_nver
    package_nver = recipe.package_nver
    package_nver.extend([(name, '*') for name in extra_pkgs])

    projects = sorted(set(module_nver + package_nver),
                      key=lambda p: p[0].lower())
    deps = []
    log.info("depsolving %s", recipe["name"])
    try:
        # This can possibly update repodata and reset the YumBase object.
        with dnflock.lock_check:
            (installed_size,
             deps) = projects_depsolve_with_size(dnflock.dbo,
                                                 projects,
                                                 recipe.group_names,
                                                 with_core=False)
    except ProjectsError as e:
        log.error("start_build depsolve: %s", str(e))
        raise RuntimeError("Problem depsolving %s: %s" %
                           (recipe["name"], str(e)))

    # Read the kickstart template for this type
    ks_template_path = joinpaths(share_dir, "composer", compose_type) + ".ks"
    ks_template = open(ks_template_path, "r").read()

    # How much space will the packages in the default template take?
    ks_version = makeVersion()
    ks = KickstartParser(ks_version,
                         errorsAreFatal=False,
                         missingIncludeIsFatal=False)
    ks.readKickstartFromString(ks_template + "\n%end\n")
    pkgs = [(name, "*") for name in ks.handler.packages.packageList]
    grps = [grp.name for grp in ks.handler.packages.groupList]
    try:
        with dnflock.lock:
            (template_size, _) = projects_depsolve_with_size(
                dnflock.dbo,
                pkgs,
                grps,
                with_core=not ks.handler.packages.nocore)
    except ProjectsError as e:
        log.error("start_build depsolve: %s", str(e))
        raise RuntimeError("Problem depsolving %s: %s" %
                           (recipe["name"], str(e)))
    log.debug("installed_size = %d, template_size=%d", installed_size,
              template_size)

    # Minimum LMC disk size is 1GiB, and anaconda bumps the estimated size up by 10% (which doesn't always work).
    installed_size = int((installed_size + template_size)) * 1.2
    log.debug("/ partition size = %d", installed_size)

    # Create the results directory
    build_id = str(uuid4())
    results_dir = joinpaths(lib_dir, "results", build_id)
    os.makedirs(results_dir)

    # Write the recipe commit hash
    commit_path = joinpaths(results_dir, "COMMIT")
    with open(commit_path, "w") as f:
        f.write(commit_id)

    # Write the original recipe
    recipe_path = joinpaths(results_dir, "blueprint.toml")
    with open(recipe_path, "w") as f:
        f.write(recipe.toml())

    # Write the frozen recipe
    frozen_recipe = recipe.freeze(deps)
    recipe_path = joinpaths(results_dir, "frozen.toml")
    with open(recipe_path, "w") as f:
        f.write(frozen_recipe.toml())

    # Write out the dependencies to the results dir
    deps_path = joinpaths(results_dir, "deps.toml")
    with open(deps_path, "w") as f:
        f.write(toml.dumps({"packages": deps}))

    # Save a copy of the original kickstart
    shutil.copy(ks_template_path, results_dir)

    with dnflock.lock:
        repos = list(dnflock.dbo.repos.iter_enabled())
    if not repos:
        raise RuntimeError("No enabled repos, canceling build.")

    # Create the git rpms, if any, and return the path to the repo under results_dir
    gitrpm_repo = create_gitrpm_repo(results_dir, recipe)

    # Create the final kickstart with repos and package list
    ks_path = joinpaths(results_dir, "final-kickstart.ks")
    with open(ks_path, "w") as f:
        ks_url = repo_to_ks(repos[0], "url")
        log.debug("url = %s", ks_url)
        f.write('url %s\n' % ks_url)
        for idx, r in enumerate(repos[1:]):
            ks_repo = repo_to_ks(r, "baseurl")
            log.debug("repo composer-%s = %s", idx, ks_repo)
            f.write('repo --name="composer-%s" %s\n' % (idx, ks_repo))

        if gitrpm_repo:
            log.debug("repo gitrpms = %s", gitrpm_repo)
            f.write('repo --name="gitrpms" --baseurl="file://%s"\n' %
                    gitrpm_repo)

        # Setup the disk for booting
        # TODO Add GPT and UEFI boot support
        f.write('clearpart --all --initlabel\n')

        # Write the root partition and it's size in MB (rounded up)
        f.write('part / --size=%d\n' % ceil(installed_size / 1024**2))

        # Some customizations modify the template before writing it
        f.write(customize_ks_template(ks_template, recipe))

        for d in deps:
            f.write(dep_nevra(d) + "\n")

        # Include the rpms from the gitrpm repo directory
        if gitrpm_repo:
            for rpm in glob(os.path.join(gitrpm_repo, "*.rpm")):
                f.write(os.path.basename(rpm)[:-4] + "\n")

        f.write("%end\n")

        # Other customizations can be appended to the kickstart
        add_customizations(f, recipe)

    # Setup the config to pass to novirt_install
    log_dir = joinpaths(results_dir, "logs/")
    cfg_args = compose_args(compose_type)

    # Get the title, project, and release version from the host
    if not os.path.exists("/etc/os-release"):
        log.error(
            "/etc/os-release is missing, cannot determine product or release version"
        )
    os_release = flatconfig("/etc/os-release")

    log.debug("os_release = %s", dict(os_release.items()))

    cfg_args["title"] = os_release.get("PRETTY_NAME", "")
    cfg_args["project"] = os_release.get("NAME", "")
    cfg_args["releasever"] = os_release.get("VERSION_ID", "")
    cfg_args["volid"] = ""
    cfg_args["extra_boot_args"] = get_kernel_append(recipe)

    cfg_args.update({
        "compression": "xz",
        "compress_args": [],
        "ks": [ks_path],
        "logfile": log_dir,
        "timeout": 60,  # 60 minute timeout
    })
    with open(joinpaths(results_dir, "config.toml"), "w") as f:
        f.write(toml.dumps(cfg_args))

    # Set the initial status
    open(joinpaths(results_dir, "STATUS"), "w").write("WAITING")

    # Set the test mode, if requested
    if test_mode > 0:
        open(joinpaths(results_dir, "TEST"), "w").write("%s" % test_mode)

    write_timestamp(results_dir, TS_CREATED)
    log.info("Adding %s (%s %s) to compose queue", build_id, recipe["name"],
             compose_type)
    os.symlink(results_dir, joinpaths(lib_dir, "queue/new/", build_id))

    return build_id
                spokeClass = v
        except TypeError:
            pass

if not spokeClass:
    try:
        spokeClass = getattr(spokeModule, spokeClassName)
    except KeyError:
        print "%s %s could not be found in %s" % (SpokeText, spokeClassName, spokeModuleName)
        sys.exit(1)


print "Running %s %s from %s" % (spokeText, spokeClass, spokeModule)

platform = getPlatform()
ksdata = makeVersion()
storage = Storage(data=ksdata, platform=platform)
storage.reset()
instclass = DefaultInstall()

payload = YumPayload(ksdata)
payload.setup(storage)

spoke = spokeClass(ksdata, storage, payload, instclass)
if hasattr(spoke, "register_event_cb"):
    spoke.register_event_cb("continue", lambda: Gtk.main_quit())
    spoke.register_event_cb("quit", lambda: Gtk.main_quit())
spoke.initialize()

if not spoke.showable:
    print "This %s is not showable, but I'll continue anyway." % spokeText
Esempio n. 45
0
 def get_parser(self):
     """Command sequence tests need a fresh parser
     for each test"""
     handler = makeVersion(self.version)
     return KickstartParser(handler)
Esempio n. 46
0
    def _parse_kickstart(self, path):
        """
        Loads the template with information from the supplied kickstart path.

        Kickstarts currently populate the meta tag, similar to the following:
          'kickstart': {
            'platform': '',
            'version':  'DEVEL',
            'language': 'en_US.UTF-8',
            'keyboard': 'us'
            'timezone': 'US/Eastern'
            'auth':     '--useshadow --passalgo=sha512
            'selinux':  '--enforcing'
            'firewall': '--enabled --service=mdns
            'xconfig':  '--startxonboot'
            'part':     '/ --size 4096 --fstype ext4',
            'services': '--enabled=NetworkManager,ModemManager --disabled=network,sshd'
            'commands': [
            ],
            'scripts': [
            ]
            'packages': [
            ]
          },
        }

        Args:
          path: Path to existing kickstart file.

        Returns:
          Nothing.

        Raises:
          IOError: An error occurred accessing the kickstart file.
        """

        ksversion = makeVersion(DEVEL)
        ksparser = pykickstart.parser.KickstartParser(ksversion)

        try:
            ksparser.readKickstart(path)

        except IOError as msg:
            print("Failed to read kickstart file '%(filename)s' : %(error_msg)s" % {"filename": path, "error_msg": msg}, file=sys.stderr)
            return

        except pykickstart.errors.KickstartError as e:
            print("Failed to parse kickstart file '%(filename)s' : %(error_msg)s" % {"filename": path, "error_msg": e}, file=sys.stderr)
            return

        handler = ksparser.handler

        meta = {}

        if handler.platform:
            meta['platform'] = handler.platform
            meta['version'] = versionToString(handler.version)

        lst = list(handler._writeOrder.keys())
        lst.sort()

        if len(lst):
            meta['commands'] = []
            for prio in lst:
                for c in handler._writeOrder[prio]:
                    # we don't store null commands (why pykickstart? why?)
                    if c.currentCmd == '':
                        continue

                    elif c.currentCmd == 'repo':
                        for r in c.__str__().split('\n'):
                            # ignore blank lines
                            if len(r.strip()) == 0:
                                continue

                            self._repos.add(Repository(r))

                    else:
                        meta['commands'].append({
                            'command':  c.currentCmd,
                            'priority': c.writePriority,
                            'data':     c.__str__()
                        })

        # parse pykickstart script
        if len(handler.scripts):
            meta['scripts'] = []

            for s in handler.scripts:
                meta['scripts'].append({
                    'data':          s.script,
                    'type':          s.type,
                    'interp':        s.interp,
                    'in_chroot':     s.inChroot,
                    'line_no':       s.lineno,
                    'error_on_fail': s.errorOnFail,
                })

        # parse pykickstart packages
        packages = handler.packages

        meta['packages'] = {
            'default':        packages.default,
            'exclude_docs':   packages.excludeDocs,
            'no_base':        not packages.addBase,
            'no_core':        packages.nocore,
            'handle_missing': (packages.handleMissing == pykickstart.constants.KS_MISSING_IGNORE),
            'install_langs':  packages.instLangs,
            'multi_lib':      packages.multiLib
        }

        if not packages.default:
            if packages.environment:
                meta['package']['environment'] = "@^{0}".format(packages.environment)

            grps = packages.groupList
            grps.sort()
            for g in grps:
                self._packages.add(Package({'n': g.__str__(), 'z': 1}))

            pkgs = packages.packageList
            pkgs.sort()
            for p in pkgs:
                self._packages.add(Package({'n': p.__str__(), 'z': 1}))

            grps = packages.excludedGroupList
            grps.sort()
            for g in grps:
                self._packages.add(Package({'n': g.__str__(), 'z': 0}))

            pkgs = packages.excludedList
            pkgs.sort()
            for p in pkgs:
                self._packages.add(Package({'n': p.__str__(), 'z': 0}))

        self._meta['kickstart'] = meta
    def test_simple_split_kickstart_parser(self):
        """This test should demonstrate usage and output of the parser."""
        ks_content = """
%pre
echo PRE
%end
network --device=ens3 --activate
network --device=ens4 --activate
%addon pony --fly=True
%end
firewall --enabled
%addon scorched --planet=Earth
nuke
%end
%post --nochroot --interpreter /usr/bin/bash
echo POST1
%end
""".lstrip()

        element1 = ("pre", "%pre\necho PRE\n%end\n", 1)
        element2 = ("network", "network --device=ens3 --activate\n", 4)
        element3 = ("network", "network --device=ens4 --activate\n", 5)
        element4 = ("pony", "%addon pony --fly=True\n%end\n", 6)
        element5 = ("firewall", "firewall --enabled\n", 8)
        element6 = ("scorched", "%addon scorched --planet=Earth\nnuke\n%end\n",
                    9)
        element7 = (
            "post",
            "%post --nochroot --interpreter /usr/bin/bash\necho POST1\n%end\n",
            12)
        expected_result = [
            element1, element2, element3, element4, element5, element6,
            element7
        ]

        filename = "ks.test.simple.cfg"

        valid_sections = VALID_SECTIONS_ANACONDA

        handler = makeVersion()
        ksparser = SplitKickstartParser(handler, valid_sections)

        # Reading kickstart from file

        with open(filename, "w") as f:
            f.write(ks_content)
        result = ksparser.split(filename)
        os.remove(filename)

        for element, expected in zip(result.all_elements, expected_result):
            assert element.filename == filename
            assert (element.name, element.content, element.lineno) == expected

        assert result.get_kickstart_from_elements(
            result.all_elements) == ks_content

        # Reading kickstart from string

        filename = ksparser.unknown_filename
        result = ksparser.split_from_string(ks_content)

        for element, expected in zip(result.all_elements, expected_result):
            assert element.filename == filename
            assert (element.name, element.content, element.lineno) == expected

        assert result.get_kickstart_from_elements(
            result.all_elements) == ks_content

        # Reading kickstart from string supplying filename

        filename = "MY_FILENAME"
        result = ksparser.split_from_string(ks_content, filename=filename)

        for element, expected in zip(result.all_elements, expected_result):
            assert element.filename == filename
            assert (element.name, element.content, element.lineno) == expected

        # Dumping kickstart

        assert result.get_kickstart_from_elements(
            result.all_elements) == ks_content
Esempio n. 48
0
 def get_parser(self):
     """Command sequence tests need a fresh parser
     for each test"""
     handler = makeVersion(self.version)
     return KickstartParser(handler)
Esempio n. 49
0
 def handler(self):
     if self._handler is None:
         self._handler = makeVersion(self.version)
     return self._handler
Esempio n. 50
0
def main(argv=None):
    op = argparse.ArgumentParser()
    op.add_argument("-f", "--from", dest="f")
    op.add_argument("-t", "--to", dest="t")
    op.add_argument("-l", "--listversions", dest="listversions", action="store_true",
                    default=False,
                    help=_("list the available versions of kickstart syntax"))

    opts = op.parse_args(argv)

    if opts.listversions:
        for key in sorted(versionMap.keys()):
            print(key)
        return 0

    if not opts.f or not opts.t:
        print(_("You must specify two syntax versions."))
        return 1

    try:
        fromHandler = makeVersion(opts.f)
        toHandler = makeVersion(opts.t)
    except KickstartVersionError as exn:
        print(_("The version %s is not supported by pykickstart") % exn)
        return 1

    fromCmdSet = getCommandSet(fromHandler)
    toCmdSet = getCommandSet(toHandler)
    bothSet = fromCmdSet & toCmdSet

    print(_("The following commands were removed in %s:") % opts.t)
    printList(sorted(fromCmdSet - toCmdSet))

    print(_("The following commands were deprecated in %s:") % opts.t)
    printList(sorted([cmd for cmd in bothSet if isinstance(toHandler.commands[cmd], DeprecatedCommand)]))

    print(_("The following commands were added in %s:") % opts.t)
    printList(sorted(toCmdSet - fromCmdSet))

    for cmd in sorted(bothSet):
        newOptList = []
        deprecatedOptList = []
        removedOptList = []

        fromCmd = fromHandler.commands[cmd]
        toCmd = toHandler.commands[cmd]

        if not hasattr(fromCmd, "op") or not hasattr(toCmd, "op"):
            continue

        fromOpt = fromCmd.op._actions
        toOpt = toCmd.op._actions

        newOptList = getOptSet(toOpt) - getOptSet(fromOpt)
        removedOptList = getOptSet(fromOpt) - getOptSet(toOpt)
        deprecatedOptList = getOptSet([cmd for cmd in toOpt if cmd.deprecated])

        if len(newOptList) > 0:
            print(_("The following options were added to the %(command_name)s command in %(version)s:") % {"command_name": cmd, "version": opts.t})
            printList(sorted(newOptList))

        if len(deprecatedOptList) > 0:
            print(_("The following options were deprecated from the %(command_name)s command in %(version)s:") % {"command_name": cmd, "version": opts.t})
            printList(sorted(deprecatedOptList))

        if len(removedOptList) > 0:
            print(_("The following options were removed from the %(command_name)s command in %(version)s:") % {"command_name": cmd, "version": opts.t})
            printList(sorted(removedOptList))

    return 0
Esempio n. 51
0
 def execute(self, parser):
     version = parser.version
     parser.handler = makeVersion(version)
Esempio n. 52
0
 def handler(self):
     if self._handler is None:
         self._handler = makeVersion(self.version)
     return self._handler
Esempio n. 53
0
def run_creator(opts, cancel_func=None):
    """Run the image creator process

    :param opts: Commandline options to control the process
    :type opts: Either a DataHolder or ArgumentParser
    :param cancel_func: Function that returns True to cancel build
    :type cancel_func: function
    :returns: The result directory and the disk image path.
    :rtype: Tuple of str

    This function takes the opts arguments and creates the selected output image.
    See the cmdline --help for livemedia-creator for the possible options

    (Yes, this is not ideal, but we can fix that later)
    """
    result_dir = None

    # Parse the kickstart
    if opts.ks:
        ks_version = makeVersion()
        ks = KickstartParser(ks_version,
                             errorsAreFatal=False,
                             missingIncludeIsFatal=False)
        ks.readKickstart(opts.ks[0])

    # live iso usually needs dracut-live so warn the user if it is missing
    if opts.ks and opts.make_iso:
        if "dracut-live" not in ks.handler.packages.packageList:
            log.error("dracut-live package is missing from the kickstart.")
            raise RuntimeError(
                "dracut-live package is missing from the kickstart.")

    # Make the disk or filesystem image
    if not opts.disk_image and not opts.fs_image:
        if not opts.ks:
            raise RuntimeError("Image creation requires a kickstart file")

        # Check the kickstart for problems
        errors = check_kickstart(ks, opts)
        if errors:
            list(log.error(e) for e in errors)
            raise RuntimeError("\n".join(errors))

        # Make the image. Output of this is either a partitioned disk image or a fsimage
        try:
            disk_img = make_image(opts, ks, cancel_func=cancel_func)
        except InstallError as e:
            log.error("ERROR: Image creation failed: %s", e)
            raise RuntimeError("Image creation failed: %s" % e)

    if opts.image_only:
        return (result_dir, disk_img)

    if opts.make_iso:
        work_dir = tempfile.mkdtemp(prefix="lmc-work-")
        log.info("working dir is %s", work_dir)

        if (opts.fs_image or opts.no_virt) and not opts.disk_image:
            # Create iso from a filesystem image
            disk_img = opts.fs_image or disk_img

            if not make_squashfs(opts, disk_img, work_dir):
                log.error("squashfs.img creation failed")
                raise RuntimeError("squashfs.img creation failed")

            if cancel_func and cancel_func():
                raise RuntimeError("ISO creation canceled")

            with Mount(disk_img, opts="loop") as mount_dir:
                result_dir = make_livecd(opts, mount_dir, work_dir)
        else:
            # Create iso from a partitioned disk image
            disk_img = opts.disk_image or disk_img
            with PartitionMount(disk_img) as img_mount:
                if img_mount and img_mount.mount_dir:
                    make_runtime(opts, img_mount.mount_dir, work_dir,
                                 calculate_disk_size(opts, ks) / 1024.0)
                    result_dir = make_livecd(opts, img_mount.mount_dir,
                                             work_dir)

        # --iso-only removes the extra build artifacts, keeping only the boot.iso
        if opts.iso_only and result_dir:
            boot_iso = joinpaths(result_dir, "images/boot.iso")
            if not os.path.exists(boot_iso):
                log.error("%s is missing, skipping --iso-only.", boot_iso)
            else:
                iso_dir = tempfile.mkdtemp(prefix="lmc-result-")
                dest_file = joinpaths(iso_dir, opts.iso_name or "boot.iso")
                shutil.move(boot_iso, dest_file)
                shutil.rmtree(result_dir)
                result_dir = iso_dir

        # cleanup the mess
        # cleanup work_dir?
        if disk_img and not (opts.keep_image or opts.disk_image
                             or opts.fs_image):
            os.unlink(disk_img)
            log.info("Disk image erased")
            disk_img = None
    elif opts.make_appliance:
        if not opts.ks:
            networks = []
        else:
            networks = ks.handler.network.network
        make_appliance(opts.disk_image or disk_img, opts.app_name,
                       opts.app_template, opts.app_file, networks, opts.ram,
                       opts.vcpus or 1, opts.arch, opts.title, opts.project,
                       opts.releasever)
    elif opts.make_pxe_live:
        work_dir = tempfile.mkdtemp(prefix="lmc-work-")
        log.info("working dir is %s", work_dir)
        disk_img = opts.fs_image or opts.disk_image or disk_img
        log.debug("disk image is %s", disk_img)

        result_dir = make_live_images(opts, work_dir, disk_img)
        if result_dir is None:
            log.error("Creating PXE live image failed.")
            raise RuntimeError("Creating PXE live image failed.")

    if opts.result_dir != opts.tmp and result_dir:
        copytree(result_dir, opts.result_dir, preserve=False)
        shutil.rmtree(result_dir)
        result_dir = None

    return (result_dir, disk_img)
Esempio n. 54
0
                help=_("list the available versions of kickstart syntax"))

opts = op.parse_args(sys.argv[1:])

if opts.listversions:
    for key in sorted(versionMap.keys()):
        print(key)

    sys.exit(1)

if not opts.f or not opts.t:
    print(_("You must specify two syntax versions."))
    sys.exit(1)

try:
    fromHandler = makeVersion(opts.f)
    toHandler = makeVersion(opts.t)
except KickstartVersionError as exn:
    print(_("The version %s is not supported by pykickstart") % exn)
    sys.exit(1)

fromCmdSet = getCommandSet(fromHandler)
toCmdSet = getCommandSet(toHandler)
bothSet = fromCmdSet & toCmdSet

print(_("The following commands were removed in %s:") % opts.t)
printList(sorted(fromCmdSet - toCmdSet))

print(_("The following commands were deprecated in %s:") % opts.t)
printList(sorted([cmd for cmd in bothSet if isinstance(toHandler.commands[cmd], DeprecatedCommand)]))
Esempio n. 55
0
def main(argv):
    op = argparse.ArgumentParser(usage="%(prog)s [options] ksfile", add_help=False)
    op.add_argument("ksfile", nargs="?",
                    help=_("filename or URL to read from"))
    op.add_argument("-e", "--firsterror", dest="firsterror", action="store_true",
                    default=False, help=_("halt after the first error or warning"))
    op.add_argument("-i", "--followincludes", dest="followincludes",
                    action="store_true", default=False,
                    help=_("parse include files when %%include is seen"))
    op.add_argument("-l", "--listversions", dest="listversions", action="store_true",
                    default=False,
                    help=_("list the available versions of kickstart syntax"))
    op.add_argument("-v", "--version", dest="version", default=DEVEL,
                    help=_("version of kickstart syntax to validate against"))
    op.add_argument("-h", "--help", dest="help", action="store_true", default=False,
                    help=_("show this help message and exit"))

    opts = op.parse_args(argv)

    # parse --help manually b/c we don't want to sys.exit before the
    # tests have finished
    if opts.help:
        return (0, op.format_help().split("\n"))

    if opts.listversions:
        versions = []
        for key in sorted(versionMap.keys()):
            versions.append(key)
        return (0, versions)

    if not opts.ksfile:
        return (1, op.format_usage().split("\n"))

    destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
    try:
        f = load_to_file(opts.ksfile, "%s/ks.cfg" % destdir)
    except KickstartError as e:
        return (cleanup(destdir),
                [_("Error reading %(filename)s:\n%(version)s") % {"filename": opts.ksfile, "version": e}])

    try:
        handler = makeVersion(opts.version)
    except KickstartVersionError:
        return (cleanup(destdir),
                [_("The version %s is not supported by pykickstart") % opts.version])

    ksparser = KickstartParser(handler, followIncludes=opts.followincludes,
                               errorsAreFatal=opts.firsterror)

    # turn kickstart parse warnings into errors
    warnings.filterwarnings(action="error", category=KickstartParseWarning)

    processedFile = None

    try:
        processedFile = preprocessKickstart(f)
        ksparser.readKickstart(processedFile)
        return (cleanup(destdir, processedFile, exitval=ksparser.errorsCount), [])
    except KickstartDeprecationWarning as err:
        return (cleanup(destdir, processedFile),
                [_("File uses a deprecated option or command.\n%s") % err])
    except KickstartParseError as err:
        return (cleanup(destdir, processedFile), [str(err)])
    except KickstartError:
        return (cleanup(destdir, processedFile),
                [_("General kickstart error in input file")])
    except Exception as e:
        return (cleanup(destdir, processedFile),
                [_("General error in input file:  %s") % e])
Esempio n. 56
0
from pykickstart.parser import *
from pykickstart.version import makeVersion

ksparser = KickstartParser(makeVersion())
ksparser.readKickstart("ks.cfg")
Esempio n. 57
0
    def to_kickstart(self):
        """
        Represent the template as a kickstart file.

        Args:
          None

        Returns:
          Kickstart formatted file.
        """
        ksversion = makeVersion(DEVEL)
        ksparser = pykickstart.parser.KickstartParser(ksversion)

        handler = ksparser.handler
        packages = handler.packages

        if 'kickstart' in self._meta:
            # populate general

            # populate commands
            if 'commands' in self._meta['kickstart']:
                for c in self._meta['kickstart']['commands']:
                    ksparser.readKickstartFromString(c['data'], reset=False)

            # populate scripts
            if 'scripts' in self._meta['kickstart']:
                for s in self._meta['kickstart']['scripts']:
                    handler.scripts.append(
                        pykickstart.parser.Script(s['data'], interp=s['interp'], inChroot=s['in_chroot'], type=s['type'],
                            lineno=s['line_no'], errorOnFail=s['error_on_fail'])
                    )

            # populate general package parameters
            if 'packages' in self._meta['kickstart']:
                mp = self._meta['kickstart']['packages']

                if 'default' in mp:
                    packages.default = mp['default']

                if 'exclude_docs' in mp:
                    packages.excludeDocs = mp['exclude_docs']

                if 'no_base' in mp:
                    packages.addBase = not mp['no_base']

                if 'no_core' in mp:
                    packages.nocore = mp['no_core']

        #       if 'handle_missing' in mp:
        #           packages.handleMissing = pykickstart.constants.KS_MISSING_IGNORE

                if 'install_langs' in mp:
                    packages.instLangs = mp['install_langs']

                if 'multi_lib' in mp:
                    packages.multiLib = mp['multi_lib']

        # populate repos (technically commands)
        for r in self.repos:
            ksparser.readKickstartFromString(r.to_kickstart(), reset=False)

        # process packages
        for p in self.packages:
            if p.included():
                packages.packageList.append(p.name)

            else:
                packages.excludedList.append(p.name)

        template = ('# Canvas generated template - {1}\n'
                    '# UUID: {0}\n'
                    '# Author: {2}\n'
                    '# Title: {3}\n'
                    '# Description:\n'
                    "# {4}\n\n").format(
                        self._uuid, self._name, self._user, self._title, self._description
                    )

        template += ksparser.handler.__str__()

        return template
Esempio n. 58
0
def main(argv):
    op = argparse.ArgumentParser(usage="%(prog)s [options] ksfile", add_help=False)
    op.add_argument("ksfile", nargs="?",
                    help=_("filename or URL to read from"))
    op.add_argument("-e", "--firsterror", dest="firsterror", action="store_true",
                    default=False, help=_("halt after the first error or warning"))
    op.add_argument("-i", "--followincludes", dest="followincludes",
                    action="store_true", default=False,
                    help=_("parse include files when %%include is seen"))
    op.add_argument("-l", "--listversions", dest="listversions", action="store_true",
                    default=False,
                    help=_("list the available versions of kickstart syntax"))
    op.add_argument("-v", "--version", dest="version", default=DEVEL,
                    help=_("version of kickstart syntax to validate against"))
    op.add_argument("-h", "--help", dest="help", action="store_true", default=False,
                    help=_("show this help message and exit"))

    opts = op.parse_args(argv)

    # parse --help manually b/c we don't want to sys.exit before the
    # tests have finished
    if opts.help:
        return (0, op.format_help().split("\n"))

    if opts.listversions:
        versions = []
        for key in sorted(versionMap.keys()):
            versions.append(key)
        return (0, versions)

    if not opts.ksfile:
        return (1, op.format_usage().split("\n"))

    destdir = tempfile.mkdtemp("", "ksvalidator-tmp-", "/tmp")
    try:
        f = load_to_file(opts.ksfile, "%s/ks.cfg" % destdir)
    except KickstartError as e:
        return (cleanup(destdir),
                [_("Error reading %(filename)s:\n%(version)s") % {"filename": opts.ksfile, "version": e}])

    try:
        handler = makeVersion(opts.version)
    except KickstartVersionError:
        return (cleanup(destdir),
                [_("The version %s is not supported by pykickstart") % opts.version])

    ksparser = KickstartParser(handler, followIncludes=opts.followincludes,
                               errorsAreFatal=opts.firsterror)

    # turn DeprecationWarnings into errors
    warnings.filterwarnings("error")

    processedFile = None

    try:
        processedFile = preprocessKickstart(f)
        ksparser.readKickstart(processedFile)
        return (cleanup(destdir, processedFile, exitval=ksparser.errorsCount), [])
    except DeprecationWarning as err:
        return (cleanup(destdir, processedFile),
                [_("File uses a deprecated option or command.\n%s") % err])
    except KickstartParseError as err:
        return (cleanup(destdir, processedFile), [str(err)])
    except KickstartError:
        return (cleanup(destdir, processedFile),
                [_("General kickstart error in input file")])
    except Exception as e:
        return (cleanup(destdir, processedFile),
                [_("General error in input file:  %s") % e])
Esempio n. 59
0
    def on_activate_open(self, *args):
        fs = gtk.FileChooserDialog(action=gtk.FILE_CHOOSER_ACTION_OPEN,
                                   buttons=(gtk.STOCK_CANCEL,
                                            gtk.RESPONSE_CANCEL,
                                            gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        fs.set_default_size(-1, -1)
        fs.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
        fs.set_transient_for(self.toplevel)
        result = fs.run()
        file = fs.get_filename()

        if result == gtk.RESPONSE_OK:
            if os.access(file, os.R_OK) == 1:
                self.ksHandler = makeVersion()
                self.parser = KickstartParser(self.ksHandler)

                msg = None

                try:
                    self.parser.readKickstart(file)
                except (KickstartParseError, KickstartValueError) as e:
                    msg = _("The following error was found while parsing your "
                            "kickstart configuration:\n\n%s" % e)
                except KickstartError:
                    msg = _(
                        "The kickstart file %s could not be opened.") % file

                if msg:
                    dlg = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR,
                                            gtk.BUTTONS_OK, msg)
                    dlg.set_title(_("Error Parsing Kickstart Config"))
                    dlg.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
                    dlg.set_modal(True)
                    dlg.run()
                    dlg.destroy()
                    fs.destroy()
                    return

                # Refresh ksdata pointers in every subclass for the new
                # data we loaded in from the file.
                for cl in [
                        self.basic_class, self.bootloader_class,
                        self.install_class, self.partition_class,
                        self.network_class, self.auth_class, self.X_class,
                        self.firewall_class, self.packages_class,
                        self.scripts_class
                ]:
                    cl.updateKS(self.ksHandler)

                self.applyKickstart()
                self.toplevel.show()
            else:
                dlg = gtk.MessageDialog(
                    None, 0, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK,
                    (_("The file \"%s\" cannot be accessed.")) % file)
                dlg.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
                dlg.set_icon(iconPixbuf)
                dlg.run()
                dlg.destroy()

        fs.destroy()