2019-06-10 22:39:39 +02:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
|
|
|
;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
|
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix remote)
|
|
|
|
#:use-module (guix ssh)
|
|
|
|
#:use-module (guix gexp)
|
2019-08-15 18:09:58 +02:00
|
|
|
#:use-module (guix i18n)
|
2019-06-10 22:39:39 +02:00
|
|
|
#:use-module (guix inferior)
|
|
|
|
#:use-module (guix store)
|
|
|
|
#:use-module (guix monads)
|
|
|
|
#:use-module (guix modules)
|
|
|
|
#:use-module (guix derivations)
|
2019-08-09 20:24:57 +02:00
|
|
|
#:use-module (guix utils)
|
2019-06-10 22:39:39 +02:00
|
|
|
#:use-module (ssh popen)
|
2019-08-28 18:51:12 +02:00
|
|
|
#:use-module (ssh channel)
|
2019-06-10 22:39:39 +02:00
|
|
|
#:use-module (srfi srfi-1)
|
2019-08-15 10:05:04 +02:00
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
#:use-module (srfi srfi-35)
|
2019-06-10 22:39:39 +02:00
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:export (remote-eval))
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;;
|
|
|
|
;;; Note: This API is experimental and subject to change!
|
|
|
|
;;;
|
|
|
|
;;; Evaluate a gexp on a remote machine, over SSH, ensuring that all the
|
|
|
|
;;; elements the gexp refers to are deployed beforehand. This is useful for
|
|
|
|
;;; expressions that have side effects; for pure expressions, you would rather
|
|
|
|
;;; build a derivation remotely or offload it.
|
|
|
|
;;;
|
|
|
|
;;; Code:
|
|
|
|
|
2019-08-15 10:05:04 +02:00
|
|
|
(define* (remote-pipe-for-gexp lowered session #:optional become-command)
|
|
|
|
"Return a remote pipe for the given SESSION to evaluate LOWERED. If
|
|
|
|
BECOME-COMMAND is given, use that to invoke the remote Guile REPL."
|
2019-06-10 22:39:39 +02:00
|
|
|
(define shell-quote
|
|
|
|
(compose object->string object->string))
|
|
|
|
|
2019-08-15 10:05:04 +02:00
|
|
|
(define repl-command
|
|
|
|
(append (or become-command '())
|
|
|
|
(list
|
|
|
|
(string-append (derivation-input-output-path
|
|
|
|
(lowered-gexp-guile lowered))
|
|
|
|
"/bin/guile")
|
|
|
|
"--no-auto-compile")
|
|
|
|
(append-map (lambda (directory)
|
|
|
|
`("-L" ,directory))
|
|
|
|
(lowered-gexp-load-path lowered))
|
|
|
|
(append-map (lambda (directory)
|
|
|
|
`("-C" ,directory))
|
|
|
|
(lowered-gexp-load-path lowered))
|
|
|
|
`("-c"
|
|
|
|
,(shell-quote (lowered-gexp-sexp lowered)))))
|
2019-06-10 22:39:39 +02:00
|
|
|
|
2019-08-15 10:05:04 +02:00
|
|
|
(let ((pipe (apply open-remote-pipe* session OPEN_READ repl-command)))
|
|
|
|
(when (eof-object? (peek-char pipe))
|
2019-08-28 18:51:12 +02:00
|
|
|
(let ((status (channel-get-exit-status pipe)))
|
|
|
|
(close-port pipe)
|
|
|
|
(raise (condition
|
|
|
|
(&message
|
|
|
|
(message (format #f (G_ "remote command '~{~a~^ ~}' failed \
|
|
|
|
with status ~a")
|
|
|
|
repl-command status)))))))
|
2019-08-15 10:05:04 +02:00
|
|
|
pipe))
|
|
|
|
|
|
|
|
(define* (%remote-eval lowered session #:optional become-command)
|
2019-06-10 22:39:39 +02:00
|
|
|
"Evaluate LOWERED, a lowered gexp, in SESSION. This assumes that all the
|
2019-08-15 10:05:04 +02:00
|
|
|
prerequisites of EXP are already available on the host at SESSION. If
|
|
|
|
BECOME-COMMAND is given, use that to invoke the remote Guile REPL."
|
|
|
|
(let* ((pipe (remote-pipe-for-gexp lowered session become-command))
|
2019-06-10 22:39:39 +02:00
|
|
|
(result (read-repl-response pipe)))
|
|
|
|
(close-port pipe)
|
|
|
|
result))
|
|
|
|
|
|
|
|
(define (trampoline exp)
|
|
|
|
"Return a \"trampoline\" gexp that evaluates EXP and writes the evaluation
|
|
|
|
result to the current output port using the (guix repl) protocol."
|
|
|
|
(define program
|
2019-08-09 20:24:57 +02:00
|
|
|
(program-file "remote-exp.scm" exp))
|
2019-06-10 22:39:39 +02:00
|
|
|
|
|
|
|
(with-imported-modules (source-module-closure '((guix repl)))
|
|
|
|
#~(begin
|
|
|
|
(use-modules (guix repl))
|
2019-07-15 17:58:01 +02:00
|
|
|
|
|
|
|
;; We use CURRENT-OUTPUT-PORT for REPL messages, so redirect PROGRAM's
|
|
|
|
;; output to CURRENT-ERROR-PORT so that it does not interfere.
|
|
|
|
(send-repl-response '(with-output-to-port (current-error-port)
|
|
|
|
(lambda ()
|
|
|
|
(primitive-load #$program)))
|
2019-06-10 22:39:39 +02:00
|
|
|
(current-output-port))
|
2019-07-15 17:58:01 +02:00
|
|
|
|
2019-06-10 22:39:39 +02:00
|
|
|
(force-output))))
|
|
|
|
|
|
|
|
(define* (remote-eval exp session
|
|
|
|
#:key
|
|
|
|
(build-locally? #t)
|
2019-08-09 20:24:57 +02:00
|
|
|
(system (%current-system))
|
2019-06-10 22:39:39 +02:00
|
|
|
(module-path %load-path)
|
2019-08-15 10:08:22 +02:00
|
|
|
(socket-name (%daemon-socket-uri))
|
2019-08-15 10:05:04 +02:00
|
|
|
(become-command #f))
|
2019-06-10 22:39:39 +02:00
|
|
|
"Evaluate EXP, a gexp, on the host at SESSION, an SSH session. Ensure that
|
|
|
|
all the elements EXP refers to are built and deployed to SESSION beforehand.
|
|
|
|
When BUILD-LOCALLY? is true, said dependencies are built locally and sent to
|
|
|
|
the remote store afterwards; otherwise, dependencies are built directly on the
|
|
|
|
remote store."
|
2019-08-09 20:24:57 +02:00
|
|
|
(mlet* %store-monad ((lowered (lower-gexp (trampoline exp)
|
|
|
|
#:system system
|
|
|
|
#:guile-for-build #f
|
|
|
|
#:module-path %load-path))
|
|
|
|
(remote -> (connect-to-remote-daemon session
|
|
|
|
socket-name)))
|
2019-06-10 22:39:39 +02:00
|
|
|
(define inputs
|
2019-07-10 18:39:25 +02:00
|
|
|
(cons (lowered-gexp-guile lowered)
|
2019-06-10 22:39:39 +02:00
|
|
|
(lowered-gexp-inputs lowered)))
|
|
|
|
|
2019-07-09 23:05:01 +02:00
|
|
|
(define sources
|
|
|
|
(lowered-gexp-sources lowered))
|
2019-06-10 22:39:39 +02:00
|
|
|
|
|
|
|
(if build-locally?
|
2019-07-15 16:02:44 +02:00
|
|
|
(let ((to-send (append (append-map derivation-input-output-paths
|
|
|
|
inputs)
|
2019-07-09 23:05:01 +02:00
|
|
|
sources)))
|
2019-06-10 22:39:39 +02:00
|
|
|
(mbegin %store-monad
|
2019-07-09 23:05:01 +02:00
|
|
|
(built-derivations inputs)
|
2019-06-10 22:39:39 +02:00
|
|
|
((store-lift send-files) to-send remote #:recursive? #t)
|
|
|
|
(return (close-connection remote))
|
2019-08-15 10:05:04 +02:00
|
|
|
(return (%remote-eval lowered session become-command))))
|
2019-07-09 23:05:01 +02:00
|
|
|
(let ((to-send (append (map (compose derivation-file-name
|
|
|
|
derivation-input-derivation)
|
|
|
|
inputs)
|
|
|
|
sources)))
|
2019-06-10 22:39:39 +02:00
|
|
|
(mbegin %store-monad
|
|
|
|
((store-lift send-files) to-send remote #:recursive? #t)
|
2019-07-09 23:05:01 +02:00
|
|
|
(return (build-derivations remote inputs))
|
2019-06-10 22:39:39 +02:00
|
|
|
(return (close-connection remote))
|
2019-08-15 10:05:04 +02:00
|
|
|
(return (%remote-eval lowered session become-command)))))))
|