compshare-cli 0.4.4__py3-none-any.whl → 0.5.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
compshare_cli/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  """CompShare command-line client."""
2
2
 
3
- __version__ = "0.4.4"
3
+ __version__ = "0.5.1"
@@ -2,7 +2,6 @@ from __future__ import annotations
2
2
 
3
3
  import os
4
4
  import shlex
5
- import subprocess
6
5
  import sys
7
6
  import time
8
7
  from datetime import datetime
@@ -64,8 +63,10 @@ from compshare_cli.ssh import (
64
63
  execute_captured,
65
64
  execute_captured_with_password,
66
65
  execute_with_password,
66
+ run_command,
67
67
  scp_download_command,
68
68
  scp_upload_command,
69
+ ssh_login_command,
69
70
  )
70
71
  from compshare_cli.ssh_cache import DEFAULT_TTL, SSHCredentialCache
71
72
 
@@ -495,8 +496,7 @@ def _remote_job_connection(
495
496
  raise UsageError(tr("Instance {instance} has no SSH login command.", instance=instance))
496
497
  raw_password = host.get("Password")
497
498
  password = decode_password(str(raw_password)) if raw_password is not None else None
498
- argv = [*shlex.split(str(login_command))]
499
- argv[1:1] = ["-o", f"ConnectTimeout={connect_timeout}"]
499
+ argv = ssh_login_command(login_command, connect_timeout=connect_timeout)
500
500
  argv.append(shell_command(script))
501
501
  return argv, password, credential_source
502
502
 
@@ -540,9 +540,9 @@ def _remote_job_records(
540
540
 
541
541
 
542
542
  def _remote_job_stream(state: Runtime, instance: str, script: str) -> int:
543
- argv, password, credential_source = _remote_job_connection(state, instance, script)
544
- exit_code = execute_with_password(argv, password) if password else subprocess.call(argv)
545
- if exit_code == 255 and credential_source == "cache":
543
+ argv, password, _ = _remote_job_connection(state, instance, script)
544
+ exit_code = execute_with_password(argv, password) if password else run_command(argv)
545
+ if exit_code == 255:
546
546
  SSHCredentialCache().delete(_ssh_cache_profile(state), instance)
547
547
  return exit_code
548
548
 
@@ -2891,16 +2891,14 @@ def ssh(
2891
2891
  password = decode_password(str(raw_password)) if raw_password is not None else None
2892
2892
  if not command:
2893
2893
  raise UsageError(tr("Instance {instance} has no SSH login command.", instance=instance))
2894
- argv = [*shlex.split(command), *(remote_command or [])]
2894
+ argv = ssh_login_command(command, connect_timeout=connect_timeout)
2895
2895
  if remote_command:
2896
- argv[1:1] = ["-o", f"ConnectTimeout={connect_timeout}"]
2896
+ argv.append(joined_command(remote_command))
2897
2897
  if print_only or (state.json_output and not remote_command):
2898
2898
  Renderer(state.json_output, state.show_sensitive).data(
2899
2899
  {
2900
2900
  "instance": instance,
2901
- "command": command
2902
- if not remote_command
2903
- else f"{command} {shlex.join(remote_command)}",
2901
+ "command": shlex.join(argv),
2904
2902
  "password": password,
2905
2903
  "credential_source": credential_source,
2906
2904
  }
@@ -2920,7 +2918,7 @@ def ssh(
2920
2918
  "code": execution.error_code,
2921
2919
  "message": message,
2922
2920
  }
2923
- if credential_source == "cache" and execution.phase in {
2921
+ if use_cache and execution.phase in {
2924
2922
  "authentication",
2925
2923
  "connection",
2926
2924
  "ssh",
@@ -2956,21 +2954,13 @@ def ssh(
2956
2954
  err=True,
2957
2955
  )
2958
2956
  else:
2959
- if exit_code == 255 and credential_source == "cache":
2957
+ if exit_code == 255 and use_cache:
2960
2958
  SSHCredentialCache().delete(_ssh_cache_profile(state), instance)
2961
2959
  raise typer.Exit(exit_code)
2962
2960
  if password and not state.show_sensitive:
2963
2961
  typer.echo(tr("Password hidden; rerun with --show-sensitive to display it."))
2964
- elif not password:
2965
- typer.echo(
2966
- tr(
2967
- "The API did not return a password. Run `compshare instance password {instance}` "
2968
- "to set one.",
2969
- instance=instance,
2970
- )
2971
- )
2972
- exit_code = subprocess.call(argv)
2973
- if exit_code == 255 and credential_source == "cache":
2962
+ exit_code = run_command(argv)
2963
+ if exit_code == 255 and use_cache:
2974
2964
  SSHCredentialCache().delete(_ssh_cache_profile(state), instance)
2975
2965
  raise typer.Exit(exit_code)
2976
2966
 
@@ -2994,6 +2984,22 @@ def cp(
2994
2984
  "--auto-password/--no-auto-password",
2995
2985
  help="Automatically enter the password returned by the API.",
2996
2986
  ),
2987
+ connect_timeout: int = typer.Option(
2988
+ 30, "--connect-timeout", min=1, help="Maximum SSH connection time in seconds."
2989
+ ),
2990
+ use_cache: bool = typer.Option(
2991
+ True,
2992
+ "--cache/--no-cache",
2993
+ help="Cache SSH connection data to avoid repeated instance queries.",
2994
+ ),
2995
+ cache_ttl: int = typer.Option(
2996
+ DEFAULT_TTL, "--cache-ttl", min=1, help="SSH connection cache lifetime in seconds."
2997
+ ),
2998
+ refresh: bool = typer.Option(
2999
+ False,
3000
+ "--refresh",
3001
+ help="Refresh SSH connection data from the API before connecting.",
3002
+ ),
2997
3003
  ) -> None:
2998
3004
  source_is_remote = source_path.startswith(":")
2999
3005
  destination_is_remote = destination_path.startswith(":")
@@ -3019,14 +3025,16 @@ def cp(
3019
3025
  raise UsageError(tr("Remote path cannot be empty."))
3020
3026
 
3021
3027
  state = runtime(ctx)
3022
- _, _, host = locate_instance(state, instance)
3028
+ _, _, host, _ = _locate_ssh_instance(
3029
+ state, instance, use_cache=use_cache, refresh=refresh, cache_ttl=cache_ttl
3030
+ )
3023
3031
  login_command = host.get("SshLoginCommand")
3024
3032
  raw_password = host.get("Password")
3025
3033
  password = decode_password(str(raw_password)) if raw_password is not None else None
3026
3034
  if not login_command:
3027
3035
  raise UsageError(tr("Instance {instance} has no SSH login command.", instance=instance))
3028
3036
  try:
3029
- ssh_argv = shlex.split(str(login_command))
3037
+ ssh_argv = ssh_login_command(login_command, connect_timeout=connect_timeout)
3030
3038
  if direction == "download":
3031
3039
  argv = scp_download_command(ssh_argv, remote_path, str(local), recursive=True)
3032
3040
  else:
@@ -3061,6 +3069,8 @@ def cp(
3061
3069
  "code": execution.error_code,
3062
3070
  "message": message,
3063
3071
  }
3072
+ if use_cache and execution.phase in {"authentication", "connection", "ssh"}:
3073
+ SSHCredentialCache().delete(_ssh_cache_profile(state), instance)
3064
3074
  Renderer(True, state.show_sensitive).data(
3065
3075
  {
3066
3076
  "instance": instance,
@@ -3084,15 +3094,12 @@ def cp(
3084
3094
  err=True,
3085
3095
  )
3086
3096
  else:
3097
+ if exit_code == 255 and use_cache:
3098
+ SSHCredentialCache().delete(_ssh_cache_profile(state), instance)
3087
3099
  raise typer.Exit(exit_code)
3088
3100
  if password and not state.show_sensitive:
3089
3101
  typer.echo(tr("Password hidden; rerun with --show-sensitive to display it."))
3090
- elif not password:
3091
- typer.echo(
3092
- tr(
3093
- "The API did not return a password. Run `compshare instance password {instance}` "
3094
- "to set one.",
3095
- instance=instance,
3096
- )
3097
- )
3098
- raise typer.Exit(subprocess.call(argv))
3102
+ exit_code = run_command(argv)
3103
+ if exit_code == 255 and use_cache:
3104
+ SSHCredentialCache().delete(_ssh_cache_profile(state), instance)
3105
+ raise typer.Exit(exit_code)