-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaem_bundle.py
167 lines (145 loc) · 4.57 KB
/
aem_bundle.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright: (c) 2019, Lean Delivery Team <[email protected]>
# GNU General Public License v3.0+ (see COPYING or
# https://www.gnu.org/licenses/gpl-3.0.txt)
import requests
from ansible.module_utils.basic import *
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = u'''
---
module: aem_bundle
author:
- Lean Delivery Team
version_added: "1.0"
short_description: Manage AEM Bundles
description:
- Manage AEM Bundles
options:
name:
description:
- The name of the bundle
required: true
action:
description:
- start, stop or refresh the Bundle
required: true
choices: [start, stop, refresh]
admin_user:
description:
- AEM admin user account name
required: true
admin_password:
description:
- AEM admin user account password
required: true
url:
description:
- URL of AEM node
required: true
'''
EXAMPLES = u'''
# Start AEM bundle
- aem_bundle:
name: com.day.crx.crxde-support
action: start
admin_user: admin
admin_password: pa$$w0rd
url: https://aem-node.example.com:4502
# Stop AEM bundle
- aem_bundle:
name: com.day.crx.crxde-support
action: stop
admin_user: admin
admin_password: pa$$w0rd
url: https://aem-node.example.com:4502
# Refresh AEM bundle
- aem_bundle:
name: com.day.crx.crxde-support
action: refresh
admin_user: admin
admin_password: pa$$w0rd
url: https://aem-node.example.com:4502
'''
class AEMBundle(object):
"""docstring for AEMBundle"""
def __init__(self, arg):
# super(AEMBundle, self).__init__()
self.module = arg
self.name = self.module.params['name']
self.action = self.module.params['action']
self.admin_user = self.module.params['admin_user']
self.admin_password = self.module.params['admin_password']
self.url = self.module.params['url']
self.changed = False
self.msg = []
self._get_bnd_status()
def _get_bnd_status(self):
aem_request = requests.get('%s/system/console/bundles/%s.json' %
(self.url, self.name),
auth=(self.admin_user,
self.admin_password))
if aem_request.status_code == 200:
self.exists = True
if aem_request.json()['data'][0]['state'] == 'Active':
self.active = True
else:
self.active = False
else:
self.exists = False
self.active = False
def do_action(self):
aem_request = requests.post(
'%s/system/console/bundles/%s' %
(self.url, self.name), data={
'action': self.action}, auth=(
self.admin_user, self.admin_password))
if aem_request.status_code != 200:
self.module.fail_json(
msg='failed to perform %s action on %s bundle - %s' %
(self.action, aem_request.status_code, aem_request.json()))
self.changed = True
self.msg.append(
'action %s was performmed on bundle %s' %
(self.action, self.name))
def apply_task(self):
if self.exists:
if self.action == 'start':
if not self.active:
self.do_action()
elif self.action == 'stop':
if self.active:
self.do_action()
else:
self.do_action()
else:
self.module.fail_json(msg="can't find bundle '%s'" % (self.name))
def show_message(self):
if self.changed:
msg = ','.join(self.msg)
self.module.exit_json(changed=True, msg=msg)
else:
self.module.exit_json(changed=False)
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(required=True, type='str'),
action=dict(
default='start',
type='str',
choices=[
'start',
'stop',
'refresh']),
admin_user=dict(required=True, type='str'),
admin_password=dict(required=True, type='str', no_log=True),
url=dict(required=True, type='str')
),
supports_check_mode=False
)
bundle = AEMBundle(module)
bundle.apply_task()
bundle.show_message()
main()