forked from remyma/ansible-karaf-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaraf_repo.py
139 lines (104 loc) · 3.76 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ansible.module_utils.basic import *
"""
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:
'''
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 = "{0} 'feature:{1}'"
CLIENT_KARAF_COMMAND_WITH_ARGS = "{0} 'feature:{1} {2}'"
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
"""
cmd = CLIENT_KARAF_COMMAND_WITH_ARGS.format(client_bin, PACKAGE_STATE_MAP[STATE_PRESENT], repo_url)
rc, out, err = module.run_command(cmd)
if rc != 0:
reason = parse_error(out)
module.fail_json(msg=reason)
# TODO : check if repo is added.
return True, cmd, out, err
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
"""
cmd = CLIENT_KARAF_COMMAND_WITH_ARGS.format(client_bin, PACKAGE_STATE_MAP[STATE_ABSENT], repo_url)
rc, out, err = module.run_command(cmd)
if rc != 0:
reason = parse_error(out)
module.fail_json(msg=reason)
# TODO : check if repo is removed.
return True, cmd, out, err
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
"""
cmd = CLIENT_KARAF_COMMAND_WITH_ARGS.format(client_bin, PACKAGE_STATE_MAP[STATE_REFRESH], repo_url)
rc, out, err = module.run_command(cmd)
if rc != 0:
reason = parse_error(out)
module.fail_json(msg=reason)
return True, cmd, out, err
def parse_error(string):
reason = "reason: "
try:
return string[string.index(reason) + len(reason):].strip()
except ValueError:
return string
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"]
changed = False
cmd = ''
out = ''
err = ''
if state == STATE_PRESENT:
changed, cmd, out, err = add_repo(client_bin, module, url)
elif state == STATE_PRESENT:
changed, cmd, out, err = remove_repo(client_bin, module, url)
elif state == STATE_REFRESH:
changed, cmd, out, err = refresh_repo(client_bin, module, url)
module.exit_json(changed=changed, cmd=cmd, name=url, state=state, stdout=out, stderr=err)
if __name__ == '__main__':
main()