utils.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gnu installer utils)
  20. #:use-module (gnu services herd)
  21. #:use-module (guix utils)
  22. #:use-module (guix build utils)
  23. #:use-module (guix i18n)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-9)
  26. #:use-module (srfi srfi-9 gnu)
  27. #:use-module (srfi srfi-19)
  28. #:use-module (srfi srfi-34)
  29. #:use-module (srfi srfi-35)
  30. #:use-module (ice-9 control)
  31. #:use-module (ice-9 match)
  32. #:use-module (ice-9 popen)
  33. #:use-module (ice-9 rdelim)
  34. #:use-module (ice-9 regex)
  35. #:use-module (ice-9 format)
  36. #:use-module (ice-9 textual-ports)
  37. #:export (<secret>
  38. secret?
  39. make-secret
  40. secret-content
  41. read-lines
  42. read-all
  43. nearest-exact-integer
  44. read-percentage
  45. run-external-command-with-handler
  46. run-external-command-with-line-hooks
  47. run-command
  48. run-command-in-installer
  49. syslog-port
  50. %syslog-line-hook
  51. installer-log-port
  52. %installer-log-line-hook
  53. %default-installer-line-hooks
  54. installer-log-line
  55. call-with-time
  56. let/time
  57. with-server-socket
  58. current-server-socket
  59. current-clients
  60. send-to-clients
  61. with-silent-shepherd))
  62. (define-record-type <secret>
  63. (make-secret content)
  64. secret?
  65. (content secret-content))
  66. (set-record-type-printer!
  67. <secret>
  68. (lambda (secret port)
  69. (format port "<secret>")))
  70. (define* (read-lines #:optional (port (current-input-port)))
  71. "Read lines from PORT and return them as a list."
  72. (let loop ((line (read-line port))
  73. (lines '()))
  74. (if (eof-object? line)
  75. (reverse lines)
  76. (loop (read-line port)
  77. (cons line lines)))))
  78. (define (read-all file)
  79. "Return the content of the given FILE as a string."
  80. (call-with-input-file file
  81. get-string-all))
  82. (define (nearest-exact-integer x)
  83. "Given a real number X, return the nearest exact integer, with ties going to
  84. the nearest exact even integer."
  85. (inexact->exact (round x)))
  86. (define (read-percentage percentage)
  87. "Read PERCENTAGE string and return the corresponding percentage as a
  88. number. If no percentage is found, return #f"
  89. (let ((result (string-match "^([0-9]+)%$" percentage)))
  90. (and result
  91. (string->number (match:substring result 1)))))
  92. (define* (run-external-command-with-handler handler command)
  93. "Run command specified by the list COMMAND in a child with output handler
  94. HANDLER. HANDLER is a procedure taking an input port, to which the command
  95. will write its standard output and error. Returns the integer status value of
  96. the child process as returned by waitpid."
  97. (match-let (((input . output) (pipe)))
  98. ;; Hack to work around Guile bug 52835
  99. (define dup-output (duplicate-port output "w"))
  100. ;; Void pipe, but holds the pid for close-pipe.
  101. (define dummy-pipe
  102. (with-input-from-file "/dev/null"
  103. (lambda ()
  104. (with-output-to-port output
  105. (lambda ()
  106. (with-error-to-port dup-output
  107. (lambda ()
  108. (apply open-pipe* (cons "" command)))))))))
  109. (close-port output)
  110. (close-port dup-output)
  111. (handler input)
  112. (close-port input)
  113. (close-pipe dummy-pipe)))
  114. (define (run-external-command-with-line-hooks line-hooks command)
  115. "Run command specified by the list COMMAND in a child, processing each
  116. output line with the procedures in LINE-HOOKS. Returns the integer status
  117. value of the child process as returned by waitpid."
  118. (define (handler input)
  119. (and
  120. (and=> (get-line input)
  121. (lambda (line)
  122. (if (eof-object? line)
  123. #f
  124. (begin (for-each (lambda (f) (f line))
  125. (append line-hooks
  126. %default-installer-line-hooks))
  127. #t))))
  128. (handler input)))
  129. (run-external-command-with-handler handler command))
  130. (define* (run-command command)
  131. "Run COMMAND, a list of strings. Return true if COMMAND exited
  132. successfully, #f otherwise."
  133. (define (pause)
  134. (format #t (G_ "Press Enter to continue.~%"))
  135. (send-to-clients '(pause))
  136. (match (select (cons (current-input-port) (current-clients))
  137. '() '())
  138. (((port _ ...) _ _)
  139. (read-line port))))
  140. (installer-log-line "running command ~s" command)
  141. (define result (run-external-command-with-line-hooks
  142. (list %display-line-hook)
  143. command))
  144. (define exit-val (status:exit-val result))
  145. (define term-sig (status:term-sig result))
  146. (define stop-sig (status:stop-sig result))
  147. (define succeeded?
  148. (cond
  149. ((and exit-val (not (zero? exit-val)))
  150. (installer-log-line "command ~s exited with value ~a"
  151. command exit-val)
  152. (format #t (G_ "Command ~s exited with value ~a")
  153. command exit-val)
  154. #f)
  155. (term-sig
  156. (installer-log-line "command ~s killed by signal ~a"
  157. command term-sig)
  158. (format #t (G_ "Command ~s killed by signal ~a")
  159. command term-sig)
  160. #f)
  161. (stop-sig
  162. (installer-log-line "command ~s stopped by signal ~a"
  163. command stop-sig)
  164. (format #t (G_ "Command ~s stopped by signal ~a")
  165. command stop-sig)
  166. #f)
  167. (else
  168. (installer-log-line "command ~s succeeded" command)
  169. (format #t (G_ "Command ~s succeeded") command)
  170. #t)))
  171. (newline)
  172. (pause)
  173. succeeded?)
  174. (define run-command-in-installer
  175. (make-parameter
  176. (lambda (. args)
  177. (raise
  178. (condition
  179. (&serious)
  180. (&message (message "run-command-in-installer not set")))))))
  181. ;;;
  182. ;;; Logging.
  183. ;;;
  184. (define (call-with-time thunk kont)
  185. "Call THUNK and pass KONT the elapsed time followed by THUNK's return
  186. values."
  187. (let* ((start (current-time time-monotonic))
  188. (result (call-with-values thunk list))
  189. (end (current-time time-monotonic)))
  190. (apply kont (time-difference end start) result)))
  191. (define-syntax-rule (let/time ((time result exp)) body ...)
  192. (call-with-time (lambda () exp) (lambda (time result) body ...)))
  193. (define (open-syslog-port)
  194. "Return an open port (a socket) to /dev/log or #f if that wasn't possible."
  195. (let ((sock (socket AF_UNIX SOCK_DGRAM 0)))
  196. (catch 'system-error
  197. (lambda ()
  198. (connect sock AF_UNIX "/dev/log")
  199. (setvbuf sock 'line)
  200. sock)
  201. (lambda args
  202. (close-port sock)
  203. #f))))
  204. (define syslog-port
  205. (let ((port #f))
  206. (lambda ()
  207. "Return an output port to syslog."
  208. (unless port
  209. (set! port (open-syslog-port)))
  210. (or port (%make-void-port "w")))))
  211. (define (%syslog-line-hook line)
  212. (format (syslog-port) "installer[~d]: ~a~%" (getpid) line))
  213. (define-syntax syslog
  214. (lambda (s)
  215. "Like 'format', but write to syslog."
  216. (syntax-case s ()
  217. ((_ fmt args ...)
  218. (string? (syntax->datum #'fmt))
  219. (with-syntax ((fmt (string-append "installer[~d]: "
  220. (syntax->datum #'fmt))))
  221. #'(format (syslog-port) fmt (getpid) args ...))))))
  222. (define (open-new-log-port)
  223. (define now (localtime (time-second (current-time))))
  224. (define filename
  225. (format #f "/tmp/installer.~a.log"
  226. (strftime "%F.%T" now)))
  227. (open filename (logior O_RDWR
  228. O_CREAT)))
  229. (define installer-log-port
  230. (let ((port #f))
  231. (lambda ()
  232. "Return an input and output port to the installer log."
  233. (unless port
  234. (set! port (open-new-log-port)))
  235. port)))
  236. (define (%installer-log-line-hook line)
  237. (format (installer-log-port) "~a~%" line))
  238. (define (%display-line-hook line)
  239. (display line)
  240. (newline))
  241. (define %default-installer-line-hooks
  242. (list %syslog-line-hook
  243. %installer-log-line-hook))
  244. (define-syntax installer-log-line
  245. (lambda (s)
  246. "Like 'format', but uses the default line hooks, and only formats one line."
  247. (syntax-case s ()
  248. ((_ fmt args ...)
  249. (string? (syntax->datum #'fmt))
  250. #'(let ((formatted (format #f fmt args ...)))
  251. (for-each (lambda (f) (f formatted))
  252. %default-installer-line-hooks))))))
  253. ;;;
  254. ;;; Client protocol.
  255. ;;;
  256. (define %client-socket-file
  257. ;; Unix-domain socket where the installer accepts connections.
  258. "/var/guix/installer-socket")
  259. (define current-server-socket
  260. ;; Socket on which the installer is currently accepting connections, or #f.
  261. (make-parameter #f))
  262. (define current-clients
  263. ;; List of currently connected clients.
  264. (make-parameter '()))
  265. (define* (open-server-socket
  266. #:optional (socket-file %client-socket-file))
  267. "Open SOCKET-FILE as a Unix-domain socket to accept incoming connections and
  268. return it."
  269. (mkdir-p (dirname socket-file))
  270. (when (file-exists? socket-file)
  271. (delete-file socket-file))
  272. (let ((sock (socket AF_UNIX SOCK_STREAM 0)))
  273. (bind sock AF_UNIX socket-file)
  274. (listen sock 0)
  275. sock))
  276. (define (call-with-server-socket thunk)
  277. (if (current-server-socket)
  278. (thunk)
  279. (let ((socket (open-server-socket)))
  280. (dynamic-wind
  281. (const #t)
  282. (lambda ()
  283. (parameterize ((current-server-socket socket))
  284. (thunk)))
  285. (lambda ()
  286. (close-port socket))))))
  287. (define-syntax-rule (with-server-socket exp ...)
  288. "Evaluate EXP with 'current-server-socket' parameterized to a currently
  289. accepting socket."
  290. (call-with-server-socket (lambda () exp ...)))
  291. (define* (send-to-clients exp)
  292. "Send EXP to all the current clients."
  293. (define remainder
  294. (fold (lambda (client remainder)
  295. (catch 'system-error
  296. (lambda ()
  297. (write exp client)
  298. (newline client)
  299. (force-output client)
  300. (cons client remainder))
  301. (lambda args
  302. ;; We might get EPIPE if the client disconnects; when that
  303. ;; happens, remove CLIENT from the set of available clients.
  304. (let ((errno (system-error-errno args)))
  305. (if (memv errno (list EPIPE ECONNRESET ECONNABORTED))
  306. (begin
  307. (installer-log-line
  308. "removing client ~s due to ~s while replying"
  309. (fileno client) (strerror errno))
  310. (false-if-exception (close-port client))
  311. remainder)
  312. (cons client remainder))))))
  313. '()
  314. (current-clients)))
  315. (current-clients (reverse remainder))
  316. exp)
  317. (define-syntax-rule (with-silent-shepherd exp ...)
  318. "Evaluate EXP while discarding shepherd messages."
  319. (parameterize ((shepherd-message-port
  320. (%make-void-port "w")))
  321. exp ...))