summaryrefslogtreecommitdiff
path: root/gnu/machine
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2022-01-09 21:55:43 +0100
committerLudovic Courtès <ludo@gnu.org>2022-01-09 23:17:17 +0100
commit7f20e59a13a6acc3331e04185b8f1ed2538dcd0a (patch)
treedd7b670044397d56445e5d880a9eae7be8ebb979 /gnu/machine
parent1684ed6537fbd91ae5c14fb0314564e71799d390 (diff)
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 (<machine-ssh-configuration>): 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'.
Diffstat (limited to 'gnu/machine')
-rw-r--r--gnu/machine/ssh.scm44
1 files changed, 29 insertions, 15 deletions
diff --git a/gnu/machine/ssh.scm b/gnu/machine/ssh.scm
index ecd02e336c1..22688f46f4b 100644
--- a/gnu/machine/ssh.scm
+++ b/gnu/machine/ssh.scm
@@ -1,6 +1,6 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org> 2;;; Copyright © 2019 Jakob L. Kreuze <zerodaysfordays@sdf.org>
3;;; Copyright © 2020, 2021 Ludovic Courtès <ludo@gnu.org> 3;;; Copyright © 2020-2022 Ludovic Courtès <ludo@gnu.org>
4;;; 4;;;
5;;; This file is part of GNU Guix. 5;;; This file is part of GNU Guix.
6;;; 6;;;
@@ -26,6 +26,7 @@
26 #:use-module (gnu system uuid) 26 #:use-module (gnu system uuid)
27 #:use-module ((gnu services) #:select (sexp->system-provenance)) 27 #:use-module ((gnu services) #:select (sexp->system-provenance))
28 #:use-module (guix diagnostics) 28 #:use-module (guix diagnostics)
29 #:use-module (guix memoization)
29 #:use-module (guix gexp) 30 #:use-module (guix gexp)
30 #:use-module (guix i18n) 31 #:use-module (guix i18n)
31 #:use-module (guix modules) 32 #:use-module (guix modules)
@@ -83,6 +84,7 @@
83(define-record-type* <machine-ssh-configuration> machine-ssh-configuration 84(define-record-type* <machine-ssh-configuration> machine-ssh-configuration
84 make-machine-ssh-configuration 85 make-machine-ssh-configuration
85 machine-ssh-configuration? 86 machine-ssh-configuration?
87 this-machine-ssh-configuration
86 (host-name machine-ssh-configuration-host-name) ; string 88 (host-name machine-ssh-configuration-host-name) ; string
87 (system machine-ssh-configuration-system) ; string 89 (system machine-ssh-configuration-system) ; string
88 (build-locally? machine-ssh-configuration-build-locally? ; boolean 90 (build-locally? machine-ssh-configuration-build-locally? ; boolean
@@ -98,29 +100,41 @@
98 (identity machine-ssh-configuration-identity ; path to a private key 100 (identity machine-ssh-configuration-identity ; path to a private key
99 (default #f)) 101 (default #f))
100 (session machine-ssh-configuration-session ; session 102 (session machine-ssh-configuration-session ; session
101 (default #f)) 103 (thunked)
104 (default
105 ;; By default, open the session once and cache it.
106 (open-machine-ssh-session* this-machine-ssh-configuration)))
102 (host-key machine-ssh-configuration-host-key ; #f | string 107 (host-key machine-ssh-configuration-host-key ; #f | string
103 (default #f))) 108 (default #f)))
104 109
110(define (open-machine-ssh-session config)
111 "Open an SSH session for CONFIG, a <machine-ssh-configuration> record."
112 (let ((host-name (machine-ssh-configuration-host-name config))
113 (user (machine-ssh-configuration-user config))
114 (port (machine-ssh-configuration-port config))
115 (identity (machine-ssh-configuration-identity config))
116 (host-key (machine-ssh-configuration-host-key config)))
117 (unless host-key
118 (warning (G_ "<machine-ssh-configuration> without a 'host-key' \
119is deprecated~%")))
120 (open-ssh-session host-name
121 #:user user
122 #:port port
123 #:identity identity
124 #:host-key host-key)))
125
126(define open-machine-ssh-session*
127 (mlambdaq (config)
128 "Memoizing variant of 'open-machine-ssh-session'."
129 (open-machine-ssh-session config)))
130
105(define (machine-ssh-session machine) 131(define (machine-ssh-session machine)
106 "Return the SSH session that was given in MACHINE's configuration, or create 132 "Return the SSH session that was given in MACHINE's configuration, or create
107one from the configuration's parameters if one was not provided." 133one from the configuration's parameters if one was not provided."
108 (maybe-raise-unsupported-configuration-error machine) 134 (maybe-raise-unsupported-configuration-error machine)
109 (let ((config (machine-configuration machine))) 135 (let ((config (machine-configuration machine)))
110 (or (machine-ssh-configuration-session config) 136 (or (machine-ssh-configuration-session config)
111 (let ((host-name (machine-ssh-configuration-host-name config)) 137 (open-machine-ssh-session config))))
112 (user (machine-ssh-configuration-user config))
113 (port (machine-ssh-configuration-port config))
114 (identity (machine-ssh-configuration-identity config))
115 (host-key (machine-ssh-configuration-host-key config)))
116 (unless host-key
117 (warning (G_ "<machine-ssh-configuration> without a 'host-key' \
118is deprecated~%")))
119 (open-ssh-session host-name
120 #:user user
121 #:port port
122 #:identity identity
123 #:host-key host-key)))))
124 138
125 139
126;;; 140;;;