2018-09-10 20:51:14 +02:00
|
|
|
;;; docker-machine.el --- Emacs interface to docker-machine -*- lexical-binding: t -*-
|
|
|
|
|
|
|
|
;; Author: Ben Swift <ben@benswift.me>
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2018-10-02 15:54:39 +02:00
|
|
|
(require 's)
|
|
|
|
(require 'dash)
|
2018-09-10 20:51:14 +02:00
|
|
|
(require 'tablist)
|
2018-10-02 15:54:39 +02:00
|
|
|
(require 'magit-popup)
|
|
|
|
|
|
|
|
(require 'docker-group)
|
|
|
|
(require 'docker-utils)
|
|
|
|
|
|
|
|
(defgroup docker-machine nil
|
|
|
|
"Docker machine customization group."
|
|
|
|
:group 'docker)
|
|
|
|
|
|
|
|
(defcustom docker-machine-default-sort-key '("Name" . nil)
|
|
|
|
"Sort key for docker machines.
|
|
|
|
|
|
|
|
This should be a cons cell (NAME . FLIP) where
|
|
|
|
NAME is a string matching one of the column names
|
|
|
|
and FLIP is a boolean to specify the sort order."
|
|
|
|
:group 'docker-machine
|
|
|
|
:type '(cons (choice (const "Name")
|
|
|
|
(const "Active")
|
|
|
|
(const "Driver")
|
|
|
|
(const "State")
|
|
|
|
(const "URL")
|
|
|
|
(const "Swarm")
|
|
|
|
(const "Docker")
|
|
|
|
(const "Errors"))
|
|
|
|
(choice (const :tag "Ascending" nil)
|
|
|
|
(const :tag "Descending" t))))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
|
|
|
(defun docker-machine-parse (line)
|
|
|
|
"Convert a LINE from \"docker machine ls\" to a `tabulated-list-entries' entry."
|
|
|
|
(let ((data (s-split "\t" line)))
|
|
|
|
(list (car data) (apply #'vector data))))
|
|
|
|
|
|
|
|
(defun docker-machine-entries ()
|
|
|
|
"Return the docker machines data for `tabulated-list-entries'."
|
|
|
|
(let* ((fmt "{{.Name}}\\t{{.Active}}\\t{{.DriverName}}\\t{{.State}}\\t{{.URL}}\\t{{.Swarm}}\\t{{.DockerVersion}}\\t{{.Error}}")
|
2018-10-02 15:54:39 +02:00
|
|
|
(data (docker-machine-run "ls" docker-machine-ls-arguments (format "--format=\"%s\"" fmt)))
|
2018-09-10 20:51:14 +02:00
|
|
|
(lines (s-split "\n" data t)))
|
|
|
|
(-map #'docker-machine-parse lines)))
|
|
|
|
|
|
|
|
(defun docker-machine-refresh ()
|
|
|
|
"Refresh the machines list."
|
|
|
|
(setq tabulated-list-entries (docker-machine-entries)))
|
|
|
|
|
|
|
|
(defun docker-machine-read-name ()
|
|
|
|
"Read a machine name."
|
|
|
|
(completing-read "Machine: " (-map #'car (docker-machine-entries))))
|
|
|
|
|
|
|
|
(defun docker-machine-run (action &rest args)
|
2018-10-02 15:54:39 +02:00
|
|
|
"Execute \"docker-machine ACTION\" using ARGS."
|
|
|
|
(let ((command (format "docker-machine %s %s" action (s-join " " (-flatten (-non-nil args))))))
|
2018-09-10 20:51:14 +02:00
|
|
|
(message command)
|
|
|
|
(shell-command-to-string command)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-config (name)
|
|
|
|
"Print the connection config for the machine NAME."
|
|
|
|
(interactive (list (docker-machine-read-name)))
|
|
|
|
(docker-machine-run "config" name))
|
|
|
|
|
2018-10-02 15:54:39 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-create (name driver)
|
|
|
|
"Create a machine NAME using DRIVER."
|
|
|
|
(interactive "sName: \nsDriver: ")
|
|
|
|
(docker-machine-run "create" name "-d" driver))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-env (name)
|
|
|
|
"Parse and set environment variables from \"docker-machine env NAME\" output."
|
|
|
|
(interactive (list (docker-machine-read-name)))
|
|
|
|
(--each-while
|
|
|
|
(s-lines (docker-machine-run "env" name))
|
|
|
|
(s-prefix? "export" it)
|
|
|
|
(docker-machine-env-export it)))
|
|
|
|
|
|
|
|
(defun docker-machine-env-export (line)
|
|
|
|
"Export the env for LINE."
|
|
|
|
(let ((index (s-index-of "=" line)))
|
|
|
|
(unless index
|
|
|
|
(error (format "Cannot find separator in %s" line)))
|
|
|
|
(setenv (substring line (length "export ") index) (substring line (+ 2 index) -1))))
|
|
|
|
|
2018-09-10 20:51:14 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-inspect (name)
|
|
|
|
"Inspect information about the machine NAME."
|
|
|
|
(interactive (list (docker-machine-read-name)))
|
|
|
|
(docker-machine-run "inspect" name))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-ip (name)
|
|
|
|
"Get the IP address of the machine NAME."
|
|
|
|
(interactive (list (docker-machine-read-name)))
|
|
|
|
(docker-machine-run "ip" name))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-kill (name)
|
|
|
|
"Kill the machine NAME."
|
|
|
|
(interactive (list (docker-machine-read-name)))
|
|
|
|
(docker-machine-run "kill" name))
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-10-02 15:54:39 +02:00
|
|
|
(defun docker-machine-restart (name)
|
|
|
|
"Restart the machine NAME."
|
|
|
|
(interactive (list (docker-machine-read-name) current-prefix-arg))
|
|
|
|
(docker-machine-run "restart" name))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-rm (name &optional force)
|
|
|
|
"Destroy or uncommand the machine NAME. If FORCE is set, use \"--force\"."
|
|
|
|
(interactive (list (docker-machine-read-name) current-prefix-arg))
|
|
|
|
(docker-machine-run "rm" (when force "--force") name))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-start (name)
|
|
|
|
"Start the machine NAME."
|
|
|
|
(interactive (list (docker-machine-read-name)))
|
|
|
|
(docker-machine-run "start" name))
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-10-02 15:54:39 +02:00
|
|
|
(defun docker-machine-status (name)
|
|
|
|
"Get the status of the machine NAME."
|
2018-09-10 20:51:14 +02:00
|
|
|
(interactive (list (docker-machine-read-name)))
|
2018-10-02 15:54:39 +02:00
|
|
|
(docker-machine-run "status" name))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machine-stop (name)
|
|
|
|
"Stop the machine NAME."
|
|
|
|
(interactive (list (docker-machine-read-name) current-prefix-arg))
|
|
|
|
(docker-machine-run "stop" name))
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-10-02 15:54:39 +02:00
|
|
|
(defun docker-machine-upgrade (name)
|
|
|
|
"Upgrade the machine NAME to the latest version of Docker."
|
|
|
|
(interactive (list (docker-machine-read-name)))
|
|
|
|
(docker-machine-run "upgrade" name))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
2018-10-02 15:54:39 +02:00
|
|
|
(defun docker-machine-env-selection ()
|
|
|
|
"Run \"docker-machine env\" on selected machine."
|
2018-09-10 20:51:14 +02:00
|
|
|
(interactive)
|
2018-10-02 15:54:39 +02:00
|
|
|
(let ((marked (docker-utils-get-marked-items-ids)))
|
|
|
|
(when (/= (length marked) 1)
|
|
|
|
(error "Can only set environment vars for one machine at a time"))
|
|
|
|
(docker-machine-env (car marked))
|
|
|
|
(tablist-revert)))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
|
|
|
(defun docker-machine-restart-selection ()
|
|
|
|
"Run `docker-machine-restart' on the machines selection."
|
|
|
|
(interactive)
|
|
|
|
(--each (docker-utils-get-marked-items-ids)
|
2018-10-02 15:54:39 +02:00
|
|
|
(docker-machine-run "restart" (docker-machine-restart-arguments) it))
|
2018-09-10 20:51:14 +02:00
|
|
|
(tablist-revert))
|
|
|
|
|
|
|
|
(defun docker-machine-rm-selection ()
|
|
|
|
"Run `docker-machine-rm' on the machines selection."
|
|
|
|
(interactive)
|
|
|
|
(--each (docker-utils-get-marked-items-ids)
|
2018-10-02 15:54:39 +02:00
|
|
|
(docker-machine-run "rm" (docker-machine-rm-arguments) it))
|
2018-09-10 20:51:14 +02:00
|
|
|
(tablist-revert))
|
|
|
|
|
2018-10-02 15:54:39 +02:00
|
|
|
(defun docker-machine-start-selection ()
|
|
|
|
"Run `docker-machine-start' on the machines selection."
|
2018-09-10 20:51:14 +02:00
|
|
|
(interactive)
|
2018-10-02 15:54:39 +02:00
|
|
|
(--each (docker-utils-get-marked-items-ids)
|
|
|
|
(docker-machine-run "start" (docker-machine-start-arguments) it))
|
|
|
|
(tablist-revert))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
2018-10-02 15:54:39 +02:00
|
|
|
(defun docker-machine-stop-selection ()
|
|
|
|
"Run `docker-machine-stop' on the machines selection."
|
|
|
|
(interactive)
|
|
|
|
(--each (docker-utils-get-marked-items-ids)
|
|
|
|
(docker-machine-run "stop" (docker-machine-stop-arguments) it))
|
|
|
|
(tablist-revert))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
|
|
|
(magit-define-popup docker-machine-env-popup
|
|
|
|
"Popup for setting up environment variables."
|
2018-10-02 15:54:39 +02:00
|
|
|
'docker-machine
|
2018-09-10 20:51:14 +02:00
|
|
|
:man-page "docker-machine-env"
|
|
|
|
:actions '((?E "Env" docker-machine-env-selection))
|
|
|
|
:setup-function #'docker-utils-setup-popup)
|
|
|
|
|
2018-10-02 15:54:39 +02:00
|
|
|
(magit-define-popup docker-machine-ls-popup
|
|
|
|
"Popup for listing machines."
|
|
|
|
'docker-machine
|
|
|
|
:man-page "docker-machine-ls"
|
|
|
|
:options '((?f "Filter" "--filter ")
|
|
|
|
(?t "Timeout" "--timeout "))
|
|
|
|
:actions `((?l "List" ,(docker-utils-set-then-call 'docker-machine-ls-arguments 'tablist-revert))))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
|
|
|
(magit-define-popup docker-machine-restart-popup
|
|
|
|
"Popup for restarting machines."
|
2018-10-02 15:54:39 +02:00
|
|
|
'docker-machine
|
2018-09-10 20:51:14 +02:00
|
|
|
:man-page "docker-machine-restart"
|
|
|
|
:actions '((?R "Restart" docker-machine-restart-selection))
|
|
|
|
:setup-function #'docker-utils-setup-popup)
|
|
|
|
|
|
|
|
(magit-define-popup docker-machine-rm-popup
|
|
|
|
"Popup for removing machines."
|
2018-10-02 15:54:39 +02:00
|
|
|
'docker-machine
|
2018-09-10 20:51:14 +02:00
|
|
|
:man-page "docker-machine-rm"
|
2018-10-02 15:54:39 +02:00
|
|
|
:switches '((?f "Force" "-f")
|
|
|
|
(?y "Automatic yes" "-y"))
|
2018-09-10 20:51:14 +02:00
|
|
|
:actions '((?D "Remove" docker-machine-rm-selection))
|
|
|
|
:default-arguments '("-y")
|
|
|
|
:setup-function #'docker-utils-setup-popup)
|
|
|
|
|
2018-10-02 15:54:39 +02:00
|
|
|
(magit-define-popup docker-machine-start-popup
|
|
|
|
"Popup for starting machines."
|
|
|
|
'docker-machine
|
|
|
|
:man-page "docker-machine-start"
|
|
|
|
:actions '((?S "Start" docker-machine-start-selection))
|
|
|
|
:setup-function #'docker-utils-setup-popup)
|
|
|
|
|
|
|
|
(magit-define-popup docker-machine-stop-popup
|
|
|
|
"Popup for stoping machines."
|
|
|
|
'docker-machine
|
|
|
|
:man-page "docker-machine-stop"
|
|
|
|
:actions '((?O "Stop" docker-machine-stop-selection))
|
|
|
|
:setup-function #'docker-utils-setup-popup)
|
|
|
|
|
2018-09-10 20:51:14 +02:00
|
|
|
(magit-define-popup docker-machine-help-popup
|
|
|
|
"Help popup for docker machine."
|
2018-10-02 15:54:39 +02:00
|
|
|
'docker-machine
|
2018-09-10 20:51:14 +02:00
|
|
|
:actions '("Docker machines help"
|
|
|
|
(?C "Create" docker-machine-create)
|
|
|
|
(?D "Remove" docker-machine-rm-popup)
|
|
|
|
(?E "Env" docker-machine-env-popup)
|
|
|
|
(?O "Stop" docker-machine-stop-popup)
|
|
|
|
(?R "Restart" docker-machine-restart-popup)
|
|
|
|
(?S "Start" docker-machine-start-popup)
|
2018-10-02 15:54:39 +02:00
|
|
|
(?l "List" docker-machine-ls-popup)))
|
2018-09-10 20:51:14 +02:00
|
|
|
|
|
|
|
(defvar docker-machine-mode-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map "?" 'docker-machine-help-popup)
|
|
|
|
(define-key map "C" 'docker-machine-create)
|
|
|
|
(define-key map "D" 'docker-machine-rm-popup)
|
|
|
|
(define-key map "E" 'docker-machine-env-popup)
|
|
|
|
(define-key map "O" 'docker-machine-stop-popup)
|
|
|
|
(define-key map "R" 'docker-machine-restart-popup)
|
|
|
|
(define-key map "S" 'docker-machine-start-popup)
|
2018-10-02 15:54:39 +02:00
|
|
|
(define-key map "l" 'docker-machine-ls-popup)
|
2018-09-10 20:51:14 +02:00
|
|
|
map)
|
|
|
|
"Keymap for `docker-machine-mode'.")
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun docker-machines ()
|
|
|
|
"List docker machines."
|
|
|
|
(interactive)
|
|
|
|
(docker-utils-pop-to-buffer "*docker-machines*")
|
|
|
|
(docker-machine-mode)
|
|
|
|
(tablist-revert))
|
|
|
|
|
|
|
|
(define-derived-mode docker-machine-mode tabulated-list-mode "Machines Menu"
|
|
|
|
"Major mode for handling a list of docker machines."
|
|
|
|
(setq tabulated-list-format [("Name" 16 t)("Active" 7 t)("Driver" 12 t)("State" 12 t)("URL" 30 t)("Swarm" 10 t)("Docker" 10 t)("Errors" 10 t)])
|
|
|
|
(setq tabulated-list-padding 2)
|
2018-10-02 15:54:39 +02:00
|
|
|
(setq tabulated-list-sort-key docker-machine-default-sort-key)
|
2018-09-10 20:51:14 +02:00
|
|
|
(add-hook 'tabulated-list-revert-hook 'docker-machine-refresh nil t)
|
|
|
|
(tabulated-list-init-header)
|
|
|
|
(tablist-minor-mode))
|
|
|
|
|
|
|
|
(provide 'docker-machine)
|
|
|
|
|
|
|
|
;;; docker-machine.el ends here
|