Skip to content

Commit a0b952b

Browse files
committed
fix: read envs from envvars
Signed-off-by: Aaron Pham <contact@aarnphm.xyz>
1 parent 79f4a96 commit a0b952b

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

src/openllm/__main__.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _select_target(bento: BentoInfo, targets: list[DeploymentTarget]) -> Deploym
120120
return selected
121121

122122

123-
def _select_action(bento: BentoInfo, score: float) -> None:
123+
def _select_action(bento: BentoInfo, score: float, context: typing.Optional[str] = None) -> None:
124124
if score > 0:
125125
options: list[typing.Any] = [
126126
questionary.Separator('Available actions'),
@@ -180,17 +180,27 @@ def _select_action(bento: BentoInfo, score: float) -> None:
180180
output(f' $ openllm serve {bento}', style='orange')
181181
elif action == 'deploy':
182182
ensure_cloud_context()
183-
targets = get_cloud_machine_spec()
183+
targets = get_cloud_machine_spec(context=context)
184184
target = _select_target(bento, targets)
185185
try:
186-
cloud_deploy(bento, target)
186+
cloud_deploy(bento, target, context=context)
187187
finally:
188188
output('\nUse this command to run the action again:', style='green')
189189
output(f' $ openllm deploy {bento} --instance-type {target.name}', style='orange')
190190

191191

192192
@app.command(help='get started interactively')
193-
def hello(repo: typing.Optional[str] = None) -> None:
193+
def hello(
194+
repo: typing.Optional[str] = None,
195+
env: typing.Optional[list[str]] = typer.Option(
196+
None,
197+
'--env',
198+
help='Environment variables to pass to the deployment command. Format: NAME or NAME=value. Can be specified multiple times.',
199+
),
200+
context: typing.Optional[str] = typer.Option(
201+
None, '--context', help='BentoCloud context name to pass to the deployment command.'
202+
),
203+
) -> None:
194204
cmd_update()
195205
INTERACTIVE.set(True)
196206

@@ -211,7 +221,7 @@ def hello(repo: typing.Optional[str] = None) -> None:
211221

212222
bento_name, repo = _select_bento_name(models, target)
213223
bento, score = _select_bento_version(models, target, bento_name, repo)
214-
_select_action(bento, score)
224+
_select_action(bento, score, context=context)
215225

216226

217227
@app.command(help='start an OpenAI API compatible chat server and chat in browser')
@@ -278,15 +288,20 @@ def deploy(
278288
'--env',
279289
help='Environment variables to pass to the deployment command. Format: NAME or NAME=value. Can be specified multiple times.',
280290
),
291+
context: typing.Optional[str] = typer.Option(
292+
None, '--context', help='BentoCloud context name to pass to the deployment command.'
293+
),
281294
) -> None:
282295
cmd_update()
283296
if verbose:
284297
VERBOSE_LEVEL.set(20)
285298
bento = ensure_bento(model, repo_name=repo)
286299
if instance_type is not None:
287-
return cloud_deploy(bento, DeploymentTarget(accelerators=[], name=instance_type), cli_envs=env)
300+
return cloud_deploy(
301+
bento, DeploymentTarget(accelerators=[], name=instance_type), cli_envs=env, context=context
302+
)
288303
targets = sorted(
289-
filter(lambda x: can_run(bento, x) > 0, get_cloud_machine_spec()),
304+
filter(lambda x: can_run(bento, x) > 0, get_cloud_machine_spec(context=context)),
290305
key=lambda x: can_run(bento, x),
291306
reverse=True,
292307
)
@@ -295,7 +310,7 @@ def deploy(
295310
raise typer.Exit(1)
296311
target = targets[0]
297312
output(f'Recommended instance type: {target.name}', style='green')
298-
cloud_deploy(bento, target, cli_envs=env)
313+
cloud_deploy(bento, target, cli_envs=env, context=context)
299314

300315

301316
@app.callback(invoke_without_command=True)

src/openllm/cloud.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def _get_deploy_cmd(
2121
bento: BentoInfo,
2222
target: typing.Optional[DeploymentTarget] = None,
2323
cli_envs: typing.Optional[list[str]] = None,
24+
context: typing.Optional[str] = None,
2425
) -> tuple[list[str], EnvVars]:
2526
cmd = ['bentoml', 'deploy', bento.bentoml_tag]
2627
env = EnvVars({'BENTOML_HOME': f'{bento.repo.path}/bentoml'})
@@ -45,10 +46,15 @@ def _get_deploy_cmd(
4546

4647
# Process envs defined in bento.yaml, skipping those overridden by CLI
4748
required_envs = bento.bento_yaml.get('envs', [])
49+
50+
all_required_env_names = [env['name'] for env in required_envs if 'name' in env]
4851
required_env_names = [
4952
env['name']
5053
for env in required_envs
51-
if 'name' in env and env['name'] not in explicit_envs and not env.get('value')
54+
if 'name' in env
55+
and env['name'] not in explicit_envs
56+
and not env.get('value')
57+
and env['name'] not in os.environ
5258
]
5359
if required_env_names:
5460
output(
@@ -85,13 +91,21 @@ def _get_deploy_cmd(
8591
raise typer.Exit(1)
8692
cmd += ['--env', f'{name}={value}']
8793

94+
# Add any required envs from os.environ that haven't been handled yet
95+
for name in all_required_env_names:
96+
if name in os.environ:
97+
cmd += ['--env', f'{name}={os.environ.get(name)}']
98+
8899
# Add explicitly provided env vars from CLI
89100
for name, value in explicit_envs.items():
90101
cmd += ['--env', f'{name}={value}']
91102

92103
if target:
93104
cmd += ['--instance-type', target.name]
94105

106+
if context:
107+
cmd += ['--context', context]
108+
95109
base_config = resolve_cloud_config()
96110
if not base_config.exists():
97111
raise Exception('Cannot find cloud config.')
@@ -148,9 +162,11 @@ def ensure_cloud_context() -> None:
148162
raise typer.Exit(1)
149163

150164

151-
def get_cloud_machine_spec() -> list[DeploymentTarget]:
165+
def get_cloud_machine_spec(context: typing.Optional[str] = None) -> list[DeploymentTarget]:
152166
ensure_cloud_context()
153167
cmd = ['bentoml', 'deployment', 'list-instance-types', '-o', 'json']
168+
if context:
169+
cmd += ['--context', context]
154170
try:
155171
result = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
156172
instance_types = json.loads(result)
@@ -174,8 +190,11 @@ def get_cloud_machine_spec() -> list[DeploymentTarget]:
174190

175191

176192
def deploy(
177-
bento: BentoInfo, target: DeploymentTarget, cli_envs: typing.Optional[list[str]] = None
193+
bento: BentoInfo,
194+
target: DeploymentTarget,
195+
cli_envs: typing.Optional[list[str]] = None,
196+
context: typing.Optional[str] = None,
178197
) -> None:
179198
ensure_cloud_context()
180-
cmd, env = _get_deploy_cmd(bento, target, cli_envs=cli_envs)
199+
cmd, env = _get_deploy_cmd(bento, target, cli_envs=cli_envs, context=context)
181200
run_command(cmd, env=env, cwd=None)

0 commit comments

Comments
 (0)