From 7f20e59a13a6acc3331e04185b8f1ed2538dcd0a Mon Sep 17 00:00:00 2001 From: Ludovic Courtès Date: Sun, 9 Jan 2022 21:55:43 +0100 Subject: machine: ssh: Open a single SSH session per machine. Previously, any call to 'managed-host-remote-eval' and similar would open a new SSH session to the host. With this change, an SSH session is opened once, cached, and then reused by all subsequent calls to 'machine-ssh-session'. * gnu/machine/ssh.scm (): Add 'this-machine-ssh-configuration'. [session]: Mark as thunked and change default value to an 'open-machine-ssh-session*' call. (open-machine-ssh-session, open-machine-ssh-session*): New procedures. (machine-ssh-session): Replace inline code by call to 'open-machine-ssh-session'. --- gnu/machine/ssh.scm | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/gnu/machine/ssh.scm b/gnu/machine/ssh.scm index ecd02e336c..22688f46f4 100644 --- a/gnu/machine/ssh.scm +++ b/gnu/machine/ssh.scm @@ -1,6 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2019 Jakob L. Kreuze -;;; Copyright © 2020, 2021 Ludovic Courtès +;;; Copyright © 2020-2022 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -26,6 +26,7 @@ #:use-module (gnu system uuid) #:use-module ((gnu services) #:select (sexp->system-provenance)) #:use-module (guix diagnostics) + #:use-module (guix memoization) #:use-module (guix gexp) #:use-module (guix i18n) #:use-module (guix modules) @@ -83,6 +84,7 @@ (define-record-type* machine-ssh-configuration make-machine-ssh-configuration machine-ssh-configuration? + this-machine-ssh-configuration (host-name machine-ssh-configuration-host-name) ; string (system machine-ssh-configuration-system) ; string (build-locally? machine-ssh-configuration-build-locally? ; boolean @@ -98,29 +100,41 @@ (identity machine-ssh-configuration-identity ; path to a private key (default #f)) (session machine-ssh-configuration-session ; session - (default #f)) + (thunked) + (default + ;; By default, open the session once and cache it. + (open-machine-ssh-session* this-machine-ssh-configuration))) (host-key machine-ssh-configuration-host-key ; #f | string (default #f))) +(define (open-machine-ssh-session config) + "Open an SSH session for CONFIG, a record." + (let ((host-name (machine-ssh-configuration-host-name config)) + (user (machine-ssh-configuration-user config)) + (port (machine-ssh-configuration-port config)) + (identity (machine-ssh-configuration-identity config)) + (host-key (machine-ssh-configuration-host-key config))) + (unless host-key + (warning (G_ " without a 'host-key' \ +is deprecated~%"))) + (open-ssh-session host-name + #:user user + #:port port + #:identity identity + #:host-key host-key))) + +(define open-machine-ssh-session* + (mlambdaq (config) + "Memoizing variant of 'open-machine-ssh-session'." + (open-machine-ssh-session config))) + (define (machine-ssh-session machine) "Return the SSH session that was given in MACHINE's configuration, or create one from the configuration's parameters if one was not provided." (maybe-raise-unsupported-configuration-error machine) (let ((config (machine-configuration machine))) (or (machine-ssh-configuration-session config) - (let ((host-name (machine-ssh-configuration-host-name config)) - (user (machine-ssh-configuration-user config)) - (port (machine-ssh-configuration-port config)) - (identity (machine-ssh-configuration-identity config)) - (host-key (machine-ssh-configuration-host-key config))) - (unless host-key - (warning (G_ " without a 'host-key' \ -is deprecated~%"))) - (open-ssh-session host-name - #:user user - #:port port - #:identity identity - #:host-key host-key))))) + (open-machine-ssh-session config)))) ;;; -- cgit v1.2.3