discover.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020 Mathieu Othacehe <othacehe@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix scripts discover)
  19. #:use-module (guix avahi)
  20. #:use-module (guix config)
  21. #:use-module (guix scripts)
  22. #:use-module (guix ui)
  23. #:use-module (guix utils)
  24. #:use-module (guix build syscalls)
  25. #:use-module (guix build utils)
  26. #:use-module (guix scripts publish)
  27. #:use-module (ice-9 rdelim)
  28. #:use-module (srfi srfi-37)
  29. #:export (read-substitute-urls
  30. guix-discover))
  31. (define (show-help)
  32. (format #t (G_ "Usage: guix discover [OPTION]...
  33. Discover Guix related services using Avahi.\n"))
  34. (display (G_ "
  35. -c, --cache=DIRECTORY cache discovery results in DIRECTORY"))
  36. (display (G_ "
  37. -h, --help display this help and exit"))
  38. (display (G_ "
  39. -V, --version display version information and exit"))
  40. (newline)
  41. (show-bug-report-information))
  42. (define %options
  43. (list (option '(#\c "cache") #t #f
  44. (lambda (opt name arg result)
  45. (alist-cons 'cache arg result)))
  46. (option '(#\h "help") #f #f
  47. (lambda _
  48. (show-help)
  49. (exit 0)))
  50. (option '(#\V "version") #f #f
  51. (lambda _
  52. (show-version-and-exit "guix discover")))))
  53. (define %default-options
  54. `((cache . ,%state-directory)))
  55. ;;;
  56. ;;; Publish servers.
  57. ;;;
  58. (define %publish-services
  59. ;; Set of discovered publish services.
  60. (make-hash-table))
  61. (define (publish-file cache-directory)
  62. "Return the name of the file storing the discovered publish services inside
  63. CACHE-DIRECTORY."
  64. (let ((directory (string-append cache-directory "/discover")))
  65. (string-append directory "/publish")))
  66. (define %publish-file
  67. (make-parameter (publish-file %state-directory)))
  68. (define* (write-publish-file #:key (file (%publish-file)))
  69. "Dump the content of %PUBLISH-SERVICES hash table into FILE. Use a write
  70. lock on FILE to synchronize with any potential readers."
  71. (with-atomic-file-output file
  72. (lambda (port)
  73. (hash-for-each
  74. (lambda (name service)
  75. (format port "http://~a:~a~%"
  76. (avahi-service-address service)
  77. (avahi-service-port service)))
  78. %publish-services)))
  79. (chmod file #o644))
  80. (define* (read-substitute-urls #:key (file (%publish-file)))
  81. "Read substitute urls list from FILE and return it. Use a read lock on FILE
  82. to synchronize with the writer."
  83. (if (file-exists? file)
  84. (call-with-input-file file
  85. (lambda (port)
  86. (let loop ((url (read-line port))
  87. (urls '()))
  88. (if (eof-object? url)
  89. urls
  90. (loop (read-line port) (cons url urls))))))
  91. '()))
  92. ;;;
  93. ;;; Entry point.
  94. ;;;
  95. (define %services
  96. ;; List of services we want to discover.
  97. (list publish-service-type))
  98. (define (service-proc action service)
  99. (let ((name (avahi-service-name service))
  100. (type (avahi-service-type service)))
  101. (when (string=? type publish-service-type)
  102. (case action
  103. ((new-service)
  104. (hash-set! %publish-services name service))
  105. ((remove-service)
  106. (hash-remove! %publish-services name)))
  107. (write-publish-file))))
  108. (define-command (guix-discover . args)
  109. (category internal)
  110. (synopsis "discover Guix related services using Avahi")
  111. (with-error-handling
  112. (let* ((opts (args-fold* args %options
  113. (lambda (opt name arg result)
  114. (leave (G_ "~A: unrecognized option~%") name))
  115. (lambda (arg result)
  116. (leave (G_ "~A: extraneous argument~%") arg))
  117. %default-options))
  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. (avahi-browse-service-thread service-proc
  124. #:types %services)))))