2020-11-05 18:05:34 +08:00
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
2022-11-29 16:47:58 +08:00
|
|
|
def generate_cmd(cmd='ceph',
|
|
|
|
sub_cmd=None,
|
|
|
|
args=None,
|
|
|
|
user_key=None,
|
|
|
|
cluster='ceph',
|
|
|
|
user='client.admin',
|
|
|
|
container_image=None,
|
|
|
|
interactive=False):
|
2020-11-05 18:05:34 +08:00
|
|
|
'''
|
|
|
|
Generate 'ceph' command line to execute
|
|
|
|
'''
|
|
|
|
|
2022-11-29 16:47:58 +08:00
|
|
|
if user_key is None:
|
2020-11-26 03:51:09 +08:00
|
|
|
user_key = '/etc/ceph/{}.{}.keyring'.format(cluster, user)
|
|
|
|
|
2022-11-29 16:47:58 +08:00
|
|
|
cmd = pre_generate_cmd(cmd, container_image=container_image, interactive=interactive) # noqa: E501
|
2020-11-05 18:05:34 +08:00
|
|
|
|
|
|
|
base_cmd = [
|
|
|
|
'-n',
|
|
|
|
user,
|
|
|
|
'-k',
|
|
|
|
user_key,
|
|
|
|
'--cluster',
|
|
|
|
cluster
|
|
|
|
]
|
2022-11-29 16:47:58 +08:00
|
|
|
|
|
|
|
if sub_cmd is not None:
|
|
|
|
base_cmd.extend(sub_cmd)
|
|
|
|
|
|
|
|
cmd.extend(base_cmd) if args is None else cmd.extend(base_cmd + args)
|
2020-11-05 18:05:34 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2021-01-06 21:07:38 +08:00
|
|
|
def container_exec(binary, container_image, interactive=False):
|
2020-11-05 18:05:34 +08:00
|
|
|
'''
|
|
|
|
Build the docker CLI to run a command inside a container
|
|
|
|
'''
|
|
|
|
|
|
|
|
container_binary = os.getenv('CEPH_CONTAINER_BINARY')
|
2021-01-06 21:07:38 +08:00
|
|
|
command_exec = [container_binary, 'run']
|
|
|
|
|
|
|
|
if interactive:
|
|
|
|
command_exec.extend(['--interactive'])
|
|
|
|
|
|
|
|
command_exec.extend(['--rm',
|
|
|
|
'--net=host',
|
|
|
|
'-v', '/etc/ceph:/etc/ceph:z',
|
|
|
|
'-v', '/var/lib/ceph/:/var/lib/ceph/:z',
|
|
|
|
'-v', '/var/log/ceph/:/var/log/ceph/:z',
|
|
|
|
'--entrypoint=' + binary, container_image])
|
2020-11-05 18:05:34 +08:00
|
|
|
return command_exec
|
|
|
|
|
|
|
|
|
|
|
|
def is_containerized():
|
|
|
|
'''
|
|
|
|
Check if we are running on a containerized cluster
|
|
|
|
'''
|
|
|
|
|
|
|
|
if 'CEPH_CONTAINER_IMAGE' in os.environ:
|
|
|
|
container_image = os.getenv('CEPH_CONTAINER_IMAGE')
|
|
|
|
else:
|
|
|
|
container_image = None
|
|
|
|
|
|
|
|
return container_image
|
|
|
|
|
|
|
|
|
2022-11-29 16:47:58 +08:00
|
|
|
def pre_generate_cmd(cmd, container_image=None, interactive=False):
|
2020-11-05 18:05:34 +08:00
|
|
|
'''
|
2022-11-29 16:47:58 +08:00
|
|
|
Generate ceph prefix command
|
2020-11-05 18:05:34 +08:00
|
|
|
'''
|
|
|
|
if container_image:
|
2022-11-29 16:47:58 +08:00
|
|
|
cmd = container_exec(cmd, container_image, interactive=interactive)
|
2020-11-05 18:05:34 +08:00
|
|
|
else:
|
2022-11-29 16:47:58 +08:00
|
|
|
cmd = [cmd]
|
2020-11-05 18:05:34 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
|
2024-03-14 20:36:07 +08:00
|
|
|
def exec_command(module, cmd, stdin=None, check_rc=False):
|
2020-11-05 18:05:34 +08:00
|
|
|
'''
|
|
|
|
Execute command(s)
|
|
|
|
'''
|
|
|
|
|
2021-01-14 10:11:39 +08:00
|
|
|
binary_data = False
|
|
|
|
if stdin:
|
|
|
|
binary_data = True
|
2024-03-14 20:36:07 +08:00
|
|
|
rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data, check_rc=check_rc) # noqa: E501
|
2020-11-05 18:05:34 +08:00
|
|
|
|
|
|
|
return rc, cmd, out, err
|
|
|
|
|
|
|
|
|
2022-11-29 16:47:58 +08:00
|
|
|
def exit_module(module, out, rc, cmd, err, startd, changed=False, diff=dict(before="", after="")): # noqa: E501
|
2020-11-05 18:05:34 +08:00
|
|
|
endd = datetime.datetime.now()
|
|
|
|
delta = endd - startd
|
|
|
|
|
|
|
|
result = dict(
|
|
|
|
cmd=cmd,
|
|
|
|
start=str(startd),
|
|
|
|
end=str(endd),
|
|
|
|
delta=str(delta),
|
|
|
|
rc=rc,
|
|
|
|
stdout=out.rstrip("\r\n"),
|
|
|
|
stderr=err.rstrip("\r\n"),
|
|
|
|
changed=changed,
|
2022-02-05 02:33:18 +08:00
|
|
|
diff=diff
|
2020-11-05 18:05:34 +08:00
|
|
|
)
|
|
|
|
module.exit_json(**result)
|
2021-01-18 03:17:30 +08:00
|
|
|
|
2021-01-15 00:52:39 +08:00
|
|
|
|
2021-01-18 03:17:30 +08:00
|
|
|
def fatal(message, module):
|
|
|
|
'''
|
|
|
|
Report a fatal error and exit
|
|
|
|
'''
|
|
|
|
|
|
|
|
if module:
|
|
|
|
module.fail_json(msg=message, rc=1)
|
|
|
|
else:
|
2022-08-02 05:39:35 +08:00
|
|
|
raise Exception(message)
|