cephadm: verify config file exists when inferring it

Fixes: https://tracker.ceph.com/issues/54571

Signed-off-by: Adam King <adking@redhat.com>
This commit is contained in:
Adam King 2022-03-15 16:41:15 -04:00
parent 73f8d0fdcd
commit 1568875a28
2 changed files with 26 additions and 8 deletions

View File

@ -1894,16 +1894,26 @@ def infer_config(func: FuncT) -> FuncT:
if ctx.config:
logger.debug('Using specified config: %s' % ctx.config)
return func(ctx)
def config_path(daemon_type: str, daemon_name: str) -> str:
data_dir = get_data_dir(ctx.fsid, ctx.data_dir, daemon_type, daemon_name)
return os.path.join(data_dir, 'config')
if 'fsid' in ctx and ctx.fsid:
name = ctx.name if 'name' in ctx else None
if not name:
daemon_list = list_daemons(ctx, detail=False)
for daemon in daemon_list:
if daemon.get('name', '').startswith('mon.') and daemon.get('fsid', '') == ctx.fsid:
if (
daemon.get('name', '').startswith('mon.')
and daemon.get('fsid', '') == ctx.fsid
and daemon.get('style', '') == 'cephadm:v1'
and os.path.exists(config_path('mon', daemon['name'].split('.', 1)[1]))
):
name = daemon['name']
break
if name:
ctx.config = f'/var/lib/ceph/{ctx.fsid}/{name}/config'
ctx.config = config_path(name.split('.', 1)[0], name.split('.', 1)[1])
if ctx.config:
logger.info('Inferring config %s' % ctx.config)
elif os.path.exists(SHELL_DEFAULT_CONF):

View File

@ -473,14 +473,21 @@ docker.io/ceph/daemon-base:octopus
'00000000-0000-0000-0000-0000deadbeef',
None,
None,
[{'name': 'mon.a', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}],
[{'name': 'mon.a', 'fsid': '00000000-0000-0000-0000-0000deadbeef', 'style': 'cephadm:v1'}],
'/var/lib/ceph/00000000-0000-0000-0000-0000deadbeef/mon.a/config',
),
(
'00000000-0000-0000-0000-0000deadbeef',
None,
None,
[{'name': 'mon.a', 'fsid': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'}],
[{'name': 'mon.a', 'fsid': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'style': 'cephadm:v1'}],
cd.SHELL_DEFAULT_CONF,
),
(
'00000000-0000-0000-0000-0000deadbeef',
None,
None,
[{'name': 'mon.a', 'fsid': '00000000-0000-0000-0000-0000deadbeef', 'style': 'legacy'}],
cd.SHELL_DEFAULT_CONF,
),
(
@ -494,7 +501,7 @@ docker.io/ceph/daemon-base:octopus
'00000000-0000-0000-0000-0000deadbeef',
'/foo/bar.conf',
'mon.a',
[{'name': 'mon.a'}],
[{'name': 'mon.a', 'style': 'cephadm:v1'}],
'/foo/bar.conf',
),
(
@ -520,7 +527,8 @@ docker.io/ceph/daemon-base:octopus
),
])
@mock.patch('cephadm.call')
def test_infer_config(self, _call, fsid, config, name, list_daemons, result, cephadm_fs):
@mock.patch('cephadm.logger')
def test_infer_config(self, logger, _call, fsid, config, name, list_daemons, result, cephadm_fs):
# build the context
ctx = cd.CephadmContext()
ctx.fsid = fsid
@ -532,8 +540,8 @@ docker.io/ceph/daemon-base:octopus
mock_fn.return_value = 0
infer_config = cd.infer_config(mock_fn)
# mock the shell config
cephadm_fs.create_file(cd.SHELL_DEFAULT_CONF)
# mock the config file
cephadm_fs.create_file(result)
# test
with mock.patch('cephadm.list_daemons', return_value=list_daemons):