Skip to content

Commit 29d6ae6

Browse files
asm0deuzguits
authored andcommitted
Import modules from ceph-ansible project
Signed-off-by: Teoman ONAY <tonay@ibm.com>
1 parent 1dfbdf2 commit 29d6ae6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+12201
-0
lines changed

plugins/module_utils/ca_common.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import os
2+
import datetime
3+
from typing import List
4+
from ansible.module_utils.basic import AnsibleModule
5+
6+
7+
def generate_cmd(cmd='ceph',
8+
sub_cmd=None,
9+
args=None,
10+
user_key=None,
11+
cluster='ceph',
12+
user='client.admin',
13+
container_image=None,
14+
interactive=False):
15+
'''
16+
Generate 'ceph' command line to execute
17+
'''
18+
19+
if user_key is None:
20+
user_key = '/etc/ceph/{}.{}.keyring'.format(cluster, user)
21+
22+
cmd = pre_generate_cmd(cmd, container_image=container_image, interactive=interactive) # noqa: E501
23+
24+
base_cmd = [
25+
'-n',
26+
user,
27+
'-k',
28+
user_key,
29+
'--cluster',
30+
cluster
31+
]
32+
33+
if sub_cmd is not None:
34+
base_cmd.extend(sub_cmd)
35+
36+
cmd.extend(base_cmd) if args is None else cmd.extend(base_cmd + args)
37+
38+
return cmd
39+
40+
41+
def container_exec(binary, container_image, interactive=False):
42+
'''
43+
Build the docker CLI to run a command inside a container
44+
'''
45+
46+
container_binary = os.getenv('CEPH_CONTAINER_BINARY')
47+
command_exec = [container_binary, 'run']
48+
49+
if interactive:
50+
command_exec.extend(['--interactive'])
51+
52+
command_exec.extend(['--rm',
53+
'--net=host',
54+
'-v', '/etc/ceph:/etc/ceph:z',
55+
'-v', '/var/lib/ceph/:/var/lib/ceph/:z',
56+
'-v', '/var/log/ceph/:/var/log/ceph/:z',
57+
'--entrypoint=' + binary, container_image])
58+
return command_exec
59+
60+
61+
def is_containerized():
62+
'''
63+
Check if we are running on a containerized cluster
64+
'''
65+
66+
if 'CEPH_CONTAINER_IMAGE' in os.environ:
67+
container_image = os.getenv('CEPH_CONTAINER_IMAGE')
68+
else:
69+
container_image = None
70+
71+
return container_image
72+
73+
74+
def pre_generate_cmd(cmd, container_image=None, interactive=False):
75+
'''
76+
Generate ceph prefix command
77+
'''
78+
if container_image:
79+
cmd = container_exec(cmd, container_image, interactive=interactive)
80+
else:
81+
cmd = [cmd]
82+
83+
return cmd
84+
85+
86+
def exec_command(module, cmd, stdin=None, check_rc=False):
87+
'''
88+
Execute command(s)
89+
'''
90+
91+
binary_data = False
92+
if stdin:
93+
binary_data = True
94+
rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data, check_rc=check_rc) # noqa: E501
95+
96+
return rc, cmd, out, err
97+
98+
99+
def build_base_cmd(module: "AnsibleModule") -> List[str]:
100+
cmd = ['cephadm']
101+
docker = module.params.get('docker')
102+
image = module.params.get('image')
103+
fsid = module.params.get('fsid')
104+
105+
if docker:
106+
cmd.append('--docker')
107+
if image:
108+
cmd.extend(['--image', image])
109+
110+
cmd.append('shell')
111+
112+
if fsid:
113+
cmd.extend(['--fsid', fsid])
114+
115+
return cmd
116+
117+
118+
def build_base_cmd_orch(module: "AnsibleModule") -> List[str]:
119+
cmd = build_base_cmd(module)
120+
cmd.extend(['ceph', 'orch'])
121+
122+
return cmd
123+
124+
125+
def exit_module(module, out, rc, cmd, err, startd, changed=False, diff=dict(before="", after="")): # noqa: E501
126+
endd = datetime.datetime.now()
127+
delta = endd - startd
128+
129+
result = dict(
130+
cmd=cmd,
131+
start=str(startd),
132+
end=str(endd),
133+
delta=str(delta),
134+
rc=rc,
135+
stdout=out.rstrip("\r\n"),
136+
stderr=err.rstrip("\r\n"),
137+
changed=changed,
138+
diff=diff
139+
)
140+
module.exit_json(**result)
141+
142+
143+
def fatal(message, module):
144+
'''
145+
Report a fatal error and exit
146+
'''
147+
148+
if module:
149+
module.fail_json(msg=message, rc=1)
150+
else:
151+
raise Exception(message)

0 commit comments

Comments
 (0)