-
Notifications
You must be signed in to change notification settings - Fork 1
/
helm-lxc.el
528 lines (471 loc) · 19.5 KB
/
helm-lxc.el
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
;;; helm-lxc.el --- Helm interface to manage LXC containers -*- lexical-binding: t -*-
;; Copyright (C) 2019 montag451
;; Author: montag451
;; URL: https://github.com/montag451/helm-lxc
;; Keywords: helm, lxc, convenience
;; Version: 0.2.0
;; Package-Requires: ((emacs "25") (cl-lib "0.5") (helm "2.9.4") (lxc-tramp "0.2.0"))
;; This file is NOT part of GNU Emacs.
;;; License:
;; 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; `helm-lxc' provides a Helm interface to manage your LXC containers.
;;
;; With `helm-lxc' you can list the containers on your local machine
;; and on remote machines. You can start, stop, restart, freeze,
;; unfreeze your containers. You can also start a shell inside a
;; container.
;;; Code:
(require 'helm)
(require 'tramp)
(require 'lxc-tramp)
(require 'subr-x)
(require 'shell)
(eval-when-compile
(require 'cl-lib))
(defgroup helm-lxc nil
"Helm interface to manage LXC containers."
:prefix "helm-lxc-"
:link '(url-link :tag "GitHub" "https://github.com/montag451/helm-lxc")
:group 'helm)
(defface helm-lxc-face-running
'((t . (:foreground "green")))
"Face use to colorize the name of running containers."
:group 'helm-lxc)
(defface helm-lxc-face-stopped
'((t . (:foreground "red")))
"Face use to colorize the name of stopped containers."
:group 'helm-lxc)
(defface helm-lxc-face-frozen
'((t . (:foreground "blue")))
"Face use to colorize the name of frozen containers."
:group 'helm-lxc)
(defcustom helm-lxc-hosts '(("localhost" . "/sudo::"))
"Alist of hosts to check for containers.
Each member of the alist is of the form (NAME . TRAMP-PATH).
TRAMP-PATH specify where to get information about containers.
NAME is the name of the entry and is used for display purpose.
If you use nil as TRAMP-PATH for an entry of the alist, all the
commands for this entry will be run on the local machine as the
user running Emacs."
:group 'helm-lxc
:type '(alist :key-type (string :tag "Name")
:value-type (string :tag "Tramp file name")))
(defcustom helm-lxc-clean-up-on-shell-exit nil
"Do some cleanup when a shell exits.
If non-nil, when a shell (created by attaching to a container)
exits its buffer is killed and its window, if any, is quit."
:group 'helm-lxc
:type 'boolean)
(defcustom helm-lxc-attach-with-ssh nil
"Attach to the container using SSH instead of `lxc-tramp'.
If nil, SSH will never be used to attach to the container. If
non-nil, SSH will be used if the container has at least an IP
address (the first one returned by `lxc-info' is used)."
:group 'helm-lxc
:type 'boolean)
(defcustom helm-lxc-attach-ssh-user "root"
"User used to connect to container when `helm-lxc-attach-with-ssh' is non-nil."
:group 'helm-lxc
:type 'string)
(defvar helm-lxc-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map helm-map)
(define-key map (kbd "C-s") #'helm-lxc-start-persistent)
(define-key map (kbd "C-d") #'helm-lxc-stop-persistent)
(define-key map (kbd "C-k") #'helm-lxc-destroy-persistent)
(define-key map (kbd "M-c") #'helm-lxc-clear-cache-persistent)
map))
(defvar helm-lxc--help-message
"* Helm LXC
* Commands
\\<helm-lxc-map>
\\[helm-lxc-start-persistent] Start the selected/marked containers
\\[helm-lxc-stop-persistent] Stop the selected/marked containers
\\[helm-lxc-destroy-persistent] Destroy the selected/marked containers
\\[helm-lxc-clear-cache-persistent] Clean the cache for the selected/marked containers")
(defvar helm-lxc--cache (make-hash-table :test 'equal))
(defun helm-lxc--face-from-state (state)
"Return the face used to colorize a container in state STATE."
(pcase state
("running" 'helm-lxc-face-running)
("stopped" 'helm-lxc-face-stopped)
("frozen" 'helm-lxc-face-frozen)))
(defun helm-lxc--process-sentinel (proc _event)
"Kill the buffer associated with PROC.
It also quits the window displaying the buffer if any."
(unless (process-live-p proc)
(let* ((buffer (process-buffer proc))
(win (get-buffer-window buffer)))
(if win
(quit-window t win)
(kill-buffer buffer)))))
(defun helm-lxc--process-file (&rest args)
"Run `process-file' with `non-essential' set to nil.
Helm set `non-essential' to non-nil which prevent TRAMP from
opening connections. ARGS is passed as-is to `process-file'."
(let ((non-essential))
(apply #'process-file args)))
(defun helm-lxc--process-lines (host program &optional delete-trailing-ws &rest args)
"Execute PROGRAM on HOST with ARGS, returning its output as a list of lines.
It is an equivalent of `process-lines' working also on remote
machines. HOST is a path that will be used to set the
`default-directory' used to run PROGRAM. If HOST is a remote
path (i.e `file-remote-p' returns a non-nil value for HOST),
PROGRAM will be run on the remote host. If DELETE-TRAILING-WS is
non-nil, trailing whitespace will be removed from every line of
output."
(with-temp-buffer
(let ((default-directory (or host default-directory)))
(when (zerop (apply 'helm-lxc--process-file program nil t nil args))
(when delete-trailing-ws
(let ((delete-trailing-lines t))
(delete-trailing-whitespace)))
(goto-char (point-min))
(let (lines)
(while (not (eobp))
(let ((line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
(push line lines))
(forward-line))
(nreverse lines))))))
(defun helm-lxc--list-containers (host)
"List containers on HOST."
(if-let ((containers (gethash host helm-lxc--cache)))
containers
(puthash host
(let ((containers (helm-lxc--process-lines host "lxc-ls" t "-1")))
(mapcar (lambda (c)
(let* ((info (helm-lxc--get-container-info host c))
(state (alist-get 'state info))
(face (helm-lxc--face-from-state state)))
(propertize c 'face face 'helm-lxc info)))
containers))
helm-lxc--cache)))
(defun helm-lxc--get-container-info (host container)
"Get information for CONTAINER on HOST."
(let* ((state (downcase (car (helm-lxc--process-lines
host
"lxc-info"
nil
"-s" "-H" "-n" container))))
(is-stopped (string-equal state "stopped"))
(pid (and (not is-stopped)
(car (helm-lxc--process-lines
host
"lxc-info"
nil
"-p" "-H" "-n" container))))
(ips (and (not is-stopped)
(split-string
(or (car (helm-lxc--process-lines
host
"lxc-info"
nil
"-i" "-H" "-n" container))
"")
","
t))))
`((host . ,host)
(name . ,container)
(state . ,state)
(pid . ,pid)
(ips . ,ips))))
(defun helm-lxc--get-candidates (&optional marked)
"Get selected or marked candidates.
If MARKED is non-nil, returns all candidates currently marked."
(or (and marked (helm-marked-candidates :all-sources t))
(list (helm-get-selection nil 'withprop))))
(defun helm-lxc--get-container-from-candidate (candidate)
"Extract container information from CANDIDATE."
(get-text-property 0 'helm-lxc candidate))
(defun helm-lxc--clear-cache-for-candidate (candidate)
"Clear the cache for CANDIDATE."
(let* ((container (helm-lxc--get-container-from-candidate candidate))
(host (alist-get 'host container)))
(puthash host nil helm-lxc--cache)))
(defun helm-lxc--clear-cache (&optional _candidate)
"Clear the cache for all marked candidates."
(let ((candidates (helm-lxc--get-candidates t)))
(dolist (candidate candidates)
(helm-lxc--clear-cache-for-candidate candidate))))
(cl-defun helm-lxc--create-action (action
&optional (marked t) clear-cache
&rest args)
"Create an action that will execute ACTION.
ACTION is a LXC command (e.g \"stop\", \"start\", ...). If
MARKED is non-nil, ACTION is executed for each marked containers.
If CLEAR-CACHE is non-nil, it means that the cache for each
candidates is cleared."
(lambda (candidate &optional from-chain)
(let (rc (last-error 0))
(dolist (candidate
(or (and from-chain (list candidate))
(helm-lxc--get-candidates marked))
last-error)
(let* ((container (helm-lxc--get-container-from-candidate candidate))
(name (alist-get 'name container))
(host (alist-get 'host container))
(default-directory (or host default-directory))
(cmd (format "lxc-%s" action)))
(setq rc (apply #'helm-lxc--process-file
cmd nil nil nil "-n" name args))
(unless (zerop rc)
(setq last-error rc)
(message "helm-lxc: lxc-%s on %s failed with return code %d"
action name rc))
(when clear-cache
(helm-lxc--clear-cache-for-candidate candidate)))))))
(defun helm-lxc--create-action-chain (marked clear-cache &rest actions)
"Create an action that will execute ACTIONS sequentially.
See `helm-lxc--create-action' for an explanation of the MARKED
and CLEAR-CACHE arguments."
(lambda (_candidate)
(let ((rc 0))
(dolist (candidate (helm-lxc--get-candidates marked) rc)
(catch 'break
(dolist (action actions)
(setq rc (pcase action
((pred stringp)
(funcall (helm-lxc--create-action action nil clear-cache)
candidate t))
((pred functionp)
(funcall action candidate))
(`(,name . ,args)
(funcall (apply #'helm-lxc--create-action
name nil clear-cache args)
candidate t))
(bad (error "Bad action: %S" bad))))
(unless (and rc (zerop rc))
(throw 'break rc))))))))
(defun helm-lxc--spawn-shell (name)
"Spawn a shell whose buffer name is NAME."
(let* ((remote (file-remote-p default-directory))
(shell-file-name "/bin/bash")
(histfile-env-name "HISTFILE")
(prev-histfile (getenv histfile-env-name)))
(unwind-protect
(let ((histfile (expand-file-name (concat remote "~/.bash_history")))
(tramp-histfile-override nil))
(when remote
(setenv histfile-env-name histfile))
(let* ((buffer (shell name))
(proc (get-buffer-process buffer)))
(when (and proc helm-lxc-clean-up-on-shell-exit)
(add-function :after (process-sentinel proc)
#'helm-lxc--process-sentinel))))
(setenv histfile-env-name prev-histfile))))
(defun helm-lxc--attach (_candidate)
"Spawn a shell inside the selected container."
(let* ((candidate (car (helm-lxc--get-candidates)))
(container (helm-lxc--get-container-from-candidate candidate))
(name (alist-get 'name container))
(host (alist-get 'host container))
(ip (car (alist-get 'ips container)))
(use-ssh (and helm-lxc-attach-with-ssh ip))
(attach-method (or (and use-ssh "ssh") "lxc"))
(attach-user (or (and use-ssh helm-lxc-attach-ssh-user)
"root"))
(attach-host (or (and use-ssh ip) name))
(default-directory (concat
(if host (format "%s|" (substring host 0 -1)) "/")
(format "%s:%s@%s:"
attach-method
attach-user
attach-host))))
(helm-lxc--spawn-shell (format "*shell %s@%s*" attach-user name))
0))
(defun helm-lxc--connect-to-host (_candidate)
"Connect to the host where the selected container is present."
(let* ((candidate (car (helm-lxc--get-candidates)))
(container (helm-lxc--get-container-from-candidate candidate))
(host (alist-get 'host container))
(user (or (file-remote-p host 'user) "unknown"))
(hostname (file-remote-p host 'host))
(default-directory (or host default-directory)))
(helm-lxc--spawn-shell (format "*shell %s@%s*" user hostname))
0))
(defun helm-lxc--show-container-info (_candidate)
"Show information about the selected container in a buffer."
(let* ((buffer (get-buffer-create "*helm lxc info*"))
(candidate (car (helm-lxc--get-candidates)))
(container (helm-lxc--get-container-from-candidate candidate))
(host (alist-get 'host container)))
(with-current-buffer buffer
(erase-buffer)
(insert (format "Name: %s\n" (alist-get 'name container)))
(insert (format "Host: %s\n" (or (and (not host) (system-name))
(file-remote-p host 'host))))
(insert (format "State: %s\n" (alist-get 'state container)))
(when-let ((pid (alist-get 'pid container)))
(insert (format "PID: %s\n" pid)))
(when-let ((ips (alist-get 'ips container)))
(insert (format "IPs: %s\n" (string-join ips " ")))))
(pop-to-buffer buffer)))
(defvar helm-lxc--action-destroy
(helm-lxc--create-action "destroy" t t))
(defvar helm-lxc--action-freeze
(helm-lxc--create-action-chain
t
t
"freeze"
'("wait" "-s" "FROZEN")))
(defvar helm-lxc--action-unfreeze
(helm-lxc--create-action-chain
t
t
"unfreeze"
'("wait" "-s" "RUNNING")))
(defvar helm-lxc--action-unfreeze-and-attach
(helm-lxc--create-action-chain
t
t
helm-lxc--action-unfreeze
'helm-lxc--attach))
(defvar helm-lxc--action-stop
(helm-lxc--create-action-chain
t
t
"stop"
'("wait" "-s" "STOPPED")))
(defvar helm-lxc--action-stop-and-destroy
(helm-lxc--create-action-chain
t
t
helm-lxc--action-stop
helm-lxc--action-destroy))
(defvar helm-lxc--action-start
(helm-lxc--create-action-chain
t
t
'("start" "-d")
'("wait" "-s" "RUNNING")))
(defvar helm-lxc--action-start-and-attach
(helm-lxc--create-action-chain
t
t
helm-lxc--action-start
'helm-lxc--attach))
(defvar helm-lxc--action-restart
(helm-lxc--create-action-chain
t
t
helm-lxc--action-stop
helm-lxc--action-start))
(defvar helm-lxc--action-restart-and-attach
(helm-lxc--create-action-chain
t
t
helm-lxc--action-stop
helm-lxc--action-start
'helm-lxc--attach))
(defun helm-lxc--action-transformer (_actions _candidate)
"Compute the actions that can be executed for the selected/marked containers."
(let* ((candidates (helm-lxc--get-candidates t))
(nb-candidates (length candidates))
(states (mapcar
(lambda (cand)
(let ((c (helm-lxc--get-container-from-candidate cand)))
(alist-get 'state c)))
candidates))
(uniq-states (cl-remove-duplicates states :test 'equal))
(connect-action '("Connect to host" . helm-lxc--connect-to-host)))
(cond
((= nb-candidates 1)
(pcase (car states)
("running" `(("Attach" . helm-lxc--attach)
("Stop" . ,helm-lxc--action-stop)
("Stop and destroy" . ,helm-lxc--action-stop-and-destroy)
("Restart" . ,helm-lxc--action-restart)
("Restart and attach" . ,helm-lxc--action-restart-and-attach)
("Freeze" . ,helm-lxc--action-freeze)
,connect-action))
("stopped" `(("Start and attach" . ,helm-lxc--action-start-and-attach)
("Start" . ,helm-lxc--action-start)
("Destroy" . ,helm-lxc--action-destroy)
,connect-action))
("frozen" `(("Unfreeze and attach" . ,helm-lxc--action-unfreeze-and-attach)
("Unfreeze" . ,helm-lxc--action-unfreeze)
,connect-action))))
((= (length uniq-states) 1)
(pcase (car uniq-states)
("running" `(("Stop" . ,helm-lxc--action-stop)
("Stop and destroy" . ,helm-lxc--action-stop-and-destroy)
("Restart" . ,helm-lxc--action-restart)
("Freeze" . ,helm-lxc--action-freeze)))
("stopped" `(("Start" . ,helm-lxc--action-start)
("Destroy" . ,helm-lxc--action-destroy)))
("frozen" `(("Unfreeze" . ,helm-lxc--action-unfreeze))))))))
(defun helm-lxc--build-sources ()
"Create the helm sources used by `helm-lxc'.
It returns one source per entry in `helm-lxc-hosts'."
(cl-loop for (name . tramp-path) in helm-lxc-hosts
collect
(helm-build-in-buffer-source name
:init (let ((tramp-path-1 tramp-path))
(lambda ()
(helm-init-candidates-in-buffer
'global
(helm-lxc--list-containers tramp-path-1))))
:keymap helm-lxc-map
:get-line 'buffer-substring
:marked-with-props 'withprop
:action-transformer 'helm-lxc--action-transformer
:persistent-action 'helm-lxc--show-container-info
:persistent-help "Show container info"
:help-message 'helm-lxc--help-message)))
(defun helm-lxc-start-persistent ()
"Start container without quitting helm."
(interactive)
(with-helm-alive-p
(helm-attrset 'start-action helm-lxc--action-start)
(helm-execute-persistent-action 'start-action)
(helm-lxc--clear-cache)
(helm-force-update)))
(put 'helm-lxc-start-persistent 'helm-only t)
(defun helm-lxc-stop-persistent ()
"Stop a container without quitting helm."
(interactive)
(with-helm-alive-p
(helm-attrset 'stop-action helm-lxc--action-stop)
(helm-execute-persistent-action 'stop-action)
(helm-lxc--clear-cache)
(helm-force-update)))
(put 'helm-lxc-stop-persistent 'helm-only t)
(defun helm-lxc-destroy-persistent ()
"Destroy a container without quitting helm."
(interactive)
(with-helm-alive-p
(helm-attrset 'destroy-action helm-lxc--action-destroy)
(helm-execute-persistent-action 'destroy-action)
(helm-lxc--clear-cache)
(helm-force-update)))
(put 'helm-lxc-destroy-persistent 'helm-only t)
(defun helm-lxc-clear-cache-persistent ()
"Clear the cache without quitting helm."
(interactive)
(with-helm-alive-p
(helm-attrset 'clear-cache-action #'helm-lxc--clear-cache)
(helm-execute-persistent-action 'clear-cache-action)
(helm-force-update)))
(put 'helm-lxc-clear-cache-persistent 'helm-only t)
;;;###autoload
(defun helm-lxc ()
"Bring up the `helm-lxc' interface."
(interactive)
(helm
:buffer "*helm lxc*"
:sources (helm-lxc--build-sources)))
(provide 'helm-lxc)
;;; helm-lxc.el ends here