summaryrefslogtreecommitdiff
path: root/packaging/os/rhn_channel.py
blob: 7a7735b84b5ed7c0d9b5453cedeb90504b985ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/python

# (c) Vincent Van de Kussen
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.

DOCUMENTATION = '''
---
module: rhn_channel
short_description: Adds or removes Red Hat software channels
description:
    - Adds or removes Red Hat software channels
version_added: "1.1"
author: "Vincent Van der Kussen (@vincentvdk)"
notes:
    - this module fetches the system id from RHN.
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/Satellite
        required: true
        default: null
    state:
        description:
            - whether the channel should be present or not
        required: false
        default: present
    url:
        description:
            - The full url to the RHN/Satellite api
        required: true
    user:
        description:
            - RHN/Satellite user
        required: true
    password:
        description:
            - "the user's password"
        required: true
'''

EXAMPLES = '''
- rhn_channel:
    name: rhel-x86_64-server-v2vwin-6
    sysname: server01
    url: https://rhn.redhat.com/rpc/api
    user: rhnuser
    password: guessme
'''

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

# ------------------------------------------------------- #

# unused:
#
#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(channelname, client, session, sysname, sys_id):
    channels = base_channels(client, session, sys_id)
    channels.append(channelname)
    return client.system.setChildChannels(session, sys_id, channels)

# ------------------------------------------------------- #

def unsubscribe_channels(channelname, client, session, sysname, sys_id):
    channels = base_channels(client, session, sys_id)
    channels.remove(channelname)
    return client.system.setChildChannels(session, sys_id, channels)

# ------------------------------------------------------- #

def base_channels(client, session, sys_id):
    basechan = client.channel.software.listSystemChannels(session, sys_id)
    try:
        chans = [item['label'] for item in basechan]
    except KeyError:
        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),
            user = dict(required=True),
            password = dict(required=True, aliases=['pwd']),
        )
#        supports_check_mode=True
    )

    state = module.params['state']
    channelname = module.params['name']
    systname = module.params['sysname']
    saturl = module.params['url']
    user = module.params['user']
    password = module.params['password']
    
    #initialize connection
    client = xmlrpclib.Server(saturl, verbose=0)
    session = client.auth.login(user, password)
     
    # 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="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)


# import module snippets
from ansible.module_utils.basic import *
main()