summaryrefslogtreecommitdiff
path: root/contrib/inventory/rudder.py
blob: 4722fcf1e41ca281359ad9e522e661ae7f5cb6ee (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python

# Copyright (c) 2015, Normation SAS
#
# Inspired by the EC2 inventory plugin:
# https://github.com/ansible/ansible/blob/devel/contrib/inventory/ec2.py
#
# 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/>.

######################################################################

'''
Rudder external inventory script
=================================

Generates inventory that Ansible can understand by making API request to
a Rudder server. This script is compatible with Rudder 2.10 or later.

The output JSON includes all your Rudder groups, containing the hostnames of
their nodes. Groups and nodes have a variable called rudder_group_id and
rudder_node_id, which is the Rudder internal id of the item, allowing to identify
them uniquely. Hosts variables also include your node properties, which are
key => value properties set by the API and specific to each node.

This script assumes there is an rudder.ini file alongside it. To specify a
different path to rudder.ini, define the RUDDER_INI_PATH environment variable:

    export RUDDER_INI_PATH=/path/to/my_rudder.ini

You have to configure your Rudder server information, either in rudder.ini or
by overriding it with environment variables:

    export RUDDER_API_VERSION='latest'
    export RUDDER_API_TOKEN='my_token'
    export RUDDER_API_URI='https://rudder.local/rudder/api'
'''


import sys
import os
import re
import argparse
import httplib2 as http
from time import time
from ansible.module_utils import six
from ansible.module_utils.six.moves import configparser
from ansible.module_utils.six.moves.urllib.parse import urlparse

import json


class RudderInventory(object):
    def __init__(self):
        ''' Main execution path '''

        # Empty inventory by default
        self.inventory = {}

        # Read settings and parse CLI arguments
        self.read_settings()
        self.parse_cli_args()

        # Create connection
        self.conn = http.Http(disable_ssl_certificate_validation=self.disable_ssl_validation)

        # Cache
        if self.args.refresh_cache:
            self.update_cache()
        elif not self.is_cache_valid():
            self.update_cache()
        else:
            self.load_cache()

        data_to_print = {}

        if self.args.host:
            data_to_print = self.get_host_info(self.args.host)
        elif self.args.list:
            data_to_print = self.get_list_info()

        print(self.json_format_dict(data_to_print, True))

    def read_settings(self):
        ''' Reads the settings from the rudder.ini file '''
        if six.PY2:
            config = configparser.SafeConfigParser()
        else:
            config = configparser.ConfigParser()
        rudder_default_ini_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'rudder.ini')
        rudder_ini_path = os.path.expanduser(os.path.expandvars(os.environ.get('RUDDER_INI_PATH', rudder_default_ini_path)))
        config.read(rudder_ini_path)

        self.token = os.environ.get('RUDDER_API_TOKEN', config.get('rudder', 'token'))
        self.version = os.environ.get('RUDDER_API_VERSION', config.get('rudder', 'version'))
        self.uri = os.environ.get('RUDDER_API_URI', config.get('rudder', 'uri'))

        self.disable_ssl_validation = config.getboolean('rudder', 'disable_ssl_certificate_validation')
        self.group_name = config.get('rudder', 'group_name')
        self.fail_if_name_collision = config.getboolean('rudder', 'fail_if_name_collision')

        self.cache_path = config.get('rudder', 'cache_path')
        self.cache_max_age = config.getint('rudder', 'cache_max_age')

    def parse_cli_args(self):
        ''' Command line argument processing '''

        parser = argparse.ArgumentParser(description='Produce an Ansible Inventory file based on Rudder inventory')
        parser.add_argument('--list', action='store_true', default=True,
                            help='List instances (default: True)')
        parser.add_argument('--host', action='store',
                            help='Get all the variables about a specific instance')
        parser.add_argument('--refresh-cache', action='store_true', default=False,
                            help='Force refresh of cache by making API requests to Rudder (default: False - use cache files)')
        self.args = parser.parse_args()

    def is_cache_valid(self):
        ''' Determines if the cache files have expired, or if it is still valid '''

        if os.path.isfile(self.cache_path):
            mod_time = os.path.getmtime(self.cache_path)
            current_time = time()
            if (mod_time + self.cache_max_age) > current_time:
                return True

        return False

    def load_cache(self):
        ''' Reads the cache from the cache file sets self.cache '''

        cache = open(self.cache_path, 'r')
        json_cache = cache.read()

        try:
            self.inventory = json.loads(json_cache)
        except ValueError as e:
            self.fail_with_error('Could not parse JSON response from local cache', 'parsing local cache')

    def write_cache(self):
        ''' Writes data in JSON format to a file '''

        json_data = self.json_format_dict(self.inventory, True)
        cache = open(self.cache_path, 'w')
        cache.write(json_data)
        cache.close()

    def get_nodes(self):
        ''' Gets the nodes list from Rudder '''

        path = '/nodes?select=nodeAndPolicyServer'
        result = self.api_call(path)

        nodes = {}

        for node in result['data']['nodes']:
            nodes[node['id']] = {}
            nodes[node['id']]['hostname'] = node['hostname']
            if 'properties' in node:
                nodes[node['id']]['properties'] = node['properties']
            else:
                nodes[node['id']]['properties'] = []

        return nodes

    def get_groups(self):
        ''' Gets the groups list from Rudder '''

        path = '/groups'
        result = self.api_call(path)

        groups = {}

        for group in result['data']['groups']:
            groups[group['id']] = {'hosts': group['nodeIds'], 'name': self.to_safe(group[self.group_name])}

        return groups

    def update_cache(self):
        ''' Fetches the inventory information from Rudder and creates the inventory '''

        nodes = self.get_nodes()
        groups = self.get_groups()

        inventory = {}

        for group in groups:
            # Check for name collision
            if self.fail_if_name_collision:
                if groups[group]['name'] in inventory:
                    self.fail_with_error('Name collision on groups: "%s" appears twice' % groups[group]['name'], 'creating groups')
            # Add group to inventory
            inventory[groups[group]['name']] = {}
            inventory[groups[group]['name']]['hosts'] = []
            inventory[groups[group]['name']]['vars'] = {}
            inventory[groups[group]['name']]['vars']['rudder_group_id'] = group
            for node in groups[group]['hosts']:
                # Add node to group
                inventory[groups[group]['name']]['hosts'].append(nodes[node]['hostname'])

        properties = {}

        for node in nodes:
            # Check for name collision
            if self.fail_if_name_collision:
                if nodes[node]['hostname'] in properties:
                    self.fail_with_error('Name collision on hosts: "%s" appears twice' % nodes[node]['hostname'], 'creating hosts')
            # Add node properties to inventory
            properties[nodes[node]['hostname']] = {}
            properties[nodes[node]['hostname']]['rudder_node_id'] = node
            for node_property in nodes[node]['properties']:
                properties[nodes[node]['hostname']][self.to_safe(node_property['name'])] = node_property['value']

        inventory['_meta'] = {}
        inventory['_meta']['hostvars'] = properties

        self.inventory = inventory

        if self.cache_max_age > 0:
            self.write_cache()

    def get_list_info(self):
        ''' Gets inventory information from local cache '''

        return self.inventory

    def get_host_info(self, hostname):
        ''' Gets information about a specific host from local cache '''

        if hostname in self.inventory['_meta']['hostvars']:
            return self.inventory['_meta']['hostvars'][hostname]
        else:
            return {}

    def api_call(self, path):
        ''' Performs an API request '''

        headers = {
            'X-API-Token': self.token,
            'X-API-Version': self.version,
            'Content-Type': 'application/json;charset=utf-8'
        }

        target = urlparse(self.uri + path)
        method = 'GET'
        body = ''

        try:
            response, content = self.conn.request(target.geturl(), method, body, headers)
        except Exception:
            self.fail_with_error('Error connecting to Rudder server')

        try:
            data = json.loads(content)
        except ValueError as e:
            self.fail_with_error('Could not parse JSON response from Rudder API', 'reading API response')

        return data

    def fail_with_error(self, err_msg, err_operation=None):
        ''' Logs an error to std err for ansible-playbook to consume and exit '''
        if err_operation:
            err_msg = 'ERROR: "{err_msg}", while: {err_operation}'.format(
                err_msg=err_msg, err_operation=err_operation)
        sys.stderr.write(err_msg)
        sys.exit(1)

    def json_format_dict(self, data, pretty=False):
        ''' Converts a dict to a JSON object and dumps it as a formatted
        string '''

        if pretty:
            return json.dumps(data, sort_keys=True, indent=2)
        else:
            return json.dumps(data)

    def to_safe(self, word):
        ''' Converts 'bad' characters in a string to underscores so they can be
        used as Ansible variable names '''

        return re.sub(r'[^A-Za-z0-9\_]', '_', word)


# Run the script
RudderInventory()