about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/docker-20180820.1130/docker-machine.el
blob: 0ae90342c648ff8f6bd2f66d6b9966922e5c579a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
;;; 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:

(require 's)
(require 'dash)
(require 'tablist)
(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))))

(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}}")
         (data (docker-machine-run "ls" docker-machine-ls-arguments (format "--format=\"%s\"" fmt)))
         (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)
  "Execute \"docker-machine ACTION\" using ARGS."
  (let ((command (format "docker-machine %s %s" action (s-join " " (-flatten (-non-nil args))))))
    (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))

;;;###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))))

;;;###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
(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))

;;;###autoload
(defun docker-machine-start (name)
  "Start the machine NAME."
  (interactive (list (docker-machine-read-name)))
  (docker-machine-run "start" name))

;;;###autoload
(defun docker-machine-status (name)
  "Get the status of the machine NAME."
  (interactive (list (docker-machine-read-name)))
  (docker-machine-run "status" name))

;;;###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
(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))

(defun docker-machine-env-selection ()
  "Run \"docker-machine env\" on selected machine."
  (interactive)
  (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)))

(defun docker-machine-restart-selection ()
  "Run `docker-machine-restart' on the machines selection."
  (interactive)
  (--each (docker-utils-get-marked-items-ids)
    (docker-machine-run "restart" (docker-machine-restart-arguments) it))
  (tablist-revert))

(defun docker-machine-rm-selection ()
  "Run `docker-machine-rm' on the machines selection."
  (interactive)
  (--each (docker-utils-get-marked-items-ids)
    (docker-machine-run "rm" (docker-machine-rm-arguments) it))
  (tablist-revert))

(defun docker-machine-start-selection ()
  "Run `docker-machine-start' on the machines selection."
  (interactive)
  (--each (docker-utils-get-marked-items-ids)
    (docker-machine-run "start" (docker-machine-start-arguments) it))
  (tablist-revert))

(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))

(magit-define-popup docker-machine-env-popup
  "Popup for setting up environment variables."
  'docker-machine
  :man-page "docker-machine-env"
  :actions '((?E "Env" docker-machine-env-selection))
  :setup-function #'docker-utils-setup-popup)

(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))))

(magit-define-popup docker-machine-restart-popup
  "Popup for restarting machines."
  'docker-machine
  :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."
  'docker-machine
  :man-page "docker-machine-rm"
  :switches '((?f "Force" "-f")
              (?y "Automatic yes" "-y"))
  :actions  '((?D "Remove" docker-machine-rm-selection))
  :default-arguments '("-y")
  :setup-function #'docker-utils-setup-popup)

(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)

(magit-define-popup docker-machine-help-popup
  "Help popup for docker machine."
  'docker-machine
  :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)
             (?l "List"       docker-machine-ls-popup)))

(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)
    (define-key map "l" 'docker-machine-ls-popup)
    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)
  (setq tabulated-list-sort-key docker-machine-default-sort-key)
  (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