copy.scm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2019, 2020, 2021 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 scripts copy)
  19. #:use-module (guix ui)
  20. #:use-module (guix scripts)
  21. #:use-module (guix ssh)
  22. #:use-module ((ssh session) #:select (disconnect!))
  23. #:use-module (guix store)
  24. #:use-module ((guix status) #:select (with-status-verbosity))
  25. #:use-module (guix utils)
  26. #:use-module (guix derivations)
  27. #:use-module (guix scripts build)
  28. #:use-module ((guix scripts archive) #:select (options->derivations+files))
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-11)
  31. #:use-module (srfi srfi-37)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 format)
  34. #:export (guix-copy))
  35. ;;;
  36. ;;; Exchanging store items over SSH.
  37. ;;;
  38. (define (ssh-spec->user+host+port spec)
  39. "Parse SPEC, a string like \"user@host:port\" or just \"host\", and return
  40. three values: the user name (or #f), the host name, and the TCP port
  41. number (or #f) corresponding to SPEC."
  42. (define tokens
  43. (char-set #\@ #\:))
  44. (match (string-tokenize spec (char-set-complement tokens))
  45. ((host)
  46. (values #f host #f))
  47. ((left right)
  48. (if (string-index spec #\@)
  49. (values left right #f)
  50. (values #f left (string->number right))))
  51. ((user host port)
  52. (match (string->number port)
  53. ((? integer? port)
  54. (values user host port))
  55. (x
  56. (leave (G_ "~a: invalid TCP port number~%") port))))
  57. (x
  58. (leave (G_ "~a: invalid SSH specification~%") spec))))
  59. (define (warn-if-empty items)
  60. (when (null? items)
  61. (warning (G_ "no arguments specified, nothing to copy~%"))))
  62. (define (send-to-remote-host local target opts)
  63. "Send ITEMS to TARGET. ITEMS is a list of store items or package names; for ;
  64. package names, build the underlying packages before sending them."
  65. (let-values (((user host port)
  66. (ssh-spec->user+host+port target))
  67. ((drv items)
  68. (options->derivations+files local opts)))
  69. (warn-if-empty items)
  70. (and (build-derivations local drv)
  71. (let* ((session (open-ssh-session host #:user user
  72. #:port (or port 22)))
  73. (remote (connect-to-remote-daemon session))
  74. (sent (send-files local items remote
  75. #:recursive? #t)))
  76. (close-connection remote)
  77. (format #t "~{~a~%~}" sent)
  78. sent))))
  79. (define (retrieve-from-remote-host local source opts)
  80. "Retrieve ITEMS from SOURCE."
  81. (let*-values (((user host port)
  82. (ssh-spec->user+host+port source))
  83. ((session)
  84. (open-ssh-session host #:user user #:port (or port 22)))
  85. ((remote)
  86. (connect-to-remote-daemon session)))
  87. ;; TODO: Here we could to compute and build the derivations on REMOTE
  88. ;; rather than on LOCAL (one-off offloading) but that is currently too
  89. ;; slow due to the many RPC round trips. So we just assume that REMOTE
  90. ;; contains ITEMS.
  91. (let*-values (((drv items)
  92. (options->derivations+files local opts))
  93. ((retrieved)
  94. (begin
  95. (warn-if-empty items)
  96. (retrieve-files local items remote #:recursive? #t))))
  97. (close-connection remote)
  98. (disconnect! session)
  99. (format #t "~{~a~%~}" retrieved)
  100. retrieved)))
  101. ;;;
  102. ;;; Options.
  103. ;;;
  104. (define (show-help)
  105. (display (G_ "Usage: guix copy [OPTION]... ITEMS...
  106. Copy ITEMS to or from the specified host over SSH.\n"))
  107. (display (G_ "
  108. --to=HOST send ITEMS to HOST"))
  109. (display (G_ "
  110. --from=HOST receive ITEMS from HOST"))
  111. (display (G_ "
  112. -v, --verbosity=LEVEL use the given verbosity LEVEL"))
  113. (newline)
  114. (show-build-options-help)
  115. (newline)
  116. (display (G_ "
  117. -h, --help display this help and exit"))
  118. (display (G_ "
  119. -V, --version display version information and exit"))
  120. (newline)
  121. (show-bug-report-information))
  122. (define %options
  123. ;; Specifications of the command-line options.
  124. (cons* (option '("to") #t #f
  125. (lambda (opt name arg result)
  126. (alist-cons 'destination arg result)))
  127. (option '("from") #t #f
  128. (lambda (opt name arg result)
  129. (alist-cons 'source arg result)))
  130. (option '(#\v "verbosity") #t #f
  131. (lambda (opt name arg result)
  132. (let ((level (string->number* arg)))
  133. (alist-cons 'verbosity level
  134. (alist-delete 'verbosity result)))))
  135. (option '(#\n "dry-run") #f #f
  136. (lambda (opt name arg result)
  137. (alist-cons 'dry-run? #t result)))
  138. (option '(#\h "help") #f #f
  139. (lambda args
  140. (show-help)
  141. (exit 0)))
  142. (option '(#\V "version") #f #f
  143. (lambda args
  144. (show-version-and-exit "guix copy")))
  145. (option '(#\s "system") #t #f
  146. (lambda (opt name arg result)
  147. (alist-cons 'system arg
  148. (alist-delete 'system result eq?))))
  149. %standard-build-options))
  150. (define %default-options
  151. `((system . ,(%current-system))
  152. (substitutes? . #t)
  153. (offload? . #t)
  154. (graft? . #t)
  155. (print-build-trace? . #t)
  156. (print-extended-build-trace? . #t)
  157. (multiplexed-build-output? . #t)
  158. (debug . 0)
  159. (verbosity . 3)))
  160. ;;;
  161. ;;; Entry point.
  162. ;;;
  163. (define-command (guix-copy . args)
  164. (category plumbing)
  165. (synopsis "copy store items remotely over SSH")
  166. (with-error-handling
  167. (let* ((opts (parse-command-line args %options (list %default-options)))
  168. (source (assoc-ref opts 'source))
  169. (target (assoc-ref opts 'destination)))
  170. (with-store store
  171. (set-build-options-from-command-line store opts)
  172. (with-build-handler (build-notifier #:use-substitutes?
  173. (assoc-ref opts 'substitutes?)
  174. #:verbosity
  175. (assoc-ref opts 'verbosity)
  176. #:dry-run?
  177. (assoc-ref opts 'dry-run?))
  178. (with-status-verbosity (assoc-ref opts 'verbosity)
  179. (cond (target (send-to-remote-host store target opts))
  180. (source (retrieve-from-remote-host store source opts))
  181. (else (leave (G_ "use '--to' or '--from'~%"))))))))))