[anaconda 2/3] Install selected ks repos to target (#1119867)

Vratislav Podzimek vpodzime at redhat.com
Fri Jul 25 06:24:43 UTC 2014


On Thu, 2014-07-24 at 18:34 -0700, Brian C. Lane wrote:
> This implements writing selected kickstart repos to the target system.
> Add --install to the repo commands in the kickstart and the repo will be
> written to /etc/yum.repos.d/<repoid>.repo
> ---
>  anaconda.spec.in                   |   2 +-
>  pyanaconda/kickstart.py            |   4 +-
>  pyanaconda/packaging/yumpayload.py | 135 ++++++++++++++++++++++---------------
>  3 files changed, 82 insertions(+), 59 deletions(-)
> 
> diff --git a/anaconda.spec.in b/anaconda.spec.in
> index 685886c..a24162b 100644
> --- a/anaconda.spec.in
> +++ b/anaconda.spec.in
> @@ -21,7 +21,7 @@ Source0: %{name}-%{version}.tar.bz2
>  # Also update in AM_GNU_GETTEXT_VERSION in configure.ac
>  %define gettextver 0.18.3
>  %define intltoolver 0.31.2-3
> -%define pykickstartver 1.99.57
> +%define pykickstartver 1.99.58
>  %define yumver 3.4.3-91
>  %define dnfver 0.4.18
>  %define partedver 1.8.1
> diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
> index 09f2a88..bbcc5a9 100644
> --- a/pyanaconda/kickstart.py
> +++ b/pyanaconda/kickstart.py
> @@ -1426,7 +1426,7 @@ class RaidData(commands.raid.F18_RaidData):
>                                       parents=request)
>              storage.createDevice(luksdev)
>  
> -class RepoData(commands.repo.F15_RepoData):
> +class RepoData(commands.repo.F21_RepoData):
>      def __init__(self, *args, **kwargs):
>          """ Add enabled kwarg
>  
> @@ -1435,7 +1435,7 @@ class RepoData(commands.repo.F15_RepoData):
>          """
>          self.enabled = kwargs.pop("enabled", True)
>  
> -        commands.repo.F15_RepoData.__init__(self, *args, **kwargs)
> +        commands.repo.F21_RepoData.__init__(self, *args, **kwargs)
>  
>  class RootPw(commands.rootpw.F18_RootPw):
>      def execute(self, storage, ksdata, instClass, users):
> diff --git a/pyanaconda/packaging/yumpayload.py b/pyanaconda/packaging/yumpayload.py
> index 6dca7f6..a36ef55 100644
> --- a/pyanaconda/packaging/yumpayload.py
> +++ b/pyanaconda/packaging/yumpayload.py
> @@ -58,6 +58,7 @@ try:
>      # handler setup.  We already set one up so we don't need it to run.
>      # yum may give us an API to fiddle this at a later time.
>      yum.logginglevels._added_handlers = True
> +    from yum.Errors import RepoError, RepoMDError, GroupsError
>  except ImportError:
>      log.error("import of yum failed")
>      yum = None
> @@ -299,6 +300,65 @@ reposdir=%s
>              root = self._yum.conf.installroot
>              self._yum.conf.cachedir = self._yum.conf.cachedir[len(root):]
>  
> +    def _writeYumRepo(self, repo, repo_path):
> +        """ Write a repo object to a yum repo.conf file
> +
> +            :param repo: Yum repository object
> +            :param string repo_path: Path to write the repo to
> +            :raises: PayloadSetupError if the repo doesn't have a url
> +        """
> +        with open(repo_path, "w") as f:
> +            f.write("[%s]\n" % repo.id)
> +            f.write("name=%s\n" % repo.id)
> +            if self.isRepoEnabled(repo.id):
> +                f.write("enabled=1\n")
> +            else:
> +                f.write("enabled=0\n")
> +
> +            if repo.mirrorlist:
> +                f.write("mirrorlist=%s\n" % repo.mirrorlist)
> +            elif repo.metalink:
> +                f.write("metalink=%s\n" % repo.metalink)
> +            elif repo.baseurl:
> +                f.write("baseurl=%s\n" % repo.baseurl[0])
> +            else:
> +                f.close()
> +                os.unlink(repo_path)
> +                raise PayloadSetupError("repo %s has no baseurl, mirrorlist or metalink", repo.id)
I'm not sure about that, but doesn't this path result in the file being
closed twice? Is it just silently ignored or does it cause an error?

> +
> +            # kickstart repo modifiers
> +            ks_repo = self.getAddOnRepo(repo.id)
> +            if ks_repo:
> +                if ks_repo.noverifyssl:
> +                    f.write("sslverify=0\n")
> +
> +                if ks_repo.proxy:
> +                    try:
> +                        proxy = ProxyString(ks_repo.proxy)
> +                        f.write("proxy=%s\n" % (proxy.noauth_url,))
> +                        if proxy.username:
> +                            f.write("proxy_username=%s\n" % (proxy.username,))
> +                        if proxy.password:
> +                            f.write("proxy_password=%s\n" % (proxy.password,))
> +                    except ProxyStringError as e:
> +                        log.error("Failed to parse proxy for _writeInstallConfig %s: %s",
> +                                  ks_repo.proxy, e)
> +
> +                if ks_repo.cost:
> +                    f.write("cost=%d\n" % ks_repo.cost)
> +
> +                if ks_repo.includepkgs:
> +                    f.write("includepkgs=%s\n"
> +                            % ",".join(ks_repo.includepkgs))
> +
> +                if ks_repo.excludepkgs:
> +                    f.write("exclude=%s\n"
> +                            % ",".join(ks_repo.excludepkgs))
> +
> +                if ks_repo.ignoregroups:
> +                    f.write("enablegroups=0\n")
This long block could be indented less if the condition is negated and
followed with return.

> +
> +
>      def _writeInstallConfig(self):
>          """ Write out the yum config that will be used to install packages.
>  
> @@ -315,53 +375,10 @@ reposdir=%s
>          for repo in self._yum.repos.listEnabled():
>              cfg_path = "%s/%s.repo" % (self._repos_dir, repo.id)
>              log.debug("writing repository file %s for repository %s", cfg_path, repo.id)
> -            ks_repo = self.getAddOnRepo(repo.id)
> -            with open(cfg_path, "w") as f:
> -                f.write("[%s]\n" % repo.id)
> -                f.write("name=Install - %s\n" % repo.id)
> -                f.write("enabled=1\n")
> -                if repo.mirrorlist:
> -                    f.write("mirrorlist=%s" % repo.mirrorlist)
> -                elif repo.metalink:
> -                    f.write("metalink=%s" % repo.metalink)
> -                elif repo.baseurl:
> -                    f.write("baseurl=%s\n" % repo.baseurl[0])
> -                else:
> -                    log.error("repo %s has no baseurl, mirrorlist or metalink", repo.id)
> -                    f.close()
> -                    os.unlink(cfg_path)
> -                    continue
> -
> -                # kickstart repo modifiers
> -                if ks_repo:
> -                    if ks_repo.noverifyssl:
> -                        f.write("sslverify=0\n")
> -
> -                    if ks_repo.proxy:
> -                        try:
> -                            proxy = ProxyString(ks_repo.proxy)
> -                            f.write("proxy=%s\n" % (proxy.noauth_url,))
> -                            if proxy.username:
> -                                f.write("proxy_username=%s\n" % (proxy.username,))
> -                            if proxy.password:
> -                                f.write("proxy_password=%s\n" % (proxy.password,))
> -                        except ProxyStringError as e:
> -                            log.error("Failed to parse proxy for _writeInstallConfig %s: %s",
> -                                      ks_repo.proxy, e)
> -
> -                    if ks_repo.cost:
> -                        f.write("cost=%d\n" % ks_repo.cost)
> -
> -                    if ks_repo.includepkgs:
> -                        f.write("includepkgs=%s\n"
> -                                % ",".join(ks_repo.includepkgs))
> -
> -                    if ks_repo.excludepkgs:
> -                        f.write("exclude=%s\n"
> -                                % ",".join(ks_repo.excludepkgs))
> -
> -                    if ks_repo.ignoregroups:
> -                        f.write("enablegroups=0\n")
> +            try:
> +                self._writeYumRepo(repo, cfg_path)
> +            except PayloadSetupError as e:
> +                log.error(e)
>  
>          releasever = self._yum.conf.yumvar['releasever']
>          self._writeYumConfig()
> @@ -455,8 +472,6 @@ reposdir=%s
>  
>      def isRepoEnabled(self, repo_id):
>          """ Return True if repo is enabled. """
> -        from yum.Errors import RepoError
> -
>          try:
>              return self.getRepo(repo_id).enabled
>          except RepoError:
> @@ -722,8 +737,6 @@ reposdir=%s
>  
>      def _getRepoMetadata(self, yumrepo):
>          """ Retrieve repo metadata if we don't already have it. """
> -        from yum.Errors import RepoError, RepoMDError
> -
>          # And try to grab its metadata.  We do this here so it can be done
>          # on a per-repo basis, so we can then get some finer grained error
>          # handling and recovery.
> @@ -765,8 +778,6 @@ reposdir=%s
>  
>      def _addYumRepo(self, name, baseurl, mirrorlist=None, proxyurl=None, **kwargs):
>          """ Add a yum repo to the YumBase instance. """
> -        from yum.Errors import RepoError
> -
>          needsAdding = True
>  
>          # First, delete any pre-existing repo with the same name.
> @@ -999,7 +1010,6 @@ reposdir=%s
>      @property
>      def _yumGroups(self):
>          """ yum.comps.Comps instance. """
> -        from yum.Errors import RepoError, GroupsError
>          with _yum_lock:
>              if not self._groups:
>                  if not self.needsNetwork or nm_is_connected():
> @@ -1109,8 +1119,6 @@ reposdir=%s
>      ###
>      @property
>      def packages(self):
> -        from yum.Errors import RepoError
> -
>          with _yum_lock:
>              if not self._packages:
>                  if self.needsNetwork and not nm_is_connected():
> @@ -1489,6 +1497,21 @@ reposdir=%s
>  
>          self.writeMultiLibConfig()
>  
> +        # Write selected kickstart repos to target system
> +        for ks_repo in (ks for ks in (self.getAddOnRepo(r) for r in self.addOns) if ks.install):
> +            try:
> +                repo = self.getRepo(ks_repo.name)
> +                if not repo:
> +                    continue
> +            except RepoError:
> +                continue
> +            repo_path = iutil.getSysroot() + "/etc/yum.repos.d/%s.repo" % repo.id
The "/etc/yum.repos.d" path should be a constant defined at the top of
the module. Or maybe the whole template.

-- 
Vratislav Podzimek

Anaconda Rider | Red Hat, Inc. | Brno - Czech Republic



More information about the anaconda-patches mailing list