]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: black format packagers.py
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 17 Aug 2023 19:12:15 +0000 (15:12 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Wed, 30 Aug 2023 18:02:15 +0000 (14:02 -0400)
Signed-off-by: John Mulligan <jmulligan@redhat.com>
Pair-programmed-with: Adam King <adking@redhat.com>
Co-authored-by: Adam King <adking@redhat.com>
src/cephadm/cephadmlib/packagers.py

index 97dd8801a2338ecd8a58f0d330c699de27128ccc..9187b718411414cf93c9f54b6bfe106d5ae36899 100644 (file)
@@ -39,14 +39,20 @@ def get_distro():
 
 
 class Packager(object):
-    def __init__(self, ctx: CephadmContext,
-                 stable: Optional[str] = None, version: Optional[str] = None,
-                 branch: Optional[str] = None, commit: Optional[str] = None):
-        assert \
-            (stable and not version and not branch and not commit) or \
-            (not stable and version and not branch and not commit) or \
-            (not stable and not version and branch) or \
-            (not stable and not version and not branch and not commit)
+    def __init__(
+        self,
+        ctx: CephadmContext,
+        stable: Optional[str] = None,
+        version: Optional[str] = None,
+        branch: Optional[str] = None,
+        commit: Optional[str] = None,
+    ):
+        assert (
+            (stable and not version and not branch and not commit)
+            or (not stable and version and not branch and not commit)
+            or (not stable and not version and branch)
+            or (not stable and not version and not branch and not commit)
+        )
         self.ctx = ctx
         self.stable = stable
         self.version = version
@@ -69,7 +75,13 @@ class Packager(object):
     def install_podman(self) -> None:
         raise NotImplementedError
 
-    def query_shaman(self, distro: str, distro_version: Any, branch: Optional[str], commit: Optional[str]) -> str:
+    def query_shaman(
+        self,
+        distro: str,
+        distro_version: Any,
+        branch: Optional[str],
+        commit: Optional[str],
+    ) -> str:
         # query shaman
         arch = platform.uname().machine
         logger.info('Fetching repo metadata from shaman and chacra...')
@@ -83,14 +95,18 @@ class Packager(object):
         try:
             shaman_response = urlopen(shaman_url)
         except HTTPError as err:
-            logger.error('repository not found in shaman (might not be available yet)')
+            logger.error(
+                'repository not found in shaman (might not be available yet)'
+            )
             raise Error('%s, failed to fetch %s' % (err, shaman_url))
         chacra_url = ''
         try:
             chacra_url = shaman_response.geturl()
             chacra_response = urlopen(chacra_url)
         except HTTPError as err:
-            logger.error('repository not found in chacra (might not be available yet)')
+            logger.error(
+                'repository not found in chacra (might not be available yet)'
+            )
             raise Error('%s, failed to fetch %s' % (err, chacra_url))
         return chacra_response.read().decode('utf-8')
 
@@ -115,11 +131,20 @@ class Apt(Packager):
         'debian': 'debian',
     }
 
-    def __init__(self, ctx: CephadmContext,
-                 stable: Optional[str], version: Optional[str], branch: Optional[str], commit: Optional[str],
-                 distro: Optional[str], distro_version: Optional[str], distro_codename: Optional[str]) -> None:
-        super(Apt, self).__init__(ctx, stable=stable, version=version,
-                                  branch=branch, commit=commit)
+    def __init__(
+        self,
+        ctx: CephadmContext,
+        stable: Optional[str],
+        version: Optional[str],
+        branch: Optional[str],
+        commit: Optional[str],
+        distro: Optional[str],
+        distro_version: Optional[str],
+        distro_codename: Optional[str],
+    ) -> None:
+        super(Apt, self).__init__(
+            ctx, stable=stable, version=version, branch=branch, commit=commit
+        )
         assert distro
         self.ctx = ctx
         self.distro = self.DISTRO_NAMES[distro]
@@ -130,14 +155,14 @@ class Apt(Packager):
         return '/etc/apt/sources.list.d/ceph.list'
 
     def add_repo(self) -> None:
-
         url, name = self.repo_gpgkey()
         logger.info('Installing repo GPG key from %s...' % url)
         try:
             response = urlopen(url)
         except HTTPError as err:
-            logger.error('failed to fetch GPG repo key from %s: %s' % (
-                url, err))
+            logger.error(
+                'failed to fetch GPG repo key from %s: %s' % (url, err)
+            )
             raise Error('failed to fetch GPG key')
         key = response.read()
         with open('/etc/apt/trusted.gpg.d/ceph.%s.gpg' % name, 'wb') as f:
@@ -145,13 +170,20 @@ class Apt(Packager):
 
         if self.version:
             content = 'deb %s/debian-%s/ %s main\n' % (
-                self.ctx.repo_url, self.version, self.distro_codename)
+                self.ctx.repo_url,
+                self.version,
+                self.distro_codename,
+            )
         elif self.stable:
             content = 'deb %s/debian-%s/ %s main\n' % (
-                self.ctx.repo_url, self.stable, self.distro_codename)
+                self.ctx.repo_url,
+                self.stable,
+                self.distro_codename,
+            )
         else:
-            content = self.query_shaman(self.distro, self.distro_codename, self.branch,
-                                        self.commit)
+            content = self.query_shaman(
+                self.distro, self.distro_codename, self.branch, self.commit
+            )
 
         logger.info('Installing repo file at %s...' % self.repo_path())
         with open(self.repo_path(), 'w') as f:
@@ -194,8 +226,10 @@ class Apt(Packager):
             self.install(['docker.io'])
 
     def kubic_repo_url(self) -> str:
-        return 'https://download.opensuse.org/repositories/devel:/kubic:/' \
-               'libcontainers:/stable/xUbuntu_%s/' % self.distro_version
+        return (
+            'https://download.opensuse.org/repositories/devel:/kubic:/'
+            'libcontainers:/stable/xUbuntu_%s/' % self.distro_version
+        )
 
     def kubic_repo_path(self) -> str:
         return '/etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list'
@@ -212,13 +246,16 @@ class Apt(Packager):
         try:
             response = urlopen(url)
         except HTTPError as err:
-            logger.error('failed to fetch GPG repo key from %s: %s' % (
-                url, err))
+            logger.error(
+                'failed to fetch GPG repo key from %s: %s' % (url, err)
+            )
             raise Error('failed to fetch GPG key')
         key = response.read().decode('utf-8')
         tmp_key = write_tmp(key, 0, 0)
         keyring = self.kubic_repo_gpgkey_path()
-        call_throws(self.ctx, ['apt-key', '--keyring', keyring, 'add', tmp_key.name])
+        call_throws(
+            self.ctx, ['apt-key', '--keyring', keyring, 'add', tmp_key.name]
+        )
 
         logger.info('Installing repo file at %s...' % self.kubic_repo_path())
         content = 'deb %s /\n' % self.kubic_repo_url()
@@ -249,21 +286,30 @@ class YumDnf(Packager):
         'mariner': ('mariner', 'cm'),
     }
 
-    def __init__(self, ctx: CephadmContext,
-                 stable: Optional[str], version: Optional[str], branch: Optional[str], commit: Optional[str],
-                 distro: Optional[str], distro_version: Optional[str]) -> None:
-        super(YumDnf, self).__init__(ctx, stable=stable, version=version,
-                                     branch=branch, commit=commit)
+    def __init__(
+        self,
+        ctx: CephadmContext,
+        stable: Optional[str],
+        version: Optional[str],
+        branch: Optional[str],
+        commit: Optional[str],
+        distro: Optional[str],
+        distro_version: Optional[str],
+    ) -> None:
+        super(YumDnf, self).__init__(
+            ctx, stable=stable, version=version, branch=branch, commit=commit
+        )
         assert distro
         assert distro_version
         self.ctx = ctx
         self.major = int(distro_version.split('.')[0])
         self.distro_normalized = self.DISTRO_NAMES[distro][0]
         self.distro_code = self.DISTRO_NAMES[distro][1] + str(self.major)
-        if (self.distro_code == 'fc' and self.major >= 30) or \
-           (self.distro_code == 'el' and self.major >= 8):
+        if (self.distro_code == 'fc' and self.major >= 30) or (
+            self.distro_code == 'el' and self.major >= 8
+        ):
             self.tool = 'dnf'
-        elif (self.distro_code == 'cm'):
+        elif self.distro_code == 'cm':
             self.tool = 'tdnf'
         else:
             self.tool = 'yum'
@@ -330,20 +376,32 @@ class YumDnf(Packager):
     def repo_baseurl(self) -> str:
         assert self.stable or self.version
         if self.version:
-            return '%s/rpm-%s/%s' % (self.ctx.repo_url, self.version,
-                                     self.distro_code)
+            return '%s/rpm-%s/%s' % (
+                self.ctx.repo_url,
+                self.version,
+                self.distro_code,
+            )
         else:
-            return '%s/rpm-%s/%s' % (self.ctx.repo_url, self.stable,
-                                     self.distro_code)
+            return '%s/rpm-%s/%s' % (
+                self.ctx.repo_url,
+                self.stable,
+                self.distro_code,
+            )
 
     def validate(self) -> None:
         if self.distro_code.startswith('fc'):
-            raise Error('Ceph team does not build Fedora specific packages and therefore cannot add repos for this distro')
+            raise Error(
+                'Ceph team does not build Fedora specific packages and therefore cannot add repos for this distro'
+            )
         if self.distro_code == 'el7':
             if self.stable and self.stable >= 'pacific':
-                raise Error('Ceph does not support pacific or later for this version of this linux distro and therefore cannot add a repo for it')
+                raise Error(
+                    'Ceph does not support pacific or later for this version of this linux distro and therefore cannot add a repo for it'
+                )
             if self.version and self.version.split('.')[0] >= '16':
-                raise Error('Ceph does not support 16.y.z or later for this version of this linux distro and therefore cannot add a repo for it')
+                raise Error(
+                    'Ceph does not support 16.y.z or later for this version of this linux distro and therefore cannot add a repo for it'
+                )
 
         if self.stable or self.version:
             # we know that yum & dnf require there to be a
@@ -354,16 +412,19 @@ class YumDnf(Packager):
                 urlopen(test_url)
             except HTTPError as err:
                 logger.error('unable to fetch repo metadata: %r', err)
-                raise Error('failed to fetch repository metadata. please check'
-                            ' the provided parameters are correct and try again')
+                raise Error(
+                    'failed to fetch repository metadata. please check'
+                    ' the provided parameters are correct and try again'
+                )
 
     def add_repo(self) -> None:
         if self.stable or self.version:
             content = ''
             for n, t in {
-                    'Ceph': '$basearch',
-                    'Ceph-noarch': 'noarch',
-                    'Ceph-source': 'SRPMS'}.items():
+                'Ceph': '$basearch',
+                'Ceph-noarch': 'noarch',
+                'Ceph-source': 'SRPMS',
+            }.items():
                 content += '[%s]\n' % (n)
                 content += self.custom_repo(
                     name='Ceph %s' % t,
@@ -374,9 +435,9 @@ class YumDnf(Packager):
                 )
                 content += '\n\n'
         else:
-            content = self.query_shaman(self.distro_normalized, self.major,
-                                        self.branch,
-                                        self.commit)
+            content = self.query_shaman(
+                self.distro_normalized, self.major, self.branch, self.commit
+            )
 
         logger.info('Writing repo to %s...' % self.repo_path())
         with open(self.repo_path(), 'w') as f:
@@ -384,7 +445,9 @@ class YumDnf(Packager):
 
         if self.distro_code.startswith('el'):
             logger.info('Enabling EPEL...')
-            call_throws(self.ctx, [self.tool, 'install', '-y', 'epel-release'])
+            call_throws(
+                self.ctx, [self.tool, 'install', '-y', 'epel-release']
+            )
 
     def rm_repo(self) -> None:
         if os.path.exists(self.repo_path()):
@@ -399,17 +462,21 @@ class YumDnf(Packager):
 
 
 class Zypper(Packager):
-    DISTRO_NAMES = [
-        'sles',
-        'opensuse-tumbleweed',
-        'opensuse-leap'
-    ]
-
-    def __init__(self, ctx: CephadmContext,
-                 stable: Optional[str], version: Optional[str], branch: Optional[str], commit: Optional[str],
-                 distro: Optional[str], distro_version: Optional[str]) -> None:
-        super(Zypper, self).__init__(ctx, stable=stable, version=version,
-                                     branch=branch, commit=commit)
+    DISTRO_NAMES = ['sles', 'opensuse-tumbleweed', 'opensuse-leap']
+
+    def __init__(
+        self,
+        ctx: CephadmContext,
+        stable: Optional[str],
+        version: Optional[str],
+        branch: Optional[str],
+        commit: Optional[str],
+        distro: Optional[str],
+        distro_version: Optional[str],
+    ) -> None:
+        super(Zypper, self).__init__(
+            ctx, stable=stable, version=version, branch=branch, commit=commit
+        )
         assert distro is not None
         self.ctx = ctx
         self.tool = 'zypper'
@@ -453,19 +520,26 @@ class Zypper(Packager):
     def repo_baseurl(self) -> str:
         assert self.stable or self.version
         if self.version:
-            return '%s/rpm-%s/%s' % (self.ctx.repo_url,
-                                     self.stable, self.distro)
+            return '%s/rpm-%s/%s' % (
+                self.ctx.repo_url,
+                self.stable,
+                self.distro,
+            )
         else:
-            return '%s/rpm-%s/%s' % (self.ctx.repo_url,
-                                     self.stable, self.distro)
+            return '%s/rpm-%s/%s' % (
+                self.ctx.repo_url,
+                self.stable,
+                self.distro,
+            )
 
     def add_repo(self) -> None:
         if self.stable or self.version:
             content = ''
             for n, t in {
-                    'Ceph': '$basearch',
-                    'Ceph-noarch': 'noarch',
-                    'Ceph-source': 'SRPMS'}.items():
+                'Ceph': '$basearch',
+                'Ceph-noarch': 'noarch',
+                'Ceph-source': 'SRPMS',
+            }.items():
                 content += '[%s]\n' % (n)
                 content += self.custom_repo(
                     name='Ceph %s' % t,
@@ -476,9 +550,9 @@ class Zypper(Packager):
                 )
                 content += '\n\n'
         else:
-            content = self.query_shaman(self.distro, self.distro_version,
-                                        self.branch,
-                                        self.commit)
+            content = self.query_shaman(
+                self.distro, self.distro_version, self.branch, self.commit
+            )
 
         logger.info('Writing repo to %s...' % self.repo_path())
         with open(self.repo_path(), 'w') as f:
@@ -496,21 +570,45 @@ class Zypper(Packager):
         self.install(['podman'])
 
 
-def create_packager(ctx: CephadmContext,
-                    stable: Optional[str] = None, version: Optional[str] = None,
-                    branch: Optional[str] = None, commit: Optional[str] = None) -> Packager:
+def create_packager(
+    ctx: CephadmContext,
+    stable: Optional[str] = None,
+    version: Optional[str] = None,
+    branch: Optional[str] = None,
+    commit: Optional[str] = None,
+) -> Packager:
     distro, distro_version, distro_codename = get_distro()
     if distro in YumDnf.DISTRO_NAMES:
-        return YumDnf(ctx, stable=stable, version=version,
-                      branch=branch, commit=commit,
-                      distro=distro, distro_version=distro_version)
+        return YumDnf(
+            ctx,
+            stable=stable,
+            version=version,
+            branch=branch,
+            commit=commit,
+            distro=distro,
+            distro_version=distro_version,
+        )
     elif distro in Apt.DISTRO_NAMES:
-        return Apt(ctx, stable=stable, version=version,
-                   branch=branch, commit=commit,
-                   distro=distro, distro_version=distro_version,
-                   distro_codename=distro_codename)
+        return Apt(
+            ctx,
+            stable=stable,
+            version=version,
+            branch=branch,
+            commit=commit,
+            distro=distro,
+            distro_version=distro_version,
+            distro_codename=distro_codename,
+        )
     elif distro in Zypper.DISTRO_NAMES:
-        return Zypper(ctx, stable=stable, version=version,
-                      branch=branch, commit=commit,
-                      distro=distro, distro_version=distro_version)
-    raise Error('Distro %s version %s not supported' % (distro, distro_version))
+        return Zypper(
+            ctx,
+            stable=stable,
+            version=version,
+            branch=branch,
+            commit=commit,
+            distro=distro,
+            distro_version=distro_version,
+        )
+    raise Error(
+        'Distro %s version %s not supported' % (distro, distro_version)
+    )