configure ControlMaster and ControlPath for SSH connections

This should speed up deployments by not having to reconnect to the server on each command
This commit is contained in:
Jörg Thalheim
2025-05-01 15:48:48 +02:00
parent f3f4ebfc71
commit c430ff6253
2 changed files with 19 additions and 1 deletions

View File

@@ -41,6 +41,22 @@ class Host:
self.command_prefix = self.host
if not self.user:
self.user = "root"
home = Path.home()
if home.exists() and os.access(home, os.W_OK):
control_path = home / ".ssh"
if not control_path.exists():
try:
control_path.mkdir(exist_ok=True)
except OSError:
pass
else:
self.ssh_options["ControlMaster"] = "auto"
# Can we make this a temporary directory?
self.ssh_options["ControlPath"] = str(
control_path / "clan-%h-%p-%r"
)
# We use a short ttl because we want to mainly re-use the connection during the cli run
self.ssh_options["ControlPersist"] = "1m"
def __str__(self) -> str:
return self.target

View File

@@ -117,7 +117,9 @@ def test_parse_deployment_address(
assert result.user == expected_user or (
expected_user == "" and result.user == "root"
)
assert result.ssh_options == expected_options
for key, value in expected_options.items():
assert result.ssh_options[key] == value
def test_parse_ssh_options() -> None: