From 1f52a133b86a4cda298bc1ed0fd340488f67adbb Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 5 Mar 2013 16:41:30 -0800 Subject: [PATCH] improve consistency between cobbler tops and pillar modules; use shared cobbler credentials --- salt/pillar/cobbler.py | 32 ++++++++++++++++++++++---------- salt/tops/cobbler.py | 40 ++++++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/salt/pillar/cobbler.py b/salt/pillar/cobbler.py index cc382f3bad..61b9278e82 100644 --- a/salt/pillar/cobbler.py +++ b/salt/pillar/cobbler.py @@ -1,18 +1,19 @@ ''' Cobbler Pillar ============== -A pillar module to pull data from Cobbler via its API -into the pillar dictionary. +A pillar module to pull data from Cobbler via its API into the pillar dictionary. +The same cobbler.* parameters are used for both the Cobbler tops and Cobbler pillar +modules. .. code-block:: yaml ext_pillar: - cobbler: - - url: https://example.com/ # Cobbler base URL. Default: http://localhost/ - - user: username # Cobbler username. Default is no username. - - password: password # Cobbler password. Default is no password. - key: cobbler # Nest results within this key. By default, values are not nested. - only: [parameters] # Add only these keys to pillar. + cobbler.url: https://example.com/cobbler_api #default is http://localhost/cobbler_api + cobbler.user: username # default is no username + cobbler.password: password # default is no password ''' # Import python libs @@ -20,20 +21,31 @@ import logging import xmlrpclib +__opts__ = {'cobbler.url': 'http://localhost/cobbler_api', + 'cobbler.user': None, + 'cobbler.password': None + } + + # Set up logging log = logging.getLogger(__name__) -def ext_pillar(pillar, url='http://localhost/', user=None, password=None, key=None, only=[]): + +def ext_pillar(pillar, key=None, only=[]): ''' Read pillar data from Cobbler via its API. ''' - hostname = __opts__['id'] - log.info("Querying cobbler for information for %r", hostname) + url = __opts__['cobbler.url'] + user = __opts__['cobbler.user'] + password = __opts__['cobbler.password'] + + minion_id = __opts__['id'] + log.info("Querying cobbler at %r for information for %r", url, minion_id) try: - server = xmlrpclib.Server('%s/cobbler_api' % url, allow_none=True) + server = xmlrpclib.Server(url, allow_none=True) if user: server = (server, server.login(user, password)) - result = server.get_blended_data(None, hostname) + result = server.get_blended_data(None, minion_id) except Exception: log.exception( 'Could not connect to cobbler.' diff --git a/salt/tops/cobbler.py b/salt/tops/cobbler.py index f89e5ba64d..0f6c4f0c91 100644 --- a/salt/tops/cobbler.py +++ b/salt/tops/cobbler.py @@ -3,14 +3,16 @@ Cobbler Tops ============ Cobbler Tops is a master tops subsystem used to look up mapping information -from Cobbler via its API. +from Cobbler via its API. The same cobbler.* parameters are used for both +the Cobbler tops and Cobbler pillar modules. .. code-block:: yaml master_tops: - cobbler: - - host: https://example.com/ #default is http://localhost/ - - user: username # default is no username - - password: password # default is no password + cobbler: {} + + cobbler.url: https://example.com/cobbler_api #default is http://localhost/cobbler_api + cobbler.user: username # default is no username + cobbler.password: password # default is no password ''' @@ -22,31 +24,29 @@ import xmlrpclib # Set up logging log = logging.getLogger(__name__) -def __virtual__(): - ''' - Only run if properly configured - ''' - if __opts__['master_tops'].get('cobbler'): - return 'cobbler' - return False + +__opts__ = {'cobbler.url': 'http://localhost/cobbler_api', + 'cobbler.user': None, + 'cobbler.password': None + } def top(**kwargs): ''' - Look up top data for a host in Cobbler + Look up top data in Cobbler for for a minion. ''' - host = __opts__['master_tops']['cobbler'].get('host', 'http://localhost/') - user = __opts__['master_tops']['cobbler'].get('user', None) - password = __opts__['master_tops']['cobbler'].get('password', None) + url = __opts__['cobbler.url'] + user = __opts__['cobbler.user'] + password = __opts__['cobbler.password'] - hostname = kwargs['opts']['id'] + minion_id = kwargs['opts']['id'] - log.info("Querying cobbler for information for %r", hostname) + log.info("Querying cobbler for information for %r", minion_id) try: - server = xmlrpclib.Server('%s/cobbler_api' % host, allow_none=True) + server = xmlrpclib.Server(url, allow_none=True) if user: server = (server, server.login(user, password)) - data = server.get_blended_data(None, hostname) + data = server.get_blended_data(None, minion_id) except Exception: log.exception( 'Could not connect to cobbler.'