summaryrefslogtreecommitdiffstats
path: root/src/cephadm/box/util.py
blob: 7dcf883f8a377c55ea375c34e2fcc0098f6bd0e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
55
56
57
58
59
60
61
62
63
64
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
100
101
102
103
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import json
import os
import subprocess
import sys
import copy
from abc import ABCMeta, abstractmethod
from enum import Enum
from typing import Any, Callable, Dict, List

class Colors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

class Config:
    args = {
        'fsid': '00000000-0000-0000-0000-0000deadbeef',
        'config_folder': '/etc/ceph/',
        'config': '/etc/ceph/ceph.conf',
        'keyring': '/etc/ceph/ceph.keyring',
        'loop_img': 'loop-images/loop.img',
        'engine': 'podman',
        'docker_yaml': 'docker-compose-docker.yml',
        'docker_v1_yaml': 'docker-compose.cgroup1.yml',
        'podman_yaml': 'docker-compose-podman.yml',
        'loop_img_dir': 'loop-images',
    }

    @staticmethod
    def set(key, value):
        Config.args[key] = value

    @staticmethod
    def get(key):
        if key in Config.args:
            return Config.args[key]
        return None

    @staticmethod
    def add_args(args: Dict[str, str]) -> None:
        Config.args.update(args)

class Target:
    def __init__(self, argv, subparsers):
        self.argv = argv
        self.parser = subparsers.add_parser(
            self.__class__.__name__.lower(), help=self.__class__._help
        )

    def set_args(self):
        """
        adding the required arguments of the target should go here, example:
        self.parser.add_argument(..)
        """
        raise NotImplementedError()

    def main(self):
        """
        A target will be setup by first calling this main function
        where the parser is initialized.
        """
        args = self.parser.parse_args(self.argv)
        Config.add_args(vars(args))
        function = getattr(self, args.action)
        function()


def ensure_outside_container(func) -> Callable:
    def wrapper(*args, **kwargs):
        if not inside_container():
            return func(*args, **kwargs)
        else:
            raise RuntimeError('This command should be ran outside a container')

    return wrapper


def ensure_inside_container(func) -> bool:
    def wrapper(*args, **kwargs):
        if inside_container():
            return func(*args, **kwargs)
        else:
            raise RuntimeError('This command should be ran inside a container')

    return wrapper


def colored(msg, color: Colors):
    return color + msg + Colors.ENDC

class BoxType(str, Enum):
  SEED = 'seed'
  HOST = 'host'

class HostContainer:
    def __init__(self, _name, _type) -> None:
        self._name: str = _name
        self._type: BoxType = _type

    @property
    def name(self) -> str:
        return self._name

    @property
    def type(self) -> BoxType:
        return self._type
    def __str__(self) -> str:
        return f'{self.name} {self.type}'

def run_shell_command(command: str, expect_error=False, verbose=True, expect_exit_code=0) -> str:
    if Config.get('verbose'):
        print(f'{colored("Running command", Colors.HEADER)}: {colored(command, Colors.OKBLUE)}')

    process = subprocess.Popen(
        command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )

    out = ''
    err = ''
    # let's read when output comes so it is in real time
    while True:
        # TODO: improve performance of this part, I think this part is a problem
        pout = process.stdout.read(1).decode('latin1')
        if pout == '' and process.poll() is not None:
            break
        if pout:
            if Config.get('verbose') and verbose:
                sys.stdout.write(pout)
                sys.stdout.flush()
            out += pout

    process.wait()

    err += process.stderr.read().decode('latin1').strip()
    out = out.strip()

    if process.returncode != 0 and not expect_error and process.returncode != expect_exit_code:
        err = colored(err, Colors.FAIL);
        
        raise RuntimeError(f'Failed command: {command}\n{err}\nexit code: {process.returncode}')
        sys.exit(1)
    return out


def run_dc_shell_commands(commands: str, container: HostContainer, expect_error=False) -> str:
    for command in commands.split('\n'):
        command = command.strip()
        if not command:
            continue
        run_dc_shell_command(command.strip(), container, expect_error=expect_error)

def run_shell_commands(commands: str, expect_error=False) -> str:
    for command in commands.split('\n'):
        command = command.strip()
        if not command:
            continue
        run_shell_command(command, expect_error=expect_error)

@ensure_inside_container
def run_cephadm_shell_command(command: str, expect_error=False) -> str:
    config = Config.get('config')
    keyring = Config.get('keyring')
    fsid = Config.get('fsid')

    with_cephadm_image = 'CEPHADM_IMAGE=quay.ceph.io/ceph-ci/ceph:main'
    out = run_shell_command(
        f'{with_cephadm_image} cephadm --verbose shell --fsid {fsid} --config {config} --keyring {keyring} -- {command}',
        expect_error,
    )
    return out


def run_dc_shell_command(
        command: str, container: HostContainer, expect_error=False
) -> str:
    out = get_container_engine().run_exec(container, command, expect_error=expect_error)
    return out

def inside_container() -> bool:
    return os.path.exists('/.box_container')

def get_container_id(container_name: str):
    return run_shell_command(f"{engine()} ps | \grep " + container_name + " | awk '{ print $1 }'")

def engine():
    return Config.get('engine')

def engine_compose():
    return f'{engine()}-compose'

def get_seed_name():
    if engine() == 'docker':
        return 'seed'
    elif engine() == 'podman':
        return 'box_hosts_0'
    else:
        print(f'unkown engine {engine()}')
        sys.exit(1)


@ensure_outside_container
def get_boxes_container_info(with_seed: bool = False) -> Dict[str, Any]:
    # NOTE: this could be cached
    ips_query = engine() + " inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} %tab% {{.Name}} %tab% {{.Config.Hostname}}' $("+ engine() + " ps -aq) --format json"
    containers = json.loads(run_shell_command(ips_query, verbose=False))
    # FIXME: if things get more complex a class representing a container info might be useful,
    # for now representing data this way is faster.
    info = {'size': 0, 'ips': [], 'container_names': [], 'hostnames': []}
    for container in containers:
        # Most commands use hosts only
        name = container['Name']
        if name.startswith('box_hosts'):
            if not with_seed and name == get_seed_name():
                continue
            info['size'] += 1
            print(container['NetworkSettings'])
            if 'Networks' in container['NetworkSettings']:
                info['ips'].append(container['NetworkSettings']['Networks']['box_network']['IPAddress'])
            else:
                info['ips'].append('n/a')
            info['container_names'].append(name)
            info['hostnames'].append(container['Config']['Hostname'])
    return info


def get_orch_hosts():
    if inside_container():
        orch_host_ls_out = run_cephadm_shell_command('ceph orch host ls --format json')
    else:
        orch_host_ls_out = run_dc_shell_command(f'cephadm shell --keyring /etc/ceph/ceph.keyring --config /etc/ceph/ceph.conf -- ceph orch host ls --format json', 
                                                get_container_engine().get_seed())
        sp = orch_host_ls_out.split('\n')
        orch_host_ls_out  = sp[len(sp) - 1]
    hosts = json.loads(orch_host_ls_out)
    return hosts


class ContainerEngine(metaclass=ABCMeta):
    @property
    @abstractmethod
    def command(self) -> str: pass

    @property
    @abstractmethod
    def seed_name(self) -> str: pass

    @property
    @abstractmethod
    def dockerfile(self) -> str: pass

    @property
    def host_name_prefix(self) -> str: 
        return 'box_hosts_'

    @abstractmethod
    def up(self, hosts: int): pass

    def run_exec(self, container: HostContainer, command: str, expect_error: bool = False):
        return run_shell_command(' '.join([self.command, 'exec', container.name, command]), 
                                 expect_error=expect_error) 

    def run(self, engine_command: str, expect_error: bool = False):
        return run_shell_command(' '.join([self.command, engine_command]), expect_error=expect_error) 

    def get_containers(self) -> List[HostContainer]:
        ps_out = json.loads(run_shell_command('podman ps --format json'))
        containers = [] 
        for container in ps_out:
            if not container['Names']:
                raise RuntimeError(f'Container {container} missing name')
            name = container['Names'][0]
            if name == self.seed_name:
                containers.append(HostContainer(name, BoxType.SEED))
            elif name.startswith(self.host_name_prefix):
                containers.append(HostContainer(name, BoxType.HOST))
        return containers

    def get_seed(self) -> HostContainer:
        for container in self.get_containers():
            if container.type == BoxType.SEED:
                return container
        raise RuntimeError('Missing seed container')

    def get_container(self, container_name: str):
        containers = self.get_containers()
        for container in containers:
            if container.name == container_name:
                return container
        return None


    def restart(self):
        pass


class DockerEngine(ContainerEngine):
    command = 'docker'
    seed_name = 'seed'
    dockerfile = 'DockerfileDocker'

    def restart(self):
        run_shell_command('systemctl restart docker')

    def up(self, hosts: int):
        dcflags = f'-f {Config.get("docker_yaml")}'
        if not os.path.exists('/sys/fs/cgroup/cgroup.controllers'):
            dcflags += f' -f {Config.get("docker_v1_yaml")}'
        run_shell_command(f'{engine_compose()} {dcflags} up --scale hosts={hosts} -d')

class PodmanEngine(ContainerEngine):
    command = 'podman'
    seed_name = 'box_hosts_0'
    dockerfile = 'DockerfilePodman'

    CAPS = [
            "SYS_ADMIN",
            "NET_ADMIN",
            "SYS_TIME",
            "SYS_RAWIO",
            "MKNOD",
            "NET_RAW",
            "SETUID",
            "SETGID",
            "CHOWN",
            "SYS_PTRACE",
            "SYS_TTY_CONFIG",
            "CAP_AUDIT_WRITE",
            "CAP_AUDIT_CONTROL",
            ]

    VOLUMES = [
                '../../../:/ceph:z',
                '../:/cephadm:z',
                '/run/udev:/run/udev',
                '/sys/dev/block:/sys/dev/block',
                '/sys/fs/cgroup:/sys/fs/cgroup:ro',
                '/dev/fuse:/dev/fuse',
                '/dev/disk:/dev/disk',
                '/sys/devices/virtual/block:/sys/devices/virtual/block',
                '/sys/block:/dev/block',
                '/dev/mapper:/dev/mapper',
                '/dev/mapper/control:/dev/mapper/control',
            ]

    TMPFS = ['/run', '/tmp']

    # FIXME: right now we are assuming every service will be exposed through the seed, but this is far
    # from the truth. Services can be deployed on different hosts so we need a system to manage this.
    SEED_PORTS = [
            8443, # dashboard
            3000, # grafana
            9093, # alertmanager
            9095  # prometheus
            ]


    def setup_podman_env(self, hosts: int = 1, osd_devs={}):
        network_name = 'box_network'
        networks = run_shell_command('podman network ls')
        if network_name not in networks:
            run_shell_command(f'podman network create -d bridge {network_name}')

        args = [
                '--group-add', 'keep-groups', 
                '--device', '/dev/fuse' ,
                '-it' ,
                '-d',
                '-e', 'CEPH_BRANCH=main',
                '--stop-signal', 'RTMIN+3'
                ]

        for cap in self.CAPS:
            args.append('--cap-add')
            args.append(cap)

        for volume in self.VOLUMES:
            args.append('-v')
            args.append(volume)

        for tmp in self.TMPFS:
            args.append('--tmpfs')
            args.append(tmp)


        for osd_dev in osd_devs.values():
            device = osd_dev["device"]
            args.append('--device')
            args.append(f'{device}:{device}')


        for host in range(hosts+1): # 0 will be the seed
            options = copy.copy(args)
            options.append('--name')
            options.append(f'box_hosts_{host}')
            options.append('--network')
            options.append(f'{network_name}')
            if host == 0:
                for port in self.SEED_PORTS:
                    options.append('-p')
                    options.append(f'{port}:{port}')

            options.append('cephadm-box')
            options = ' '.join(options)

            run_shell_command(f'podman run {options}')

    def up(self, hosts: int):
        import osd
        self.setup_podman_env(hosts=hosts, osd_devs=osd.load_osd_devices())

def get_container_engine() -> ContainerEngine:
    if engine() == 'docker':
        return DockerEngine()
    else:
        return PodmanEngine()