Newer
Older
#!/usr/bin/env python
# Copyright 2018 99cloud
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
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
import subprocess # nosec
DOCUMENTATION = '''
---
module: kolla_ceph_keyring
short_description: >
Module for update ceph client keyring caps in kolla.
description:
- A module used to update ceph client keyring caps in kolla.
options:
name:
description:
- the client name in ceph
required: True
type: str
container_name:
description:
- the ceph mon container name
required: True
default: ceph_mon
type: str
caps:
description:
- the ceph auth caps
required: True
type: dict
author: Jeffrey Zhang
'''
EXAMPLES = '''
- name: configure admin client caps
kolla_ceph_keyring:
name: client.admin
container_name: ceph_mon
caps:
mon: 'allow *'
osd: 'allow *'
mgr: 'allow *'
'''
enoent_re = re.compile(r"\bENOENT\b")
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
class CephKeyring(object):
def __init__(self, name, caps, container_name='ceph_mon'):
self.name = name
self.caps = caps
self.container_name = container_name
self.changed = False
self.message = None
def _run(self, cmd):
_prefix = ['docker', 'exec', self.container_name]
cmd = _prefix + cmd
proc = subprocess.Popen(cmd, # nosec
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = proc.communicate()
retcode = proc.poll()
if retcode != 0:
output = 'stdout: "%s", stderr: "%s"' % (stdout, stderr)
raise subprocess.CalledProcessError(retcode, cmd, output)
return stdout
def _format_caps(self):
caps = []
for obj in sorted(self.caps):
caps.extend([obj, self.caps[obj]])
return caps
def parse_stdout(self, stdout):
keyring = json.loads(stdout)
# there should be only one element
return keyring[0]
def ensure_keyring(self):
try:
stdout = self.get_keyring()
except subprocess.CalledProcessError as e:
if e.returncode != 2 or not enoent_re.search(e.output):
# this is not a missing keyring case
raise
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
# keyring doesn't exsit, try to create it
stdout = self.create_keyring()
self.changed = True
self.message = 'ceph keyring for %s is created' % self.name
keyring = self.parse_stdout(stdout)
if keyring['caps'] != self.caps:
self.update_caps()
stdout = self.get_keyring()
keyring = self.parse_stdout(stdout)
self.changed = True
self.message = 'ceph keyring for %s is updated' % self.name
self.keyring = keyring
return self.keyring
def get_keyring(self):
ceph_cmd = ['ceph', '--format', 'json', 'auth', 'get', self.name]
return self._run(ceph_cmd)
def update_caps(self):
ceph_cmd = ['ceph', '--format', 'json', 'auth', 'caps', self.name]
caps = self._format_caps()
ceph_cmd.extend(caps)
self._run(ceph_cmd)
def create_keyring(self):
ceph_cmd = ['ceph', '--format', 'json', 'auth',
'get-or-create', self.name]
caps = self._format_caps()
ceph_cmd.extend(caps)
return self._run(ceph_cmd)
def main():
specs = dict(
name=dict(type='str', required=True),
container_name=dict(type='str', default='ceph_mon'),
caps=dict(type='dict', required=True)
)
module = AnsibleModule(argument_spec=specs) # noqa
params = module.params
ceph_keyring = CephKeyring(params['name'],
params['caps'],
params['container_name'])
try:
keyring = ceph_keyring.ensure_keyring()
module.exit_json(changed=ceph_keyring.changed,
keyring=keyring,
message=ceph_keyring.message)
except subprocess.CalledProcessError as ex:
msg = ('Failed to call command: %s returncode: %s output: %s' %
(ex.cmd, ex.returncode, ex.output))
module.fail_json(msg=msg)
from ansible.module_utils.basic import * # noqa
if __name__ == "__main__":
main()