Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.5 KiB

  1. ;;; docker-tramp.el --- TRAMP integration for docker containers -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2015 Mario Rodas <marsam@users.noreply.github.com>
  3. ;; Author: Mario Rodas <marsam@users.noreply.github.com>
  4. ;; URL: https://github.com/emacs-pe/docker-tramp.el
  5. ;; Keywords: docker, convenience
  6. ;; Version: 0.1
  7. ;; Package-Requires: ((emacs "24") (cl-lib "0.5"))
  8. ;; This file is NOT part of GNU Emacs.
  9. ;;; License:
  10. ;; This program is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;
  22. ;; `docker-tramp.el' offers a TRAMP method for Docker containers.
  23. ;;
  24. ;; > **NOTE**: `docker-tramp.el' relies in the `docker exec` command. Tested
  25. ;; > with docker version 1.6.x but should work with versions >1.3
  26. ;;
  27. ;; ## Usage
  28. ;;
  29. ;; Offers the TRAMP method `docker` to access running containers
  30. ;;
  31. ;; C-x C-f /docker:user@container:/path/to/file
  32. ;;
  33. ;; where
  34. ;; user is the user that you want to use (optional)
  35. ;; container is the id or name of the container
  36. ;;
  37. ;; ## Troubleshooting
  38. ;;
  39. ;; ### Tramp hangs on Alpine container
  40. ;;
  41. ;; Busyboxes built with the `ENABLE_FEATURE_EDITING_ASK_TERMINAL' config option
  42. ;; send also escape sequences, which `tramp-wait-for-output' doesn't ignores
  43. ;; correctly. Tramp upstream fixed in [98a5112][] and is available since
  44. ;; Tramp>=2.3.
  45. ;;
  46. ;; For older versions of Tramp you can dump [docker-tramp-compat.el][] in your
  47. ;; `load-path' somewhere and add the following to your `init.el', which
  48. ;; overwrites `tramp-wait-for-output' with the patch applied:
  49. ;;
  50. ;; (require 'tramp-docker-compat)
  51. ;;
  52. ;; [98a5112]: http://git.savannah.gnu.org/cgit/tramp.git/commit/?id=98a511248a9405848ed44de48a565b0b725af82c
  53. ;; [docker-tramp-compat.el]: https://github.com/emacs-pe/docker-tramp.el/raw/master/docker-tramp-compat.el
  54. ;;; Code:
  55. (eval-when-compile (require 'cl-lib))
  56. (require 'tramp)
  57. (require 'tramp-cache)
  58. (defgroup docker-tramp nil
  59. "TRAMP integration for Docker containers."
  60. :prefix "docker-tramp-"
  61. :group 'applications
  62. :link '(url-link :tag "Github" "https://github.com/emacs-pe/docker-tramp.el")
  63. :link '(emacs-commentary-link :tag "Commentary" "docker-tramp"))
  64. (defcustom docker-tramp-docker-executable "docker"
  65. "Path to docker executable."
  66. :type 'string
  67. :group 'docker-tramp)
  68. ;;;###autoload
  69. (defcustom docker-tramp-docker-options nil
  70. "List of docker options."
  71. :type '(repeat string)
  72. :group 'docker-tramp)
  73. (defcustom docker-tramp-use-names nil
  74. "Whether use names instead of id."
  75. :type 'boolean
  76. :group 'docker-tramp)
  77. ;;;###autoload
  78. (defconst docker-tramp-completion-function-alist
  79. '((docker-tramp--parse-running-containers ""))
  80. "Default list of (FUNCTION FILE) pairs to be examined for docker method.")
  81. ;;;###autoload
  82. (defconst docker-tramp-method "docker"
  83. "Method to connect docker containers.")
  84. (defun docker-tramp--running-containers ()
  85. "Collect docker running containers.
  86. Return a list of containers of the form: \(ID NAME\)"
  87. (cl-loop for line in (cdr (ignore-errors (apply #'process-lines docker-tramp-docker-executable (append docker-tramp-docker-options (list "ps")))))
  88. for info = (split-string line "[[:space:]]+" t)
  89. collect (cons (car info) (last info))))
  90. (defun docker-tramp--parse-running-containers (&optional ignored)
  91. "Return a list of (user host) tuples.
  92. TRAMP calls this function with a filename which is IGNORED. The
  93. user is an empty string because the docker TRAMP method uses bash
  94. to connect to the default user containers."
  95. (cl-loop for (id name) in (docker-tramp--running-containers)
  96. collect (list "" (if docker-tramp-use-names name id))))
  97. ;;;###autoload
  98. (defun docker-tramp-cleanup ()
  99. "Cleanup TRAMP cache for docker method."
  100. (interactive)
  101. (let ((containers (apply 'append (docker-tramp--running-containers))))
  102. (maphash (lambda (key _)
  103. (and (vectorp key)
  104. (string-equal docker-tramp-method (tramp-file-name-method key))
  105. (not (member (tramp-file-name-host key) containers))
  106. (remhash key tramp-cache-data)))
  107. tramp-cache-data))
  108. (setq tramp-cache-data-changed t)
  109. (if (zerop (hash-table-count tramp-cache-data))
  110. (ignore-errors (delete-file tramp-persistency-file-name))
  111. (tramp-dump-connection-properties)))
  112. ;;;###autoload
  113. (defun docker-tramp-add-method ()
  114. "Add docker tramp method."
  115. (add-to-list 'tramp-methods
  116. `(,docker-tramp-method
  117. (tramp-login-program ,docker-tramp-docker-executable)
  118. (tramp-login-args (,docker-tramp-docker-options ("exec" "-it") ("-u" "%u") ("%h") ("sh")))
  119. (tramp-remote-shell "/bin/sh")
  120. (tramp-remote-shell-args ("-i" "-c")))))
  121. ;;;###autoload
  122. (eval-after-load 'tramp
  123. '(progn
  124. (docker-tramp-add-method)
  125. (tramp-set-completion-function docker-tramp-method docker-tramp-completion-function-alist)))
  126. (provide 'docker-tramp)
  127. ;; Local Variables:
  128. ;; indent-tabs-mode: nil
  129. ;; End:
  130. ;;; docker-tramp.el ends here