diff options
| author | Mathieu Othacehe <othacehe@gnu.org> | 2020-06-09 10:33:04 +0200 |
|---|---|---|
| committer | Mathieu Othacehe <othacehe@gnu.org> | 2020-06-09 10:33:04 +0200 |
| commit | 5f7c4416b5afb95ece26bdb1d000e026387d002f (patch) | |
| tree | 48e96c93adac5a612c300b1eedb25f4ef78157b6 /gnu | |
| parent | f73ed5579157a074093bae7a2ffb59a85412be0d (diff) | |
Revert "installer: utils: Dump command output to syslog when testing."
This reverts commit f73ed5579157a074093bae7a2ffb59a85412be0d. This was pushed
by error, as this is not reviewed yet.
Diffstat (limited to 'gnu')
| -rw-r--r-- | gnu/installer/utils.scm | 164 |
1 files changed, 44 insertions, 120 deletions
diff --git a/gnu/installer/utils.scm b/gnu/installer/utils.scm index d73698df156..5f8fe8ca019 100644 --- a/gnu/installer/utils.scm +++ b/gnu/installer/utils.scm | |||
| @@ -22,13 +22,8 @@ | |||
| 22 | #:use-module (guix build utils) | 22 | #:use-module (guix build utils) |
| 23 | #:use-module (guix i18n) | 23 | #:use-module (guix i18n) |
| 24 | #:use-module (srfi srfi-1) | 24 | #:use-module (srfi srfi-1) |
| 25 | #:use-module (srfi srfi-11) | ||
| 26 | #:use-module (srfi srfi-26) | ||
| 27 | #:use-module (srfi srfi-34) | 25 | #:use-module (srfi srfi-34) |
| 28 | #:use-module (srfi srfi-34) | ||
| 29 | #:use-module (srfi srfi-35) | ||
| 30 | #:use-module (ice-9 match) | 26 | #:use-module (ice-9 match) |
| 31 | #:use-module (ice-9 popen) | ||
| 32 | #:use-module (ice-9 rdelim) | 27 | #:use-module (ice-9 rdelim) |
| 33 | #:use-module (ice-9 regex) | 28 | #:use-module (ice-9 regex) |
| 34 | #:use-module (ice-9 format) | 29 | #:use-module (ice-9 format) |
| @@ -73,6 +68,50 @@ number. If no percentage is found, return #f" | |||
| 73 | (and result | 68 | (and result |
| 74 | (string->number (match:substring result 1))))) | 69 | (string->number (match:substring result 1))))) |
| 75 | 70 | ||
| 71 | (define* (run-command command #:key locale) | ||
| 72 | "Run COMMAND, a list of strings, in the given LOCALE. Return true if | ||
| 73 | COMMAND exited successfully, #f otherwise." | ||
| 74 | (define env (environ)) | ||
| 75 | |||
| 76 | (define (pause) | ||
| 77 | (format #t (G_ "Press Enter to continue.~%")) | ||
| 78 | (send-to-clients '(pause)) | ||
| 79 | (environ env) ;restore environment variables | ||
| 80 | (match (select (cons (current-input-port) (current-clients)) | ||
| 81 | '() '()) | ||
| 82 | (((port _ ...) _ _) | ||
| 83 | (read-line port)))) | ||
| 84 | |||
| 85 | (setenv "PATH" "/run/current-system/profile/bin") | ||
| 86 | |||
| 87 | (when locale | ||
| 88 | (let ((supported? (false-if-exception | ||
| 89 | (setlocale LC_ALL locale)))) | ||
| 90 | ;; If LOCALE is not supported, then set LANGUAGE, which might at | ||
| 91 | ;; least give us translated messages. | ||
| 92 | (if supported? | ||
| 93 | (setenv "LC_ALL" locale) | ||
| 94 | (setenv "LANGUAGE" | ||
| 95 | (string-take locale | ||
| 96 | (or (string-index locale #\_) | ||
| 97 | (string-length locale))))))) | ||
| 98 | |||
| 99 | (guard (c ((invoke-error? c) | ||
| 100 | (newline) | ||
| 101 | (format (current-error-port) | ||
| 102 | (G_ "Command failed with exit code ~a.~%") | ||
| 103 | (invoke-error-exit-status c)) | ||
| 104 | (syslog "command ~s failed with exit code ~a" | ||
| 105 | command (invoke-error-exit-status c)) | ||
| 106 | (pause) | ||
| 107 | #f)) | ||
| 108 | (syslog "running command ~s~%" command) | ||
| 109 | (apply invoke command) | ||
| 110 | (syslog "command ~s succeeded~%" command) | ||
| 111 | (newline) | ||
| 112 | (pause) | ||
| 113 | #t)) | ||
| 114 | |||
| 76 | 115 | ||
| 77 | ;;; | 116 | ;;; |
| 78 | ;;; Logging. | 117 | ;;; Logging. |
| @@ -180,118 +219,3 @@ accepting socket." | |||
| 180 | 219 | ||
| 181 | (current-clients (reverse remainder)) | 220 | (current-clients (reverse remainder)) |
| 182 | exp) | 221 | exp) |
| 183 | |||
| 184 | |||
| 185 | ;;; | ||
| 186 | ;;; Run commands. | ||
| 187 | ;;; | ||
| 188 | |||
| 189 | ;; XXX: This is taken from (guix build utils) and could be factorized. | ||
| 190 | (define (open-pipe-with-stderr program . args) | ||
| 191 | "Run PROGRAM with ARGS in an input pipe, but, unlike 'open-pipe*', redirect | ||
| 192 | both its standard output and standard error to the pipe. Return two value: | ||
| 193 | the pipe to read PROGRAM's data from, and the PID of the child process running | ||
| 194 | PROGRAM." | ||
| 195 | ;; 'open-pipe*' doesn't attempt to capture stderr in any way, which is why | ||
| 196 | ;; we need to roll our own. | ||
| 197 | (match (pipe) | ||
| 198 | ((input . output) | ||
| 199 | (match (primitive-fork) | ||
| 200 | (0 | ||
| 201 | (dynamic-wind | ||
| 202 | (const #t) | ||
| 203 | (lambda () | ||
| 204 | (close-port input) | ||
| 205 | (close-port (syslog-port)) | ||
| 206 | (dup2 (fileno output) 1) | ||
| 207 | (dup2 (fileno output) 2) | ||
| 208 | (apply execlp program program args)) | ||
| 209 | (lambda () | ||
| 210 | (primitive-exit 127)))) | ||
| 211 | (pid | ||
| 212 | (close-port output) | ||
| 213 | (values input pid)))))) | ||
| 214 | |||
| 215 | (define invoke-log-port | ||
| 216 | ;; Port used by INVOKE-WITH-LOG for logging. | ||
| 217 | (make-parameter #f)) | ||
| 218 | |||
| 219 | (define* (invoke-with-log program . args) | ||
| 220 | "Invoke PROGRAM with ARGS and log PROGRAM's standard output and standard | ||
| 221 | error to INVOKE-LOG-PORT. If PROGRAM succeeds, print nothing and return the | ||
| 222 | unspecified value; otherwise, raise a '&message' error condition with the | ||
| 223 | status code. This procedure is very similar to INVOKE/QUIET with the | ||
| 224 | noticeable difference that the program output, that can be quite heavy, is not | ||
| 225 | stored but directly sent to INVOKE-LOG-PORT if defined." | ||
| 226 | (let-values (((pipe pid) | ||
| 227 | (apply open-pipe-with-stderr program args))) | ||
| 228 | (let loop () | ||
| 229 | (match (read-line pipe) | ||
| 230 | ((? eof-object?) | ||
| 231 | (close-port pipe) | ||
| 232 | (match (waitpid pid) | ||
| 233 | ((_ . status) | ||
| 234 | (unless (zero? status) | ||
| 235 | (raise | ||
| 236 | (condition (&invoke-error | ||
| 237 | (program program) | ||
| 238 | (arguments args) | ||
| 239 | (exit-status (status:exit-val status)) | ||
| 240 | (term-signal (status:term-sig status)) | ||
| 241 | (stop-signal (status:stop-sig status))))))))) | ||
| 242 | (line | ||
| 243 | (and=> (invoke-log-port) (cut format <> "~a~%" line)) | ||
| 244 | (loop)))))) | ||
| 245 | |||
| 246 | (define* (run-command command #:key locale) | ||
| 247 | "Run COMMAND, a list of strings, in the given LOCALE. Return true if | ||
| 248 | COMMAND exited successfully, #f otherwise." | ||
| 249 | (define env (environ)) | ||
| 250 | |||
| 251 | (define (pause) | ||
| 252 | (format #t (G_ "Press Enter to continue.~%")) | ||
| 253 | (send-to-clients '(pause)) | ||
| 254 | (environ env) ;restore environment variables | ||
| 255 | (match (select (cons (current-input-port) (current-clients)) | ||
| 256 | '() '()) | ||
| 257 | (((port _ ...) _ _) | ||
| 258 | (read-line port)))) | ||
| 259 | |||
| 260 | (setenv "PATH" "/run/current-system/profile/bin") | ||
| 261 | |||
| 262 | (when locale | ||
| 263 | (let ((supported? (false-if-exception | ||
| 264 | (setlocale LC_ALL locale)))) | ||
| 265 | ;; If LOCALE is not supported, then set LANGUAGE, which might at | ||
| 266 | ;; least give us translated messages. | ||
| 267 | (if supported? | ||
| 268 | (setenv "LC_ALL" locale) | ||
| 269 | (setenv "LANGUAGE" | ||
| 270 | (string-take locale | ||
| 271 | (or (string-index locale #\_) | ||
| 272 | (string-length locale))))))) | ||
| 273 | |||
| 274 | (guard (c ((invoke-error? c) | ||
| 275 | (newline) | ||
| 276 | (format (current-error-port) | ||
| 277 | (G_ "Command failed with exit code ~a.~%") | ||
| 278 | (invoke-error-exit-status c)) | ||
| 279 | (syslog "command ~s failed with exit code ~a" | ||
| 280 | command (invoke-error-exit-status c)) | ||
| 281 | (pause) | ||
| 282 | #f)) | ||
| 283 | (syslog "running command ~s~%" command) | ||
| 284 | ;; If there are any connected clients, assume that we are running | ||
| 285 | ;; installation tests. In that case, dump the standard and error outputs | ||
| 286 | ;; to syslog. | ||
| 287 | (let ((testing? (not (null? (current-clients))))) | ||
| 288 | (if testing? | ||
| 289 | (parameterize ((invoke-log-port (syslog-port))) | ||
| 290 | (apply invoke-with-log command)) | ||
| 291 | (apply invoke command))) | ||
| 292 | (syslog "command ~s succeeded~%" command) | ||
| 293 | (newline) | ||
| 294 | (pause) | ||
| 295 | #t)) | ||
| 296 | |||
| 297 | ;;; utils.scm ends here | ||
