discover.scm 5.2 KB

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