summaryrefslogtreecommitdiff
path: root/contrib/inventory/brook.py
blob: b8c4bd7341b56c19aa956de32236b82808109c89 (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
#!/usr/bin/env python
# Copyright 2016 Doalitic.
#
# 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/>.

"""
Brook.io external inventory script
==================================

Generates inventory that Ansible can understand by making API requests to Brook.io via the libbrook
library. Hence, such dependency must be installed in the system to run this script.

The default configuration file is named 'brook.ini' and is located alongside this script. You can
choose any other file by setting the BROOK_INI_PATH environment variable.

If param 'project_id' is left blank in 'brook.ini', the inventory includes all the instances in
projects where the requesting user belongs. Otherwise, only instances from the given project are
included, provided the requesting user belongs to it.

The following variables are established for every host. They can be retrieved from the hostvars
dictionary.
 - brook_pid: str
 - brook_name: str
 - brook_description: str
 - brook_project: str
 - brook_template: str
 - brook_region: str
 - brook_zone: str
 - brook_status: str
 - brook_tags: list(str)
 - brook_internal_ips: list(str)
 - brook_external_ips: list(str)
 - brook_created_at
 - brook_updated_at
 - ansible_ssh_host

Instances are grouped by the following categories:
 - tag:
   A group is created for each tag. E.g. groups 'tag_foo' and 'tag_bar' are created if there exist
   instances with tags 'foo' and/or 'bar'.
 - project:
   A group is created for each project. E.g. group 'project_test' is created if a project named
   'test' exist.
 - status:
   A group is created for each instance state. E.g. groups 'status_RUNNING' and 'status_PENDING'
   are created if there are instances in running and pending state.

Examples:
  Execute uname on all instances in project 'test'
  $ ansible -i brook.py project_test -m shell -a "/bin/uname -a"

  Install nginx on all debian web servers tagged with 'www'
  $ ansible -i brook.py tag_www -m apt -a "name=nginx state=present"

  Run site.yml playbook on web servers
  $ ansible-playbook -i brook.py site.yml -l tag_www

Support:
  This script is tested on Python 2.7 and 3.4. It may work on other versions though.

Author: Francisco Ros <fjros@doalitic.com>
Version: 0.2
"""


import sys
import os

try:
    from ConfigParser import SafeConfigParser as ConfigParser
except ImportError:
    from configparser import ConfigParser

import json

try:
    import libbrook
except Exception:
    sys.exit('Brook.io inventory script requires libbrook. See https://github.com/doalitic/libbrook')


class BrookInventory:

    _API_ENDPOINT = 'https://api.brook.io'

    def __init__(self):
        self._configure_from_file()
        self.client = self.get_api_client()
        self.inventory = self.get_inventory()

    def _configure_from_file(self):
        """Initialize from .ini file.

        Configuration file is assumed to be named 'brook.ini' and to be located on the same
        directory than this file, unless the environment variable BROOK_INI_PATH says otherwise.
        """

        brook_ini_default_path = \
            os.path.join(os.path.dirname(os.path.realpath(__file__)), 'brook.ini')
        brook_ini_path = os.environ.get('BROOK_INI_PATH', brook_ini_default_path)

        config = ConfigParser(defaults={
            'api_token': '',
            'project_id': ''
        })
        config.read(brook_ini_path)
        self.api_token = config.get('brook', 'api_token')
        self.project_id = config.get('brook', 'project_id')

        if not self.api_token:
            sys.exit('You must provide (at least) your Brook.io API token to generate the dynamic '
                     'inventory.')

    def get_api_client(self):
        """Authenticate user via the provided credentials and return the corresponding API client.
        """

        # Get JWT token from API token
        #
        unauthenticated_client = libbrook.ApiClient(host=self._API_ENDPOINT)
        auth_api = libbrook.AuthApi(unauthenticated_client)
        api_token = libbrook.AuthTokenRequest()
        api_token.token = self.api_token
        jwt = auth_api.auth_token(token=api_token)

        # Create authenticated API client
        #
        return libbrook.ApiClient(host=self._API_ENDPOINT,
                                  header_name='Authorization',
                                  header_value='Bearer %s' % jwt.token)

    def get_inventory(self):
        """Generate Ansible inventory.
        """

        groups = dict()
        meta = dict()
        meta['hostvars'] = dict()

        instances_api = libbrook.InstancesApi(self.client)
        projects_api = libbrook.ProjectsApi(self.client)
        templates_api = libbrook.TemplatesApi(self.client)

        # If no project is given, get all projects the requesting user has access to
        #
        if not self.project_id:
            projects = [project.id for project in projects_api.index_projects()]
        else:
            projects = [self.project_id]

        # Build inventory from instances in all projects
        #
        for project_id in projects:
            project = projects_api.show_project(project_id=project_id)
            for instance in instances_api.index_instances(project_id=project_id):
                # Get template used for this instance if known
                template = templates_api.show_template(template_id=instance.template) if instance.template else None

                # Update hostvars
                try:
                    meta['hostvars'][instance.name] = \
                        self.hostvars(project, instance, template, instances_api)
                except libbrook.rest.ApiException:
                    continue

                # Group by project
                project_group = 'project_%s' % project.name
                if project_group in groups:
                    groups[project_group].append(instance.name)
                else:
                    groups[project_group] = [instance.name]

                # Group by status
                status_group = 'status_%s' % meta['hostvars'][instance.name]['brook_status']
                if status_group in groups:
                    groups[status_group].append(instance.name)
                else:
                    groups[status_group] = [instance.name]

                # Group by tags
                tags = meta['hostvars'][instance.name]['brook_tags']
                for tag in tags:
                    tag_group = 'tag_%s' % tag
                    if tag_group in groups:
                        groups[tag_group].append(instance.name)
                    else:
                        groups[tag_group] = [instance.name]

        groups['_meta'] = meta
        return groups

    def hostvars(self, project, instance, template, api):
        """Return the hostvars dictionary for the given instance.

        Raise libbrook.rest.ApiException if it cannot retrieve all required information from the
        Brook.io API.
        """

        hostvars = instance.to_dict()
        hostvars['brook_pid'] = hostvars.pop('pid')
        hostvars['brook_name'] = hostvars.pop('name')
        hostvars['brook_description'] = hostvars.pop('description')
        hostvars['brook_project'] = hostvars.pop('project')
        hostvars['brook_template'] = hostvars.pop('template')
        hostvars['brook_region'] = hostvars.pop('region')
        hostvars['brook_zone'] = hostvars.pop('zone')
        hostvars['brook_created_at'] = hostvars.pop('created_at')
        hostvars['brook_updated_at'] = hostvars.pop('updated_at')
        del hostvars['id']
        del hostvars['key']
        del hostvars['provider']
        del hostvars['image']

        # Substitute identifiers for names
        #
        hostvars['brook_project'] = project.name
        hostvars['brook_template'] = template.name if template else None

        # Retrieve instance state
        #
        status = api.status_instance(project_id=project.id, instance_id=instance.id)
        hostvars.update({'brook_status': status.state})

        # Retrieve instance tags
        #
        tags = api.instance_tags(project_id=project.id, instance_id=instance.id)
        hostvars.update({'brook_tags': tags})

        # Retrieve instance addresses
        #
        addresses = api.instance_addresses(project_id=project.id, instance_id=instance.id)
        internal_ips = [address.address for address in addresses if address.scope == 'internal']
        external_ips = [address.address for address in addresses
                        if address.address and address.scope == 'external']
        hostvars.update({'brook_internal_ips': internal_ips})
        hostvars.update({'brook_external_ips': external_ips})
        try:
            hostvars.update({'ansible_ssh_host': external_ips[0]})
        except IndexError:
            raise libbrook.rest.ApiException(status='502', reason='Instance without public IP')

        return hostvars


# Run the script
#
brook = BrookInventory()
print(json.dumps(brook.inventory))