Przeglądaj źródła

Extend repoquery command (of lib_utils role) to ignore excluders

Jan Chaloupka 8 lat temu
rodzic
commit
13b58846f6

+ 28 - 2
roles/lib_utils/library/repoquery.py

@@ -34,6 +34,7 @@ import json  # noqa: F401
 import os  # noqa: F401
 import re  # noqa: F401
 import shutil  # noqa: F401
+import tempfile  # noqa: F401
 
 try:
     import ruamel.yaml as yaml  # noqa: F401
@@ -421,15 +422,16 @@ class RepoqueryCLI(object):
 class Repoquery(RepoqueryCLI):
     ''' Class to wrap the repoquery
     '''
-    # pylint: disable=too-many-arguments
+    # pylint: disable=too-many-arguments,too-many-instance-attributes
     def __init__(self, name, query_type, show_duplicates,
-                 match_version, verbose):
+                 match_version, ignore_excluders, verbose):
         ''' Constructor for YumList '''
         super(Repoquery, self).__init__(None)
         self.name = name
         self.query_type = query_type
         self.show_duplicates = show_duplicates
         self.match_version = match_version
+        self.ignore_excluders = ignore_excluders
         self.verbose = verbose
 
         if self.match_version:
@@ -437,6 +439,8 @@ class Repoquery(RepoqueryCLI):
 
         self.query_format = "%{version}|%{release}|%{arch}|%{repo}|%{version}-%{release}"
 
+        self.tmp_file = None
+
     def build_cmd(self):
         ''' build the repoquery cmd options '''
 
@@ -448,6 +452,9 @@ class Repoquery(RepoqueryCLI):
         if self.show_duplicates:
             repo_cmd.append('--show-duplicates')
 
+        if self.ignore_excluders:
+            repo_cmd.append('--config=' + self.tmp_file.name)
+
         repo_cmd.append(self.name)
 
         return repo_cmd
@@ -519,6 +526,20 @@ class Repoquery(RepoqueryCLI):
     def repoquery(self):
         '''perform a repoquery '''
 
+        if self.ignore_excluders:
+            # Duplicate yum.conf and reset exclude= line to an empty string
+            # to clear a list of all excluded packages
+            self.tmp_file = tempfile.NamedTemporaryFile()
+
+            with open("/etc/yum.conf", "r") as file_handler:
+                yum_conf_lines = file_handler.readlines()
+
+            yum_conf_lines = ["exclude=" if l.startswith("exclude=") else l for l in yum_conf_lines]
+
+            with open(self.tmp_file.name, "w") as file_handler:
+                file_handler.writelines(yum_conf_lines)
+                file_handler.flush()
+
         repoquery_cmd = self.build_cmd()
 
         rval = self._repoquery_cmd(repoquery_cmd, True, 'raw')
@@ -541,6 +562,9 @@ class Repoquery(RepoqueryCLI):
         else:
             rval['package_found'] = False
 
+        if self.ignore_excluders:
+            self.tmp_file.close()
+
         return rval
 
     @staticmethod
@@ -552,6 +576,7 @@ class Repoquery(RepoqueryCLI):
             params['query_type'],
             params['show_duplicates'],
             params['match_version'],
+            params['ignore_excluders'],
             params['verbose'],
         )
 
@@ -592,6 +617,7 @@ def main():
             verbose=dict(default=False, required=False, type='bool'),
             show_duplicates=dict(default=False, required=False, type='bool'),
             match_version=dict(default=None, required=False, type='str'),
+            ignore_excluders=dict(default=False, required=False, type='bool'),
         ),
         supports_check_mode=False,
         required_if=[('show_duplicates', True, ['name'])],

+ 1 - 0
roles/lib_utils/library/yedit.py

@@ -34,6 +34,7 @@ import json  # noqa: F401
 import os  # noqa: F401
 import re  # noqa: F401
 import shutil  # noqa: F401
+import tempfile  # noqa: F401
 
 try:
     import ruamel.yaml as yaml  # noqa: F401

+ 1 - 0
roles/lib_utils/src/ansible/repoquery.py

@@ -18,6 +18,7 @@ def main():
             verbose=dict(default=False, required=False, type='bool'),
             show_duplicates=dict(default=False, required=False, type='bool'),
             match_version=dict(default=None, required=False, type='str'),
+            ignore_excluders=dict(default=False, required=False, type='bool'),
         ),
         supports_check_mode=False,
         required_if=[('show_duplicates', True, ['name'])],

+ 26 - 2
roles/lib_utils/src/class/repoquery.py

@@ -5,15 +5,16 @@
 class Repoquery(RepoqueryCLI):
     ''' Class to wrap the repoquery
     '''
-    # pylint: disable=too-many-arguments
+    # pylint: disable=too-many-arguments,too-many-instance-attributes
     def __init__(self, name, query_type, show_duplicates,
-                 match_version, verbose):
+                 match_version, ignore_excluders, verbose):
         ''' Constructor for YumList '''
         super(Repoquery, self).__init__(None)
         self.name = name
         self.query_type = query_type
         self.show_duplicates = show_duplicates
         self.match_version = match_version
+        self.ignore_excluders = ignore_excluders
         self.verbose = verbose
 
         if self.match_version:
@@ -21,6 +22,8 @@ class Repoquery(RepoqueryCLI):
 
         self.query_format = "%{version}|%{release}|%{arch}|%{repo}|%{version}-%{release}"
 
+        self.tmp_file = None
+
     def build_cmd(self):
         ''' build the repoquery cmd options '''
 
@@ -32,6 +35,9 @@ class Repoquery(RepoqueryCLI):
         if self.show_duplicates:
             repo_cmd.append('--show-duplicates')
 
+        if self.ignore_excluders:
+            repo_cmd.append('--config=' + self.tmp_file.name)
+
         repo_cmd.append(self.name)
 
         return repo_cmd
@@ -103,6 +109,20 @@ class Repoquery(RepoqueryCLI):
     def repoquery(self):
         '''perform a repoquery '''
 
+        if self.ignore_excluders:
+            # Duplicate yum.conf and reset exclude= line to an empty string
+            # to clear a list of all excluded packages
+            self.tmp_file = tempfile.NamedTemporaryFile()
+
+            with open("/etc/yum.conf", "r") as file_handler:
+                yum_conf_lines = file_handler.readlines()
+
+            yum_conf_lines = ["exclude=" if l.startswith("exclude=") else l for l in yum_conf_lines]
+
+            with open(self.tmp_file.name, "w") as file_handler:
+                file_handler.writelines(yum_conf_lines)
+                file_handler.flush()
+
         repoquery_cmd = self.build_cmd()
 
         rval = self._repoquery_cmd(repoquery_cmd, True, 'raw')
@@ -125,6 +145,9 @@ class Repoquery(RepoqueryCLI):
         else:
             rval['package_found'] = False
 
+        if self.ignore_excluders:
+            self.tmp_file.close()
+
         return rval
 
     @staticmethod
@@ -136,6 +159,7 @@ class Repoquery(RepoqueryCLI):
             params['query_type'],
             params['show_duplicates'],
             params['match_version'],
+            params['ignore_excluders'],
             params['verbose'],
         )
 

+ 1 - 0
roles/lib_utils/src/lib/import.py

@@ -9,6 +9,7 @@ import json  # noqa: F401
 import os  # noqa: F401
 import re  # noqa: F401
 import shutil  # noqa: F401
+import tempfile  # noqa: F401
 
 try:
     import ruamel.yaml as yaml  # noqa: F401

+ 1 - 0
roles/lib_utils/src/test/unit/test_repoquery.py

@@ -37,6 +37,7 @@ class RepoQueryTest(unittest.TestCase):
             'verbose': False,
             'show_duplicates': False,
             'match_version': None,
+            'ignore_excluders': False,
         }
 
         valid_stderr = '''Repo rhel-7-server-extras-rpms forced skip_if_unavailable=True due to: /etc/pki/entitlement/3268107132875399464-key.pem

+ 8 - 5
roles/openshift_excluder/README.md

@@ -25,16 +25,19 @@ None
 
 Dependencies
 ------------
+- openshift_facts
+- openshift_repos
+- lib_utils
 
 Tasks to include
 ----------------
 
-- exclude: enable excluders (assuming excluders are installed)
-- unexclude: disable excluders (assuming excluders are installed)
+- exclude: enable excluders
+- unexclude: disable excluders
 - install: install excluders (installation is followed by excluder enabling)
-- enable: enable excluders (optionally with installation step)
-- disabled: disable excluders (optionally with installation and status step, the status check that can override which excluder gets enabled/disabled)
-- status: determine status of excluders
+- enable: enable excluders (install excluder(s) if not installed)
+- disabled: disable excluders (install excluder(s) if not installed)
+
 
 Example Playbook
 ----------------

+ 1 - 0
roles/openshift_excluder/meta/main.yml

@@ -14,3 +14,4 @@ galaxy_info:
 dependencies:
 - { role: openshift_facts }
 - { role: openshift_repos }
+- { role: lib_utils }

+ 15 - 9
roles/openshift_excluder/tasks/verify_excluder.yml

@@ -5,15 +5,21 @@
 # - openshift_upgrade_target
 - block:
   - name: Get available excluder version
-    command: >
-      {{ repoquery_cmd }} --qf '%{version}' "{{ excluder }}"
-    register: excluder_version
-    failed_when: false
-    changed_when: false
+    repoquery:
+      name: "{{ excluder }}"
+      ignore_excluders: true
+    register: excluder_out
+
+  - fail:
+      msg: "Package {{ excluder }} not found"
+    when: not excluder_out.results.package_found
+
+  - set_fact:
+      excluder_version: "{{ excluder_out.results.versions.available_versions.0 }}"
 
   - name: "{{ excluder }} version detected"
     debug:
-      msg: "{{ excluder }}: {{ excluder_version.stdout }}"
+      msg: "{{ excluder }}: {{ excluder_version }}"
 
   - name: Printing upgrade target version
     debug:
@@ -21,9 +27,9 @@
 
   - name: Check the available {{ excluder }} version is at most of the upgrade target version
     fail:
-      msg: "Available {{ excluder }} version {{ excluder_version.stdout }} is higher than the upgrade target version"
+      msg: "Available {{ excluder }} version {{ excluder_version }} is higher than the upgrade target version"
     when:
-    - "{{ excluder_version.stdout != '' }}"
-    - "{{ excluder_version.stdout.split('.')[0:2] | join('.') | version_compare(openshift_upgrade_target.split('.')[0:2] | join('.'), '>', strict=True) }}"
+    - "{{ excluder_version != '' }}"
+    - "{{ excluder_version.split('.')[0:2] | join('.') | version_compare(openshift_upgrade_target.split('.')[0:2] | join('.'), '>', strict=True) }}"
   when:
   - not openshift.common.is_atomic | bool

+ 1 - 0
roles/openshift_version/meta/main.yml

@@ -16,3 +16,4 @@ dependencies:
 - role: openshift_docker_facts
 - role: docker
   when: openshift.common.is_containerized | default(False) | bool and not skip_docker_role | default(False) | bool
+- role: lib_utils

+ 10 - 2
roles/openshift_version/tasks/main.yml

@@ -89,8 +89,16 @@
 - block:
   - name: Set openshift_version for containerized installation
     include: set_version_containerized.yml
-  - name: Determine openshift rpm version
-    include: rpm_version.yml
+  - name: Get available {{ openshift.common.service_type}} version
+    repoquery:
+      name: "{{ openshift.common.service_type}}"
+      ignore_excluders: true
+    register: rpm_results
+  - fail:
+      msg: "Package {{ openshift.common.service_type}} not found"
+    when: not rpm_results.results.package_found
+  - set_fact:
+      openshift_rpm_version: "{{ rpm_results.results.versions.available_versions.0 | default('0.0', True) }}"
   - name: Fail if rpm version and docker image version are different
     fail:
       msg: "OCP rpm version {{ openshift_rpm_version }} is different from OCP image version {{ openshift_version }}"

+ 0 - 44
roles/openshift_version/tasks/rpm_version.yml

@@ -1,44 +0,0 @@
----
-# input_variables:
-# - repoquery_cmd
-# - openshift.common.service_type
-# output_variables:
-# - openshift_rpm_version
-
-# if {{ openshift.common.service_type}}-excluder is enabled,
-# the repoquery for {{ openshift.common.service_type}} will not work.
-# Thus, create a temporary yum,conf file where exclude= is set to an empty list
-- name: Create temporary yum.conf file
-  command: mktemp -d /tmp/yum.conf.XXXXXX
-  register: yum_conf_temp_file_result
-
-- set_fact:
-    yum_conf_temp_file: "{{yum_conf_temp_file_result.stdout}}/yum.conf"
-
-- name: Copy yum.conf into the temporary file
-  copy:
-    src: /etc/yum.conf
-    dest: "{{ yum_conf_temp_file }}"
-    remote_src: True
-
-- name: Clear the exclude= list in the temporary yum.conf
-  lineinfile:
-    # since ansible 2.3 s/dest/path
-    dest: "{{ yum_conf_temp_file }}"
-    regexp: '^exclude='
-    line: 'exclude='
-
-- name: Gather common package version
-  command: >
-    {{ repoquery_cmd }} --config "{{ yum_conf_temp_file }}" --qf '%{version}' "{{ openshift.common.service_type}}"
-  register: common_version
-  failed_when: false
-  changed_when: false
-
-- name: Delete the temporary yum.conf
-  file:
-    path: "{{ yum_conf_temp_file_result.stdout }}"
-    state: absent
-
-- set_fact:
-    openshift_rpm_version: "{{ common_version.stdout | default('0.0', True) }}"

+ 13 - 3
roles/openshift_version/tasks/set_version_rpm.yml

@@ -8,7 +8,17 @@
   - openshift_version is not defined
 
 - block:
-  - include: rpm_version.yml
+  - name: Get available {{ openshift.common.service_type}} version
+    repoquery:
+      name: "{{ openshift.common.service_type}}"
+      ignore_excluders: true
+    register: rpm_results
+
+  - fail:
+      msg: "Package {{ openshift.common.service_type}} not found"
+    when: not rpm_results.results.package_found
+
   - set_fact:
-      openshift_version: "{{ openshift_rpm_version }}"
-  when: openshift_version is not defined
+      openshift_version: "{{ rpm_results.results.versions.available_versions.0 | default('0.0', True) }}"
+  when:
+  - openshift_version is not defined