all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob c253ee562a921730368819d434a6fbe65fc81073 9887 bytes (raw)
name: src/cuirass/remote-worker.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
 
;;; remote-worker.scm -- Remote build worker.
;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
;;;
;;; This file is part of Cuirass.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (cuirass remote-worker)
  #:use-module (cuirass remote)
  #:use-module (gcrypt pk-crypto)
  #:use-module (guix)
  #:use-module (guix avahi)
  #:use-module (guix config)
  #:use-module (guix diagnostics)
  #:use-module (guix pki)
  #:use-module (guix records)
  #:use-module (guix scripts)
  #:use-module (guix ui)
  #:use-module (guix build syscalls)
  #:use-module (guix scripts publish)
  #:use-module (simple-zmq)
  #:use-module (rnrs bytevectors)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-37)
  #:use-module (ice-9 atomic)
  #:use-module (ice-9 match)
  #:use-module (ice-9 threads)

  #:export (remote-worker))

;; Indicate if the process has to be stopped.
(define %stop-process?
  (make-atomic-box #f))

(define (show-help)
  (format #t (G_ "Usage: remote-worker [OPTION]...
Start a remote build worker.\n"))
  (display (G_ "
  -w, --workers=COUNT       start COUNT parallel workers"))
  (display (G_ "
  -p, --publish-port=PORT   publish substitutes on PORT"))
  (display (G_ "
      --public-key=FILE     use FILE as the public key for signatures"))
  (display (G_ "
      --private-key=FILE    use FILE as the private key for signatures"))
  (newline)
  (display (G_ "
  -h, --help                display this help and exit"))
  (display (G_ "
  -V, --version             display version information and exit"))
  (newline)
  (show-bug-report-information))

(define %options
  (list (option '(#\h "help") #f #f
                (lambda _
                  (show-help)
                  (exit 0)))
        (option '(#\V "version") #f #f
                (lambda _
                  (show-version-and-exit "guix publish")))
        (option '(#\w "workers") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'workers (string->number* arg) result)))
        (option '(#\p "publish-port") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'publish-port (string->number* arg) result)))
        (option '("public-key") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'public-key-file arg result)))
        (option '("private-key") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'private-key-file arg result)))))

(define %default-options
  `((workers . 1)
    (publish-port . 5558)
    (public-key-file . ,%public-key-file)
    (private-key-file . ,%private-key-file)))

\f
;;;
;;; ZMQ connection.
;;;

(define %zmq-context
  (zmq-create-context))

(define (zmq-backend-endpoint address port)
  "Return a ZMQ endpoint identifying the build server available by TCP at
ADDRESS and PORT."
  (string-append "tcp://" address ":" (number->string port)))

(define (zmq-dealer-socket)
  "The ZMQ socket to communicate with the worker threads."
  (zmq-create-socket %zmq-context ZMQ_DEALER))

\f
;;;
;;; Worker.
;;;

;; The port of the local publish server.
(define %local-publish-port
  (make-atomic-box #f))

(define (server-publish-url address port)
  "Return the server publish url at ADDRESS and PORT."
  (string-append "http://" address ":" (number->string port)))

(define (service-txt->publish-port txt)
  "Parse the service TXT record and return the server publish port."
  (define (parse-txt)
    (fold (lambda (param params)
            (match (string-split param #\=)
              ((key value)
               (cons (cons (string->symbol key) value)
                     params))))
          '()
          txt))

  (let ((params (parse-txt)))
    (string->number (assq-ref params 'publish))))

(define (service->publish-url service)
  "Return the URL of the publish server corresponding to the service with the
given NAME."
  (let* ((address (avahi-service-address service))
         (txt (avahi-service-txt service))
         (publish-port
          (service-txt->publish-port txt)))
    (server-publish-url address publish-port)))

(define (service->local-publish-url service)
  "Return the URL of the local publish server."
  (let* ((local-address (avahi-service-local-address service))
         (port (atomic-box-ref %local-publish-port)))
    (server-publish-url local-address port)))

(define* (run-build drv service #:key reply)
  "Build DRV and send messages upon build start, failure or completion to the
build server identified by SERVICE-NAME using the REPLY procedure.

The publish server of the build server is added to the list of the store
substitutes-urls.  This way derivations that are not present on the worker can
still be substituted."
  (with-store store
    (let ((publish-url (service->publish-url service))
          (local-publish-url (service->local-publish-url service)))
      (add-substitute-url store publish-url)
      (reply (zmq-build-started-message drv))
      (guard (c ((store-protocol-error? c)
                 (info (G_ "Derivation `~a' build failed: ~a~%")
                       drv (store-protocol-error-message c))
                 (reply (zmq-build-failed-message drv))))
        (if (build-derivations store (list drv))
            (reply (zmq-build-succeeded-message drv local-publish-url))
            (reply (zmq-build-failed-message drv)))))))

(define* (run-command command service #:key reply)
  "Run COMMAND.  SERVICE-NAME is the name of the build server that sent the
command.  REPLY is a procedure that can be used to reply to this server."
  (match (zmq-read-message command)
    (('build ('drv drv) ('system system))
     (info (G_ "Building `~a' derivation.~%") drv)
     (run-build drv service #:reply reply))
    (('no-build)
     #t)))

(define (start-worker worker service)
  "Start a worker thread named NAME, reading commands from the DEALER socket
and executing them.  The worker can reply on the same socket."
  (define (reply socket client)
    (lambda (message)
      (zmq-send-msg-parts-bytevector
       socket
       (list (zmq-empty-delimiter) client
             (zmq-empty-delimiter) (string->bv message)))))

  (define (ready socket)
    (zmq-send-msg-parts-bytevector
     socket
     (list (make-bytevector 0)
           (string->bv
            (zmq-worker-ready-message (worker->sexp worker))))))

  (define (request-work socket)
    (let ((name (worker-name worker)))
      (zmq-send-msg-parts-bytevector
       socket
       (list (make-bytevector 0)
             (string->bv (zmq-worker-request-work-message name))))))

  (call-with-new-thread
   (lambda ()
     (set-thread-name (worker-name worker))
     (let* ((socket (zmq-dealer-socket))
            (address (avahi-service-address service))
            (port (avahi-service-port service))
            (endpoint (zmq-backend-endpoint address port)))
       (zmq-connect socket endpoint)
       (ready socket)
       (let loop ()
         (request-work socket)
         (match (zmq-get-msg-parts-bytevector socket '())
           ((empty client empty command)
            (run-command (bv->string command) service
                         #:reply (reply socket client))))
         (sleep 1)
         (loop))))))

\f
;;;
;;; Entry point.
;;;

;; The PID of the publish process.
(define %publish-pid
  (make-atomic-box #f))

(define (signal-handler)
  "Catch SIGINT to stop the Avahi event loop and the publish process before
exiting."
  (sigaction SIGINT
    (lambda (signum)
      (let ((publish-pid (atomic-box-ref %publish-pid)))
        (atomic-box-set! %stop-process? #t)

        (and publish-pid
             (begin
               (kill publish-pid SIGHUP)
               (waitpid publish-pid)))

        (exit 1)))))

(define (remote-worker args)
  (with-error-handling
    (let* ((opts (args-fold* args %options
                             (lambda (opt name arg result)
                               (leave (G_ "~A: unrecognized option~%") name))
                             (lambda (arg result)
                               (leave (G_ "~A: extraneous argument~%") arg))
                             %default-options))
           (workers (assoc-ref opts 'workers))
           (publish-port (assoc-ref opts 'publish-port))
           (public-key
            (read-file-sexp
             (assoc-ref opts 'public-key-file)))
           (private-key
            (read-file-sexp
             (assoc-ref opts 'private-key-file))))

      (atomic-box-set! %local-publish-port publish-port)

      (atomic-box-set!
       %publish-pid
       (publish-server publish-port
                       #:public-key public-key
                       #:private-key private-key))

      (avahi-browse-service-thread
       (lambda (action service)
         (case action
           ((new-service)
            (for-each (lambda (n)
                        (start-worker (worker
                                       (name (generate-worker-name))
                                       (systems '("x86_64-linux")))
                                      service))
                      (iota workers)))))
       #:types (list remote-server-service-type)
       #:stop-loop? (lambda ()
                      (atomic-box-ref %stop-process?))))))

debug log:

solving c253ee5 ...
found c253ee5 in https://yhetil.org/guix/87czzso4dj.fsf@gnu.org/

applying [1/1] https://yhetil.org/guix/87czzso4dj.fsf@gnu.org/
diff --git a/src/cuirass/remote-worker.scm b/src/cuirass/remote-worker.scm
new file mode 100644
index 0000000..c253ee5

Checking patch src/cuirass/remote-worker.scm...
Applied patch src/cuirass/remote-worker.scm cleanly.

index at:
100644 c253ee562a921730368819d434a6fbe65fc81073	src/cuirass/remote-worker.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.