summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/remote_management/foreman/_katello.py
blob: e0974201884eb0ba5e6f7e27cc4af869e1dd002f (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright: (c) 2016, Eric D Helms <ericdhelms@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['deprecated'],
                    'supported_by': 'community'}

DOCUMENTATION = '''
---
module: katello
short_description: Manage Katello Resources
deprecated:
    removed_in: "2.12"
    why: "Replaced by re-designed individual modules living at https://github.com/theforeman/foreman-ansible-modules"
    alternative: https://github.com/theforeman/foreman-ansible-modules
description:
    - Allows the management of Katello resources inside your Foreman server.
version_added: "2.3"
author:
- Eric D Helms (@ehelms)
requirements:
    - nailgun >= 0.28.0
    - python >= 2.6
    - datetime
options:
    server_url:
        description:
            - URL of Foreman server.
        required: true
    username:
        description:
            - Username on Foreman server.
        required: true
    password:
        description:
            - Password for user accessing Foreman server.
        required: true
    entity:
        description:
            - The Foreman resource that the action will be performed on (e.g. organization, host).
        choices:

            - repository
            - manifest
            - repository_set
            - sync_plan
            - content_view
            - lifecycle_environment
            - activation_key
            - product

        required: true
    action:
        description:
            - action associated to the entity resource to set or edit in dictionary format.
            - Possible Action in relation to Entitys.
            - "sync (available when entity=product or entity=repository)"
            - "publish (available when entity=content_view)"
            - "promote (available when entity=content_view)"
        choices:
            - sync
            - publish
            - promote
        required: false
    params:
        description:
            - Parameters associated to the entity resource and action, to set or edit in dictionary format.
            - Each choice may be only available with specific entitys and actions.
            - "Possible Choices are in the format of param_name ([entry,action,action,...],[entity,..],...)."
            - The action "None" means no action specified.
            - Possible Params in relation to entity and action.
            - "name ([product,sync,None], [repository,sync], [repository_set,None], [sync_plan,None],"
            - "[content_view,promote,publish,None], [lifecycle_environment,None], [activation_key,None])"
            - "organization ([product,sync,None] ,[repository,sync,None], [repository_set,None], [sync_plan,None], "
            - "[content_view,promote,publish,None], [lifecycle_environment,None], [activation_key,None])"
            - "content ([manifest,None])"
            - "product ([repository,sync,None], [repository_set,None], [sync_plan,None])"
            - "basearch ([repository_set,None])"
            - "releaserver ([repository_set,None])"
            - "sync_date ([sync_plan,None])"
            - "interval ([sync_plan,None])"
            - "repositories ([content_view,None])"
            - "from_environment ([content_view,promote])"
            - "to_environment([content_view,promote])"
            - "prior ([lifecycle_environment,None])"
            - "content_view ([activation_key,None])"
            - "lifecycle_environment ([activation_key,None])"
        required: true
    task_timeout:
        description:
            - The timeout in seconds to wait for the started Foreman action to finish.
            - If the timeout is reached and the Foreman action did not complete, the ansible task fails. However the foreman action does not get canceled.
        default: 1000
        version_added: "2.7"
        required: false
    verify_ssl:
        description:
            - verify the ssl/https connection (e.g for a valid certificate)
        default: false
        type: bool
        required: false
'''

EXAMPLES = '''
---
# Simple Example:

- name: Create Product
  katello:
      username: admin
      password: admin
      server_url: https://fakeserver.com
      entity: product
      params:
        name: Centos 7
  delegate_to: localhost

# Abstraction Example:
# katello.yml
---
- name: "{{ name }}"
  katello:
      username: admin
      password: admin
      server_url: https://fakeserver.com
      entity: "{{ entity }}"
      params: "{{ params }}"
  delegate_to: localhost

# tasks.yml
---
- include: katello.yml
  vars:
    name: Create Dev Environment
    entity: lifecycle_environment
    params:
      name: Dev
      prior: Library
      organization: Default Organization

- include: katello.yml
  vars:
    name: Create Centos Product
    entity: product
    params:
      name: Centos 7
      organization: Default Organization

- include: katello.yml
  vars:
    name: Create 7.2 Repository
    entity: repository
    params:
      name: Centos 7.2
      product: Centos 7
      organization: Default Organization
      content_type: yum
      url: http://mirror.centos.org/centos/7/os/x86_64/

- include: katello.yml
  vars:
      name: Create Centos 7 View
      entity: content_view
      params:
        name: Centos 7 View
        organization: Default Organization
        repositories:
          - name: Centos 7.2
            product: Centos 7

- include: katello.yml
  vars:
      name: Enable RHEL Product
      entity: repository_set
      params:
        name: Red Hat Enterprise Linux 7 Server (RPMs)
        product: Red Hat Enterprise Linux Server
        organization: Default Organization
        basearch: x86_64
        releasever: 7

- include: katello.yml
  vars:
      name: Promote Contentview Environment with longer timeout
      task_timeout: 10800
      entity: content_view
      action: promote
      params:
        name: MyContentView
        organization: MyOrganisation
        from_environment: Testing
        to_environment: Production

# Best Practices

# In Foreman, things can be done in parallel.
# When a conflicting action is already running,
# the task will fail instantly instead of waiting for the already running action to complete.
# So you should use a "until success" loop to catch this.

- name: Promote Contentview Environment with increased Timeout
  katello:
  username: ansibleuser
  password: supersecret
  task_timeout: 10800
  entity: content_view
  action: promote
  params:
    name: MyContentView
    organization: MyOrganisation
    from_environment: Testing
    to_environment: Production
  register: task_result
  until: task_result is success
  retries: 9
  delay: 120

'''

RETURN = '''# '''

import datetime
import os
import traceback

try:
    from nailgun import entities, entity_fields, entity_mixins
    from nailgun.config import ServerConfig
    HAS_NAILGUN_PACKAGE = True
except Exception:
    HAS_NAILGUN_PACKAGE = False

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native


class NailGun(object):
    def __init__(self, server, entities, module, task_timeout):
        self._server = server
        self._entities = entities
        self._module = module
        entity_mixins.TASK_TIMEOUT = task_timeout

    def find_organization(self, name, **params):
        org = self._entities.Organization(self._server, name=name, **params)
        response = org.search(set(), {'search': 'name={0}'.format(name)})

        if len(response) == 1:
            return response[0]
        else:
            self._module.fail_json(msg="No organization found for %s" % name)

    def find_lifecycle_environment(self, name, organization):
        org = self.find_organization(organization)

        lifecycle_env = self._entities.LifecycleEnvironment(self._server, name=name, organization=org)
        response = lifecycle_env.search()

        if len(response) == 1:
            return response[0]
        else:
            self._module.fail_json(msg="No Lifecycle Found found for %s" % name)

    def find_product(self, name, organization):
        org = self.find_organization(organization)

        product = self._entities.Product(self._server, name=name, organization=org)
        response = product.search()

        if len(response) == 1:
            return response[0]
        else:
            self._module.fail_json(msg="No Product found for %s" % name)

    def find_repository(self, name, product, organization):
        product = self.find_product(product, organization)

        repository = self._entities.Repository(self._server, name=name, product=product)
        repository._fields['organization'] = entity_fields.OneToOneField(entities.Organization)
        repository.organization = product.organization
        response = repository.search()

        if len(response) == 1:
            return response[0]
        else:
            self._module.fail_json(msg="No Repository found for %s" % name)

    def find_content_view(self, name, organization):
        org = self.find_organization(organization)

        content_view = self._entities.ContentView(self._server, name=name, organization=org)
        response = content_view.search()

        if len(response) == 1:
            return response[0]
        else:
            self._module.fail_json(msg="No Content View found for %s" % name)

    def organization(self, params):
        name = params['name']
        del params['name']
        org = self.find_organization(name, **params)

        if org:
            org = self._entities.Organization(self._server, name=name, id=org.id, **params)
            org.update()
        else:
            org = self._entities.Organization(self._server, name=name, **params)
            org.create()

        return True

    def manifest(self, params):
        org = self.find_organization(params['organization'])
        params['organization'] = org.id

        try:
            file = open(os.getcwd() + params['content'], 'r')
            content = file.read()
        finally:
            file.close()

        manifest = self._entities.Subscription(self._server)

        try:
            manifest.upload(
                data={'organization_id': org.id},
                files={'content': content}
            )
            return True
        except Exception as e:

            if "Import is the same as existing data" in e.message:
                return False
            else:
                self._module.fail_json(msg="Manifest import failed with %s" % to_native(e),
                                       exception=traceback.format_exc())

    def product(self, params):
        org = self.find_organization(params['organization'])
        params['organization'] = org.id

        product = self._entities.Product(self._server, **params)
        response = product.search()

        if len(response) == 1:
            product.id = response[0].id
            product.update()
        else:
            product.create()

        return True

    def sync_product(self, params):
        org = self.find_organization(params['organization'])
        product = self.find_product(params['name'], org.name)

        return product.sync()

    def repository(self, params):
        product = self.find_product(params['product'], params['organization'])
        params['product'] = product.id
        del params['organization']

        repository = self._entities.Repository(self._server, **params)
        repository._fields['organization'] = entity_fields.OneToOneField(entities.Organization)
        repository.organization = product.organization
        response = repository.search()

        if len(response) == 1:
            repository.id = response[0].id
            repository.update()
        else:
            repository.create()

        return True

    def sync_repository(self, params):
        org = self.find_organization(params['organization'])
        repository = self.find_repository(params['name'], params['product'], org.name)

        return repository.sync()

    def repository_set(self, params):
        product = self.find_product(params['product'], params['organization'])
        del params['product']
        del params['organization']

        if not product:
            return False
        else:
            reposet = self._entities.RepositorySet(self._server, product=product, name=params['name'])
            reposet = reposet.search()[0]

            formatted_name = [params['name'].replace('(', '').replace(')', '')]
            formatted_name.append(params['basearch'])

            if 'releasever' in params:
                formatted_name.append(params['releasever'])

            formatted_name = ' '.join(formatted_name)

            repository = self._entities.Repository(self._server, product=product, name=formatted_name)
            repository._fields['organization'] = entity_fields.OneToOneField(entities.Organization)
            repository.organization = product.organization
            repository = repository.search()

            if len(repository) == 0:
                if 'releasever' in params:
                    reposet.enable(data={'basearch': params['basearch'], 'releasever': params['releasever']})
                else:
                    reposet.enable(data={'basearch': params['basearch']})

        return True

    def sync_plan(self, params):
        org = self.find_organization(params['organization'])
        params['organization'] = org.id
        params['sync_date'] = datetime.datetime.strptime(params['sync_date'], "%H:%M")

        products = params['products']
        del params['products']

        sync_plan = self._entities.SyncPlan(
            self._server,
            name=params['name'],
            organization=org
        )
        response = sync_plan.search()

        sync_plan.sync_date = params['sync_date']
        sync_plan.interval = params['interval']

        if len(response) == 1:
            sync_plan.id = response[0].id
            sync_plan.update()
        else:
            response = sync_plan.create()
            sync_plan.id = response[0].id

        if products:
            ids = []

            for name in products:
                product = self.find_product(name, org.name)
                ids.append(product.id)

            sync_plan.add_products(data={'product_ids': ids})

        return True

    def content_view(self, params):
        org = self.find_organization(params['organization'])

        content_view = self._entities.ContentView(self._server, name=params['name'], organization=org)
        response = content_view.search()

        if len(response) == 1:
            content_view.id = response[0].id
            content_view.update()
        else:
            content_view = content_view.create()

        if params['repositories']:
            repos = []

            for repository in params['repositories']:
                repository = self.find_repository(repository['name'], repository['product'], org.name)
                repos.append(repository)

            content_view.repository = repos
            content_view.update(['repository'])

    def find_content_view_version(self, name, organization, environment):
        env = self.find_lifecycle_environment(environment, organization)
        content_view = self.find_content_view(name, organization)

        content_view_version = self._entities.ContentViewVersion(self._server, content_view=content_view)
        response = content_view_version.search(['content_view'], {'environment_id': env.id})

        if len(response) == 1:
            return response[0]
        else:
            self._module.fail_json(msg="No Content View version found for %s" % response)

    def publish(self, params):
        content_view = self.find_content_view(params['name'], params['organization'])

        return content_view.publish()

    def promote(self, params):
        to_environment = self.find_lifecycle_environment(params['to_environment'], params['organization'])
        version = self.find_content_view_version(params['name'], params['organization'], params['from_environment'])

        data = {'environment_id': to_environment.id}
        return version.promote(data=data)

    def lifecycle_environment(self, params):
        org = self.find_organization(params['organization'])
        prior_env = self.find_lifecycle_environment(params['prior'], params['organization'])

        lifecycle_env = self._entities.LifecycleEnvironment(self._server, name=params['name'], organization=org, prior=prior_env)
        response = lifecycle_env.search()

        if len(response) == 1:
            lifecycle_env.id = response[0].id
            lifecycle_env.update()
        else:
            lifecycle_env.create()

        return True

    def activation_key(self, params):
        org = self.find_organization(params['organization'])

        activation_key = self._entities.ActivationKey(self._server, name=params['name'], organization=org)
        response = activation_key.search()

        if len(response) == 1:
            activation_key.id = response[0].id
            activation_key.update()
        else:
            activation_key.create()

        if params['content_view']:
            content_view = self.find_content_view(params['content_view'], params['organization'])
            lifecycle_environment = self.find_lifecycle_environment(params['lifecycle_environment'], params['organization'])

            activation_key.content_view = content_view
            activation_key.environment = lifecycle_environment
            activation_key.update()

        return True


def main():
    module = AnsibleModule(
        argument_spec=dict(
            server_url=dict(type='str', required=True),
            username=dict(type='str', required=True, no_log=True),
            password=dict(type='str', required=True, no_log=True),
            entity=dict(type='str', required=True,
                        choices=['repository', 'manifest', 'repository_set', 'sync_plan',
                                 'content_view', 'lifecycle_environment', 'activation_key', 'product']),
            action=dict(type='str', choices=['sync', 'publish', 'promote']),
            verify_ssl=dict(type='bool', default=False),
            task_timeout=dict(type='int', default=1000),
            params=dict(type='dict', required=True, no_log=True),
        ),
        supports_check_mode=True,
    )

    if not HAS_NAILGUN_PACKAGE:
        module.fail_json(msg="Missing required nailgun module (check docs or install with: pip install nailgun")

    server_url = module.params['server_url']
    username = module.params['username']
    password = module.params['password']
    entity = module.params['entity']
    action = module.params['action']
    params = module.params['params']
    verify_ssl = module.params['verify_ssl']
    task_timeout = module.params['task_timeout']

    server = ServerConfig(
        url=server_url,
        auth=(username, password),
        verify=verify_ssl
    )
    ng = NailGun(server, entities, module, task_timeout)

    # Lets make an connection to the server with username and password
    try:
        org = entities.Organization(server)
        org.search()
    except Exception as e:
        module.fail_json(msg="Failed to connect to Foreman server: %s " % e)

    result = False

    if entity == 'product':
        if action == 'sync':
            result = ng.sync_product(params)
        else:
            result = ng.product(params)
    elif entity == 'repository':
        if action == 'sync':
            result = ng.sync_repository(params)
        else:
            result = ng.repository(params)
    elif entity == 'manifest':
        result = ng.manifest(params)
    elif entity == 'repository_set':
        result = ng.repository_set(params)
    elif entity == 'sync_plan':
        result = ng.sync_plan(params)
    elif entity == 'content_view':
        if action == 'publish':
            result = ng.publish(params)
        elif action == 'promote':
            result = ng.promote(params)
        else:
            result = ng.content_view(params)
    elif entity == 'lifecycle_environment':
        result = ng.lifecycle_environment(params)
    elif entity == 'activation_key':
        result = ng.activation_key(params)
    else:
        module.fail_json(changed=False, result="Unsupported entity supplied")

    module.exit_json(changed=result, result="%s updated" % entity)


if __name__ == '__main__':
    main()