Skip to content

Commit 897d7b1

Browse files
commands: enable url and rados openers for sambacc config
When building the initial sambacc configuration, make sure we support passing URL and optionally enable rados support. To support passing urls/uris no changes to the CLI parsing needs to happen but the env var parsing previously assumed a colon-separated list of paths much like the PATH env var. Keep that as a viable option for backwards compatibility but also allow the env var to contain a JSON-formatted list of strings. Example: ``` [0] $ export SAMBACC_CONFIG='["http://localhost:8080/my/config.json", "/my/config.json"]' [0] $ samba-container --identity demo <...> ``` Signed-off-by: John Mulligan <[email protected]>
1 parent defc095 commit 897d7b1

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

sambacc/commands/main.py

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717
#
1818

1919
import argparse
20+
import json
2021
import logging
2122
import os
2223
import time
2324
import typing
2425

2526
from sambacc import config
27+
from sambacc import opener
28+
from sambacc import rados_opener
2629
from sambacc import samba_cmds
30+
from sambacc import url_opener
2731

2832
from . import check # noqa: F401
2933
from . import config as config_cmds
@@ -111,25 +115,39 @@ def from_env(
111115
var: str,
112116
ename: str,
113117
default: typing.Any = None,
114-
vtype: typing.Optional[typing.Callable] = str,
118+
convert_env: typing.Optional[typing.Callable] = None,
119+
convert_value: typing.Optional[typing.Callable] = str,
115120
) -> None:
116121
value = getattr(ns, var, None)
117122
if not value:
118123
value = os.environ.get(ename, "")
119-
if vtype is not None:
120-
value = vtype(value)
124+
if convert_env is not None:
125+
value = convert_env(value)
126+
if convert_value is not None:
127+
value = convert_value(value)
121128
if value:
122129
setattr(ns, var, value)
123130

124131

125-
def split_paths(value):
126-
if not value:
127-
return value
128-
if not isinstance(value, list):
129-
value = [value]
132+
def split_entries(value):
130133
out = []
131-
for v in value:
132-
for part in v.split(":"):
134+
if not isinstance(value, str):
135+
raise ValueError(value)
136+
if not value:
137+
return out
138+
# in order to cleanly allow passing uris as config "paths" we can't
139+
# simply split on colons. Avoid coming up with a hokey custom scheme
140+
# and enter "JSON-mode" if the env var starts and ends with brackets
141+
# hinting it contains a JSON list.
142+
v = value.rstrip(None) # permit trailing whitespace (trailing only!)
143+
if v[0] == "[" and v[-1] == "]":
144+
for item in json.loads(v):
145+
if not isinstance(item, str):
146+
raise ValueError("Variable JSON must be a list of strings")
147+
out.append(item)
148+
else:
149+
# backwards compatibilty mode with `PATH` like syntax
150+
for part in value.split(":"):
133151
out.append(part)
134152
return out
135153

@@ -139,14 +157,16 @@ def env_to_cli(cli: typing.Any) -> None:
139157
cli,
140158
"config",
141159
"SAMBACC_CONFIG",
142-
vtype=split_paths,
160+
convert_env=split_entries,
161+
convert_value=None,
143162
default=DEFAULT_CONFIG,
144163
)
145164
from_env(
146165
cli,
147166
"join_files",
148167
"SAMBACC_JOIN_FILES",
149-
vtype=split_paths,
168+
convert_env=split_entries,
169+
convert_value=None,
150170
)
151171
from_env(cli, "identity", "SAMBA_CONTAINER_ID")
152172
from_env(cli, "username", "JOIN_USERNAME")
@@ -171,8 +191,11 @@ def cli(self) -> argparse.Namespace:
171191
def instance_config(self) -> config.InstanceConfig:
172192
if self._iconfig is None:
173193
cfgs = self.cli.config or []
194+
_opener = opener.FallbackOpener([url_opener.URLOpener()])
174195
self._iconfig = config.read_config_files(
175-
cfgs, require_validation=self.require_validation
196+
cfgs,
197+
require_validation=self.require_validation,
198+
opener=_opener,
176199
).get(self.cli.identity)
177200
return self._iconfig
178201

@@ -196,6 +219,10 @@ def pre_action(cli: typing.Any) -> None:
196219
if cli.samba_command_prefix:
197220
samba_cmds.set_global_prefix([cli.samba_command_prefix])
198221

222+
# should there be an option to force {en,dis}able openers?
223+
# Right now we just always try to enable rados when possible.
224+
rados_opener.enable_rados_url_opener(url_opener.URLOpener)
225+
199226

200227
def enable_logging(cli: typing.Any) -> None:
201228
logger = logging.getLogger()

0 commit comments

Comments
 (0)