command_runner.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. from typing import Any, Dict, List, Optional, Tuple
  2. from ray.util.annotations import DeveloperAPI
  3. @DeveloperAPI
  4. class CommandRunnerInterface:
  5. """Interface to run commands on a remote cluster node.
  6. **Important**: This is an INTERNAL API that is only exposed for the purpose
  7. of implementing custom node providers. It is not allowed to call into
  8. CommandRunner methods from any Ray package outside the autoscaler, only to
  9. define new implementations for use with the "external" node provider
  10. option.
  11. Command runner instances are returned by provider.get_command_runner()."""
  12. def run(
  13. self,
  14. cmd: Optional[str] = None,
  15. timeout: int = 120,
  16. exit_on_fail: bool = False,
  17. port_forward: Optional[List[Tuple[int, int]]] = None,
  18. with_output: bool = False,
  19. environment_variables: Optional[Dict[str, object]] = None,
  20. run_env: str = "auto",
  21. ssh_options_override_ssh_key: str = "",
  22. shutdown_after_run: bool = False,
  23. ) -> str:
  24. """Run the given command on the cluster node and optionally get output.
  25. WARNING: the cloudgateway needs arguments of "run" function to be json
  26. dumpable to send them over HTTP requests.
  27. Args:
  28. cmd: The command to run.
  29. timeout: The command timeout in seconds.
  30. exit_on_fail: Whether to sys exit on failure.
  31. port_forward: List of (local, remote) ports to forward, or
  32. a single tuple.
  33. with_output: Whether to return output.
  34. environment_variables (Dict[str, str | int | Dict[str, str]):
  35. Environment variables that `cmd` should be run with.
  36. run_env: Options: docker/host/auto. Used in
  37. DockerCommandRunner to determine the run environment.
  38. ssh_options_override_ssh_key: if provided, overwrites
  39. SSHOptions class with SSHOptions(ssh_options_override_ssh_key).
  40. shutdown_after_run: if provided, shutdowns down the machine
  41. after executing the command with `sudo shutdown -h now`.
  42. """
  43. raise NotImplementedError
  44. def run_rsync_up(
  45. self, source: str, target: str, options: Optional[Dict[str, Any]] = None
  46. ) -> None:
  47. """Rsync files up to the cluster node.
  48. Args:
  49. source: The (local) source directory or file.
  50. target: The (remote) destination path.
  51. """
  52. raise NotImplementedError
  53. def run_rsync_down(
  54. self, source: str, target: str, options: Optional[Dict[str, Any]] = None
  55. ) -> None:
  56. """Rsync files down from the cluster node.
  57. Args:
  58. source: The (remote) source directory or file.
  59. target: The (local) destination path.
  60. """
  61. raise NotImplementedError
  62. def remote_shell_command_str(self) -> str:
  63. """Return the command the user can use to open a shell."""
  64. raise NotImplementedError
  65. def run_init(
  66. self, *, as_head: bool, file_mounts: Dict[str, str], sync_run_yet: bool
  67. ) -> Optional[bool]:
  68. """Used to run extra initialization commands.
  69. Args:
  70. as_head: Run as head image or worker.
  71. file_mounts: Files to copy to the head and worker nodes.
  72. sync_run_yet: Whether sync has been run yet.
  73. Returns:
  74. optional: Whether initialization is necessary.
  75. """
  76. pass