forked from remyma/ansible-karaf-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
karaf_repo.py
230 lines (182 loc) · 6.18 KB
/
karaf_repo.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import *
import os.path
"""
Ansible module to manage karaf repositories
(c) 2017, Matthieu Rémy <remy.matthieu@gmail.com>
"""
DOCUMENTATION = '''
---
module: karaf_repo
short_description: Manage karaf repositories.
description:
- Manage karaf repositories in karaf console.
options:
url:
description:
- path to the repo
required: true
state:
description:
- repo state
required: false
default: present
choices: [ "present", "absent", "refresh" ]
client_bin:
description:
- path to the 'client' program in karaf, can also point to the root of the karaf installation '/opt/karaf'
required: false
default: /opt/karaf/bin/client
'''
EXAMPLES = '''
# Install karaf repo
- karaf_repo: state="present" url="mvn:org.apache.camel.karaf/apache-camel/2.18.1/xml/features"
# Uninstall karaf repo
- karaf_repo: state="absent" url="mvn:org.apache.camel.karaf/apache-camel/2.18.1/xml/features"
# Refresh karaf repo
- karaf_repo: state="refresh" url="mvn:org.apache.camel.karaf/apache-camel/2.18.1/xml/features"
'''
STATE_PRESENT = "present"
STATE_ABSENT = "absent"
STATE_REFRESH = "refresh"
PACKAGE_STATE_MAP = dict(
present="repo-add",
absent="repo-remove",
refresh="repo-refresh"
)
CLIENT_KARAF_COMMAND = "feature:{0}"
CLIENT_KARAF_COMMAND_WITH_ARGS = "feature:{0} {1}"
_KARAF_COLUMN_SEPARATOR = '\xe2\x94\x82'
def run_with_check(module, cmd, arg):
rc, out, err = module.run_command('%s -b' % (cmd,), data=arg)
if rc != 0 or \
'Error executing command' in out or \
'Command not found' in out or\
len(err) > 0:
reason = out
module.fail_json(msg=reason, cmd=cmd, cmd_err=err, cmd_return=rc)
raise Exception(out)
return out
def get_existing_repos(module, client_bin):
karaf_cmd = 'feature:repo-list'
out = run_with_check(module, client_bin, karaf_cmd)
existing_repos = {}
for line in out.split('\n'):
split = line.split(_KARAF_COLUMN_SEPARATOR)
if len(split) != 2:
continue
repo_name = split[0].strip()
repo_url = split[1].strip()
existing_repos[repo_url] = {
'name': repo_name,
'url': repo_url,
}
return existing_repos
def add_repo(client_bin, module, repo_url):
"""Call karaf client command to add a repo
:param client_bin: karaf client command bin
:param module: ansible module
:param repo_url: url of repo to add
:return: command, ouput command message, error command message
"""
arg = CLIENT_KARAF_COMMAND_WITH_ARGS.format(PACKAGE_STATE_MAP[STATE_PRESENT], repo_url)
out = run_with_check(module, client_bin, arg)
result = dict(
changed=True,
original_message='',
message='',
meta = {},
out = out,
cmd = arg,
)
repos = get_existing_repos(module, client_bin)
if repo_url not in repos:
module.fail_json(msg='Repo ("%s") did not install' % repo_url)
raise Exception(out)
return result
def remove_repo(client_bin, module, repo_url):
"""Call karaf client command to remove a repo
:param client_bin: karaf client command bin
:param module: ansible module
:param repo_url: url of repo to remove
:return: command, ouput command message, error command message
"""
arg = CLIENT_KARAF_COMMAND_WITH_ARGS.format(PACKAGE_STATE_MAP[STATE_ABSENT], repo_url)
out = run_with_check(module, client_bin, arg)
result = dict(
changed=True,
original_message='',
message='',
meta = {},
out = out,
cmd = arg,
)
repos = get_existing_repos(module, client_bin)
if repo_url in repos:
module.fail_json(msg='Repo ("%s") is still installed' % repo_url)
raise Exception(out)
return result
def refresh_repo(client_bin, module, repo_url):
"""Call karaf client command to refresh a repository
:param client_bin: karaf client command bin
:param module: ansible module
:param repo_url: url of repo to remove
:return: command, ouput command message, error command message
"""
arg = CLIENT_KARAF_COMMAND_WITH_ARGS.format(PACKAGE_STATE_MAP[STATE_REFRESH], repo_url)
out = run_with_check(module, client_bin, arg)
result = dict(
changed=True,
original_message='',
message='',
meta = {},
out = out,
cmd = arg,
)
return result
def parse_error(string):
reason = "reason: "
try:
return string[string.index(reason) + len(reason):].strip()
except ValueError:
return string
def check_client_bin_path(client_bin):
if os.path.isfile(client_bin):
return client_bin
if os.path.isdir(client_bin):
test = os.path.join(client_bin, 'bin/client')
if os.path.isfile(test):
return test
else:
raise Exception('client_bin parameter not supported: %s' % client_bin)
def main():
module = AnsibleModule(
argument_spec=dict(
url=dict(required=True),
state=dict(default="present", choices=PACKAGE_STATE_MAP.keys()),
client_bin=dict(default="/opt/karaf/bin/client", type="path")
)
)
url = module.params["url"]
state = module.params["state"]
client_bin = module.params["client_bin"]
client_bin = check_client_bin_path(client_bin)
existing_repos = get_existing_repos(module, client_bin)
result = dict(
changed=False,
original_message='',
message='',
)
if state == STATE_PRESENT and url not in existing_repos:
result = add_repo(client_bin, module, url)
elif state == STATE_ABSENT and url in existing_repos:
result = remove_repo(client_bin, module, url)
elif state == STATE_REFRESH:
if url not in existing_repos:
module.fail_json(msg='The given repository ("%s") is not available and can therefore not be refreshed' % url)
else:
result = refresh_repo(client_bin, module, url)
module.exit_json(**result)
if __name__ == '__main__':
main()