Skip to content

Commit 19378b1

Browse files
cephadm: move extract_uid_gid func to container_types module
While extract_uid_gid isn't a perfect fit for container_types it is a fairly fundamental function for working with containers in cephadm and doesn't require anything beyond types in containers_types and that module's existing imports. Moving extract_uid_gid should allow us to more easily move other functions in the future. Signed-off-by: John Mulligan <[email protected]>
1 parent 36ad485 commit 19378b1

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

src/cephadm/cephadm.py

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
CephContainer,
154154
InitContainer,
155155
is_container_running,
156+
extract_uid_gid,
156157
)
157158
from cephadmlib.decorators import (
158159
deprecated_command,
@@ -2828,37 +2829,6 @@ def _update_container_args_for_podman(
28282829
)
28292830

28302831

2831-
def extract_uid_gid(ctx, img='', file_path='/var/lib/ceph'):
2832-
# type: (CephadmContext, str, Union[str, List[str]]) -> Tuple[int, int]
2833-
2834-
if not img:
2835-
img = ctx.image
2836-
2837-
if isinstance(file_path, str):
2838-
paths = [file_path]
2839-
else:
2840-
paths = file_path
2841-
2842-
ex: Optional[Tuple[str, RuntimeError]] = None
2843-
2844-
for fp in paths:
2845-
try:
2846-
out = CephContainer(
2847-
ctx,
2848-
image=img,
2849-
entrypoint='stat',
2850-
args=['-c', '%u %g', fp]
2851-
).run(verbosity=CallVerbosity.QUIET_UNLESS_ERROR)
2852-
uid, gid = out.split(' ')
2853-
return int(uid), int(gid)
2854-
except RuntimeError as e:
2855-
ex = (fp, e)
2856-
if ex:
2857-
raise Error(f'Failed to extract uid/gid for path {ex[0]}: {ex[1]}')
2858-
2859-
raise RuntimeError('uid/gid not found')
2860-
2861-
28622832
def deploy_daemon(
28632833
ctx: CephadmContext,
28642834
ident: 'DaemonIdentity',

src/cephadm/cephadmlib/container_types.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import os
44

5-
from typing import Dict, List, Optional, Any
5+
from typing import Dict, List, Optional, Any, Union, Tuple
66

77
from .call_wrappers import call, call_throws, CallVerbosity
88
from .constants import DEFAULT_TIMEOUT
@@ -485,3 +485,34 @@ def get_running_container_name(
485485
if out.strip() == 'running':
486486
return name
487487
return None
488+
489+
490+
def extract_uid_gid(ctx, img='', file_path='/var/lib/ceph'):
491+
# type: (CephadmContext, str, Union[str, List[str]]) -> Tuple[int, int]
492+
493+
if not img:
494+
img = ctx.image
495+
496+
if isinstance(file_path, str):
497+
paths = [file_path]
498+
else:
499+
paths = file_path
500+
501+
ex: Optional[Tuple[str, RuntimeError]] = None
502+
503+
for fp in paths:
504+
try:
505+
out = CephContainer(
506+
ctx,
507+
image=img,
508+
entrypoint='stat',
509+
args=['-c', '%u %g', fp]
510+
).run(verbosity=CallVerbosity.QUIET_UNLESS_ERROR)
511+
uid, gid = out.split(' ')
512+
return int(uid), int(gid)
513+
except RuntimeError as e:
514+
ex = (fp, e)
515+
if ex:
516+
raise Error(f'Failed to extract uid/gid for path {ex[0]}: {ex[1]}')
517+
518+
raise RuntimeError('uid/gid not found')

src/cephadm/tests/test_ingress.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def test_haproxy_extract_uid_gid_haproxy():
166166
good_haproxy_json(),
167167
SAMPLE_HAPROXY_IMAGE,
168168
)
169-
with mock.patch("cephadm.CephContainer") as cc:
169+
with mock.patch("cephadmlib.container_types.CephContainer") as cc:
170170
cc.return_value.run.return_value = "500 500"
171171
uid, gid = hap.uid_gid(ctx)
172172
cc.return_value.run.assert_called()
@@ -329,7 +329,7 @@ def test_keepalived_extract_uid_gid_keepalived():
329329
good_keepalived_json(),
330330
SAMPLE_KEEPALIVED_IMAGE,
331331
)
332-
with mock.patch("cephadm.CephContainer") as cc:
332+
with mock.patch("cephadmlib.container_types.CephContainer") as cc:
333333
cc.return_value.run.return_value = "500 500"
334334
uid, gid = kad.uid_gid(ctx)
335335
cc.return_value.run.assert_called()

0 commit comments

Comments
 (0)