|
10 | 10 |
|
11 | 11 | ExceptionType = TypeVar('ExceptionType', bound=BaseException) |
12 | 12 |
|
| 13 | +def generate_cmd(cmd='ceph', |
| 14 | + sub_cmd=None, |
| 15 | + args=None, |
| 16 | + user_key=None, |
| 17 | + cluster='ceph', |
| 18 | + user='client.admin', |
| 19 | + container_image=None, |
| 20 | + interactive=False): |
| 21 | + ''' |
| 22 | + Generate 'ceph' command line to execute |
| 23 | + ''' |
| 24 | + |
| 25 | + if user_key is None: |
| 26 | + user_key = '/etc/ceph/{}.{}.keyring'.format(cluster, user) |
| 27 | + |
| 28 | + cmd = pre_generate_cmd(cmd, container_image=container_image, interactive=interactive) # noqa: E501 |
| 29 | + |
| 30 | + base_cmd = [ |
| 31 | + '-n', |
| 32 | + user, |
| 33 | + '-k', |
| 34 | + user_key, |
| 35 | + '--cluster', |
| 36 | + cluster |
| 37 | + ] |
| 38 | + |
| 39 | + if sub_cmd is not None: |
| 40 | + base_cmd.extend(sub_cmd) |
| 41 | + |
| 42 | + cmd.extend(base_cmd) if args is None else cmd.extend(base_cmd + args) |
| 43 | + |
| 44 | + return cmd |
| 45 | + |
| 46 | + |
| 47 | +def container_exec(binary, container_image, interactive=False): |
| 48 | + ''' |
| 49 | + Build the docker CLI to run a command inside a container |
| 50 | + ''' |
| 51 | + |
| 52 | + container_binary = os.getenv('CEPH_CONTAINER_BINARY') |
| 53 | + command_exec = [container_binary, 'run'] |
| 54 | + |
| 55 | + if interactive: |
| 56 | + command_exec.extend(['--interactive']) |
| 57 | + |
| 58 | + command_exec.extend(['--rm', |
| 59 | + '--net=host', |
| 60 | + '-v', '/etc/ceph:/etc/ceph:z', |
| 61 | + '-v', '/var/lib/ceph/:/var/lib/ceph/:z', |
| 62 | + '-v', '/var/log/ceph/:/var/log/ceph/:z', |
| 63 | + '--entrypoint=' + binary, container_image]) |
| 64 | + return command_exec |
| 65 | + |
| 66 | + |
| 67 | +def is_containerized(): |
| 68 | + ''' |
| 69 | + Check if we are running on a containerized cluster |
| 70 | + ''' |
| 71 | + |
| 72 | + if 'CEPH_CONTAINER_IMAGE' in os.environ: |
| 73 | + container_image = os.getenv('CEPH_CONTAINER_IMAGE') |
| 74 | + else: |
| 75 | + container_image = None |
| 76 | + |
| 77 | + return container_image |
| 78 | + |
| 79 | + |
| 80 | +def pre_generate_cmd(cmd, container_image=None, interactive=False): |
| 81 | + ''' |
| 82 | + Generate ceph prefix command |
| 83 | + ''' |
| 84 | + if container_image: |
| 85 | + cmd = container_exec(cmd, container_image, interactive=interactive) |
| 86 | + else: |
| 87 | + cmd = [cmd] |
| 88 | + |
| 89 | + return cmd |
| 90 | + |
| 91 | + |
| 92 | +def exec_command(module, cmd, stdin=None, check_rc=False): |
| 93 | + ''' |
| 94 | + Execute command(s) |
| 95 | + ''' |
| 96 | + |
| 97 | + binary_data = False |
| 98 | + if stdin: |
| 99 | + binary_data = True |
| 100 | + rc, out, err = module.run_command(cmd, data=stdin, binary_data=binary_data, check_rc=check_rc) # noqa: E501 |
| 101 | + |
| 102 | + return rc, cmd, out, err |
| 103 | + |
13 | 104 |
|
14 | 105 | def retry(exceptions: Type[ExceptionType], module: "AnsibleModule", retries: int = 20, delay: int = 1) -> Callable: |
15 | 106 | def decorator(f: Callable) -> Callable: |
|
0 commit comments