Skip to content

Commit

Permalink
refactor: use parsers for resources (#161)
Browse files Browse the repository at this point in the history
* refactor: use parsers for resources

* refactor: correctly set positional args

* fix: remove resource parser

* refactor: change help messages

* chore: fix typos

* fix: normalize called twice

* chore: change help in get resource

* fix: set default timeout
  • Loading branch information
npitsillos committed Jul 13, 2023
1 parent 8e9788b commit 4428ce0
Show file tree
Hide file tree
Showing 20 changed files with 408 additions and 687 deletions.
6 changes: 4 additions & 2 deletions jcloud/__main__.py
Expand Up @@ -5,7 +5,6 @@ def main():
from .parsers import get_main_parser

args = get_main_parser().parse_args()

if args.loglevel:
os.environ['JCLOUD_LOGLEVEL'] = args.loglevel

Expand All @@ -18,7 +17,10 @@ def main():
is_latest_version()
from jcloud import api

getattr(api, args.jc_cli.replace('-', '_'))(args)
if hasattr(args, 'subcommand'):
getattr(api, args.subcommand.replace('-', '_'))(args)
else:
getattr(api, args.jc_cli.replace('-', '_'))(args)
except KeyboardInterrupt:
pass

Expand Down
28 changes: 14 additions & 14 deletions jcloud/api.py
Expand Up @@ -226,22 +226,22 @@ def display_resources(resource_type: str, resources: List[Dict]):
async def list(args):
from rich import print

if Resources.Flow in args.resource:
if Resources.Flow in args.jc_cli:
await _list_by_phase(args.phase, args.name, args.labels)
else:
resources = await CloudFlow(flow_id=args.flow).list_resources(args.resource)
resources = await CloudFlow(flow_id=args.flow).list_resources(args.jc_cli)
print(
f'[bold]Listing {args.resource.title()}s for flow [green]{args.flow}[/green]'
f'[bold]Listing {args.jc_cli.title()}s for flow [green]{args.flow}[/green]'
)
display_resources(args.resource, resources)
display_resources(args.jc_cli, resources)


@asyncify
async def remove(args):
from rich import print
from rich.prompt import Confirm

if Resources.Flow in args.resource:
if Resources.Flow in args.jc_cli:
if args.phase is not None:
_raw_list = await _list_by_phase(args.phase, '', None)
flow_id_list = [flow['id'] for flow in _raw_list['flows']]
Expand Down Expand Up @@ -301,9 +301,9 @@ async def remove(args):

await _remove_multi(flow_id_list, args.phase)
else:
await CloudFlow(flow_id=args.flow).delete_resource(args.resource, args.name)
await CloudFlow(flow_id=args.flow).delete_resource(args.jc_cli, args.name)

print(f'Successfully removed {args.resource} with name {args.name}')
print(f'Successfully removed {args.jc_cli} with name {args.name}')


async def _remove_multi(flow_id_list, phase):
Expand Down Expand Up @@ -379,15 +379,15 @@ def new(args):
async def update(args):
from rich import print

if Resources.Flow in args.resource:
if Resources.Flow in args.jc_cli:
print(f'Updating Flow: [green]{args.flow}[/green]')
await CloudFlow(flow_id=args.flow, path=args.path).update()
else:
await CloudFlow(flow_id=args.flow, path=args.path).update_secret(
args.name, args.from_literal, args.update
)
print(
f'Succesfully updated Secret [green]{args.name}[/green]. Flow {args.flow} is restarting.'
f'Successfully updated Secret [green]{args.name}[/green]. Flow {args.flow} is restarting.'
)


Expand Down Expand Up @@ -464,7 +464,7 @@ async def logs(args):
show_lines=True,
)
console = Console()
if Resources.Flow in args.resource:
if Resources.Flow in args.jc_cli:
name = 'gateway' if args.gateway else f'executor {args.executor}'
print(f'Fetching the logs for {name} of the Flow: [green]{args.flow}[/green]')

Expand Down Expand Up @@ -520,18 +520,18 @@ async def logs(args):
async def create(args):
from rich import print

if Resources.Job in args.resource:
if Resources.Job in args.jc_cli:
await CloudFlow(flow_id=args.flow).create_job(
args.name, args.image, args.timeout, args.backofflimit, args.entrypoint
)
else:
await CloudFlow(flow_id=args.flow, path=args.path).create_secret(
args.name, args.from_literal, args.update
)
print(f'Succesfully created {args.resource} [green]{args.name}[/green].')
print(f'Successfully created {args.jc_cli} [green]{args.name}[/green].')


@asyncify
async def get(args):
resource = await CloudFlow(flow_id=args.flow).get_resource(args.resource, args.name)
display_resources(args.resource, [resource])
resource = await CloudFlow(flow_id=args.flow).get_resource(args.jc_cli, args.name)
display_resources(args.jc_cli, [resource])
10 changes: 6 additions & 4 deletions jcloud/flow.py
Expand Up @@ -105,7 +105,7 @@ def id(self) -> str:
def _loop(self):
return get_or_reuse_loop()

async def _get_post_params(self):
async def _get_post_params(self, from_validate: Optional[bool] = False):
from jcloud.normalize import flow_normalize

params, _post_kwargs = {}, {}
Expand All @@ -117,7 +117,9 @@ async def _get_post_params(self):

validate_flow_yaml_exists(_flow_path)
if not normalized(_flow_path):
_flow_path = flow_normalize(_flow_path)
_flow_path = flow_normalize(
_flow_path, output_path=_flow_path if from_validate else None
)
_data.add_field(name='spec', value=open(_flow_path))
else:
_flow_dict = load_flow_data(
Expand All @@ -138,7 +140,7 @@ async def validate(self):
async with session.post(
url=FLOWS_API + '/validate',
headers=self.auth_header,
**await self._get_post_params(),
**await self._get_post_params(from_validate=True),
) as response:
json_response = await response.json()
response.raise_for_status()
Expand All @@ -154,7 +156,7 @@ async def _deploy(self):
_validate_resposne = await self.validate()
if len(_validate_resposne['errors']) == 0:
logger.info(
f'Succesfully validated flow config. Proceeding to flow deployment...'
f'Successfully validated flow config. Proceeding to flow deployment...'
)
else:
errors = '\n'.join(_validate_resposne['errors'])
Expand Down
162 changes: 66 additions & 96 deletions jcloud/parsers/__init__.py
@@ -1,26 +1,32 @@
from typing import List
from argparse import ArgumentParser

from .helper import _chf
from ..constants import Resources


def get_main_parser(parser=None):
"""The main parser for Jina
:return: the parser
"""
from .base import set_base_parser, set_new_project_parser, set_simple_parser
from .base import set_base_parser, set_new_project_parser
from .deploy import set_deploy_parser
from .helper import _chf
from .list import set_list_parser
from .remove import set_remove_parser
from .list import set_list_resource_parser
from .get import set_get_resource_parser
from .create import set_create_resource_parser
from .remove import set_remove_resource_parser
from .status import set_status_parser
from .normalize import set_normalize_parser
from .update import set_update_parser
from .logs import set_logs_parser
from .update import set_update_resource_parser
from .logs import set_logs_resource_parser
from .custom_actions import (
set_restart_parser,
set_pause_parser,
set_resume_parser,
set_scale_parser,
set_recreate_parser,
)
from .k8s_resources.create import set_create_parser
from .k8s_resources.get import set_get_parser

# create the top-level parser
parser = set_base_parser(parser=parser)
Expand All @@ -32,13 +38,13 @@ def get_main_parser(parser=None):

sp.add_parser(
'login',
help='Login to Jina Cloud / Ecosystem.',
help='Login to Jina AI Cloud / Ecosystem.',
formatter_class=_chf,
)

sp.add_parser(
'logout',
help='Logout from Jina Cloud / Ecosystem.',
help='Logout from Jina AI Cloud / Ecosystem.',
formatter_class=_chf,
)

Expand All @@ -58,29 +64,40 @@ def get_main_parser(parser=None):
)
)

set_list_parser(
sp.add_parser(
'list',
help='List Flows, Jobs or Secrets.',
formatter_class=_chf,
)
)
resource_parsers = _add_resource_parsers(sp)

set_status_parser(
sp.add_parser(
'status',
help='Get the status of a Flow.',
formatter_class=_chf,
for resource_parser in resource_parsers:
subparser = resource_parser.add_subparsers(
dest='subcommand',
required=True,
)
)

set_remove_parser(
sp.add_parser(
'remove',
help='Remove Flow(s), a Job or a Secret.',
formatter_class=_chf,
)
)
set_list_resource_parser(subparser, resource_parser.prog)
set_remove_resource_parser(subparser, resource_parser.prog)
if Resources.Job not in resource_parser.prog:
set_update_resource_parser(subparser, resource_parser.prog)
if Resources.Flow in resource_parser.prog:
set_restart_parser(subparser)
set_pause_parser(subparser)
set_resume_parser(subparser)
set_scale_parser(subparser)
set_recreate_parser(subparser)
set_status_parser(subparser)
if (
Resources.Flow in resource_parser.prog
or Resources.Job in resource_parser.prog
):
set_logs_resource_parser(subparser, resource_parser.prog)
if (
Resources.Job in resource_parser.prog
or Resources.Secret in resource_parser.prog
):
resource = (
Resources.Job
if Resources.Job in resource_parser.prog
else Resources.Secret
)
set_create_resource_parser(subparser, resource)
set_get_resource_parser(subparser, resource)

set_new_project_parser(
sp.add_parser(
Expand All @@ -91,76 +108,29 @@ def get_main_parser(parser=None):
)
)

set_update_parser(
sp.add_parser(
'update',
help='Update a Flow or Secret.',
formatter_class=_chf,
)
)

set_restart_parser(
sp.add_parser(
'restart',
help='Restart a Flow, executor or gateway',
formatter_class=_chf,
)
)

set_pause_parser(
sp.add_parser(
'pause',
help='Pause a Flow',
formatter_class=_chf,
)
)

set_resume_parser(
sp.add_parser(
'resume',
help='Resume a paused Flow',
formatter_class=_chf,
)
)

set_scale_parser(
sp.add_parser(
'scale',
help='Scale executor of Flow',
formatter_class=_chf,
)
)
return parser

set_recreate_parser(
sp.add_parser(
'recreate',
help='Recreate deleted Flow',
formatter_class=_chf,
)
)

set_logs_parser(
sp.add_parser(
'logs',
help='Get the logs of a Flow or Job.',
formatter_class=_chf,
)
def _add_resource_parsers(subparser) -> List[ArgumentParser]:
flow_parser = subparser.add_parser(
'flow',
help='Manage Flow(s).',
formatter_class=_chf,
aliases=['flows'],
)

set_create_parser(
sp.add_parser(
'create',
help='Create a Job or a Secret for a Flow',
formatter_class=_chf,
)
job_parser = subparser.add_parser(
'job',
help='Manage Job(s).',
formatter_class=_chf,
aliases=['jobs'],
)

set_get_parser(
sp.add_parser(
'get',
help='Get a Job or Secret in a Flow.',
formatter_class=_chf,
)
secret_parser = subparser.add_parser(
'secret',
help='Manage Secret(s).',
formatter_class=_chf,
aliases=['secrets'],
)

return parser
return [flow_parser, job_parser, secret_parser]

0 comments on commit 4428ce0

Please sign in to comment.