From 67abea15b12c1a60a9da2db35e3470d8d8a128f7 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Thu, 8 Jul 2021 11:52:52 +0200 Subject: [PATCH] cephadm: use dashes for container names podman adds the current container name to the /etc/hosts file. Turns out, python's `socket.getfqdn()` differs from `hostname -f`, when we have the container names containing dots in it.: [root@sebastians-laptop /]# cat /etc/hosts 127.0.0.1 localhost ::1 localhost 127.0.1.1 sebastians-laptop foo.bar.baz.com [root@sebastians-laptop /]# hostname -f sebastians-laptop [root@sebastians-laptop /]# python3 -c 'import socket; print(socket.getfqdn())' foo.bar.baz.com Fascinatingly, this doesn't happen when using dashes. Fixes: https://tracker.ceph.com/issues/51590 Signed-off-by: Sebastian Wagner --- src/cephadm/cephadm | 45 ++++++++++++++++++++++++++++--- src/cephadm/tests/fixtures.py | 1 + src/cephadm/tests/test_cephadm.py | 40 +++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index fd70c03b33e..c81e9f5bc4d 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -2717,6 +2717,7 @@ def _write_container_cmd_to_bash(ctx, file_obj, container, comment=None, backgro # unit file, makes it easier to read and grok. file_obj.write('# ' + comment + '\n') # Sometimes, adding `--rm` to a run_cmd doesn't work. Let's remove the container manually + file_obj.write('! ' + ' '.join(container.rm_cmd(old_cname=True)) + ' 2> /dev/null\n') file_obj.write('! ' + ' '.join(container.rm_cmd()) + ' 2> /dev/null\n') # Sometimes, `podman rm` doesn't find the container. Then you'll have to add `--storage` if isinstance(ctx.container_engine, Podman): @@ -2724,6 +2725,10 @@ def _write_container_cmd_to_bash(ctx, file_obj, container, comment=None, backgro '! ' + ' '.join([shlex.quote(a) for a in container.rm_cmd(storage=True)]) + ' 2> /dev/null\n') + file_obj.write( + '! ' + + ' '.join([shlex.quote(a) for a in container.rm_cmd(old_cname=True, storage=True)]) + + ' 2> /dev/null\n') # container run command file_obj.write( @@ -3190,7 +3195,7 @@ class CephContainer: self.entrypoint = entrypoint self.args = args self.volume_mounts = volume_mounts - self.cname = cname + self._cname = cname self.container_args = container_args self.envs = envs self.privileged = privileged @@ -3201,6 +3206,36 @@ class CephContainer: self.memory_request = memory_request self.memory_limit = memory_limit + @property + def cname(self) -> str: + """ + podman adds the current container name to the /etc/hosts + file. Turns out, python's `socket.getfqdn()` differs from + `hostname -f`, when we have the container names containing + dots in it.: + + # podman run --name foo.bar.baz.com ceph/ceph /bin/bash + [root@sebastians-laptop /]# cat /etc/hosts + 127.0.0.1 localhost + ::1 localhost + 127.0.1.1 sebastians-laptop foo.bar.baz.com + [root@sebastians-laptop /]# hostname -f + sebastians-laptop + [root@sebastians-laptop /]# python3 -c 'import socket; print(socket.getfqdn())' + foo.bar.baz.com + + Fascinatingly, this doesn't happen when using dashes. + """ + return self._cname.replace('.', '-') + + @cname.setter + def cname(self, val: str) -> None: + self._cname = val + + @property + def old_cname(self) -> str: + return self._cname + def run_cmd(self) -> List[str]: cmd_args: List[str] = [ str(self.ctx.container_engine.path), @@ -3315,15 +3350,17 @@ class CephContainer: self.cname, ] + cmd - def rm_cmd(self, storage=False): - # type: (bool) -> List[str] + def rm_cmd(self, old_cname: bool = False, storage: bool = False) -> List[str]: ret = [ str(self.ctx.container_engine.path), 'rm', '-f', ] if storage: ret.append('--storage') - ret.append(self.cname) + if old_cname: + ret.append(self.old_cname) + else: + ret.append(self.cname) return ret def stop_cmd(self): diff --git a/src/cephadm/tests/fixtures.py b/src/cephadm/tests/fixtures.py index 971fe0f079a..b8f585c653a 100644 --- a/src/cephadm/tests/fixtures.py +++ b/src/cephadm/tests/fixtures.py @@ -69,6 +69,7 @@ def cephadm_fs( gid = os.getgid() with mock.patch('os.fchown'), \ + mock.patch('os.fchmod'), \ mock.patch('cephadm.extract_uid_gid', return_value=(uid, gid)): fs.create_dir(cd.DATA_DIR) diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index a4ea384708a..ac082030645 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -1593,3 +1593,43 @@ class TestCephVolume(object): with with_cephadm_ctx(cmd) as ctx: cd.command_ceph_volume(ctx) assert ctx.keyring == 'bar' + + +class TestIscsi: + def test_unit_run(self, cephadm_fs): + fsid = '9b9d7609-f4d5-4aba-94c8-effa764d96c9' + config_json = { + 'files': {'iscsi-gateway.cfg': ''} + } + with with_cephadm_ctx(['--image=ceph/ceph'], list_networks={}) as ctx: + import json + ctx.config_json = json.dumps(config_json) + ctx.fsid = fsid + cd.get_parm.return_value = config_json + iscsi = cd.CephIscsi(ctx, '9b9d7609-f4d5-4aba-94c8-effa764d96c9', 'daemon_id', config_json) + c = iscsi.get_tcmu_runner_container() + + cd.make_data_dir(ctx, fsid, 'iscsi', 'daemon_id') + cd.deploy_daemon_units( + ctx, + fsid, + 0, 0, + 'iscsi', + 'daemon_id', + c, + True, True + ) + + with open('/var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/unit.run') as f: + assert f.read() == """set -e +if ! grep -qs /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/configfs /proc/mounts; then mount -t configfs none /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/configfs; fi +# iscsi tcmu-runnter container +! /usr/bin/podman rm -f ceph-9b9d7609-f4d5-4aba-94c8-effa764d96c9-iscsi.daemon_id-tcmu 2> /dev/null +! /usr/bin/podman rm -f ceph-9b9d7609-f4d5-4aba-94c8-effa764d96c9-iscsi-daemon_id-tcmu 2> /dev/null +/usr/bin/podman run --rm --ipc=host --stop-signal=SIGTERM --net=host --entrypoint /usr/bin/tcmu-runner --privileged --group-add=disk --init --name ceph-9b9d7609-f4d5-4aba-94c8-effa764d96c9-iscsi-daemon_id-tcmu -e CONTAINER_IMAGE=ceph/ceph -e NODE_NAME=host1 -e CEPH_USE_RANDOM_NONCE=1 -e TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES=134217728 -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/config:/etc/ceph/ceph.conf:z -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/keyring:/etc/ceph/keyring:z -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/iscsi-gateway.cfg:/etc/ceph/iscsi-gateway.cfg:z -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/configfs:/sys/kernel/config -v /var/log/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9:/var/log/rbd-target-api:z -v /dev:/dev --mount type=bind,source=/lib/modules,destination=/lib/modules,ro=true ceph/ceph & +# iscsi.daemon_id +! /usr/bin/podman rm -f ceph-9b9d7609-f4d5-4aba-94c8-effa764d96c9-iscsi.daemon_id-tcmu 2> /dev/null +! /usr/bin/podman rm -f ceph-9b9d7609-f4d5-4aba-94c8-effa764d96c9-iscsi-daemon_id-tcmu 2> /dev/null +/usr/bin/podman run --rm --ipc=host --stop-signal=SIGTERM --net=host --entrypoint /usr/bin/tcmu-runner --privileged --group-add=disk --init --name ceph-9b9d7609-f4d5-4aba-94c8-effa764d96c9-iscsi-daemon_id-tcmu -e CONTAINER_IMAGE=ceph/ceph -e NODE_NAME=host1 -e CEPH_USE_RANDOM_NONCE=1 -e TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES=134217728 -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/config:/etc/ceph/ceph.conf:z -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/keyring:/etc/ceph/keyring:z -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/iscsi-gateway.cfg:/etc/ceph/iscsi-gateway.cfg:z -v /var/lib/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9/iscsi.daemon_id/configfs:/sys/kernel/config -v /var/log/ceph/9b9d7609-f4d5-4aba-94c8-effa764d96c9:/var/log/rbd-target-api:z -v /dev:/dev --mount type=bind,source=/lib/modules,destination=/lib/modules,ro=true ceph/ceph +""" + -- 2.39.5