Skip to content

Commit

Permalink
feat(deployment): updates spinner and refactors existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Aug 3, 2023
1 parent ff28002 commit 458b44e
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 94 deletions.
12 changes: 7 additions & 5 deletions riocli/deployment/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,22 +14,24 @@
import click
from click_help_colors import HelpColorsGroup

from riocli.constants import Colors
from riocli.deployment.delete import delete_deployment
from riocli.deployment.execute import execute_command
from riocli.deployment.execute import execute_command
from riocli.deployment.inspect import inspect_deployment
from riocli.deployment.list import list_deployments
from riocli.deployment.logs import deployment_logs
from riocli.deployment.ssh import ssh_init, ssh_deployment
from riocli.deployment.status import status
from riocli.deployment.wait import wait_for_deployment
from riocli.deployment.execute import execute_command
from riocli.deployment.update import update_deployment
from riocli.deployment.wait import wait_for_deployment


@click.group(
invoke_without_command=False,
cls=HelpColorsGroup,
help_headers_color='yellow',
help_options_color='green',
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
def deployment():
"""
Expand Down
45 changes: 32 additions & 13 deletions riocli/deployment/delete.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,30 +12,49 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_spinner import spinner
from click_help_colors import HelpColorsCommand

from riocli.config import new_client
from riocli.constants import Colors, Symbols
from riocli.deployment.util import name_to_guid
from riocli.utils.spinner import with_spinner


@click.command('delete')
@click.command(
'delete',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--force', '-f', '--silent', is_flag=True, default=False,
help='Skip confirmation')
@click.argument('deployment-name', type=str)
@name_to_guid
def delete_deployment(force: bool, deployment_name: str, deployment_guid: str) -> None:
@with_spinner(text="Deleting deployment...")
def delete_deployment(
force: bool,
deployment_name: str,
deployment_guid: str,
spinner=None,
) -> None:
"""
Delete the deployment from the Platform
Deletes a deployment
"""
if not force:
click.confirm('Deleting {} ({}) deployment'.format(deployment_name, deployment_guid), abort=True)
with spinner.hidden():
if not force:
click.confirm(
'Deleting {} ({}) deployment'.format(
deployment_name, deployment_guid), abort=True)

try:
with spinner():
client = new_client()
deployment = client.get_deployment(deployment_guid)
deployment.deprovision()
click.secho('Deployment deleted successfully!', fg='green')
client = new_client()
deployment = client.get_deployment(deployment_guid)
deployment.deprovision()
spinner.text = click.style(
'Deployment deleted successfully.', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except Exception as e:
click.secho(str(e), fg='red')
spinner.text = click.style(
'Failed to delete deployment: {}'.format(e), Colors.RED)
spinner.red.fail(Symbols.ERROR)
raise SystemExit(1)
41 changes: 30 additions & 11 deletions riocli/deployment/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,51 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os

import click
import typing

import click
from click_help_colors import HelpColorsCommand
from yaspin import kbi_safe_yaspin

from riocli.constants import Colors
from riocli.deployment.util import name_to_guid, select_details
from riocli.utils.execute import run_on_cloud


@click.command('execute')
@click.option('--component', 'component_name', default=None)
@click.option('--exec', 'exec_name', default=None)
@click.command(
'execute',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--component', 'component_name', default=None,
help='Name of the component in the deployment')
@click.option('--exec', 'exec_name', default=None,
help='Name of a executable in the component')
@click.argument('deployment-name', type=str)
@click.argument('command', nargs=-1)
@name_to_guid
def execute_command(component_name: str, exec_name: str, deployment_name: str, deployment_guid: str, command: typing.List[str]) -> None:
def execute_command(
component_name: str,
exec_name: str,
deployment_name: str,
deployment_guid: str,
command: typing.List[str]
) -> None:
"""
Execute commands on cloud deployment
"""
try:
comp_id, exec_id, pod_name = select_details(deployment_guid, component_name, exec_name)
stdout, stderr = run_on_cloud(deployment_guid, comp_id, exec_id, pod_name, command)

with kbi_safe_yaspin(text='Executing command `{}`...'.format(command)) as spinner:
stdout, stderr = run_on_cloud(deployment_guid, comp_id, exec_id, pod_name, command)

if stderr:
click.secho(stderr, fg='red')
click.secho(stderr, fg=Colors.RED)
if stdout:
click.secho(stdout, fg='yellow')
click.secho(stdout, fg=Colors.YELLOW)
except Exception as e:
click.secho(e, fg='red')
raise SystemExit(1)
click.secho(e, fg=Colors.RED)
raise SystemExit(1)
19 changes: 15 additions & 4 deletions riocli/deployment/inspect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,19 +12,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients.deployment import Deployment

from riocli.config import new_client
from riocli.constants import Colors
from riocli.deployment.util import name_to_guid
from riocli.utils import inspect_with_format


@click.command('inspect')
@click.command(
'inspect',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--format', '-f', 'format_type', default='yaml',
type=click.Choice(['json', 'yaml'], case_sensitive=False))
@click.argument('deployment-name')
@name_to_guid
def inspect_deployment(format_type: str, deployment_name: str, deployment_guid: str) -> None:
def inspect_deployment(
format_type: str,
deployment_name: str,
deployment_guid: str,
) -> None:
"""
Inspect the deployment resource
"""
Expand All @@ -34,7 +45,7 @@ def inspect_deployment(format_type: str, deployment_name: str, deployment_guid:
data = make_deployment_inspectable(deployment)
inspect_with_format(data, format_type)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


Expand Down
36 changes: 29 additions & 7 deletions riocli/deployment/list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,19 +14,41 @@
import typing

import click
from rapyuta_io.clients.deployment import Deployment
from click_help_colors import HelpColorsCommand
from rapyuta_io.clients.deployment import Deployment, DeploymentPhaseConstants

from riocli.config import new_client
from riocli.constants import Colors
from riocli.utils import tabulate_data

ALL_PHASES = [
DeploymentPhaseConstants.INPROGRESS,
DeploymentPhaseConstants.PROVISIONING,
DeploymentPhaseConstants.SUCCEEDED,
DeploymentPhaseConstants.FAILED_TO_START,
DeploymentPhaseConstants.PARTIALLY_DEPROVISIONED,
DeploymentPhaseConstants.DEPLOYMENT_STOPPED,
]

@click.command('list')
DEFAULT_PHASES = [
DeploymentPhaseConstants.INPROGRESS,
DeploymentPhaseConstants.PROVISIONING,
DeploymentPhaseConstants.SUCCEEDED,
DeploymentPhaseConstants.FAILED_TO_START,
]


@click.command(
'list',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--device', prompt_required=False, default='', type=str,
help='Filter the Deployment list by Device ID')
@click.option('--phase', prompt_required=False, multiple=True,
type=click.Choice(['In progress', 'Provisioning', 'Succeeded', 'Failed to start',
'Partially deprovisioned', 'Deployment stopped']),
default=['In progress', 'Provisioning', 'Succeeded', 'Failed to start'],
type=click.Choice(ALL_PHASES),
default=DEFAULT_PHASES,
help='Filter the Deployment list by Phases')
def list_deployments(device: str, phase: typing.List[str]) -> None:
"""
Expand All @@ -38,7 +60,7 @@ def list_deployments(device: str, phase: typing.List[str]) -> None:
deployments = sorted(deployments, key=lambda d: d.name.lower())
display_deployment_list(deployments, show_header=True)
except Exception as e:
click.secho(str(e), fg='red')
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1)


Expand Down
35 changes: 26 additions & 9 deletions riocli/deployment/logs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,38 +14,55 @@
import os

import click
from click_help_colors import HelpColorsCommand

from riocli.config import Configuration
from riocli.constants import Colors
from riocli.deployment.util import name_to_guid, select_details

_LOG_URL_FORMAT = '{}/deployment/logstream?tailLines={}&deploymentId={}&componentId={}&executableId={}&podName={}'


@click.command('logs')
@click.option('--component', 'component_name', default=None)
@click.option('--exec', 'exec_name', default=None)
@click.command(
'logs',
cls=HelpColorsCommand,
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--component', 'component_name', default=None,
help='Name of the component in the deployment')
@click.option('--exec', 'exec_name', default=None,
help='Name of a executable in the component')
@click.argument('deployment-name', type=str)
@name_to_guid
def deployment_logs(component_name: str, exec_name: str, deployment_name: str, deployment_guid: str) -> None:
def deployment_logs(
component_name: str,
exec_name: str,
deployment_name: str,
deployment_guid: str,
) -> None:
"""
Stream the live logs for the Deployment (only Cloud)
Stream live logs from cloud deployments (not supported for device deployments)
"""
try:
comp_id, exec_id, pod_name = select_details(deployment_guid, component_name, exec_name)
stream_deployment_logs(deployment_guid, comp_id, exec_id, pod_name)
except Exception as e:
click.secho(e, fg='red')
click.secho(e, fg=Colors.RED)
raise SystemExit(1)


def stream_deployment_logs(deployment_id, component_id, exec_id, pod_name=None):
# FIXME(ankit): The Upstream API ends up timing out when there is no log being written.
# IMO the correct behaviour should be to not timeout and keep the stream open.
config = Configuration()

url = get_log_stream_url(config, deployment_id, component_id, exec_id, pod_name)
auth = config.get_auth_header()
curl = 'curl -H "project: {}" -H "Authorization: {}" "{}"'.format(auth['project'], auth['Authorization'], url)
click.secho(curl, fg='blue')
curl = 'curl -H "project: {}" -H "Authorization: {}" "{}"'.format(
auth['project'], auth['Authorization'], url)
click.echo(click.style(curl, fg=Colors.BLUE, italic=True))

os.system(curl)


Expand Down
18 changes: 11 additions & 7 deletions riocli/deployment/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2022 Rapyuta Robotics
# Copyright 2023 Rapyuta Robotics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
ROSBagUploadTypes, OverrideOptions, TopicOverrideInfo)
from rapyuta_io.clients.routed_network import RoutedNetwork

from riocli.constants import Colors
from riocli.deployment.errors import ERRORS
from riocli.deployment.util import add_mount_volume_provision_config
from riocli.jsonschema.validate import load_schema
Expand All @@ -43,8 +44,11 @@ class Deployment(Model):
}

def find_object(self, client: Client) -> typing.Any:
guid, obj = self.rc.find_depends(
{"kind": "deployment", "nameOrGUID": self.metadata.name})
guid, obj = self.rc.find_depends({
"kind": "deployment",
"nameOrGUID": self.metadata.name,
})

return obj if guid else False

def create_object(self, client: Client, **kwargs) -> typing.Any:
Expand All @@ -71,7 +75,7 @@ def create_object(self, client: Client, **kwargs) -> typing.Any:
'>> runtime mismatch => ' +
'deployment:{}.runtime !== package:{}.runtime '.format(
self.metadata.name, pkg['packageName']
), fg='red')
), fg=Colors.RED)
return

provision_config = pkg.get_provision_configuration(plan_id)
Expand Down Expand Up @@ -395,9 +399,9 @@ def process_deployment_errors(e: DeploymentNotRunningException):
description = 'Internal rapyuta.io error'
action = support_action

code = click.style(code, fg='yellow')
description = click.style(description, fg='red')
action = click.style(action, fg='green')
code = click.style(code, fg=Colors.YELLOW)
description = click.style(description, fg=Colors.RED)
action = click.style(action, fg=Colors.GREEN)

msgs.append(err_fmt.format(code, description, action))

Expand Down
Loading

0 comments on commit 458b44e

Please sign in to comment.