From d3f6727c2c58870876999e44dc7591fecd3aa6b5 Mon Sep 17 00:00:00 2001 From: KUMAR SHIKHAR Date: Mon, 10 Jun 2024 10:32:45 +0530 Subject: [PATCH] chore: moved login to hwildevice command --- riocli/auth/login.py | 18 --------- riocli/auth/util.py | 30 -------------- riocli/hwil/__init__.py | 4 +- riocli/hwil/create.py | 21 ++-------- riocli/hwil/delete.py | 36 +++++++++-------- riocli/hwil/list.py | 3 +- riocli/hwil/login.py | 89 +++++++++++++++++++++++++++++++++++++++++ riocli/hwil/util.py | 2 - 8 files changed, 117 insertions(+), 86 deletions(-) create mode 100644 riocli/hwil/login.py diff --git a/riocli/auth/login.py b/riocli/auth/login.py index 263b81db..ec010db0 100644 --- a/riocli/auth/login.py +++ b/riocli/auth/login.py @@ -19,7 +19,6 @@ select_organization, select_project, validate_and_set_token, - validate_and_set_hwil_token ) from riocli.constants import Colors, Symbols from riocli.utils.context import get_root_context @@ -47,10 +46,6 @@ help='Make login interactive') @click.option('--auth-token', type=str, default=None, help="Login with auth token only") -@click.option('--hwil-user', type=str, - help="Username of rapyuta.io virtual devices account") -@click.option('--hwil-password', type=str, - help="Password of the rapyuta.io virtual devices account") @click.pass_context def login( ctx: click.Context, @@ -60,8 +55,6 @@ def login( project: str, interactive: bool, auth_token: str, - hwil_user: str, - hwil_password: str, ) -> None: """ Log into your rapyuta.io account using the CLI. This is required to @@ -79,8 +72,6 @@ def login( if interactive: email = email or click.prompt('Email') password = password or click.prompt('Password', hide_input=True) - hwil_user = hwil_user or click.prompt('HwilUser') - hwil_password = hwil_password or click.prompt('HwilPassword') if not email: click.secho('email not specified') @@ -90,15 +81,6 @@ def login( click.secho('password not specified') raise SystemExit(1) - if hwil_user and not hwil_password: - click.secho('hwil password not specified') - - if hwil_password and not hwil_user: - click.secho('hwil user not specified') - - if hwil_user and hwil_password: - if not validate_and_set_hwil_token(ctx, hwil_user, hwil_password): - raise SystemExit(1) ctx.obj.data['email_id'] = email ctx.obj.data['password'] = password diff --git a/riocli/auth/util.py b/riocli/auth/util.py index cf22a865..23fd9c39 100644 --- a/riocli/auth/util.py +++ b/riocli/auth/util.py @@ -18,13 +18,11 @@ from rapyuta_io.clients.rip_client import AuthTokenLevel from rapyuta_io.utils import UnauthorizedError -from riocli.hwilclient import Client as HwilClient from riocli.config import Configuration from riocli.constants import Colors, Symbols from riocli.project.util import find_project_guid, find_organization_guid from riocli.utils.selector import show_selection from riocli.utils.spinner import with_spinner -from base64 import b64encode TOKEN_LEVELS = { 0: AuthTokenLevel.LOW, @@ -115,34 +113,6 @@ def select_project( click.secho(confirmation, fg=Colors.GREEN) -@with_spinner(text='Validating hwil credentials...') -def validate_and_set_hwil_token( - ctx: click.Context, - username: str, - password: str, - spinner=None -) -> bool: - """Validates an auth token.""" - if 'environment' in ctx.obj.data: - os.environ['RIO_CONFIG'] = ctx.obj.filepath - - token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii") - client = HwilClient(auth_token=token) - - try: - client.list_devices() - ctx.obj.data['hwil_auth_token'] = token - spinner.ok(Symbols.INFO) - return True - except UnauthorizedError: - spinner.text = click.style("incorrect credentials for hwil", fg=Colors.RED) - spinner.red.fail(Symbols.ERROR) - return False - except Exception as e: - spinner.text = click.style(str(e), fg=Colors.RED) - spinner.red.fail(Symbols.ERROR) - return False - @with_spinner(text='Fetching token...') def get_token( diff --git a/riocli/hwil/__init__.py b/riocli/hwil/__init__.py index 32bf47f3..9a54344e 100644 --- a/riocli/hwil/__init__.py +++ b/riocli/hwil/__init__.py @@ -18,6 +18,7 @@ from riocli.hwil.list import list_devices from riocli.hwil.delete import delete_device from riocli.hwil.inspect import inspect_device +from riocli.hwil.login import login @click.group( @@ -33,7 +34,8 @@ def hwildevice(): pass +hwildevice.add_command(login) hwildevice.add_command(create_device) hwildevice.add_command(list_devices) hwildevice.add_command(delete_device) -hwildevice.add_command(inspect_device) \ No newline at end of file +hwildevice.add_command(inspect_device) diff --git a/riocli/hwil/create.py b/riocli/hwil/create.py index 72e348ad..bdbb0a96 100644 --- a/riocli/hwil/create.py +++ b/riocli/hwil/create.py @@ -12,14 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. import time -import typing import click from click_spinner import spinner from click_help_colors import HelpColorsCommand from riocli.constants import Colors from rapyuta_io.utils import ConflictError from rapyuta_io.clients.device import DevicePythonVersion, Device, DeviceStatus -from riocli.utils import tabulate_data from riocli.device.util import find_device_guid from riocli.hwil.util import name_to_id @@ -28,7 +26,6 @@ from rapyuta_io import Client as v1Client - @click.command('create', cls=HelpColorsCommand, help_headers_color=Colors.YELLOW, @@ -51,14 +48,14 @@ def create_device( """ Create a new virtual device on the cloud """ + client = new_client() + hwil_client = new_hwil_client() try: - client = new_client() - hwil_client = new_hwil_client() with spinner(): try: hwil_client.create_device(device_name, arch, os, codename) click.secho('HWIL Device created successfully!', fg='green') - except ConflictError as c: + except ConflictError: click.secho('HWIL Device {} already exists in cluster!'.format(device_name), fg='green') if onboard: @@ -72,7 +69,7 @@ def create_device( _onboard_hwil_device(hwil_client=hwil_client, client=client, device_name=device_name, onboard_command=onboard_command, device_uuid=device.uuid) - except ConflictError as c: + except ConflictError: click.secho('Device {} already exists in rapyuta.io!'.format(device_name), fg='green') device = client.get_device(device_id=find_device_guid(client, device_name)) if device.is_online() or device.status == DeviceStatus.INITIALIZING: @@ -105,13 +102,3 @@ def _onboard_hwil_device(hwil_client: Client, client: v1Client, device_name: str except Exception as e: click.secho(str(e), fg='red') raise SystemExit(1) - - -def _display_device_list(devices: typing.List[dict], show_header: bool = True) -> None: - headers = [] - if show_header: - headers = ('Device ID', 'Name', 'Status') - - data = [[d.uuid, d.name, d.status] for d in devices] - - tabulate_data(data, headers) diff --git a/riocli/hwil/delete.py b/riocli/hwil/delete.py index e58480c8..569c27d6 100644 --- a/riocli/hwil/delete.py +++ b/riocli/hwil/delete.py @@ -23,31 +23,33 @@ 'delete', cls=HelpColorsCommand, ) -@click.argument('device-name', type=str, default="") -@click.option('--deboard', 'deboard', is_flag=True, type=bool, default=False) +# @click.argument('device-name', type=str, default="") +@click.argument('device-names', type=str, nargs=-1) +@click.option('--offboard', 'offboard', is_flag=True, type=bool, default=False) def delete_device( - device_name: str, - deboard: bool, + device_names: tuple, + offboard: bool, ) -> None: """ delete a virtual device on the cloud """ + client = new_client() + hwil_client = new_hwil_client() try: - client = new_client() - hwil_client = new_hwil_client() with spinner(): - try: - hwil_client.delete_device(find_device_id(hwil_client, device_name)) - click.secho('HWIL Device deleted successfully!', fg='green') - except DeviceNotFound as d: - click.secho('HWIL Device already deleted!', fg='green') - - if deboard: + for device_name in device_names: try: - client.delete_device(device_id=find_device_guid(client, device_name)) - click.secho('Rapyuta.io Device deleted successfully in rapyuta.io!', fg='green') - except DeviceNotFound as d: - click.secho('Rapyuta.io Device already deleted!', fg='green') + hwil_client.delete_device(find_device_id(hwil_client, device_name)) + click.secho('HWIL Device {device_name} deleted successfully!', fg='green') + except DeviceNotFound: + click.secho('HWIL Device {device_name} already deleted!', fg='green') + + if offboard: + try: + client.delete_device(device_id=find_device_guid(client, device_name)) + click.secho('Rapyuta.io Device {device_name} deleted successfully in rapyuta.io!', fg='green') + except DeviceNotFound: + click.secho('Rapyuta.io Device {device_name} already deleted!', fg='green') except Exception as e: click.secho(str(e), fg='red') raise SystemExit(1) diff --git a/riocli/hwil/list.py b/riocli/hwil/list.py index 46ce82e2..b0dd2865 100644 --- a/riocli/hwil/list.py +++ b/riocli/hwil/list.py @@ -19,6 +19,7 @@ from riocli.constants import Colors from riocli.utils import tabulate_data + @click.command( 'list', cls=HelpColorsCommand, @@ -46,4 +47,4 @@ def _display_device_list(devices: typing.List[dict], show_header: bool = True) - data = [[d.id, d.name, d.status, d.static_ip, d.ip_address] for d in devices] - tabulate_data(data, headers) \ No newline at end of file + tabulate_data(data, headers) diff --git a/riocli/hwil/login.py b/riocli/hwil/login.py new file mode 100644 index 00000000..bc7507d9 --- /dev/null +++ b/riocli/hwil/login.py @@ -0,0 +1,89 @@ +# Copyright 2024 Rapyuta Robotics +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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 +from riocli.hwilclient import Client as HwilClient +from click_help_colors import HelpColorsCommand +from riocli.constants import Colors, Symbols +from riocli.utils.context import get_root_context +from base64 import b64encode +from rapyuta_io.utils import UnauthorizedError +from riocli.utils.spinner import with_spinner + +HWIL_LOGIN_SUCCESS = click.style('{} Successfully logged into HWIL!'.format(Symbols.SUCCESS), fg=Colors.GREEN) + + +@click.command( + 'login', + cls=HelpColorsCommand, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) +@click.option('--hwil-user', required=True, help='Username for HWIL login') +@click.option('--hwil-password', required=True, help='Password for HWIL login') +@click.pass_context +def login( + ctx: click.Context, + hwil_user: str, + hwil_password: str, +) -> None: + """Log in to HWIL.""" + + ctx = get_root_context(ctx) + try: + if hwil_user and not hwil_password: + click.secho('hwil password not specified') + + if hwil_password and not hwil_user: + click.secho('hwil user not specified') + + if hwil_user and hwil_password: + if not validate_and_set_hwil_token(ctx, hwil_user, hwil_password): + raise SystemExit(1) + except Exception as e: + click.echo(f"Login failed: {e}") + + ctx.obj.save() + + click.echo(HWIL_LOGIN_SUCCESS) + + +@with_spinner(text='Validating hwil credentials...') +def validate_and_set_hwil_token( + ctx: click.Context, + username: str, + password: str, + spinner=None +) -> bool: + """Validates an auth token.""" + if 'environment' in ctx.obj.data: + os.environ['RIO_CONFIG'] = ctx.obj.filepath + + token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii") + client = HwilClient(auth_token=token) + + try: + client.list_devices() + ctx.obj.data['hwil_auth_token'] = token + spinner.ok(Symbols.INFO) + return True + except UnauthorizedError: + spinner.text = click.style("incorrect credentials for hwil", fg=Colors.RED) + spinner.red.fail(Symbols.ERROR) + return False + except Exception as e: + spinner.text = click.style(str(e), fg=Colors.RED) + spinner.red.fail(Symbols.ERROR) + return False diff --git a/riocli/hwil/util.py b/riocli/hwil/util.py index 22a5fb9c..1edbdb68 100644 --- a/riocli/hwil/util.py +++ b/riocli/hwil/util.py @@ -1,5 +1,4 @@ import functools -import re import typing import click from riocli.constants import Colors @@ -55,4 +54,3 @@ def find_device_id(client: Client, name: str) -> str: return device.id raise DeviceNotFound(message="Hwil Device not found") -