unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob 86834a7afb3969355503a57a6505d484f6b44825 5763 bytes (raw)
name: guix/scripts/discover.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
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; 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 (guix scripts discover)
  #:use-module (guix avahi)
  #:use-module (guix config)
  #:use-module (guix scripts)
  #:use-module (guix ui)
  #:use-module (guix build syscalls)
  #:use-module (guix build utils)
  #:use-module (guix scripts publish)
  #:use-module (ice-9 rdelim)
  #:use-module (srfi srfi-37)
  #:export (read-substitute-urls

            guix-discover))

(define (show-help)
  (format #t (G_ "Usage: guix discover [OPTION]...
Discover Guix related services using Avahi.\n"))
  (display (G_ "
  -c, --cache=DIRECTORY     cache discovery results in DIRECTORY"))
  (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 '(#\c "cache") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'cache arg result)))
        (option '(#\h "help") #f #f
                (lambda _
                  (show-help)
                  (exit 0)))
        (option '(#\V "version") #f #f
                (lambda _
                  (show-version-and-exit "guix discover")))))

(define %default-options
  `((cache . ,%state-directory)))

\f
;;;
;;; Publish servers.
;;;

(define %publish-services
  ;; Set of discovered publish services.
  (make-hash-table))

(define (publish-file cache-directory)
  "Return the name of the file storing the discovered publish services inside
CACHE-DIRECTORY."
  (let ((directory (string-append cache-directory "/discover")))
    (string-append directory "/publish")))

(define %publish-file
  (make-parameter (publish-file %state-directory)))

;; XXX: Copied from (system base compile).
(define (call-once thunk)
  (let ((entered #f))
    (dynamic-wind
        (lambda ()
          (when entered
            (error "thunk may only be entered once: ~a" thunk))
          (set! entered #t))
        thunk
        (lambda () #t))))

(define* (call-with-output-file/atomic filename proc #:optional reference)
  (let* ((template (string-append filename ".XXXXXX"))
         (tmp (mkstemp! template "wb")))
    (call-once
     (lambda ()
       (with-throw-handler #t
         (lambda ()
           (proc tmp)
           ;; Chmodding by name instead of by port allows this chmod to
           ;; work on systems without fchmod, like MinGW.
           (let ((perms (or (false-if-exception (stat:perms (stat reference)))
                            (lognot (umask)))))
             (chmod template (logand #o0666 perms)))
           (close-port tmp)
           (rename-file template filename))
         (lambda args
           (close-port tmp)
           (delete-file template)))))))

(define* (write-publish-file #:key (file (%publish-file)))
  "Dump the content of %PUBLISH-SERVICES hash table into FILE.  Use a write
lock on FILE to synchronize with any potential readers."
  (call-with-output-file/atomic file
    (lambda (port)
      (hash-for-each
       (lambda (name service)
         (format port "http://~a:~a~%"
                 (avahi-service-address service)
                 (avahi-service-port service)))
       %publish-services)))
  (chmod file #o644))

(define* (read-substitute-urls #:key (file (%publish-file)))
  "Read substitute urls list from FILE and return it.  Use a read lock on FILE
to synchronize with the writer."
  (if (file-exists? file)
      (call-with-input-file file
        (lambda (port)
          (let loop ((url (read-line port))
                     (urls '()))
            (if (eof-object? url)
                urls
                (loop (read-line port) (cons url urls))))))
      '()))

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

(define %services
  ;; List of services we want to discover.
  (list publish-service-type))

(define (service-proc action service)
  (let ((name (avahi-service-name service))
        (type (avahi-service-type service)))
    (when (string=? type publish-service-type)
      (case action
        ((new-service)
         (hash-set! %publish-services name service))
        ((remove-service)
         (hash-remove! %publish-services name)))
      (write-publish-file))))

(define-command (guix-discover . args)
  (category internal)
  (synopsis "discover Guix related services using Avahi")

  (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))
           (cache (assoc-ref opts 'cache))
           (publish-file (publish-file cache)))
      (parameterize ((%publish-file publish-file))
        (mkdir-p (dirname publish-file))
        (avahi-browse-service-thread service-proc
                                     #:types %services)))))

debug log:

solving 86834a7afb ...
found 86834a7afb in https://yhetil.org/guix-patches/20201207131706.96073-1-othacehe@gnu.org/
found 007db0d49d in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 007db0d49d5b487fc326d5e82db909234ae39f05	guix/scripts/discover.scm

applying [1/1] https://yhetil.org/guix-patches/20201207131706.96073-1-othacehe@gnu.org/
diff --git a/guix/scripts/discover.scm b/guix/scripts/discover.scm
index 007db0d49d..86834a7afb 100644

Checking patch guix/scripts/discover.scm...
Applied patch guix/scripts/discover.scm cleanly.

index at:
100644 86834a7afb3969355503a57a6505d484f6b44825	guix/scripts/discover.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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).