Skip to content

Commit 9fff6da

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
commands: add setting ceph id info via --ceph-id or SAMBACC_CEPH_ID env
Add support for passing ceph user/id information via a --ceph-id cli argument or via the SAMBACC_CEPH_ID environment variable. Both can take an unadorned name like `client.bob` or it can take a more precise key-value par format like `name=client.bob` or `rados_id=bob`. The former is OK for testing but the latter is the intended mode of operation for orchestrated containers. Signed-off-by: John Mulligan <[email protected]>
1 parent 8a04f02 commit 9fff6da

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

sambacc/commands/main.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,63 @@ def global_args(parser: Parser) -> None:
108108
choices=("auto", "required", "true", "false"),
109109
help=("Perform schema based validation of configuration."),
110110
)
111+
parser.add_argument(
112+
"--ceph-id",
113+
type=_ceph_id,
114+
help=(
115+
"Specify a user/client ID to ceph libraries"
116+
"(can also be set in the environment by SAMBACC_CEPH_ID."
117+
" Ignored if Ceph RADOS libraries are not present or unused."
118+
" Pass `?` for more details)."
119+
),
120+
)
121+
122+
123+
def _ceph_id(
124+
value: typing.Union[str, dict[str, typing.Any]]
125+
) -> dict[str, typing.Any]:
126+
if not isinstance(value, str):
127+
return value
128+
if value == "?":
129+
# A hack to avoid putting tons of ceph specific info in the normal
130+
# help output. There's probably a better way to do this but it
131+
# gets the job done for now.
132+
raise argparse.ArgumentTypeError(
133+
"requested help:"
134+
" Specify names in the form"
135+
" --ceph-id=[key=value][,key=value][,...]."
136+
' Valid keys include "name" to set the exact name and "rados_id"'
137+
' to specify a name that lacks the "client." prefix (that will'
138+
"automatically get added)."
139+
" Alternatively, specify just the name to allow the system to"
140+
" guess if the name is prefixed already or not."
141+
)
142+
result: dict[str, typing.Any] = {}
143+
# complex mode
144+
if "=" in value:
145+
for part in value.split(","):
146+
if "=" not in part:
147+
raise argparse.ArgumentTypeError(
148+
f"unexpected value for ceph-id: {value!r}"
149+
)
150+
key, val = part.split("=", 1)
151+
if key == "name":
152+
result["client_name"] = val
153+
result["full_name"] = True
154+
elif key == "rados_id":
155+
result["client_name"] = val
156+
result["full_name"] = False
157+
else:
158+
b = f"unexpected key {key!r} in value for ceph-id: {value!r}"
159+
raise argparse.ArgumentTypeError(b)
160+
else:
161+
# this shorthand is meant mainly for lazy humans (me) when running test
162+
# images manually. The key-value form above is meant for automation.
163+
result["client_name"] = value
164+
# assume that if the name starts with client. it's the full name and
165+
# avoid having the ceph library double up an create client.client.x.
166+
result["full_name"] = value.startswith("client.")
167+
return result
111168

112169

113170
def from_env(
@@ -173,6 +230,7 @@ def env_to_cli(cli: typing.Any) -> None:
173230
from_env(cli, "password", "INSECURE_JOIN_PASSWORD")
174231
from_env(cli, "samba_debug_level", "SAMBA_DEBUG_LEVEL")
175232
from_env(cli, "validate_config", "SAMBACC_VALIDATE_CONFIG")
233+
from_env(cli, "ceph_id", "SAMBACC_CEPH_ID", convert_value=_ceph_id)
176234

177235

178236
class CommandContext:
@@ -227,7 +285,11 @@ def pre_action(cli: typing.Any) -> None:
227285

228286
# should there be an option to force {en,dis}able openers?
229287
# Right now we just always try to enable rados when possible.
230-
rados_opener.enable_rados_url_opener(url_opener.URLOpener)
288+
rados_opener.enable_rados_url_opener(
289+
url_opener.URLOpener,
290+
client_name=cli.ceph_id.get("client_name", ""),
291+
full_name=cli.ceph_id.get("full_name", False),
292+
)
231293

232294

233295
def enable_logging(cli: typing.Any) -> None:

0 commit comments

Comments
 (0)