remote.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix remote)
  19. #:use-module (guix ssh)
  20. #:use-module (guix gexp)
  21. #:use-module (guix i18n)
  22. #:use-module ((guix diagnostics) #:select (formatted-message))
  23. #:use-module (guix inferior)
  24. #:use-module (guix store)
  25. #:use-module (guix monads)
  26. #:use-module (guix modules)
  27. #:use-module (guix derivations)
  28. #:use-module (guix utils)
  29. #:use-module (ssh popen)
  30. #:use-module (ssh channel)
  31. #:use-module (srfi srfi-1)
  32. #:use-module (srfi srfi-34)
  33. #:use-module (srfi srfi-35)
  34. #:export (remote-eval))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; Note: This API is experimental and subject to change!
  38. ;;;
  39. ;;; Evaluate a gexp on a remote machine, over SSH, ensuring that all the
  40. ;;; elements the gexp refers to are deployed beforehand. This is useful for
  41. ;;; expressions that have side effects; for pure expressions, you would rather
  42. ;;; build a derivation remotely or offload it.
  43. ;;;
  44. ;;; Code:
  45. (define* (remote-pipe-for-gexp lowered session #:optional become-command)
  46. "Return a remote pipe for the given SESSION to evaluate LOWERED. If
  47. BECOME-COMMAND is given, use that to invoke the remote Guile REPL."
  48. (define shell-quote
  49. (compose object->string object->string))
  50. (define repl-command
  51. (append (or become-command '())
  52. (list
  53. (string-append (derivation-input-output-path
  54. (lowered-gexp-guile lowered))
  55. "/bin/guile")
  56. "--no-auto-compile")
  57. (append-map (lambda (directory)
  58. `("-L" ,directory))
  59. (lowered-gexp-load-path lowered))
  60. (append-map (lambda (directory)
  61. `("-C" ,directory))
  62. (lowered-gexp-load-path lowered))
  63. `("-c"
  64. ,(shell-quote (lowered-gexp-sexp lowered)))))
  65. (let ((pipe (apply open-remote-pipe* session OPEN_READ repl-command)))
  66. (when (eof-object? (peek-char pipe))
  67. (let ((status (channel-get-exit-status pipe)))
  68. (close-port pipe)
  69. (raise (formatted-message (G_ "remote command '~{~a~^ ~}' failed \
  70. with status ~a")
  71. repl-command status))))
  72. pipe))
  73. (define* (%remote-eval lowered session #:optional become-command)
  74. "Evaluate LOWERED, a lowered gexp, in SESSION. This assumes that all the
  75. prerequisites of EXP are already available on the host at SESSION. If
  76. BECOME-COMMAND is given, use that to invoke the remote Guile REPL."
  77. (let* ((pipe (remote-pipe-for-gexp lowered session become-command))
  78. (result (read-repl-response pipe)))
  79. (close-port pipe)
  80. result))
  81. (define (trampoline exp)
  82. "Return a \"trampoline\" gexp that evaluates EXP and writes the evaluation
  83. result to the current output port using the (guix repl) protocol."
  84. (define program
  85. (program-file "remote-exp.scm" exp))
  86. (with-imported-modules (source-module-closure '((guix repl)))
  87. #~(begin
  88. (use-modules (guix repl))
  89. ;; We use CURRENT-OUTPUT-PORT for REPL messages, so redirect PROGRAM's
  90. ;; output to CURRENT-ERROR-PORT so that it does not interfere.
  91. (send-repl-response '(with-output-to-port (current-error-port)
  92. (lambda ()
  93. (primitive-load #$program)))
  94. (current-output-port))
  95. (force-output))))
  96. (define* (remote-eval exp session
  97. #:key
  98. (build-locally? #t)
  99. (system (%current-system))
  100. (module-path %load-path)
  101. (socket-name (%daemon-socket-uri))
  102. (become-command #f))
  103. "Evaluate EXP, a gexp, on the host at SESSION, an SSH session. Ensure that
  104. all the elements EXP refers to are built and deployed to SESSION beforehand.
  105. When BUILD-LOCALLY? is true, said dependencies are built locally and sent to
  106. the remote store afterwards; otherwise, dependencies are built directly on the
  107. remote store."
  108. (mlet* %store-monad ((lowered (lower-gexp (trampoline exp)
  109. #:system system
  110. #:guile-for-build #f
  111. #:module-path %load-path))
  112. (remote -> (connect-to-remote-daemon session
  113. socket-name)))
  114. (define inputs
  115. (cons (lowered-gexp-guile lowered)
  116. (lowered-gexp-inputs lowered)))
  117. (define sources
  118. (lowered-gexp-sources lowered))
  119. (if build-locally?
  120. (let ((to-send (append (append-map derivation-input-output-paths
  121. inputs)
  122. sources)))
  123. (mbegin %store-monad
  124. (built-derivations inputs)
  125. ((store-lift send-files) to-send remote #:recursive? #t)
  126. (return (close-connection remote))
  127. (return (%remote-eval lowered session become-command))))
  128. (let ((to-send (append (map (compose derivation-file-name
  129. derivation-input-derivation)
  130. inputs)
  131. sources)))
  132. (mbegin %store-monad
  133. ((store-lift send-files) to-send remote #:recursive? #t)
  134. (return (build-derivations remote inputs))
  135. (return (close-connection remote))
  136. (return (%remote-eval lowered session become-command)))))))