Skip to content

Commit

Permalink
feat(device): add option to exec commands asynchronously (#365)
Browse files Browse the repository at this point in the history
This commit adds the option to execute command on devices and
deployments asynchronously. The default behaviour is still sync.
However, users can simply add the --async flag to execute the command in
the background.

Fixes AB#16668
  • Loading branch information
rrkumarshikhar authored Nov 22, 2024
1 parent b10301a commit c6bea52
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies = [
"python-magic>=0.4.27",
"pytz",
"pyyaml>=5.4.1",
"rapyuta-io>=2.1.1",
"rapyuta-io>=2.2.0",
"requests>=2.20.0",
"semver>=3.0.0",
"setuptools",
Expand Down
19 changes: 18 additions & 1 deletion riocli/deployment/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@
)
@click.option("--user", default="root")
@click.option("--shell", default="/bin/bash")
@click.option("--timeout", default=300)
@click.option(
"--async",
"run_async",
is_flag=True,
default=False,
help="Run the command asynchronously.",
)
@click.option(
"--exec", "exec_name", default=None, help="Name of a executable in the component"
)
Expand All @@ -44,7 +52,9 @@
def execute_command(
user: str,
shell: str,
timeout: int,
exec_name: str,
run_async: bool,
deployment_name: str,
command: typing.List[str],
) -> None:
Expand All @@ -61,6 +71,12 @@ def execute_command(
shell is ``/bin/bash``. You can also specify the user using the ``--user``
option. The default user is ``root``.
To run the command asynchronously, set the --async flag to true.
The default value is false.
To specify the timeout, use the --timeout
flag, providing the duration in seconds. The default value is 300.
Please ensure that you enclose the command in quotes to avoid
any issues with the command parsing.
Expand Down Expand Up @@ -112,10 +128,11 @@ def execute_command(
user=user,
shell=shell,
command=command,
background=False,
background=run_async,
deployment=deployment,
exec_name=exec_name,
device_name=deployment.spec.device.depends.nameOrGUID,
timeout=timeout,
)
click.echo(response)
except Exception as e:
Expand Down
30 changes: 24 additions & 6 deletions riocli/device/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,36 @@
help_options_color=Colors.GREEN,
)
@click.option("--user", default="root")
@click.option("--timeout", default=300)
@click.option("--shell", default="/bin/bash")
@click.option(
"--async",
"run_async",
is_flag=True,
default=False,
help="Run the command asynchronously.",
)
@click.argument("device-name", type=str)
@click.argument("command", nargs=-1)
@name_to_guid
def execute_command(
device_name: str, device_guid: str, user: str, shell: str, command: typing.List[str]
device_name: str,
device_guid: str,
user: str,
timeout: int,
shell: str,
run_async: bool,
command: typing.List[str],
) -> None:
"""Execute commands on a device.
You can specify the user and shell to run the command in.
To specify the user, use the --user flag. The default is
root. To specify the shell, use the --shell flag. The default
shell is /bin/bash.
You can specify the user, shell, run-async, and timeout options to customize
the command execution. To specify the user, use the --user flag.
The default is 'root'. To specify the shell, use the --shell flag.
The default shell is '/bin/bash'. To run the command asynchronously,
set the --async flag to true. The default value is false. To
specify the timeout, use the --timeout flag, providing the duration
in seconds. The default value is 300.
Make sure you put your command in quotes to avoid any issues.
Expand All @@ -54,7 +71,8 @@ def execute_command(
user=user,
shell=shell,
command=command,
background=False,
background=run_async,
timeout=timeout,
)

click.secho(response)
Expand Down
5 changes: 4 additions & 1 deletion riocli/utils/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def run_on_device(
deployment: str = None,
exec_name: str = None,
device_name: str = None,
timeout: int = 300,
) -> str:
client = new_client()

Expand Down Expand Up @@ -58,7 +59,9 @@ def run_on_device(
if deployment:
cmd = 'script -q -c "dectl exec {} -- {}"'.format(exec_name, cmd)

return device.execute_command(Command(cmd, shell=shell, bg=background, runas=user))
return device.execute_command(
Command(cmd, shell=shell, bg=background, runas=user, timeout=timeout)
)


def apply_func(
Expand Down
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c6bea52

Please sign in to comment.