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
This commit is contained in:
Adrian Likins 2017-02-07 12:07:01 -05:00 committed by GitHub
parent 530701b398
commit 7278308ca2
2 changed files with 65 additions and 40 deletions

View file

@ -104,7 +104,11 @@ options:
version_added: "2.1" version_added: "2.1"
consumer_id: consumer_id:
description: 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 required: False
default: null default: null
version_added: "2.1" version_added: "2.1"
@ -166,6 +170,9 @@ from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six.moves import configparser from ansible.module_utils.six.moves import configparser
SUBMAN_CMD = None
class RegistrationBase(object): class RegistrationBase(object):
def __init__(self, module, username=None, password=None): def __init__(self, module, username=None, password=None):
self.module = module self.module = module
@ -258,9 +265,9 @@ class Rhsm(RegistrationBase):
# Pass supplied **kwargs as parameters to subscription-manager. Ignore # Pass supplied **kwargs as parameters to subscription-manager. Ignore
# non-configuration parameters and replace '_' with '.'. For example, # non-configuration parameters and replace '_' with '.'. For example,
# 'server_hostname' becomes '--system.hostname'. # 'server_hostname' becomes '--system.hostname'.
for k,v in kwargs.items(): for k, v in kwargs.items():
if re.search(r'^(system|rhsm)_', k): 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) self.module.run_command(args, check_rc=True)
@ -272,10 +279,6 @@ class Rhsm(RegistrationBase):
* Boolean - whether the current system is currently registered to * Boolean - whether the current system is currently registered to
RHSM. 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'] args = [SUBMAN_CMD, 'identity']
rc, stdout, stderr = self.module.run_command(args, check_rc=False) rc, stdout, stderr = self.module.run_command(args, check_rc=False)
@ -285,7 +288,8 @@ class Rhsm(RegistrationBase):
return False return False
def register(self, username, password, autosubscribe, activationkey, org_id, 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 Register the current system to the provided RHSM or Sat6 server
Raises: Raises:
@ -297,6 +301,12 @@ class Rhsm(RegistrationBase):
if force_register: if force_register:
args.extend(['--force']) args.extend(['--force'])
if rhsm_baseurl:
args.extend(['--baseurl', rhsm_baseurl])
if server_insecure:
args.extend(['--insecure'])
if activationkey: if activationkey:
args.extend(['--activationkey', activationkey]) args.extend(['--activationkey', activationkey])
args.extend(['--org', org_id]) args.extend(['--org', org_id])
@ -366,22 +376,21 @@ class Rhsm(RegistrationBase):
return subscribed_pool_ids return subscribed_pool_ids
def update_subscriptions(self, regexp): def update_subscriptions(self, regexp):
changed=False changed = False
consumed_pools = RhsmPools(self.module, consumed=True) consumed_pools = RhsmPools(self.module, consumed=True)
pool_ids_to_keep = [p.get_pool_id() for p in consumed_pools.filter(regexp)] 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) serials = self.unsubscribe(serials=serials_to_remove)
subscribed_pool_ids = self.subscribe(regexp) subscribed_pool_ids = self.subscribe(regexp)
if subscribed_pool_ids or serials: if subscribed_pool_ids or serials:
changed=True changed = True
return {'changed': changed, 'subscribed_pool_ids': subscribed_pool_ids, return {'changed': changed, 'subscribed_pool_ids': subscribed_pool_ids,
'unsubscribed_serials': serials} 'unsubscribed_serials': serials}
class RhsmPool(object): class RhsmPool(object):
''' '''
Convenience class for housing subscription information Convenience class for housing subscription information
@ -389,7 +398,7 @@ class RhsmPool(object):
def __init__(self, module, **kwargs): def __init__(self, module, **kwargs):
self.module = module self.module = module
for k,v in kwargs.items(): for k, v in kwargs.items():
setattr(self, k, v) setattr(self, k, v)
def __str__(self): def __str__(self):
@ -441,7 +450,7 @@ class RhsmPools(object):
continue continue
# If a colon ':' is found, parse # If a colon ':' is found, parse
elif ':' in line: elif ':' in line:
(key, value) = line.split(':',1) (key, value) = line.split(':', 1)
key = key.strip().replace(" ", "") # To unify key = key.strip().replace(" ", "") # To unify
value = value.strip() value = value.strip()
if key in ['ProductName', 'SubscriptionName']: if key in ['ProductName', 'SubscriptionName']:
@ -451,7 +460,7 @@ class RhsmPools(object):
# Associate value with most recently recorded product # Associate value with most recently recorded product
products[-1].__setattr__(key, value) products[-1].__setattr__(key, value)
# FIXME - log some warning? # FIXME - log some warning?
#else: # else:
# warnings.warn("Unhandled subscription key/value: %s/%s" % (key,value)) # warnings.warn("Unhandled subscription key/value: %s/%s" % (key,value))
return products return products
@ -471,27 +480,44 @@ def main():
rhsm = Rhsm(None) rhsm = Rhsm(None)
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
state = dict(default='present', choices=['present', 'absent']), state=dict(default='present',
username = dict(default=None, required=False), choices=['present', 'absent']),
password = dict(default=None, required=False, no_log=True), username=dict(default=None,
server_hostname = dict(default=rhsm.config.get_option('server.hostname'), required=False), required=False),
server_insecure = dict(default=rhsm.config.get_option('server.insecure'), required=False), password=dict(default=None,
rhsm_baseurl = dict(default=rhsm.config.get_option('rhsm.baseurl'), required=False), required=False,
autosubscribe = dict(default=False, type='bool'), no_log=True),
activationkey = dict(default=None, required=False), server_hostname=dict(default=rhsm.config.get_option('server.hostname'),
org_id = dict(default=None, required=False), required=False),
environment = dict(default=None, required=False, type='str'), server_insecure=dict(default=rhsm.config.get_option('server.insecure'),
pool = dict(default='^$', required=False, type='str'), required=False),
consumer_type = dict(default=None, required=False), rhsm_baseurl=dict(default=rhsm.config.get_option('rhsm.baseurl'),
consumer_name = dict(default=None, required=False), required=False),
consumer_id = dict(default=None, required=False), autosubscribe=dict(default=False,
force_register = dict(default=False, type='bool'), type='bool'),
), activationkey=dict(default=None,
required_together = [ ['username', 'password'], ['activationkey', 'org_id'] ], required=False),
mutually_exclusive = [ ['username', 'activationkey'] ], org_id=dict(default=None,
required_if = [ [ 'state', 'present', ['username', 'activationkey'], True ] ], 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 rhsm.module = module
state = module.params['state'] state = module.params['state']
@ -500,7 +526,7 @@ def main():
server_hostname = module.params['server_hostname'] server_hostname = module.params['server_hostname']
server_insecure = module.params['server_insecure'] server_insecure = module.params['server_insecure']
rhsm_baseurl = module.params['rhsm_baseurl'] rhsm_baseurl = module.params['rhsm_baseurl']
autosubscribe = module.params['autosubscribe'] == True autosubscribe = module.params['autosubscribe']
activationkey = module.params['activationkey'] activationkey = module.params['activationkey']
org_id = module.params['org_id'] org_id = module.params['org_id']
environment = module.params['environment'] environment = module.params['environment']
@ -533,8 +559,8 @@ def main():
rhsm.enable() rhsm.enable()
rhsm.configure(**module.params) rhsm.configure(**module.params)
rhsm.register(username, password, autosubscribe, activationkey, org_id, rhsm.register(username, password, autosubscribe, activationkey, org_id,
consumer_type, consumer_name, consumer_id, force_register, consumer_type, consumer_name, consumer_id, force_register,
environment) environment, rhsm_baseurl, server_insecure)
subscribed_pool_ids = rhsm.subscribe(pool) subscribed_pool_ids = rhsm.subscribe(pool)
except Exception: except Exception:
e = get_exception() e = get_exception()

View file

@ -185,7 +185,6 @@ lib/ansible/modules/packaging/os/homebrew.py
lib/ansible/modules/packaging/os/openbsd_pkg.py lib/ansible/modules/packaging/os/openbsd_pkg.py
lib/ansible/modules/packaging/os/opkg.py lib/ansible/modules/packaging/os/opkg.py
lib/ansible/modules/packaging/os/pacman.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/rhn_register.py
lib/ansible/modules/packaging/os/swdepot.py lib/ansible/modules/packaging/os/swdepot.py
lib/ansible/modules/packaging/os/yum.py lib/ansible/modules/packaging/os/yum.py