From 1388bb7859549025189cca3c9efb31aeeefb5566 Mon Sep 17 00:00:00 2001 From: Vincent Van der Kussen Date: Sat, 23 Mar 2013 16:30:02 +0100 Subject: [PATCH 1/6] Added module rhn_channel --- library/rhn_channel.py | 138 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100755 library/rhn_channel.py diff --git a/library/rhn_channel.py b/library/rhn_channel.py new file mode 100755 index 0000000000..ca24ee3e63 --- /dev/null +++ b/library/rhn_channel.py @@ -0,0 +1,138 @@ +#!/usr/bin/python + +DOCUMENTATION = ''' +--- +module: rhn_channel +short_description: Define Red Hat software channels +description: + - Adds or removes Red Hat software channels on a system +version_added: 1.0 +author: Vincent Van der Kussen +notes: + - this module fetches the systemid from rhn. A function + to use the local systemid is provided (get_localsystem) + but not integrated + - Username and password are currently hardcoded in the main + section +requirements: + - none +options: + name: + description + - name of the software channel + required: true + default: null + sysname: + description: + - name of the system as it is known in rhn/sattelite + required: true + default: null + url: + description: + - The full url to the rhn/sattelite api + required: true +examples: + - code: rhn_channel name=rhel-x86_64-server-v2vwin-6 sysname=server01 + url=https://rhn.redhat.com/rpc/api + description: add software channel rhel-x86_64-server-v2vwin-6 + to server01 in Red Hat Network +''' + +import xmlrpclib +from operator import itemgetter +import re + + +# ------------------------------------------------------- # + +def get_systemid(client, session, sysname): + systems = client.system.listUserSystems(session) + for system in systems: + if system.get('name') == sysname: + idres = system.get('id') + idd = int(idres) + return idd + +# ------------------------------------------------------- # + +def get_localsystemid(): + f = open("/etc/sysconfig/rhn/systemid", "r") + content = f.read() + loc_id = re.search(r'\b(ID-)(\d{10})' ,content) + return loc_id.group(2) + +# ------------------------------------------------------- # + +def subscribe_channels(channels, client, session, sysname, sys_id): + c = base_channels(client, session, sys_id) + c.append(channels) + return client.channel.software.setSystemChannels(session, sys_id, c) + +# ------------------------------------------------------- # + +def unsubscribe_channels(channels, client, session, sysname, sys_id): + c = base_channels(client, session, sys_id) + c.remove(channels) + return client.channel.software.setSystemChannels(session, sys_id, c) + +# ------------------------------------------------------- # + +def base_channels(client, session, sys_id): + basechan = client.channel.software.listSystemChannels(session, sys_id) + chans = [item['channel_label'] for item in basechan] + return chans + +# ------------------------------------------------------- # + + +def main(): + + module = AnsibleModule( + argument_spec = dict( + state = dict(default='present', choices=['present', 'absent']), + name = dict(required=True), + sysname = dict(required=True), + url = dict(required=True), + ) +# supports_check_mode=True + ) + + state = module.params['state'] + channelname = module.params['name'] + systname = module.params['sysname'] + saturl = module.params['url'] + + #initialize connection + user = "kussenv" + pwd = "fubar2000" + client = xmlrpclib.Server(saturl, verbose=0) + session = client.auth.login(user, pwd) + + # get systemid + sys_id = get_systemid(client, session, systname) + + # get channels for system + chans = base_channels(client, session, sys_id) + + + if state == 'present': + if channelname in chans: + module.exit_json(changed=False, msg="Channel %s already exists" % channelname) + + else: + subscribe_channels(channelname, client, session, systname, sys_id) + module.exit_json(changed=True, msg="Channel %s added" % channelname) + + if state == 'absent': + if not channelname in chans: + module.exit_json(changed=False, msg="System not subscribed to channel %s." % channelname) + else: + unsubscribe_channels(channelname, client, session, systname, sys_id) + module.exit_json(changed=True, msg="Channel %s removed" % channelname) + + client.auth.logout(session) + + +# include magic from lib/ansible/module_common.py +#<> +main() From 3a626032ae1d6a5447c6c2f5f972903996e40dee Mon Sep 17 00:00:00 2001 From: Vincent Van der Kussen Date: Sat, 23 Mar 2013 16:32:10 +0100 Subject: [PATCH 2/6] added rhn_channel module --- library/rhn_channel.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/rhn_channel.py b/library/rhn_channel.py index ca24ee3e63..e7a8e22588 100755 --- a/library/rhn_channel.py +++ b/library/rhn_channel.py @@ -103,8 +103,8 @@ def main(): saturl = module.params['url'] #initialize connection - user = "kussenv" - pwd = "fubar2000" + user = "" + pwd = "" client = xmlrpclib.Server(saturl, verbose=0) session = client.auth.login(user, pwd) From 38c41b6f9e31ced9761036a7a8b56b63eba2e9f0 Mon Sep 17 00:00:00 2001 From: Vincent Van der Kussen Date: Sat, 23 Mar 2013 16:33:14 +0100 Subject: [PATCH 3/6] renamed module --- library/{rhn_channel.py => rhn_channel} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename library/{rhn_channel.py => rhn_channel} (100%) diff --git a/library/rhn_channel.py b/library/rhn_channel similarity index 100% rename from library/rhn_channel.py rename to library/rhn_channel From 98406462d1144156ca73d8c772fdf201986c9dbe Mon Sep 17 00:00:00 2001 From: Vincent Van der Kussen Date: Sat, 23 Mar 2013 22:04:48 +0100 Subject: [PATCH 4/6] Made user and password options that can be passed to the module --- library/rhn_channel | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/library/rhn_channel b/library/rhn_channel index e7a8e22588..1c09c66276 100755 --- a/library/rhn_channel +++ b/library/rhn_channel @@ -31,9 +31,18 @@ options: description: - The full url to the rhn/sattelite api required: true + default: https://rhn.redhat.com/rpc/api + user: + description: + - the user to login to rhn/sattelite + required: true + pwd: + description: + - the user's password + required: true examples: - code: rhn_channel name=rhel-x86_64-server-v2vwin-6 sysname=server01 - url=https://rhn.redhat.com/rpc/api + url=https://rhn.redhat.com/rpc/api user=rhnuser pwd=guessme description: add software channel rhel-x86_64-server-v2vwin-6 to server01 in Red Hat Network ''' @@ -93,6 +102,8 @@ def main(): name = dict(required=True), sysname = dict(required=True), url = dict(required=True), + user = dict(required=True), + pwd = dict(required=True), ) # supports_check_mode=True ) @@ -101,10 +112,10 @@ def main(): channelname = module.params['name'] systname = module.params['sysname'] saturl = module.params['url'] + user = module.params['user'] + pwd = module.params['pwd'] #initialize connection - user = "" - pwd = "" client = xmlrpclib.Server(saturl, verbose=0) session = client.auth.login(user, pwd) @@ -125,7 +136,7 @@ def main(): if state == 'absent': if not channelname in chans: - module.exit_json(changed=False, msg="System not subscribed to channel %s." % channelname) + module.exit_json(changed=False, msg="Not subscribed to channel %s." % channelname) else: unsubscribe_channels(channelname, client, session, systname, sys_id) module.exit_json(changed=True, msg="Channel %s removed" % channelname) From 255f2b868fc41ad9816e7ad1ca8d76ffbc3b008a Mon Sep 17 00:00:00 2001 From: Vincent Van der Kussen Date: Sun, 24 Mar 2013 09:16:58 +0100 Subject: [PATCH 5/6] Removed info about hard coded user/pwd in the documentation part --- library/rhn_channel | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/rhn_channel b/library/rhn_channel index 1c09c66276..5f40120124 100755 --- a/library/rhn_channel +++ b/library/rhn_channel @@ -12,8 +12,6 @@ notes: - this module fetches the systemid from rhn. A function to use the local systemid is provided (get_localsystem) but not integrated - - Username and password are currently hardcoded in the main - section requirements: - none options: From 81a113ad6bcb1501cd58fc615197caf75ec7344d Mon Sep 17 00:00:00 2001 From: Vincent Van der Kussen Date: Sun, 24 Mar 2013 19:45:39 +0100 Subject: [PATCH 6/6] Removed false info --- library/rhn_channel | 1 - 1 file changed, 1 deletion(-) diff --git a/library/rhn_channel b/library/rhn_channel index 5f40120124..fa5df9d453 100755 --- a/library/rhn_channel +++ b/library/rhn_channel @@ -29,7 +29,6 @@ options: description: - The full url to the rhn/sattelite api required: true - default: https://rhn.redhat.com/rpc/api user: description: - the user to login to rhn/sattelite