Skip to content

Commit

Permalink
fix: Let client cmds create their own ctx from root cli ctx info (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregataa committed Jun 15, 2022
1 parent e0b0fcd commit d5dff65
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 54 deletions.
1 change: 1 addition & 0 deletions changes/457.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cli root passes ctx info and client cmds can create ctx from it.
11 changes: 9 additions & 2 deletions src/ai/backend/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click

from .extensions import ExtendedCommandGroup
from .types import CliContextInfo


@click.group(
Expand All @@ -9,6 +10,12 @@
'help_option_names': ['-h', '--help'],
},
)
def main() -> click.Group:
@click.option('--skip-sslcert-validation',
help='<Client option> Skip SSL certificate validation for all API requests.',
is_flag=True)
@click.option('--output', type=click.Choice(['json', 'console']), default='console',
help='<Client option> Set the output style of the command results.')
@click.pass_context
def main(ctx: click.Context, **kwargs) -> None:
'''Unified Command Line Interface for Backend.ai'''
pass
ctx.obj = CliContextInfo(info=kwargs)
7 changes: 7 additions & 0 deletions src/ai/backend/cli/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from typing import Dict
import attr


@attr.define(slots=True)
class CliContextInfo:
info: Dict = attr.field()
1 change: 1 addition & 0 deletions src/ai/backend/client/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from . import server_log # noqa
from . import admin # noqa
from . import app, logs, proxy, run # noqa
from . import extensions # noqa
13 changes: 7 additions & 6 deletions src/ai/backend/client/cli/admin/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ai.backend.client.output.fields import agent_fields
from ..types import CLIContext
from . import admin
from ..extensions import pass_ctx_obj


@admin.group()
Expand All @@ -18,7 +19,7 @@ def agent():


@agent.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('agent_id')
def info(ctx: CLIContext, agent_id: str) -> None:
"""
Expand Down Expand Up @@ -46,7 +47,7 @@ def info(ctx: CLIContext, agent_id: str) -> None:


@agent.command()
@click.pass_obj
@pass_ctx_obj
@click.option('-s', '--status', type=str, default='ALIVE',
help='Filter agents by the given status.')
@click.option('--scaling-group', '--sgroup', type=str, default=None,
Expand Down Expand Up @@ -115,7 +116,7 @@ def watcher():


@watcher.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('agent', type=str)
def status(ctx: CLIContext, agent: str) -> None:
"""
Expand All @@ -135,7 +136,7 @@ def status(ctx: CLIContext, agent: str) -> None:


@watcher.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('agent', type=str)
def agent_start(ctx: CLIContext, agent: str) -> None:
"""
Expand All @@ -155,7 +156,7 @@ def agent_start(ctx: CLIContext, agent: str) -> None:


@watcher.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('agent', type=str)
def agent_stop(ctx: CLIContext, agent: str) -> None:
"""
Expand All @@ -175,7 +176,7 @@ def agent_stop(ctx: CLIContext, agent: str) -> None:


@watcher.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('agent', type=str)
def agent_restart(ctx: CLIContext, agent: str) -> None:
"""
Expand Down
13 changes: 7 additions & 6 deletions src/ai/backend/client/cli/admin/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..pretty import print_info

from ..types import CLIContext
from ..extensions import pass_ctx_obj


@admin.group()
Expand All @@ -23,7 +24,7 @@ def domain():


@domain.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str)
def info(ctx: CLIContext, name: str) -> None:
"""
Expand All @@ -40,7 +41,7 @@ def info(ctx: CLIContext, name: str) -> None:


@domain.command()
@click.pass_obj
@pass_ctx_obj
def list(ctx: CLIContext) -> None:
"""
List and manage domains.
Expand All @@ -56,7 +57,7 @@ def list(ctx: CLIContext) -> None:


@domain.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str, metavar='NAME')
@click.option('-d', '--description', type=str, default='',
help='Description of new domain')
Expand Down Expand Up @@ -106,7 +107,7 @@ def add(ctx: CLIContext, name, description, inactive, total_resource_slots,


@domain.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str, metavar='NAME')
@click.option('--new-name', type=str, help='New name of the domain')
@click.option('--description', type=str, help='Description of the domain')
Expand Down Expand Up @@ -158,7 +159,7 @@ def update(ctx: CLIContext, name, new_name, description, is_active, total_resour


@domain.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str, metavar='NAME')
def delete(ctx: CLIContext, name):
"""
Expand Down Expand Up @@ -192,7 +193,7 @@ def delete(ctx: CLIContext, name):


@domain.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str, metavar='NAME')
def purge(ctx: CLIContext, name):
"""
Expand Down
17 changes: 9 additions & 8 deletions src/ai/backend/client/cli/admin/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from ..pretty import print_info

from ..types import CLIContext
from ..extensions import pass_ctx_obj


@admin.group()
Expand All @@ -24,7 +25,7 @@ def group() -> None:


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('id_or_name', type=str)
def info(ctx: CLIContext, id_or_name: str) -> None:
"""
Expand Down Expand Up @@ -62,7 +63,7 @@ def info(ctx: CLIContext, id_or_name: str) -> None:


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.option('-d', '--domain-name', type=str, default=None,
help='Domain name to list groups belongs to it.')
def list(ctx: CLIContext, domain_name) -> None:
Expand All @@ -80,7 +81,7 @@ def list(ctx: CLIContext, domain_name) -> None:


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('domain_name', type=str, metavar='DOMAIN_NAME')
@click.argument('name', type=str, metavar='NAME')
@click.option('-d', '--description', type=str, default='',
Expand Down Expand Up @@ -130,7 +131,7 @@ def add(ctx: CLIContext, domain_name, name, description, inactive, total_resourc


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('gid', type=str, metavar='GROUP_ID')
@click.option('-n', '--name', type=str, help='New name of the group')
@click.option('-d', '--description', type=str, help='Description of the group')
Expand Down Expand Up @@ -178,7 +179,7 @@ def update(ctx: CLIContext, gid, name, description, is_active, total_resource_sl


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('gid', type=str, metavar='GROUP_ID')
def delete(ctx: CLIContext, gid):
"""
Expand Down Expand Up @@ -212,7 +213,7 @@ def delete(ctx: CLIContext, gid):


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('gid', type=str, metavar='GROUP_ID')
def purge(ctx: CLIContext, gid):
"""
Expand Down Expand Up @@ -249,7 +250,7 @@ def purge(ctx: CLIContext, gid):


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('gid', type=str, metavar='GROUP_ID')
@click.argument('user_uuids', type=str, metavar='USER_UUIDS', nargs=-1)
def add_users(ctx: CLIContext, gid, user_uuids):
Expand Down Expand Up @@ -286,7 +287,7 @@ def add_users(ctx: CLIContext, gid, user_uuids):


@group.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('gid', type=str, metavar='GROUP_ID')
@click.argument('user_uuids', type=str, metavar='USER_UUIDS', nargs=-1)
def remove_users(ctx: CLIContext, gid, user_uuids):
Expand Down
3 changes: 2 additions & 1 deletion src/ai/backend/client/cli/admin/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ..pretty import print_done, print_warn, print_fail, print_error

from ..types import CLIContext
from ..extensions import pass_ctx_obj


@admin.group()
Expand All @@ -25,7 +26,7 @@ def image() -> None:


@image.command()
@click.pass_obj
@pass_ctx_obj
@click.option('--operation', is_flag=True, help='Get operational images only')
def list(ctx: CLIContext, operation: bool) -> None:
"""
Expand Down
15 changes: 8 additions & 7 deletions src/ai/backend/client/cli/admin/keypair.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ai.backend.client.output.fields import keypair_fields
from . import admin
from ..types import CLIContext
from ..extensions import pass_ctx_obj


@admin.group()
Expand All @@ -16,7 +17,7 @@ def keypair() -> None:


@keypair.command()
@click.pass_obj
@pass_ctx_obj
def info(ctx: CLIContext) -> None:
"""
Show the server-side information of the currently configured access key.
Expand Down Expand Up @@ -45,7 +46,7 @@ def info(ctx: CLIContext) -> None:


@keypair.command()
@click.pass_obj
@pass_ctx_obj
@click.option('-u', '--user-id', type=str, default=None,
help='Show keypairs of this given user. [default: show all]')
@click.option('--is-active', type=bool, default=None,
Expand Down Expand Up @@ -100,7 +101,7 @@ def list(ctx: CLIContext, user_id, is_active, filter_, order, offset, limit) ->


@keypair.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('user-id', type=str, default=None, metavar='USERID')
@click.argument('resource-policy', type=str, default=None, metavar='RESOURCE_POLICY')
@click.option('-a', '--admin', is_flag=True,
Expand Down Expand Up @@ -149,7 +150,7 @@ def add(ctx: CLIContext, user_id, resource_policy, admin, inactive, rate_limit)


@keypair.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('access_key', type=str, default=None, metavar='ACCESSKEY')
@click.option('--resource-policy', type=str, help='Resource policy for the keypair.')
@click.option('--is-admin', type=bool, help='Set admin privilege.')
Expand Down Expand Up @@ -192,7 +193,7 @@ def update(ctx: CLIContext, access_key, resource_policy, is_admin, is_active, r


@keypair.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('access-key', type=str, metavar='ACCESSKEY')
def delete(ctx: CLIContext, access_key):
"""
Expand Down Expand Up @@ -226,7 +227,7 @@ def delete(ctx: CLIContext, access_key):


@keypair.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('access-key', type=str, metavar='ACCESSKEY')
def activate(ctx: CLIContext, access_key):
"""
Expand Down Expand Up @@ -260,7 +261,7 @@ def activate(ctx: CLIContext, access_key):


@keypair.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('access-key', type=str, metavar='ACCESSKEY')
def deactivate(ctx: CLIContext, access_key):
"""
Expand Down
11 changes: 6 additions & 5 deletions src/ai/backend/client/cli/admin/resource_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ..pretty import print_info

from ..types import CLIContext
from ..extensions import pass_ctx_obj


@admin.group()
Expand All @@ -23,7 +24,7 @@ def keypair_resource_policy() -> None:


@keypair_resource_policy.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str)
def info(ctx: CLIContext, name: str) -> None:
"""
Expand All @@ -41,7 +42,7 @@ def info(ctx: CLIContext, name: str) -> None:


@keypair_resource_policy.command()
@click.pass_obj
@pass_ctx_obj
def list(ctx):
"""
List and manage keypair resource policies.
Expand All @@ -57,7 +58,7 @@ def list(ctx):


@keypair_resource_policy.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str, default=None, metavar='NAME')
@click.option('--default-for-unspecified', type=str, default='UNLIMITED',
help='Default behavior for unspecified resources: '
Expand Down Expand Up @@ -121,7 +122,7 @@ def add(ctx: CLIContext, name, default_for_unspecified, total_resource_slots, ma


@keypair_resource_policy.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str, default=None, metavar='NAME')
@click.option('--default-for-unspecified', type=str,
help='Default behavior for unspecified resources: '
Expand Down Expand Up @@ -184,7 +185,7 @@ def update(ctx: CLIContext, name, default_for_unspecified, total_resource_slots,


@keypair_resource_policy.command()
@click.pass_obj
@pass_ctx_obj
@click.argument('name', type=str, default=None, metavar='NAME')
def delete(ctx: CLIContext, name):
"""
Expand Down

0 comments on commit d5dff65

Please sign in to comment.