discover.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020, 2021 Mathieu Othacehe <othacehe@gnu.org>
  3. ;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
  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 (guix scripts discover)
  20. #:use-module (guix avahi)
  21. #:use-module (guix config)
  22. #:use-module (guix scripts)
  23. #:use-module (guix ui)
  24. #:use-module (guix utils)
  25. #:use-module (guix build utils)
  26. #:use-module (guix scripts publish)
  27. #:use-module (avahi)
  28. #:use-module (ice-9 rdelim)
  29. #:use-module (srfi srfi-37)
  30. #:export (read-substitute-urls
  31. guix-discover))
  32. (define (show-help)
  33. (format #t (G_ "Usage: guix discover [OPTION]...
  34. Discover Guix related services using Avahi.\n"))
  35. (display (G_ "
  36. -c, --cache=DIRECTORY cache discovery results in DIRECTORY"))
  37. (display (G_ "
  38. -h, --help display this help and exit"))
  39. (display (G_ "
  40. -V, --version display version information and exit"))
  41. (newline)
  42. (show-bug-report-information))
  43. (define %options
  44. (list (option '(#\c "cache") #t #f
  45. (lambda (opt name arg result)
  46. (alist-cons 'cache arg result)))
  47. (option '(#\h "help") #f #f
  48. (lambda _
  49. (show-help)
  50. (exit 0)))
  51. (option '(#\V "version") #f #f
  52. (lambda _
  53. (show-version-and-exit "guix discover")))))
  54. (define %default-options
  55. `((cache . ,%state-directory)))
  56. ;;;
  57. ;;; Publish servers.
  58. ;;;
  59. (define %publish-services
  60. ;; Set of discovered publish services.
  61. (make-hash-table))
  62. (define (publish-file cache-directory)
  63. "Return the name of the file storing the discovered publish services inside
  64. CACHE-DIRECTORY."
  65. (let ((directory (string-append cache-directory "/discover")))
  66. (string-append directory "/publish")))
  67. (define %publish-file
  68. (make-parameter (publish-file %state-directory)))
  69. (define* (write-publish-file #:key (file (%publish-file)))
  70. "Dump the content of %PUBLISH-SERVICES hash table into FILE. Use a write
  71. lock on FILE to synchronize with any potential readers."
  72. (with-atomic-file-output file
  73. (lambda (port)
  74. (hash-for-each
  75. (lambda (name service)
  76. (format port "http://~a:~a~%"
  77. (avahi-service-address service)
  78. (avahi-service-port service)))
  79. %publish-services)))
  80. (chmod file #o644))
  81. (define* (read-substitute-urls #:key (file (%publish-file)))
  82. "Read substitute urls list from FILE and return it. Use a read lock on FILE
  83. to synchronize with the writer."
  84. (if (file-exists? file)
  85. (call-with-input-file file
  86. (lambda (port)
  87. (let loop ((url (read-line port))
  88. (urls '()))
  89. (if (eof-object? url)
  90. urls
  91. (loop (read-line port) (cons url urls))))))
  92. '()))
  93. ;;;
  94. ;;; Entry point.
  95. ;;;
  96. (define %services
  97. ;; List of services we want to discover.
  98. (list publish-service-type))
  99. (define (service-proc action service)
  100. (let ((name (avahi-service-name service))
  101. (type (avahi-service-type service)))
  102. (when (string=? type publish-service-type)
  103. (case action
  104. ((new-service)
  105. (hash-set! %publish-services name service))
  106. ((remove-service)
  107. (hash-remove! %publish-services name)))
  108. (write-publish-file))))
  109. (define-command (guix-discover . args)
  110. (category internal)
  111. (synopsis "discover Guix related services using Avahi")
  112. (with-error-handling
  113. (let* ((opts (parse-command-line args %options (list %default-options)
  114. #:build-options? #f
  115. #:argument-handler
  116. (lambda (arg result)
  117. (leave (G_ "~A: extraneous argument~%") arg))))
  118. (cache (assoc-ref opts 'cache))
  119. (publish-file (publish-file cache)))
  120. (parameterize ((%publish-file publish-file))
  121. (mkdir-p (dirname publish-file))
  122. (false-if-exception (delete-file publish-file))
  123. (catch 'avahi-error
  124. (lambda ()
  125. (avahi-browse-service-thread service-proc
  126. #:types %services))
  127. (lambda (key err function . _)
  128. (cond
  129. ((eq? err error/no-daemon)
  130. (warning (G_ "Avahi daemon is not running, \
  131. cannot auto-discover substitutes servers.~%")))
  132. (else
  133. (report-error (G_ "an Avahi error was raised by `~a': ~a~%")
  134. function (error->string err))))
  135. (exit 1)))))))