docker-socket.scm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. (library (docker-socket)
  2. (export connect-to-docker-socket
  3. call-with-output-docker-socket)
  4. (import (rnrs base)
  5. (only (guile)
  6. lambda* λ
  7. ;; sockets stuff
  8. AF_UNIX
  9. PF_UNIX
  10. SOCK_STREAM
  11. SOL_SOCKET
  12. SO_REUSEADDR
  13. make-socket-address
  14. socket
  15. setsockopt
  16. connect
  17. close)
  18. (web client)
  19. (web uri)
  20. (ice-9 iconv)))
  21. (define connect-to-docker-socket
  22. (lambda* (#:key (socket-path "/var/run/docker.sock"))
  23. (let ([docker-sock-addr (make-socket-address AF_UNIX socket-path)]
  24. [docker-sock (socket PF_UNIX SOCK_STREAM 0)])
  25. ;; socket options:
  26. ;; https://www.gnu.org/software/libc/manual/html_node/Socket_002dLevel-Options.html
  27. (setsockopt docker-sock SOL_SOCKET SO_REUSEADDR 1)
  28. ;; usage of connect:
  29. ;; https://www.gnu.org/software/guile/manual/html_node/Network-Sockets-and-Communication.html#Network-Sockets-and-Communication
  30. ;; server side would use `bind`, `accept` and `listen`.
  31. ;; client side uses `connect` and `close`.
  32. (connect docker-sock docker-sock-addr)
  33. docker-sock)))
  34. (define call-with-output-docker-socket
  35. (lambda* (proc #:key (socket-location "/var/run/docker.sock"))
  36. (let ([socket (connect-to-docker-socket #:socket-path socket-location)])
  37. (proc socket)
  38. (close socket))))