From 7278308ca2c455f2feae59dcbee0b0959b68cadf Mon Sep 17 00:00:00 2001 From: Adrian Likins Date: Tue, 7 Feb 2017 12:07:01 -0500 Subject: [PATCH] Redhat subscription (#20729) * Make unused redhat_subscriptions do something rhsm_baseurl/server_insecure were module params that were never used previously. Hook them up for register options. * pep8/style cleanups * Set a module scope SUBMAN_CMD before we ref it as a global * move ansible imports to top, remove '*' imports * remove redhat_subscriptions from pep8 legacy --- .../packaging/os/redhat_subscription.py | 104 +++++++++++------- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/lib/ansible/modules/packaging/os/redhat_subscription.py b/lib/ansible/modules/packaging/os/redhat_subscription.py index 041da5c6e1..b6e468bd70 100644 --- a/lib/ansible/modules/packaging/os/redhat_subscription.py +++ b/lib/ansible/modules/packaging/os/redhat_subscription.py @@ -104,7 +104,11 @@ options: version_added: "2.1" consumer_id: description: - - References an existing consumer ID to resume using a previous registration for this system. If the system's identity certificate is lost or corrupted, this option allows it to resume using its previous identity and subscriptions. The default is to not specify a consumer ID so a new ID is created. + - | + References an existing consumer ID to resume using a previous registration + for this system. If the system's identity certificate is lost or corrupted, + this option allows it to resume using its previous identity and subscriptions. + The default is to not specify a consumer ID so a new ID is created. required: False default: null version_added: "2.1" @@ -166,6 +170,9 @@ from ansible.module_utils.pycompat24 import get_exception from ansible.module_utils.six.moves import configparser +SUBMAN_CMD = None + + class RegistrationBase(object): def __init__(self, module, username=None, password=None): self.module = module @@ -258,9 +265,9 @@ class Rhsm(RegistrationBase): # Pass supplied **kwargs as parameters to subscription-manager. Ignore # non-configuration parameters and replace '_' with '.'. For example, # 'server_hostname' becomes '--system.hostname'. - for k,v in kwargs.items(): + for k, v in kwargs.items(): if re.search(r'^(system|rhsm)_', k): - args.append('--%s=%s' % (k.replace('_','.'), v)) + args.append('--%s=%s' % (k.replace('_', '.'), v)) self.module.run_command(args, check_rc=True) @@ -272,10 +279,6 @@ class Rhsm(RegistrationBase): * Boolean - whether the current system is currently registered to RHSM. ''' - # Quick version... - if False: - return os.path.isfile('/etc/pki/consumer/cert.pem') and \ - os.path.isfile('/etc/pki/consumer/key.pem') args = [SUBMAN_CMD, 'identity'] rc, stdout, stderr = self.module.run_command(args, check_rc=False) @@ -285,7 +288,8 @@ class Rhsm(RegistrationBase): return False def register(self, username, password, autosubscribe, activationkey, org_id, - consumer_type, consumer_name, consumer_id, force_register, environment): + consumer_type, consumer_name, consumer_id, force_register, environment, + rhsm_baseurl, server_insecure): ''' Register the current system to the provided RHSM or Sat6 server Raises: @@ -297,6 +301,12 @@ class Rhsm(RegistrationBase): if force_register: args.extend(['--force']) + if rhsm_baseurl: + args.extend(['--baseurl', rhsm_baseurl]) + + if server_insecure: + args.extend(['--insecure']) + if activationkey: args.extend(['--activationkey', activationkey]) args.extend(['--org', org_id]) @@ -366,22 +376,21 @@ class Rhsm(RegistrationBase): return subscribed_pool_ids def update_subscriptions(self, regexp): - changed=False + changed = False consumed_pools = RhsmPools(self.module, consumed=True) pool_ids_to_keep = [p.get_pool_id() for p in consumed_pools.filter(regexp)] - serials_to_remove=[p.Serial for p in consumed_pools if p.get_pool_id() not in pool_ids_to_keep] + serials_to_remove = [p.Serial for p in consumed_pools if p.get_pool_id() not in pool_ids_to_keep] serials = self.unsubscribe(serials=serials_to_remove) subscribed_pool_ids = self.subscribe(regexp) if subscribed_pool_ids or serials: - changed=True + changed = True return {'changed': changed, 'subscribed_pool_ids': subscribed_pool_ids, 'unsubscribed_serials': serials} - class RhsmPool(object): ''' Convenience class for housing subscription information @@ -389,7 +398,7 @@ class RhsmPool(object): def __init__(self, module, **kwargs): self.module = module - for k,v in kwargs.items(): + for k, v in kwargs.items(): setattr(self, k, v) def __str__(self): @@ -441,7 +450,7 @@ class RhsmPools(object): continue # If a colon ':' is found, parse elif ':' in line: - (key, value) = line.split(':',1) + (key, value) = line.split(':', 1) key = key.strip().replace(" ", "") # To unify value = value.strip() if key in ['ProductName', 'SubscriptionName']: @@ -451,7 +460,7 @@ class RhsmPools(object): # Associate value with most recently recorded product products[-1].__setattr__(key, value) # FIXME - log some warning? - #else: + # else: # warnings.warn("Unhandled subscription key/value: %s/%s" % (key,value)) return products @@ -471,27 +480,44 @@ def main(): rhsm = Rhsm(None) module = AnsibleModule( - argument_spec = dict( - state = dict(default='present', choices=['present', 'absent']), - username = dict(default=None, required=False), - password = dict(default=None, required=False, no_log=True), - server_hostname = dict(default=rhsm.config.get_option('server.hostname'), required=False), - server_insecure = dict(default=rhsm.config.get_option('server.insecure'), required=False), - rhsm_baseurl = dict(default=rhsm.config.get_option('rhsm.baseurl'), required=False), - autosubscribe = dict(default=False, type='bool'), - activationkey = dict(default=None, required=False), - org_id = dict(default=None, required=False), - environment = dict(default=None, required=False, type='str'), - pool = dict(default='^$', required=False, type='str'), - consumer_type = dict(default=None, required=False), - consumer_name = dict(default=None, required=False), - consumer_id = dict(default=None, required=False), - force_register = dict(default=False, type='bool'), - ), - required_together = [ ['username', 'password'], ['activationkey', 'org_id'] ], - mutually_exclusive = [ ['username', 'activationkey'] ], - required_if = [ [ 'state', 'present', ['username', 'activationkey'], True ] ], - ) + argument_spec=dict( + state=dict(default='present', + choices=['present', 'absent']), + username=dict(default=None, + required=False), + password=dict(default=None, + required=False, + no_log=True), + server_hostname=dict(default=rhsm.config.get_option('server.hostname'), + required=False), + server_insecure=dict(default=rhsm.config.get_option('server.insecure'), + required=False), + rhsm_baseurl=dict(default=rhsm.config.get_option('rhsm.baseurl'), + required=False), + autosubscribe=dict(default=False, + type='bool'), + activationkey=dict(default=None, + required=False), + org_id=dict(default=None, + required=False), + environment=dict(default=None, + required=False, type='str'), + pool=dict(default='^$', + required=False, + type='str'), + consumer_type=dict(default=None, + required=False), + consumer_name=dict(default=None, + required=False), + consumer_id=dict(default=None, + required=False), + force_register=dict(default=False, + type='bool'), + ), + required_together=[['username', 'password'], ['activationkey', 'org_id']], + mutually_exclusive=[['username', 'activationkey']], + required_if=[['state', 'present', ['username', 'activationkey'], True]], + ) rhsm.module = module state = module.params['state'] @@ -500,7 +526,7 @@ def main(): server_hostname = module.params['server_hostname'] server_insecure = module.params['server_insecure'] rhsm_baseurl = module.params['rhsm_baseurl'] - autosubscribe = module.params['autosubscribe'] == True + autosubscribe = module.params['autosubscribe'] activationkey = module.params['activationkey'] org_id = module.params['org_id'] environment = module.params['environment'] @@ -533,8 +559,8 @@ def main(): rhsm.enable() rhsm.configure(**module.params) rhsm.register(username, password, autosubscribe, activationkey, org_id, - consumer_type, consumer_name, consumer_id, force_register, - environment) + consumer_type, consumer_name, consumer_id, force_register, + environment, rhsm_baseurl, server_insecure) subscribed_pool_ids = rhsm.subscribe(pool) except Exception: e = get_exception() diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 1b205ce49f..18149c1430 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -185,7 +185,6 @@ lib/ansible/modules/packaging/os/homebrew.py lib/ansible/modules/packaging/os/openbsd_pkg.py lib/ansible/modules/packaging/os/opkg.py lib/ansible/modules/packaging/os/pacman.py -lib/ansible/modules/packaging/os/redhat_subscription.py lib/ansible/modules/packaging/os/rhn_register.py lib/ansible/modules/packaging/os/swdepot.py lib/ansible/modules/packaging/os/yum.py