remote.scm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #:use-module (ice-9 format)
  35. #:use-module (ice-9 match)
  36. #:export (remote-eval))
  37. ;;; Commentary:
  38. ;;;
  39. ;;; Note: This API is experimental and subject to change!
  40. ;;;
  41. ;;; Evaluate a gexp on a remote machine, over SSH, ensuring that all the
  42. ;;; elements the gexp refers to are deployed beforehand. This is useful for
  43. ;;; expressions that have side effects; for pure expressions, you would rather
  44. ;;; build a derivation remotely or offload it.
  45. ;;;
  46. ;;; Code:
  47. (define* (remote-pipe-for-gexp lowered session #:optional become-command)
  48. "Return a remote pipe for the given SESSION to evaluate LOWERED. If
  49. BECOME-COMMAND is given, use that to invoke the remote Guile REPL."
  50. (define shell-quote
  51. (compose object->string object->string))
  52. (define repl-command
  53. (append (or become-command '())
  54. (list
  55. (string-append (derivation-input-output-path
  56. (lowered-gexp-guile lowered))
  57. "/bin/guile")
  58. "--no-auto-compile")
  59. (append-map (lambda (directory)
  60. `("-L" ,directory))
  61. (lowered-gexp-load-path lowered))
  62. (append-map (lambda (directory)
  63. `("-C" ,directory))
  64. (lowered-gexp-load-path lowered))
  65. `("-c"
  66. ,(shell-quote (lowered-gexp-sexp lowered)))))
  67. (let ((pipe (apply open-remote-pipe* session OPEN_READ repl-command)))
  68. (when (eof-object? (peek-char pipe))
  69. (let ((status (channel-get-exit-status pipe)))
  70. (close-port pipe)
  71. (raise (formatted-message (G_ "remote command '~{~a~^ ~}' failed \
  72. with status ~a")
  73. repl-command status))))
  74. pipe))
  75. (define* (%remote-eval lowered session #:optional become-command)
  76. "Evaluate LOWERED, a lowered gexp, in SESSION. This assumes that all the
  77. prerequisites of EXP are already available on the host at SESSION. If
  78. BECOME-COMMAND is given, use that to invoke the remote Guile REPL."
  79. (let* ((pipe (remote-pipe-for-gexp lowered session become-command))
  80. (result (read-repl-response pipe)))
  81. (close-port pipe)
  82. result))
  83. (define (trampoline exp)
  84. "Return a \"trampoline\" gexp that evaluates EXP and writes the evaluation
  85. result to the current output port using the (guix repl) protocol."
  86. (define program
  87. (program-file "remote-exp.scm" exp))
  88. (with-imported-modules (source-module-closure '((guix repl)))
  89. #~(begin
  90. (use-modules (guix repl))
  91. ;; We use CURRENT-OUTPUT-PORT for REPL messages, so redirect PROGRAM's
  92. ;; output to CURRENT-ERROR-PORT so that it does not interfere.
  93. (send-repl-response '(with-output-to-port (current-error-port)
  94. (lambda ()
  95. (primitive-load #$program)))
  96. (current-output-port))
  97. (force-output))))
  98. (define* (remote-eval exp session
  99. #:key
  100. (build-locally? #t)
  101. (system (%current-system))
  102. (module-path %load-path)
  103. (socket-name (%daemon-socket-uri))
  104. (become-command #f))
  105. "Evaluate EXP, a gexp, on the host at SESSION, an SSH session. Ensure that
  106. all the elements EXP refers to are built and deployed to SESSION beforehand.
  107. When BUILD-LOCALLY? is true, said dependencies are built locally and sent to
  108. the remote store afterwards; otherwise, dependencies are built directly on the
  109. remote store."
  110. (mlet* %store-monad ((lowered (lower-gexp (trampoline exp)
  111. #:system system
  112. #:guile-for-build #f
  113. #:module-path %load-path))
  114. (remote -> (connect-to-remote-daemon session
  115. socket-name)))
  116. (define inputs
  117. (cons (lowered-gexp-guile lowered)
  118. (lowered-gexp-inputs lowered)))
  119. (define sources
  120. (lowered-gexp-sources lowered))
  121. (if build-locally?
  122. (let ((to-send (append (append-map derivation-input-output-paths
  123. inputs)
  124. sources)))
  125. (mbegin %store-monad
  126. (built-derivations inputs)
  127. ((store-lift send-files) to-send remote #:recursive? #t)
  128. (return (close-connection remote))
  129. (return (%remote-eval lowered session become-command))))
  130. (let ((to-send (append (map (compose derivation-file-name
  131. derivation-input-derivation)
  132. inputs)
  133. sources)))
  134. (mbegin %store-monad
  135. ((store-lift send-files) to-send remote #:recursive? #t)
  136. (return (build-derivations remote inputs))
  137. (return (close-connection remote))
  138. (return (%remote-eval lowered session become-command)))))))