utils.scm 11 KB

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