Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import ceph ansible modules #3

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/1_0_2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
major_changes:
- Import ceph-ansible modules and fixes unittests
93 changes: 93 additions & 0 deletions plugins/module_utils/ceph_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__metaclass__ = type

import datetime
import os
import time
from typing import TYPE_CHECKING, Any, List, Dict, Callable, Type, TypeVar, Optional

Expand All @@ -11,6 +12,98 @@
ExceptionType = TypeVar('ExceptionType', bound=BaseException)


def generate_cmd(cmd='ceph',
sub_cmd=None,
args=None,
user_key=None,
cluster='ceph',
user='client.admin',
container_image=None,
interactive=False):
'''
Generate 'ceph' command line to execute
'''

if user_key is None:
user_key = '/etc/ceph/{}.{}.keyring'.format(cluster, user)

cmd = pre_generate_cmd(cmd, container_image=container_image, interactive=interactive) # noqa: E501

base_cmd = [
'-n',
user,
'-k',
user_key,
'--cluster',
cluster
]

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)

return cmd


def container_exec(binary, container_image, interactive=False):
'''
Build the docker CLI to run a command inside a container
'''

container_binary = os.getenv('CEPH_CONTAINER_BINARY')
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])
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


def pre_generate_cmd(cmd, container_image=None, interactive=False):
'''
Generate ceph prefix command
'''
if container_image:
cmd = container_exec(cmd, container_image, interactive=interactive)
else:
cmd = [cmd]

return cmd


def exec_command(module, cmd, stdin=None, check_rc=False):
'''
Execute command(s)
'''

binary_data = False
if stdin:
binary_data = True
rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data, check_rc=check_rc) # noqa: E501

return rc, cmd, out, err


def retry(exceptions: Type[ExceptionType], module: "AnsibleModule", retries: int = 20, delay: int = 1) -> Callable:
def decorator(f: Callable) -> Callable:
def _retry(*args: Any, **kwargs: Any) -> Callable:
Expand Down
26 changes: 26 additions & 0 deletions plugins/module_utils/ceph_crush_rule_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type


try:
from ansible_collections.ceph.automation.plugins.module_utils.ceph_common import generate_cmd
except ImportError:
from module_utils.ceph_common import generate_cmd


def get_rule(module, container_image=None):
'''
Get existing crush rule
'''

cluster = module.params.get('cluster')
name = module.params.get('name')

args = ['dump', name, '--format=json']

cmd = generate_cmd(sub_cmd=['osd', 'crush', 'rule'],
args=args,
cluster=cluster,
container_image=container_image)

return cmd
15 changes: 15 additions & 0 deletions plugins/module_utils/ceph_key_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type


def exec_commands(module, cmd_list):
'''
Execute command(s)
'''

for cmd in cmd_list:
rc, out, err = module.run_command(cmd)
if rc != 0:
return rc, cmd, out, err

return rc, cmd, out, err
Loading
Loading