From 4814de00ce644fe04490c4f2768f5489f8e8d63e Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Wed, 26 Jul 2023 10:29:57 -0400 Subject: [PATCH] cephadm: convert create_daemon_dirs to use a DaemonIdentity argument Signed-off-by: John Mulligan --- src/cephadm/cephadm.py | 40 ++++++++++---------- src/cephadm/tests/test_cephadm.py | 62 +++++++++++++++---------------- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index c8b84d4a381..d0adaa542fc 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -2137,10 +2137,16 @@ def get_daemon_args(ctx, fsid, daemon_type, daemon_id): return r -def create_daemon_dirs(ctx, fsid, daemon_type, daemon_id, uid, gid, - config=None, keyring=None): - # type: (CephadmContext, str, str, Union[int, str], int, int, Optional[str], Optional[str]) -> None - ident = DaemonIdentity(fsid, daemon_type, daemon_id) +def create_daemon_dirs( + ctx: CephadmContext, + ident: 'DaemonIdentity', + uid: int, + gid: int, + config: Optional[str] = None, + keyring: Optional[str] = None, +) -> None: + # unpack fsid and daemon_type from ident because they're used very frequently + fsid, daemon_type = ident.fsid, ident.daemon_type data_dir = make_data_dir(ctx, ident, uid=uid, gid=gid) if daemon_type in Ceph.daemons: @@ -2214,34 +2220,34 @@ def create_daemon_dirs(ctx, fsid, daemon_type, daemon_id, uid, gid, f.write(content) elif daemon_type == NFSGanesha.daemon_type: - nfs_ganesha = NFSGanesha.init(ctx, fsid, daemon_id) + nfs_ganesha = NFSGanesha.init(ctx, fsid, ident.daemon_id) nfs_ganesha.create_daemon_dirs(data_dir, uid, gid) elif daemon_type == CephIscsi.daemon_type: - ceph_iscsi = CephIscsi.init(ctx, fsid, daemon_id) + ceph_iscsi = CephIscsi.init(ctx, fsid, ident.daemon_id) ceph_iscsi.create_daemon_dirs(data_dir, uid, gid) elif daemon_type == CephNvmeof.daemon_type: - ceph_nvmeof = CephNvmeof.init(ctx, fsid, daemon_id) + ceph_nvmeof = CephNvmeof.init(ctx, fsid, ident.daemon_id) ceph_nvmeof.create_daemon_dirs(data_dir, uid, gid) elif daemon_type == HAproxy.daemon_type: - haproxy = HAproxy.init(ctx, fsid, daemon_id) + haproxy = HAproxy.init(ctx, fsid, ident.daemon_id) haproxy.create_daemon_dirs(data_dir, uid, gid) elif daemon_type == Keepalived.daemon_type: - keepalived = Keepalived.init(ctx, fsid, daemon_id) + keepalived = Keepalived.init(ctx, fsid, ident.daemon_id) keepalived.create_daemon_dirs(data_dir, uid, gid) elif daemon_type == CustomContainer.daemon_type: - cc = CustomContainer.init(ctx, fsid, daemon_id) + cc = CustomContainer.init(ctx, fsid, ident.daemon_id) cc.create_daemon_dirs(data_dir, uid, gid) elif daemon_type == SNMPGateway.daemon_type: - sg = SNMPGateway.init(ctx, fsid, daemon_id) + sg = SNMPGateway.init(ctx, fsid, ident.daemon_id) sg.create_daemon_conf() - _write_custom_conf_files(ctx, daemon_type, str(daemon_id), fsid, uid, gid) + _write_custom_conf_files(ctx, daemon_type, ident.daemon_id, fsid, uid, gid) def _write_custom_conf_files(ctx: CephadmContext, daemon_type: str, daemon_id: str, fsid: str, uid: int, gid: int) -> None: @@ -2848,7 +2854,7 @@ def deploy_daemon( tmp_config = write_tmp(config, uid, gid) # --mkfs - create_daemon_dirs(ctx, fsid, daemon_type, daemon_id, uid, gid) + create_daemon_dirs(ctx, ident, uid, gid) assert ident.daemon_type == 'mon' mon_dir = get_data_dir(ident, ctx.data_dir) log_dir = get_log_dir(fsid, ctx.log_dir) @@ -2876,11 +2882,7 @@ def deploy_daemon( f.write(config) else: # dirs, conf, keyring - create_daemon_dirs( - ctx, - fsid, daemon_type, daemon_id, - uid, gid, - config, keyring) + create_daemon_dirs(ctx, ident, uid, gid, config, keyring) # only write out unit files and start daemon # with systemd if this is not a reconfig @@ -4921,7 +4923,7 @@ def prepare_create_mon( ) -> Tuple[str, str]: logger.info('Creating mon...') ident = DaemonIdentity(fsid, 'mon', mon_id) - create_daemon_dirs(ctx, fsid, 'mon', mon_id, uid, gid) + create_daemon_dirs(ctx, ident, uid, gid) mon_dir = get_data_dir(ident, ctx.data_dir) log_dir = get_log_dir(fsid, ctx.log_dir) out = CephContainer( diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index ddccdb7bc5e..ebf7d42c339 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -1250,14 +1250,14 @@ class TestMonitoring(object): } }) - _cephadm.create_daemon_dirs(ctx, - fsid, - daemon_type, - daemon_id, - uid, - gid, - config=None, - keyring=None) + _cephadm.create_daemon_dirs( + ctx, + _cephadm.DaemonIdentity(fsid, daemon_type, daemon_id), + uid, + gid, + config=None, + keyring=None, + ) prefix = '{data_dir}/{fsid}/{daemon_type}.{daemon_id}'.format( data_dir=ctx.data_dir, @@ -1280,14 +1280,14 @@ class TestMonitoring(object): # assert uid/gid after redeploy new_uid = uid+1 new_gid = gid+1 - _cephadm.create_daemon_dirs(ctx, - fsid, - daemon_type, - daemon_id, - new_uid, - new_gid, - config=None, - keyring=None) + _cephadm.create_daemon_dirs( + ctx, + _cephadm.DaemonIdentity(fsid, daemon_type, daemon_id), + new_uid, + new_gid, + config=None, + keyring=None, + ) for file,content in expected.items(): file = os.path.join(prefix, file) assert os.stat(file).st_uid == new_uid @@ -2279,11 +2279,10 @@ class TestSNMPGateway: _cephadm.get_parm.return_value = self.V2c_config c = _cephadm.get_container(ctx, fsid, 'snmp-gateway', 'daemon_id') - _cephadm.make_data_dir( - ctx, _cephadm.DaemonIdentity(fsid, 'snmp-gateway', 'daemon_id') - ) + ident = _cephadm.DaemonIdentity(fsid, 'snmp-gateway', 'daemon_id') + _cephadm.make_data_dir(ctx, ident) - _cephadm.create_daemon_dirs(ctx, fsid, 'snmp-gateway', 'daemon_id', 0, 0) + _cephadm.create_daemon_dirs(ctx, ident, 0, 0) with open(f'/var/lib/ceph/{fsid}/snmp-gateway.daemon_id/snmp-gateway.conf', 'r') as f: conf = f.read().rstrip() assert conf == 'SNMP_NOTIFIER_COMMUNITY=public' @@ -2311,11 +2310,10 @@ class TestSNMPGateway: _cephadm.get_parm.return_value = self.V3_no_priv_config c = _cephadm.get_container(ctx, fsid, 'snmp-gateway', 'daemon_id') - _cephadm.make_data_dir( - ctx, _cephadm.DaemonIdentity(fsid, 'snmp-gateway', 'daemon_id') - ) + ident = _cephadm.DaemonIdentity(fsid, 'snmp-gateway', 'daemon_id') + _cephadm.make_data_dir(ctx, ident) - _cephadm.create_daemon_dirs(ctx, fsid, 'snmp-gateway', 'daemon_id', 0, 0) + _cephadm.create_daemon_dirs(ctx, ident, 0, 0) with open(f'/var/lib/ceph/{fsid}/snmp-gateway.daemon_id/snmp-gateway.conf', 'r') as f: conf = f.read() assert conf == 'SNMP_NOTIFIER_AUTH_USERNAME=myuser\nSNMP_NOTIFIER_AUTH_PASSWORD=mypassword\n' @@ -2343,11 +2341,10 @@ class TestSNMPGateway: _cephadm.get_parm.return_value = self.V3_priv_config c = _cephadm.get_container(ctx, fsid, 'snmp-gateway', 'daemon_id') - _cephadm.make_data_dir( - ctx, _cephadm.DaemonIdentity(fsid, 'snmp-gateway', 'daemon_id') - ) + ident = _cephadm.DaemonIdentity(fsid, 'snmp-gateway', 'daemon_id') + _cephadm.make_data_dir(ctx, ident) - _cephadm.create_daemon_dirs(ctx, fsid, 'snmp-gateway', 'daemon_id', 0, 0) + _cephadm.create_daemon_dirs(ctx, ident, 0, 0) with open(f'/var/lib/ceph/{fsid}/snmp-gateway.daemon_id/snmp-gateway.conf', 'r') as f: conf = f.read() assert conf == 'SNMP_NOTIFIER_AUTH_USERNAME=myuser\nSNMP_NOTIFIER_AUTH_PASSWORD=mypassword\nSNMP_NOTIFIER_PRIV_PASSWORD=mysecret\n' @@ -2557,7 +2554,8 @@ class TestJaeger: ctx.config_json = json.dumps(self.single_es_node_conf) ctx.fsid = fsid c = _cephadm.get_container(ctx, fsid, 'jaeger-collector', 'daemon_id') - _cephadm.create_daemon_dirs(ctx, fsid, 'jaeger-collector', 'daemon_id', 0, 0) + ident = _cephadm.DaemonIdentity(fsid, 'jaeger-collector', 'daemon_id') + _cephadm.create_daemon_dirs(ctx, ident, 0, 0) _cephadm.deploy_daemon_units( ctx, fsid, @@ -2578,7 +2576,8 @@ class TestJaeger: ctx.config_json = json.dumps(self.multiple_es_nodes_conf) ctx.fsid = fsid c = _cephadm.get_container(ctx, fsid, 'jaeger-collector', 'daemon_id') - _cephadm.create_daemon_dirs(ctx, fsid, 'jaeger-collector', 'daemon_id', 0, 0) + ident = _cephadm.DaemonIdentity(fsid, 'jaeger-collector', 'daemon_id') + _cephadm.create_daemon_dirs(ctx, ident, 0, 0) _cephadm.deploy_daemon_units( ctx, fsid, @@ -2599,7 +2598,8 @@ class TestJaeger: ctx.config_json = json.dumps(self.agent_conf) ctx.fsid = fsid c = _cephadm.get_container(ctx, fsid, 'jaeger-agent', 'daemon_id') - _cephadm.create_daemon_dirs(ctx, fsid, 'jaeger-agent', 'daemon_id', 0, 0) + ident = _cephadm.DaemonIdentity(fsid, 'jaeger-agent', 'daemon_id') + _cephadm.create_daemon_dirs(ctx, ident, 0, 0) _cephadm.deploy_daemon_units( ctx, fsid, -- 2.39.5