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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
|
;;; govc.el --- Interface to govc for managing VMware ESXi and vCenter
;; Author: The govc developers
;; URL: https://github.com/vmware/govmomi/tree/master/govc/emacs
;; Keywords: convenience
;; Version: 0.18.0
;; Package-Requires: ((emacs "24.3") (dash "1.5.0") (s "1.9.0") (magit-popup "2.0.50") (json-mode "1.6.0"))
;; This file is NOT part of GNU Emacs.
;; Copyright (c) 2016 VMware, Inc. All Rights Reserved.
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
;;; Commentary:
;; The goal of this package is to provide a simple interface for commonly used
;; govc commands within Emacs. This includes table based inventory/state modes
;; for vms, hosts, datastores and pools. The keymap for each mode provides
;; shortcuts for easily feeding the data in view to other govc commands.
;;
;; Within the various govc modes, press `?' to see a popup menu of options.
;; A menu bar is enabled for certain modes, such as `govc-vm-mode' and `govc-host-mode'.
;; There is also a `govc' menu at all times under the `Tools' menu.
;;
;; The recommended way to install govc.el is via MELPA (http://melpa.org/).
;;; Code:
(eval-when-compile
(require 'cl))
(require 'dash)
(require 'diff)
(require 'dired)
(require 'json-mode)
(require 'magit-popup)
(require 'url-parse)
(require 's)
(autoload 'auth-source-search "auth-source")
(defgroup govc nil
"Emacs customization group for govc."
:group 'convenience)
(defcustom govc-keymap-prefix "C-c ;"
"Prefix for `govc-mode'."
:group 'govc)
(defcustom govc-command "govc"
"Executable path to the govc utility."
:type 'string
:group 'govc)
(defvar govc-command-map
(let ((map (make-sparse-keymap)))
(define-key map "h" 'govc-host)
(define-key map "p" 'govc-pool)
(define-key map "v" 'govc-vm)
(define-key map "s" 'govc-datastore)
(define-key map "?" 'govc-popup)
map)
"Keymap for `govc-mode' after `govc-keymap-prefix' was pressed.")
(defvar govc-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd govc-keymap-prefix) govc-command-map)
map)
"Keymap for `govc-mode'.")
;;;###autoload
(define-minor-mode govc-mode
"Running `govc-global-mode' creates key bindings to the various govc modes.
The default prefix is `C-c ;' and can be changed by setting `govc-keymap-prefix'.
\\{govc-mode-map\}"
nil govc-mode-line govc-mode-map
:group 'govc)
;;;###autoload
(define-globalized-minor-mode govc-global-mode govc-mode govc-mode)
(defcustom govc-mode-line
'(:eval (format " govc[%s]" (or (govc-session-name) "-")))
"Mode line lighter for govc."
:group 'govc
:type 'sexp
:risky t)
;;; Tabulated list mode extensions (derived from https://github.com/Silex/docker.el tabulated-list-ext.el)
(defun govc-tabulated-list-mark ()
"Mark and move to the next line."
(interactive)
(tabulated-list-put-tag (char-to-string dired-marker-char) t))
(defun govc-tabulated-list-unmark ()
"Unmark and move to the next line."
(interactive)
(tabulated-list-put-tag "" t))
(defun govc-tabulated-list-toggle-marks ()
"Toggle mark."
(interactive)
(save-excursion
(goto-char (point-min))
(let ((cmd))
(while (not (eobp))
(setq cmd (char-after))
(tabulated-list-put-tag
(if (eq cmd dired-marker-char)
""
(char-to-string dired-marker-char)) t)))))
(defun govc-tabulated-list-unmark-all ()
"Unmark all."
(interactive)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(tabulated-list-put-tag "" t))))
(defun govc-selection ()
"Get the current selection as a list of names."
(let ((selection))
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(when (eq (char-after) ?*)
(add-to-list 'selection (tabulated-list-get-id)))
(forward-line)))
(or selection (let ((id (tabulated-list-get-id)))
(if id
(list id))))))
(defun govc-do-selection (fn action)
"Call FN with `govc-selection' confirming ACTION."
(let* ((selection (govc-selection))
(count (length selection))
(prompt (if (= count 1)
(car selection)
(format "* [%d] marked" count))))
(if (yes-or-no-p (format "%s %s ?" action prompt))
(funcall fn selection))))
(defun govc-copy-selection ()
"Copy current selection or region to the kill ring."
(interactive)
(if (region-active-p)
(copy-region-as-kill (mark) (point) 'region)
(kill-new (message "%s" (s-join " " (--map (format "\"%s\"" it) (govc-selection)))))))
(defvar govc-font-lock-keywords
`((,(let ((host-expression "\\b[-a-z0-9]+\\b")) ;; Hostname
(concat
(mapconcat 'identity (make-list 3 host-expression) "\\.")
"\\(\\." host-expression "\\)*")) .
(0 font-lock-variable-name-face))
(,(mapconcat 'identity (make-list 4 "[0-9]+") "\\.") ;; IP address
. (0 font-lock-variable-name-face))
("\"[^\"]*\"" . (0 font-lock-string-face))
("'[^']*'" . (0 font-lock-string-face))
("[.0-9]+%" . (0 font-lock-type-face))
("\\<\\(success\\|poweredOn\\)\\>" . (1 font-lock-doc-face))
("\\<\\(error\\|poweredOff\\)\\>" . (1 font-lock-warning-face))
("\\<\\(running\\|info\\)\\>" . (1 font-lock-variable-name-face))
("\\<\\(warning\\|suspended\\)\\>" . (1 font-lock-keyword-face))
("\\<\\(verbose\\|trivia\\)\\>" . (1 whitespace-line))
(,dired-re-maybe-mark . (0 dired-mark-face))
("types.ManagedObjectReference\\(.*\\)" . (1 dired-directory-face))
("[^ ]*/$" . (0 dired-directory-face))
("\\.\\.\\.$" . (0 dired-symlink-face))
("^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][A-Z][0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9][A-Z]|" . (0 font-lock-comment-face))
("^\\([ A-Za-z0-9_]+: \\).*" . (1 font-lock-comment-face))
("<[^>]*>" . (0 font-lock-comment-face))
("\\[[^]]*\\]" . (0 font-lock-comment-face))
("([^)]*)" . (0 font-lock-comment-face))))
(defvar govc-tabulated-list-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "m" 'govc-tabulated-list-mark)
(define-key map "u" 'govc-tabulated-list-unmark)
(define-key map "t" 'govc-tabulated-list-toggle-marks)
(define-key map "U" 'govc-tabulated-list-unmark-all)
(define-key map (kbd "M-&") 'govc-shell-command)
(define-key map (kbd "M-w") 'govc-copy-selection)
(define-key map (kbd "M-E") 'govc-copy-environment)
map)
"Keymap for `govc-tabulated-list-mode'.")
(define-derived-mode govc-tabulated-list-mode tabulated-list-mode "Tabulated govc"
"Generic table bindings to mark/unmark rows."
(setq-local font-lock-defaults
'(govc-font-lock-keywords t nil nil beginning-of-line)))
;;; Keymap helpers for generating menus and popups
(defun govc-keymap-list (keymap)
"Return a list of (key name function) for govc bindings in the given KEYMAP.
The name returned is the first word of the function `documentation'."
(let ((map))
(map-keymap
(lambda (k f)
(when (keymapp f)
(setq map (append map
(--map (and (setcar it (kbd (format "M-%s" (char-to-string (car it))))) it)
(govc-keymap-list f)))))
(when (and (symbolp f)
(s-starts-with? "govc-" (symbol-name f)))
(if (not (eq ?? k))
(add-to-list 'map (list k (car (split-string (documentation f))) f))))) keymap)
map))
(defun govc-keymap-menu (keymap)
"Return a list of [key function t] for govc bindings in the given KEYMAP.
For use with `easy-menu-define'."
(-map (lambda (item)
(vector (nth 1 item) (nth 2 item) t))
(govc-keymap-list keymap)))
(defun govc-key-description (key)
"Call `key-description' ensuring KEY is a sequence."
(key-description (if (integerp key) (list key) key)))
(defun govc-keymap-list-to-help (keymap)
"Convert KEYMAP to list of help text."
(--map (list (govc-key-description (car it))
(car (split-string (documentation (nth 2 it)) "\\.")))
keymap))
(defun govc-keymap-popup-help ()
"Default keymap help for `govc-keymap-popup'."
(append (govc-keymap-list-to-help (govc-keymap-list govc-tabulated-list-mode-map))
'(("g" "Refresh current buffer")
("C-h m" "Show all key bindings"))))
(defun govc-keymap-popup (keymap)
"Convert a `govc-keymap-list' using KEYMAP for use with `magit-define-popup'.
Keys in the ASCII range of 32-97 are mapped to popup commands, all others are listed as help text."
(let* ((maps (--separate (and (integerp (car it))
(>= (car it) 32)
(<= (car it) 97))
(govc-keymap-list keymap)))
(help (govc-keymap-list-to-help (cadr maps))))
(append
'("Commands")
(car maps)
(list (s-join "\n" (--map (format " %-6s %s" (car it) (cadr it))
(append help (govc-keymap-popup-help))))
nil))))
;;; govc process helpers
(defcustom govc-urls nil
"List of URLs for use with `govc-session'.
The `govc-session-name' displayed by `govc-mode-line' uses `url-target' (anchor)
if set, otherwise `url-host' is used.
Example:
```
(setq govc-urls '(\"root:vagrant@localhost:18443#Vagrant-ESXi\"
\"root:password@192.168.1.192#Intel-NUC\"
\"Administrator@vsphere.local:password!@vcva-clovervm\"))
```
To enter a URL that is not in the list, prefix `universal-argument', for example:
`\\[universal-argument] \\[govc-vm]'
To avoid putting your credentials in a variable, you can use the
auth-source search integration.
```
(setq govc-urls '(\"myserver-vmware-2\"))
```
And then put this line in your `auth-sources' (e.g. `~/.authinfo.gpg'):
```
machine myserver-vmware-2 login tzz password mypass url \"myserver-vmware-2.some.domain.here:443?insecure=true\"
```
Which will result in the URL \"tzz:mypass@myserver-vmware-2.some.domain.here:443?insecure=true\".
For more details on `auth-sources', see Info node `(auth) Help for users'.
When in `govc-vm' or `govc-host' mode, a default URL is composed with the
current session credentials and the IP address of the current vm/host and
the vm/host name as the session name. This makes it easier to connect to
nested ESX/vCenter VMs or directly to an ESX host."
:group 'govc
:type '(repeat (string :tag "vcenter URL or auth-source machine reference")))
(defvar-local govc-session-url nil
"ESX or vCenter URL set by `govc-session' via `govc-urls' selection.")
(defvar-local govc-session-path nil)
(defvar-local govc-session-insecure nil
"Skip verification of server certificate when true.
This variable is set to the value of the `GOVC_INSECURE' env var by default.
It can also be set per-url via the query string (insecure=true). For example:
```
(setq govc-urls '(\"root:password@hostname?insecure=true\"))
```")
(defvar-local govc-session-datacenter nil
"Datacenter to use for the current `govc-session'.
If the endpoint has a single Datacenter it will be used by default, otherwise
`govc-session' will prompt for selection. It can also be set per-url via the
query string. For example:
```
(setq govc-urls '(\"root:password@hostname?datacenter=dc1\"))
```")
(defvar-local govc-session-datastore nil
"Datastore to use for the current `govc-session'.
If the endpoint has a single Datastore it will be used by default, otherwise
`govc-session' will prompt for selection. It can also be set per-url via the
query string. For example:
```
(setq govc-urls '(\"root:password@hostname?datastore=vsanDatastore\"))
```")
(defvar-local govc-session-network nil
"Network to use for the current `govc-session'.")
(defvar-local govc-filter nil
"Resource path filter.")
(defvar-local govc-args nil
"Additional govc arguments.")
(defun govc-session-name ()
"Return a name for the current session.
Derived from `govc-session-url' if set, otherwise from the 'GOVC_URL' env var.
Return value is the url anchor if set, otherwise the hostname is returned."
(let* ((u (or govc-session-url (getenv "GOVC_URL")))
(url (if u (govc-url-parse u))))
(if url
(concat (or (url-target url) (url-host url)) govc-session-path))))
(defun govc-format-command (command &rest args)
"Format govc COMMAND ARGS."
(format "%s %s %s" govc-command command
(s-join " " (--map (format "\"%s\"" it)
(-flatten (-non-nil args))))))
(defconst govc-environment-map (--map (cons (concat "GOVC_" (upcase it))
(intern (concat "govc-session-" it)))
'("url" "insecure" "datacenter" "datastore" "network"))
"Map of `GOVC_*' environment variable names to `govc-session-*' symbol names.")
(defun govc-environment (&optional unset)
"Return `process-environment' for govc.
Optionally clear govc env if UNSET is non-nil."
(let ((process-environment (copy-sequence process-environment)))
(dolist (e govc-environment-map)
(setenv (car e) (unless unset (symbol-value (cdr e)))))
process-environment))
(defun govc-export-environment (arg)
"Set if ARG is \\[universal-argument], unset if ARG is \\[negative-argument]."
(if (equal arg '-)
(progn (setq process-environment (govc-environment t))
(cons "unset" (--map (car it)
govc-environment-map)))
(progn (setq process-environment (govc-environment))
(cons "export" (--map (format "%s='%s'" (car it) (or (symbol-value (cdr it)) ""))
govc-environment-map)))))
(defun govc-copy-environment (&optional arg)
"Export session to `process-environment' and `kill-ring'.
Optionally set `GOVC_*' vars in `process-environment' using prefix
\\[universal-argument] ARG or unset with prefix \\[negative-argument] ARG."
(interactive "P")
(message (kill-new (if arg (s-join " " (govc-export-environment arg)) govc-session-url))))
(defun govc-process (command handler)
"Run COMMAND, calling HANDLER upon successful exit of the process."
(message "%s" command)
(let ((process-environment (govc-environment))
(exit-code))
(add-to-list 'govc-command-history command)
(with-temp-buffer
(setq exit-code (call-process-shell-command command nil (current-buffer)))
(if (zerop exit-code)
(funcall handler)
(error (buffer-string))))))
(defun govc (command &rest args)
"Execute govc COMMAND with ARGS.
Return value is `buffer-string' split on newlines."
(govc-process (govc-format-command command args)
(lambda ()
(split-string (buffer-string) "\n" t))))
(defun govc-json (command &rest args)
"Execute govc COMMAND passing arguments ARGS.
Return value is `json-read'."
(govc-process (govc-format-command command (cons "-json" args))
(lambda ()
(goto-char (point-min))
(let ((json-object-type 'plist))
(json-read)))))
(defun govc-ls-datacenter ()
"List datacenters."
(govc "ls" "-t" "Datacenter" "./..."))
(defun govc-object-prompt (prompt ls)
"PROMPT for object name via LS function. Return object without PROMPT if there is just one instance."
(let ((objs (if (listp ls) ls (funcall ls))))
(if (eq 1 (length objs))
(car objs)
(completing-read prompt objs))))
(defun govc-url-parse (url)
"A `url-generic-parse-url' wrapper to handle URL with password, but no scheme.
Also fixes the case where user contains an '@'."
(let* ((full (s-contains? "://" url))
(u (url-generic-parse-url (concat (unless full "https://") url))))
(unless full
(setf (url-type u) nil)
(setf (url-fullness u) nil))
(if (s-contains? "@" (url-host u))
(let* ((h (split-string (url-host u) "@"))
(p (split-string (car h) ":")))
(setf (url-host u) (cadr h))
(setf (url-user u) (concat (url-user u) "@" (car p)))
(setf (url-password u) (cadr p))))
u))
(defun govc-url-default ()
"Default URL when creating a new session."
(if govc-session-url
(let ((url (govc-url-parse govc-session-url)))
(if (equal major-mode 'govc-host-mode)
(progn (setf (url-host url) (govc-table-column-value "Name"))
(setf (url-target url) nil))
(progn (setf (url-host url) (govc-table-column-value "IP address"))
(setf (url-target url) (govc-table-column-value "Name"))
;; default url-user to Administrator@$domain when connecting to a vCenter VM
(let ((sts (ignore-errors (govc "sso.service.ls" "-t" "cs.identity" "-P" "wsTrust" "-U" "-u" (url-host url)))))
(if sts (setf (url-user url) (concat "Administrator@" (file-name-nondirectory (car sts))))))))
(setf (url-filename url) "") ; erase query string
(if (string-empty-p (url-user url))
(setf (url-user url) "root")) ; local workstation url has no user set
(url-recreate-url url))))
(defun govc-urls-completing-read ()
"A wrapper for `completing-read' to mask credentials in `govc-urls'."
(let ((alist))
(dolist (ent govc-urls)
(let ((u (govc-url-parse ent)))
(setf (url-password u) nil)
(add-to-list 'alist `(,(url-recreate-url u) . ,ent) t)))
(let ((u (completing-read "govc url: " (-map 'car alist))))
(cdr (assoc u alist)))))
(defun govc-session-url-lookup-auth-source (url-or-address)
"Check if URL-OR-ADDRESS is a logical name in the authinfo file.
Given URL-OR-ADDRESS `myserver-vmware-2' this function will find
a line like
machine myserver-vmware-2 login tzz password mypass url \"myserver-vmware-2.some.domain.here:443?insecure=true\"
and will return the URL \"tzz:mypass@myserver-vmware-2.some.domain.here:443?insecure=true\".
If the line is not found, the original URL-OR-ADDRESS is
returned, assuming that's what the user wanted."
(let ((found (nth 0 (auth-source-search :max 1
:host url-or-address
:require '(:user :secret :url)
:create nil))))
(if found
(format "%s:%s@%s"
(plist-get found :user)
(let ((secret (plist-get found :secret)))
(if (functionp secret)
(funcall secret)
secret))
(plist-get found :url))
url-or-address)))
(defun govc-session-set-url (url)
"Set `govc-session-url' to URL and optionally set other govc-session-* variables via URL query."
;; Replace the original URL with the auth-source lookup if there is no user.
(unless (url-user (govc-url-parse url))
(setq url (govc-session-url-lookup-auth-source url)))
(let ((q (cdr (url-path-and-query (govc-url-parse url)))))
(dolist (opt (if q (url-parse-query-string q)))
(let ((var (intern (concat "govc-session-" (car opt)))))
(if (boundp var)
(set var (cadr opt))))))
(setq govc-session-url url))
(defun govc-session ()
"Initialize a govc session."
(interactive)
(let ((url (if (or current-prefix-arg (eq 0 (length govc-urls)))
(read-string "govc url: " (govc-url-default))
(if (eq 1 (length govc-urls))
(car govc-urls)
(govc-urls-completing-read)))))
;; Wait until this point to clear so current session is preserved in the
;; event of `keyboard-quit' in `read-string'.
(setq govc-session-datacenter nil
govc-session-datastore nil
govc-session-network nil
govc-filter nil)
(govc-session-set-url url))
(unless govc-session-insecure
(setq govc-session-insecure (or (getenv "GOVC_INSECURE")
(completing-read "govc insecure: " '("true" "false")))))
(unless govc-session-datacenter
(setq govc-session-datacenter (govc-object-prompt "govc datacenter: " 'govc-ls-datacenter)))
(add-to-list 'govc-urls govc-session-url))
(defalias 'govc-current-session 'buffer-local-variables)
(defun govc-session-clone (session)
"Clone a session from SESSION buffer locals."
(dolist (v session)
(let ((s (car v)))
(when (s-starts-with? "govc-session-" (symbol-name s))
(set s (assoc-default s session))))))
(defvar govc-command-history nil
"History list for govc commands used by `govc-shell-command'.")
(defvar govc-shell--revert-cmd nil)
(defun govc-shell--revert-function (&optional _ _)
"Re-run the buffer's most recent govc-shell-run command."
(apply (car govc-shell--revert-cmd) (cdr govc-shell--revert-cmd)))
(defun govc-shell-filter (proc string)
"Process filter for govc-shell PROC, append STRING."
(when (buffer-live-p (process-buffer proc))
(with-current-buffer (process-buffer proc)
(let ((moving (= (point) (process-mark proc))))
(save-excursion
(let ((inhibit-read-only t))
(goto-char (process-mark proc))
(insert string)
(set-marker (process-mark proc) (point))))
(display-buffer (process-buffer proc))
(if moving
(with-selected-window (get-buffer-window (current-buffer))
(goto-char (point-max))))))))
(defun govc-shell-run (name args buffer)
"Run NAME command with ARGS in BUFFER."
(with-current-buffer (if (stringp buffer) (get-buffer-create buffer) buffer)
(let ((proc (get-buffer-process (current-buffer)))
(process-environment (govc-environment))
(session (govc-current-session))
(inhibit-read-only t))
(when proc
(set-process-filter proc nil)
(delete-process proc))
(erase-buffer)
(if (--any? (member (file-name-extension it) '("vmx" "vmdk")) args)
(conf-mode)
(govc-shell-mode))
(govc-session-clone session)
(setq-local govc-shell--revert-cmd `(govc-shell-run ,name ,args ,(current-buffer)))
(setq mode-line-process '(:propertize ":run" face compilation-mode-line-run))
(setq proc (apply 'start-process name (current-buffer) name args))
(set-process-sentinel proc 'govc-shell-process-sentinel)
(set-process-filter proc 'govc-shell-filter))))
(defun govc-shell-kill ()
"Kill the process started by \\[govc-shell-command]."
(interactive)
(let ((buffer (current-buffer)))
(if (get-buffer-process buffer)
(interrupt-process (get-buffer-process buffer))
(error "The %s process is not running" (downcase mode-name)))))
(defun govc-shell-process-sentinel (process event)
"Process sentinel used by `govc-shell-run'. When PROCESS exits EVENT is logged."
(when (memq (process-status process) '(exit signal))
(with-current-buffer (process-buffer process)
(setq mode-line-process nil)
(message "%s %s" (process-name process) (substring event 0 -1)))))
(defvar govc-shell-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-k") 'govc-shell-kill)
map))
(define-derived-mode govc-shell-mode special-mode "govc-shell"
"Mode for running govc commands."
(setq-local font-lock-defaults '(govc-font-lock-keywords))
(setq-local revert-buffer-function #'govc-shell--revert-function))
(defun govc-shell-command (&optional cmd buffer)
"Shell CMD in BUFFER with current `govc-session' exported as GOVC_ env vars."
(interactive)
(let* ((session (govc-current-session))
(args (if cmd (--map (format "%s" it) (-flatten (-non-nil (list govc-command cmd))))
(split-string-and-unquote (read-shell-command "command: " nil 'govc-command-history)))))
(with-current-buffer (get-buffer-create (or buffer "*govc*"))
(govc-session-clone session)
(govc-shell-run (car args) (cdr args) (current-buffer)))))
(defcustom govc-max-events 100
"Limit events output to the last N events."
:type 'integer
:group 'govc)
(defun govc-events ()
"Events via govc events -n `govc-max-events'."
(interactive)
(govc-shell-command
(list "events" "-l" "-n" govc-max-events (if current-prefix-arg "-f") (govc-selection)) "*govc-event*"))
(defun govc-tasks ()
"Tasks via govc tasks."
(interactive)
(govc-shell-command
(list "tasks" "-l" "-n" govc-max-events (if current-prefix-arg "-f") (govc-selection)) "*govc-task*"))
(defun govc-logs ()
"Logs via govc logs -n `govc-max-events'."
(interactive)
(govc-shell-command
(let ((host (govc-selection)))
(list "logs" "-n" govc-max-events (if current-prefix-arg "-f") (if host (list "-host" host)))) "*govc-log*"))
(defun govc-parse-info (output)
"Parse govc info command OUTPUT."
(let* ((entries)
(entry)
(entry-key))
(-each output
(lambda (line)
(let* ((ix (s-index-of ":" line))
(key (s-trim (substring line 0 ix)))
(val (s-trim (substring line (+ ix 1)))))
(unless entry-key
(setq entry-key key))
(when (s-equals? key entry-key)
(setq entry (make-hash-table :test 'equal))
(add-to-list 'entries entry))
(puthash key val entry))))
entries))
(defun govc-table-column-names ()
"Return a list of column names from `tabulated-list-format'."
(--map (car (aref tabulated-list-format it))
(number-sequence 0 (- (length tabulated-list-format) 1))))
(defun govc-table-column-value (key)
"Return current column value for given KEY."
(let ((names (govc-table-column-names))
(entry (tabulated-list-get-entry))
(value))
(dotimes (ix (- (length names) 1))
(if (s-equals? key (nth ix names))
(setq value (elt entry ix))))
value))
(defun govc-table-info (command &optional args)
"Convert `govc-parse-info' COMMAND ARGS output to `tabulated-list-entries' format."
(let ((names (govc-table-column-names)))
(-map (lambda (info)
(let ((id (or (gethash "Path" info)
(gethash (car names) info))))
(list id (vconcat
(--map (or (gethash it info) "-")
names)))))
(govc-parse-info (govc command args)))))
(defun govc-map-info (command &optional args)
"Populate key=val map table with govc COMMAND ARGS output."
(-map (lambda (line)
(let* ((ix (s-index-of ":" line))
(key (s-trim (substring line 0 ix)))
(val (s-trim (substring line (+ ix 1)))))
(list key (vector key val))))
(govc command args)))
(defun govc-map-info-table (entries)
"Tabulated `govc-map-info' data via ENTRIES."
(let ((session (govc-current-session))
(args (append govc-args (govc-selection)))
(buffer (get-buffer-create "*govc-info*")))
(pop-to-buffer buffer)
(tabulated-list-mode)
(setq govc-args args)
(govc-session-clone session)
(setq tabulated-list-format [("Name" 50)
("Value" 50)]
tabulated-list-padding 2
tabulated-list-entries entries)
(tabulated-list-print)))
(defun govc-type-list-entries (command)
"Convert govc COMMAND type table output to `tabulated-list-entries'."
(-map (lambda (line)
(let* ((entry (s-split-up-to " " (s-collapse-whitespace line) 2))
(name (car entry))
(type (nth 1 entry))
(value (car (last entry))))
(list name (vector name type value))))
(govc command govc-args)))
(defun govc-json-info-selection (command)
"Run govc COMMAND -json on `govc-selection'."
(if current-prefix-arg
(--each (govc-selection) (govc-json-info command it))
(govc-json-info command (govc-selection))))
(defun govc-json-diff ()
"Diff two *govc-json* buffers in view."
(let ((buffers))
(-each (window-list-1)
(lambda (w)
(with-current-buffer (window-buffer w)
(if (and (eq major-mode 'json-mode)
(s-starts-with? "*govc-json*" (buffer-name)))
(push (current-buffer) buffers)))) )
(if (= (length buffers) 2)
(pop-to-buffer
(diff-no-select (car buffers) (cadr buffers))))))
(defun govc-json-info (command selection)
"Run govc COMMAND -json on SELECTION."
(govc-process (govc-format-command command "-json" govc-args selection)
(lambda ()
(let ((buffer (get-buffer-create (concat "*govc-json*" (if current-prefix-arg selection)))))
(copy-to-buffer buffer (point-min) (point-max))
(with-current-buffer buffer
(json-mode)
;; We use `json-mode-beautify' as `json-pretty-print-buffer' does not work for `govc-host-json-info'
(json-mode-beautify))
(display-buffer buffer))))
(if current-prefix-arg
(govc-json-diff)))
(defun govc-mode-new-session ()
"Connect new session for the current govc mode."
(interactive)
(call-interactively 'govc-session)
(revert-buffer))
(defun govc-host-with-session ()
"Host-mode with current session."
(interactive)
(govc-host nil (govc-current-session)))
(defun govc-vm-with-session ()
"VM-mode with current session."
(interactive)
(govc-vm nil (govc-current-session)))
(defun govc-datastore-with-session ()
"Datastore-mode with current session."
(interactive)
(govc-datastore nil (govc-current-session)))
(defun govc-pool-with-session ()
"Pool-mode with current session."
(interactive)
(govc-pool nil (govc-current-session)))
;;; govc object mode
(defvar-local govc-object-history '("-")
"History list of visited objects.")
(defun govc-object-collect ()
"Wrapper for govc object.collect."
(interactive)
(let ((id (car govc-args)))
(add-to-list 'govc-object-history id)
(setq govc-session-path id))
(govc-type-list-entries "object.collect"))
(defun govc-object-collect-selection (&optional json)
"Expand object selection via govc object.collect.
Optionally specify JSON encoding."
(interactive)
(let* ((entry (or (tabulated-list-get-entry) (error "No entry")))
(name (elt entry 0))
(type (elt entry 1))
(val (elt entry 2)))
(setq govc-args (list (car govc-args) name))
(cond
((s-blank? val))
((and (not json) (s-ends-with? "types.ManagedObjectReference" type))
(let ((ids (govc "ls" "-L" (split-string val ","))))
(setq govc-args (list (govc-object-prompt "moid: " ids)))))
((string= val "...")
(if (s-starts-with? "[]" type) (setq json t))))
(if json
(govc-json-info "object.collect" nil)
(tabulated-list-revert))))
(defun govc-object-collect-selection-json ()
"JSON object selection via govc object.collect."
(interactive)
(govc-object-collect-selection t))
(defun govc-object-next ()
"Next managed object reference."
(interactive)
(if (search-forward "types.ManagedObjectReference" nil t)
(progn (govc-tabulated-list-unmark-all)
(tabulated-list-put-tag (char-to-string dired-marker-char)))
(goto-char (point-min))))
(defun govc-object-collect-parent ()
"Parent object selection if reachable, otherwise prompt with `govc-object-history'."
(interactive)
(if (cadr govc-args)
(let ((prop (butlast (split-string (cadr govc-args) "\\."))))
(setq govc-args (list (car govc-args) (if prop (s-join "." prop)))))
(save-excursion
(goto-char (point-min))
(if (re-search-forward "^[[:space:]]*parent" nil t)
(govc-object-collect-selection)
(let ((id (govc-object-prompt "moid: " govc-object-history)))
(setq govc-args (list id (if (string= id "-") "content")))))))
(tabulated-list-revert))
(defun govc-object (&optional moid property session)
"Object browser aka MOB (Managed Object Browser).
Optionally starting at MOID and PROPERTY if given.
Inherit SESSION if given."
(interactive)
(let ((buffer (get-buffer-create "*govc-object*")))
(if (called-interactively-p 'interactive)
(switch-to-buffer buffer)
(pop-to-buffer buffer))
(govc-object-mode)
(if session
(govc-session-clone session)
(call-interactively 'govc-session))
(setq govc-args (list (or moid "-") property))
(tabulated-list-print)))
(defun govc-object-info ()
"Object browser via govc object.collect on `govc-selection'."
(interactive)
(if (equal major-mode 'govc-object-mode)
(progn
(setq govc-args (list (govc-object-prompt "moid: " govc-object-history)))
(tabulated-list-revert))
(govc-object (tabulated-list-get-id) nil (govc-current-session))))
(defvar govc-object-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "J" 'govc-object-collect-selection-json)
(define-key map "N" 'govc-object-next)
(define-key map "O" 'govc-object-info)
(define-key map (kbd "DEL") 'govc-object-collect-parent)
(define-key map (kbd "RET") 'govc-object-collect-selection)
(define-key map "?" 'govc-object-popup)
map)
"Keymap for `govc-object-mode'.")
(define-derived-mode govc-object-mode govc-tabulated-list-mode "Object"
"Major mode for handling a govc object."
(setq tabulated-list-format [("Name" 40 t)
("Type" 40 t)
("Value" 40 t)]
tabulated-list-padding 2
tabulated-list-entries #'govc-object-collect)
(tabulated-list-init-header))
(magit-define-popup govc-object-popup
"Object popup."
:actions (govc-keymap-popup govc-object-mode-map))
;;; govc metric mode
(defun govc-metric-sample ()
"Sample metrics."
(interactive)
(govc-shell-command (list "metric.sample" govc-args govc-filter (govc-selection))))
(defun govc-metric-sample-plot ()
"Plot metric sample."
(interactive)
(let* ((type (if (and (display-images-p) (not (eq current-prefix-arg '-))) 'png 'dumb))
(max (if (member "-i" govc-args) "60" "180"))
(args (append govc-args (list "-n" max "-plot" type govc-filter)))
(session (govc-current-session))
(metrics (govc-selection))
(inhibit-read-only t))
(with-current-buffer (get-buffer-create "*govc*")
(govc-session-clone session)
(erase-buffer)
(delete-other-windows)
(if (eq type 'dumb)
(split-window-right)
(split-window-below))
(display-buffer-use-some-window (current-buffer) '((inhibit-same-window . t)))
(--each metrics
(let* ((cmd (govc-format-command "metric.sample" args it))
(data (govc-process cmd 'buffer-string)))
(if (eq type 'dumb)
(insert data)
(insert-image (create-image (string-as-unibyte data) type t))))))))
(defun govc-metric-select (metrics)
"Select metric names. METRICS is a regexp."
(interactive (list (read-regexp "Select metrics" (regexp-quote ".usage."))))
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(if (string-match-p metrics (tabulated-list-get-id))
(govc-tabulated-list-mark)
(govc-tabulated-list-unmark)))))
(defun govc-metric-info ()
"Wrapper for govc metric.info."
(govc-table-info "metric.info" (list govc-args (car govc-filter))))
(defvar govc-metric-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'govc-metric-sample)
(define-key map (kbd "P") 'govc-metric-sample-plot)
(define-key map (kbd "s") 'govc-metric-select)
map)
"Keymap for `govc-metric-mode'.")
(defun govc-metric ()
"Metrics info."
(interactive)
(let ((session (govc-current-session))
(filter (or (govc-selection) (list govc-session-path)))
(buffer (get-buffer-create "*govc-metric*")))
(pop-to-buffer buffer)
(govc-metric-mode)
(govc-session-clone session)
(if current-prefix-arg (setq govc-args '("-i" "300")))
(setq govc-filter filter)
(tabulated-list-print)))
(define-derived-mode govc-metric-mode govc-tabulated-list-mode "Metric"
"Major mode for handling a govc metric."
(setq tabulated-list-format [("Name" 35 t)
("Group" 15 t)
("Unit" 4 t)
("Level" 5 t)
("Summary" 50)]
tabulated-list-sort-key (cons "Name" nil)
tabulated-list-padding 2
tabulated-list-entries #'govc-metric-info)
(tabulated-list-init-header))
;;; govc host mode
(defun govc-ls-host ()
"List hosts."
(govc "ls" "-t" "HostSystem" "./..."))
(defun govc-esxcli-netstat-info ()
"Wrapper for govc host.esxcli network ip connection list."
(govc-table-info "host.esxcli"
(append govc-args '("-hints=false" "--" "network" "ip" "connection" "list"))))
(defun govc-esxcli-netstat (host)
"Tabulated `govc-esxcli-netstat-info' HOST."
(interactive (list (govc-object-prompt "Host: " 'govc-ls-host)))
(let ((session (govc-current-session))
(buffer (get-buffer-create "*govc-esxcli*")))
(pop-to-buffer buffer)
(tabulated-list-mode)
(setq govc-args (list "-host" host))
(govc-session-clone session)
(setq tabulated-list-format [("CCAlgo" 10 t)
("ForeignAddress" 20 t)
("LocalAddress" 20 t)
("Proto" 5 t)
("RecvQ" 5 t)
("SendQ" 5 t)
("State" 15 t)
("WorldID" 7 t)
("WorldName" 10 t)]
tabulated-list-padding 2
tabulated-list-entries #'govc-esxcli-netstat-info)
(tabulated-list-init-header)
(tabulated-list-print)))
(defun govc-host-esxcli-netstat ()
"Netstat via `govc-esxcli-netstat-info' with current host id."
(interactive)
(govc-esxcli-netstat (tabulated-list-get-id)))
(defun govc-host-info ()
"Wrapper for govc host.info."
(govc-table-info "host.info" (or govc-filter "*")))
(defun govc-host-json-info ()
"JSON via govc host.info -json on current selection."
(interactive)
(govc-json-info-selection "host.info"))
(defvar govc-host-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "E" 'govc-events)
(define-key map "L" 'govc-logs)
(define-key map "J" 'govc-host-json-info)
(define-key map "M" 'govc-metric)
(define-key map "N" 'govc-host-esxcli-netstat)
(define-key map "O" 'govc-object-info)
(define-key map "T" 'govc-tasks)
(define-key map "c" 'govc-mode-new-session)
(define-key map "p" 'govc-pool-with-session)
(define-key map "s" 'govc-datastore-with-session)
(define-key map "v" 'govc-vm-with-session)
(define-key map "?" 'govc-host-popup)
map)
"Keymap for `govc-host-mode'.")
(defun govc-host (&optional filter session)
"Host info via govc.
Optionally filter by FILTER and inherit SESSION."
(interactive)
(let ((buffer (get-buffer-create "*govc-host*")))
(pop-to-buffer buffer)
(govc-host-mode)
(if session
(govc-session-clone session)
(call-interactively 'govc-session))
(setq govc-filter filter)
(tabulated-list-print)))
(define-derived-mode govc-host-mode govc-tabulated-list-mode "Host"
"Major mode for handling a list of govc hosts."
(setq tabulated-list-format [("Name" 30 t)
("Logical CPUs" 20 t)
("CPU usage" 25 t)
("Memory" 10 t)
("Memory usage" 25 t)
("Manufacturer" 13 t)
("Boot time" 15 t)]
tabulated-list-sort-key (cons "Name" nil)
tabulated-list-padding 2
tabulated-list-entries #'govc-host-info)
(tabulated-list-init-header))
(magit-define-popup govc-host-popup
"Host popup."
:actions (govc-keymap-popup govc-host-mode-map))
(easy-menu-define govc-host-mode-menu govc-host-mode-map
"Host menu."
(cons "Host" (govc-keymap-menu govc-host-mode-map)))
;;; govc pool mode
(defun govc-pool-destroy (name)
"Destroy pool with given NAME."
(interactive (list (completing-read "Destroy pool: " (govc "ls" "-t" "ResourcePool" "host/*"))))
(govc "pool.destroy" name))
(defun govc-pool-destroy-selection ()
"Destroy via `govc-pool-destroy' on the pool selection."
(interactive)
(govc-do-selection 'govc-pool-destroy "Delete")
(tabulated-list-revert))
(defun govc-pool-info ()
"Wrapper for govc pool.info."
(govc-table-info "pool.info" (list "-a" (or govc-filter (setq govc-filter "*")))))
(defun govc-pool-json-info ()
"JSON via govc pool.info -json on current selection."
(interactive)
(govc-json-info-selection "pool.info"))
(defvar govc-pool-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "D" 'govc-pool-destroy-selection)
(define-key map "E" 'govc-events)
(define-key map "J" 'govc-pool-json-info)
(define-key map "M" 'govc-metric)
(define-key map "O" 'govc-object-info)
(define-key map "T" 'govc-tasks)
(define-key map "c" 'govc-mode-new-session)
(define-key map "h" 'govc-host-with-session)
(define-key map "s" 'govc-datastore-with-session)
(define-key map "v" 'govc-vm-with-session)
(define-key map "?" 'govc-pool-popup)
map)
"Keymap for `govc-pool-mode'.")
(defun govc-pool (&optional filter session)
"Pool info via govc.
Optionally filter by FILTER and inherit SESSION."
(interactive)
(let ((buffer (get-buffer-create "*govc-pool*")))
(pop-to-buffer buffer)
(govc-pool-mode)
(if session
(govc-session-clone session)
(call-interactively 'govc-session))
(setq govc-filter filter)
(tabulated-list-print)))
(define-derived-mode govc-pool-mode govc-tabulated-list-mode "Pool"
"Major mode for handling a list of govc pools."
(setq tabulated-list-format [("Name" 30 t)
("CPU Usage" 25 t)
("CPU Shares" 25 t)
("CPU Reservation" 25 t)
("CPU Limit" 10 t)
("Mem Usage" 25 t)
("Mem Shares" 25 t)
("Mem Reservation" 25 t)
("Mem Limit" 10 t)]
tabulated-list-sort-key (cons "Name" nil)
tabulated-list-padding 2
tabulated-list-entries #'govc-pool-info)
(tabulated-list-init-header))
(magit-define-popup govc-pool-popup
"Pool popup."
:actions (govc-keymap-popup govc-pool-mode-map))
(easy-menu-define govc-host-mode-menu govc-pool-mode-map
"Pool menu."
(cons "Pool" (govc-keymap-menu govc-pool-mode-map)))
;;; govc datastore mode
(defun govc-ls-datastore ()
"List datastores."
(govc "ls" "datastore"))
(defun govc-datastore-ls-entries ()
"Wrapper for govc datastore.ls."
(let* ((data (govc-json "datastore.ls" "-l" "-p" govc-filter))
(file (plist-get (elt data 0) :File)))
(-map (lambda (ent)
(let ((name (plist-get ent :Path))
(size (plist-get ent :FileSize))
(time (plist-get ent :Modification))
(user (plist-get ent :Owner)))
(list (concat govc-filter name)
(vector (file-size-human-readable size)
(current-time-string (date-to-time time))
name)))) file)))
(defun govc-datastore-ls-parent ()
"Up to parent folder."
(interactive)
(if (s-blank? govc-filter)
(let ((session (govc-current-session)))
(govc-datastore-mode)
(govc-session-clone session))
(setq govc-filter (file-name-directory (directory-file-name govc-filter))))
(tabulated-list-revert))
(defun govc-datastore-ls-child ()
"Open datastore folder or file."
(interactive)
(let ((id (tabulated-list-get-id)))
(if current-prefix-arg
(govc-shell-command (list "datastore.ls" "-l" "-p" "-R" id))
(if (s-ends-with? "/" id)
(progn (setq govc-filter id)
(tabulated-list-revert))
(govc-datastore-open)))))
(defun govc-datastore-open ()
"Open datastore file."
(lexical-let* ((srcfile (tabulated-list-get-id))
(srcpath (format "[%s] %s" (file-name-nondirectory govc-session-datastore) (s-chop-prefix "/" srcfile)))
(suffix (file-name-extension srcfile t))
(tmpfile (make-temp-file "govc-ds" nil suffix))
(session (govc-current-session)))
(when (yes-or-no-p (concat "Open " srcpath "?"))
(govc "datastore.download" srcfile tmpfile)
(with-current-buffer (pop-to-buffer (find-file-noselect tmpfile))
(govc-session-clone session)
(add-hook 'kill-buffer-hook (lambda ()
(with-demoted-errors
(delete-file tmpfile))) t t)
(add-hook 'after-save-hook (lambda ()
(if (yes-or-no-p (concat "Upload changes to " srcpath "?"))
(with-demoted-errors
(govc "datastore.upload" tmpfile srcfile)))) t t)))))
(defun govc-datastore-tail (&optional file)
"Tail datastore FILE."
(interactive)
(govc-shell-command
(list "datastore.tail" "-n" govc-max-events (if current-prefix-arg "-f") (or file (govc-selection)))))
(defun govc-datastore-disk-info ()
"Info datastore disk."
(interactive)
(delete-other-windows)
(govc-shell-command
(list "datastore.disk.info" "-uuid" (if current-prefix-arg "-c") (govc-selection))))
(defun govc-datastore-ls-json ()
"JSON via govc datastore.ls -json on current selection."
(interactive)
(let ((govc-args '("-l" "-p")))
(govc-json-info-selection "datastore.ls")))
(defun govc-datastore-ls-r-json ()
"Search via govc datastore.ls -json -R on current selection."
(interactive)
(let ((govc-args '("-l" "-p" "-R")))
(govc-json-info-selection "datastore.ls")))
(defun govc-datastore-mkdir (name)
"Mkdir via govc datastore.mkdir with given NAME."
(interactive (list (read-from-minibuffer "Create directory: " govc-filter)))
(govc "datastore.mkdir" name)
(tabulated-list-revert))
(defun govc-datastore-rm (paths)
"Delete datastore PATHS."
(--each paths (govc "datastore.rm" (if current-prefix-arg "-f") it)))
(defun govc-datastore-rm-selection ()
"Delete selected datastore paths."
(interactive)
(govc-do-selection 'govc-datastore-rm "Delete")
(tabulated-list-revert))
(defvar govc-datastore-ls-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "I" 'govc-datastore-disk-info)
(define-key map "J" 'govc-datastore-ls-json)
(define-key map "S" 'govc-datastore-ls-r-json)
(define-key map "D" 'govc-datastore-rm-selection)
(define-key map "T" 'govc-datastore-tail)
(define-key map "+" 'govc-datastore-mkdir)
(define-key map (kbd "DEL") 'govc-datastore-ls-parent)
(define-key map (kbd "RET") 'govc-datastore-ls-child)
(define-key map "?" 'govc-datastore-ls-popup)
map)
"Keymap for `govc-datastore-ls-mode'.")
(defun govc-datastore-ls (&optional datastore session filter)
"List govc datastore. Optionally specify DATASTORE, SESSION and FILTER."
(interactive)
(let ((buffer (get-buffer-create "*govc-datastore*")))
(pop-to-buffer buffer)
(govc-datastore-ls-mode)
(if session
(govc-session-clone session)
(call-interactively 'govc-session))
(setq govc-session-datastore (or datastore (govc-object-prompt "govc datastore: " 'govc-ls-datastore)))
(setq govc-filter filter)
(tabulated-list-print)))
(define-derived-mode govc-datastore-ls-mode govc-tabulated-list-mode "Datastore"
"Major mode govc datastore.ls."
(setq-local font-lock-defaults `(,(cdr govc-font-lock-keywords)))
(setq tabulated-list-format [("Size" 10 t)
("Modification time" 25 t)
("Name" 40 t)]
tabulated-list-sort-key (cons "Name" nil)
tabulated-list-padding 2
tabulated-list-entries #'govc-datastore-ls-entries)
(tabulated-list-init-header))
(magit-define-popup govc-datastore-ls-popup
"Datastore ls popup."
:actions (govc-keymap-popup govc-datastore-ls-mode-map))
(easy-menu-define govc-datastore-ls-mode-menu govc-datastore-ls-mode-map
"Datastore ls menu."
(cons "Datastore" (govc-keymap-menu govc-datastore-ls-mode-map)))
(defvar govc-datastore-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "J" 'govc-datastore-json-info)
(define-key map "M" 'govc-metric)
(define-key map "O" 'govc-object-info)
(define-key map (kbd "RET") 'govc-datastore-ls-selection)
(define-key map "c" 'govc-mode-new-session)
(define-key map "h" 'govc-host-with-session)
(define-key map "p" 'govc-pool-with-session)
(define-key map "v" 'govc-vm-with-session)
(define-key map "?" 'govc-datastore-popup)
map)
"Keymap for `govc-datastore-mode'.")
(defun govc-datastore-json-info ()
"JSON via govc datastore.info -json on current selection."
(interactive)
(govc-json-info-selection "datastore.info"))
(defun govc-datastore-info ()
"Wrapper for govc datastore.info."
(govc-table-info "datastore.info" (or govc-filter "*")))
(defun govc-datastore-ls-selection ()
"Browse datastore."
(interactive)
(govc-datastore-ls (tabulated-list-get-id) (govc-current-session)))
(defun govc-datastore (&optional filter session)
"Datastore info via govc.
Optionally filter by FILTER and inherit SESSION."
(interactive)
(let ((buffer (get-buffer-create "*govc-datastore*")))
(pop-to-buffer buffer)
(govc-datastore-mode)
(if session
(govc-session-clone session)
(call-interactively 'govc-session))
(setq govc-filter filter)
(tabulated-list-print)
(if (and govc-session-datastore (search-forward govc-session-datastore nil t))
(beginning-of-line))))
(define-derived-mode govc-datastore-mode tabulated-list-mode "Datastore"
"Major mode for govc datastore.info."
(setq tabulated-list-format [("Name" 15 t)
("Type" 10 t)
("Capacity" 10 t)
("Free" 10 t)
("Remote" 30 t)]
tabulated-list-sort-key (cons "Name" nil)
tabulated-list-padding 2
tabulated-list-entries #'govc-datastore-info)
(tabulated-list-init-header))
(magit-define-popup govc-datastore-popup
"Datastore popup."
:actions (govc-keymap-popup govc-datastore-mode-map))
(easy-menu-define govc-datastore-mode-menu govc-datastore-mode-map
"Datastore menu."
(cons "Datastore" (govc-keymap-menu govc-datastore-mode-map)))
;;; govc vm mode
(defun govc-vm-prompt (prompt)
"PROMPT for a vm name."
(completing-read prompt (govc "ls" "vm")))
(defun govc-vm-start (name)
"Start vm with given NAME."
(interactive (list (govc-vm-prompt "Start vm: ")))
(govc "vm.power" "-on" name))
(defun govc-vm-shutdown (name)
"Shutdown vm with given NAME."
(interactive (list (govc-vm-prompt "Shutdown vm: ")))
(govc "vm.power" "-s" "-force" name))
(defun govc-vm-reboot (name)
"Reboot vm with given NAME."
(interactive (list (govc-vm-prompt "Reboot vm: ")))
(govc "vm.power" "-r" "-force" name))
(defun govc-vm-suspend (name)
"Suspend vm with given NAME."
(interactive (list (govc-vm-prompt "Suspend vm: ")))
(govc "vm.power" "-suspend" name))
(defun govc-vm-destroy (name)
"Destroy vm with given NAME."
(interactive (list (govc-vm-prompt "Destroy vm: ")))
(govc "vm.destroy" name))
(defun govc-vm-vnc-enable (name)
"Enable vnc on vm with given NAME."
(--map (last (split-string it))
(govc "vm.vnc" "-enable"
"-port" "-1"
"-password" (format "%08x" (random (expt 16 8))) name)))
(defun govc-vm-vnc (name &optional arg)
"VNC for vm with given NAME.
By default, enable and open VNC for the given vm NAME.
With prefix \\[negative-argument] ARG, VNC will be disabled.
With prefix \\[universal-argument] ARG, VNC will be enabled but not opened."
(interactive (list (govc-vm-prompt "VNC vm: ")
current-prefix-arg))
(if (equal arg '-)
(govc "vm.vnc" "-disable" name)
(let ((urls (govc-vm-vnc-enable name)))
(unless arg
(-each (-flatten urls) 'browse-url)))))
(defun govc-vm-console (name &optional arg)
"Console for vm with given NAME.
By default, displays a console screen capture.
With prefix \\[universal-argument] ARG, launches an interactive console (VMRC)."
(interactive (list (govc-vm-prompt "Console vm: ")
current-prefix-arg))
(if arg
(browse-url (car (govc "vm.console" name)))
(let* ((data (govc-process (govc-format-command "vm.console" "-capture" "-" name) 'buffer-string))
(inhibit-read-only t))
(with-current-buffer (get-buffer-create "*govc*")
(erase-buffer)
(insert-image (create-image (string-as-unibyte data) 'png t))
(read-only-mode)
(display-buffer (current-buffer))))))
(defun govc-vm-start-selection ()
"Start via `govc-vm-start' on the current selection."
(interactive)
(govc-vm-start (govc-selection))
(tabulated-list-revert))
(defun govc-vm-shutdown-selection ()
"Shutdown via `govc-vm-shutdown' on the current selection."
(interactive)
(govc-vm-shutdown (govc-selection))
(tabulated-list-revert))
(defun govc-vm-reboot-selection ()
"Reboot via `govc-vm-reboot' on the current selection."
(interactive)
(govc-vm-reboot (govc-selection))
(tabulated-list-revert))
(defun govc-vm-suspend-selection ()
"Suspend via `govc-vm-suspend' on the current selection."
(interactive)
(govc-vm-suspend (govc-selection))
(tabulated-list-revert))
(defun govc-vm-destroy-selection ()
"Destroy via `govc-vm-destroy' on the current selection."
(interactive)
(govc-do-selection 'govc-vm-destroy "Destroy")
(tabulated-list-revert))
(defun govc-vm-vnc-selection ()
"VNC via `govc-vm-vnc' on the current selection."
(interactive)
(govc-vm-vnc (govc-selection) current-prefix-arg))
(defun govc-vm-console-selection ()
"Console via `govc-vm-console' on the current selection."
(interactive)
(govc-vm-console (tabulated-list-get-id) current-prefix-arg))
(defun govc-vm-info ()
"Wrapper for govc vm.info."
(unless (string-empty-p govc-session-datacenter)
(govc-table-info "vm.info" (list "-r" (or govc-filter (setq govc-filter "*"))))))
(defun govc-vm-host ()
"Host info via `govc-host' with host(s) of current selection."
(interactive)
(govc-host (concat "*/" (govc-table-column-value "Host"))
(govc-current-session)))
(defun govc-vm-log-directory ()
"VM log directory of current selection."
(car (govc "object.collect" "-s" (tabulated-list-get-id) "config.files.logDirectory")))
(defun govc-vm-datastore ()
"Datastore via `govc-datastore-ls' with datastore of current selection."
(interactive)
(if current-prefix-arg
(govc-datastore (s-split ", " (govc-table-column-value "Storage") t)
(govc-current-session))
(let* ((dir (govc-vm-log-directory))
(args (s-split "\\[\\|\\]" dir t)))
(govc-datastore-ls (first args) (govc-current-session) (concat (s-trim (second args)) "/")))))
(defun govc-vm-logs ()
"Logs via `govc-datastore-tail' with logDirectory of current selection."
(interactive)
(if (tabulated-list-get-id)
(govc-datastore-tail (concat (govc-vm-log-directory) "/vmware.log"))
(govc-logs)))
(defun govc-vm-ping ()
"Ping VM."
(interactive)
(let ((ping-program-options '("-c" "20")))
(ping (govc-table-column-value "IP address"))))
(defun govc-vm-device-ls ()
"Devices via `govc-device' on the current selection."
(interactive)
(govc-device (tabulated-list-get-id)
(govc-current-session)))
(defun govc-vm-extra-config ()
"Populate table with govc vm.info -e output."
(let* ((data (govc-json "vm.info" govc-args))
(vms (plist-get data :VirtualMachines))
(info))
(mapc
(lambda (vm)
(let* ((config (plist-get vm :Config))
(name (plist-get config :Name)))
(mapc (lambda (x)
(let ((key (plist-get x :Key))
(val (plist-get x :Value)))
(push (list key (vector key val)) info)))
(plist-get config :ExtraConfig))
(if (> (length vms) 1)
(push (list name (vector "vm.name" name)) info))))
vms)
info))
(defun govc-vm-extra-config-table ()
"ExtraConfig via `govc-vm-extra-config' on the current selection."
(interactive)
(govc-map-info-table #'govc-vm-extra-config))
(defun govc-vm-json-info ()
"JSON via govc vm.info -json on current selection."
(interactive)
(govc-json-info-selection "vm.info"))
(defvar govc-vm-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "E" 'govc-events)
(define-key map "L" 'govc-vm-logs)
(define-key map "J" 'govc-vm-json-info)
(define-key map "O" 'govc-object-info)
(define-key map "T" 'govc-tasks)
(define-key map "X" 'govc-vm-extra-config-table)
(define-key map (kbd "RET") 'govc-vm-device-ls)
(define-key map "C" 'govc-vm-console-selection)
(define-key map "V" 'govc-vm-vnc-selection)
(define-key map "D" 'govc-vm-destroy-selection)
(define-key map "^" 'govc-vm-start-selection)
(define-key map "!" 'govc-vm-shutdown-selection)
(define-key map "@" 'govc-vm-reboot-selection)
(define-key map "&" 'govc-vm-suspend-selection)
(define-key map "H" 'govc-vm-host)
(define-key map "M" 'govc-metric)
(define-key map "P" 'govc-vm-ping)
(define-key map "S" 'govc-vm-datastore)
(define-key map "c" 'govc-mode-new-session)
(define-key map "h" 'govc-host-with-session)
(define-key map "p" 'govc-pool-with-session)
(define-key map "s" 'govc-datastore-with-session)
(define-key map "?" 'govc-vm-popup)
map)
"Keymap for `govc-vm-mode'.")
(defun govc-vm (&optional filter session)
"VM info via govc.
Optionally filter by FILTER and inherit SESSION."
(interactive)
(let ((buffer (get-buffer-create "*govc-vm*")))
(pop-to-buffer buffer)
(govc-vm-mode)
(if session
(govc-session-clone session)
(call-interactively 'govc-session))
(setq govc-filter filter)
(tabulated-list-print)))
(define-derived-mode govc-vm-mode govc-tabulated-list-mode "VM"
"Major mode for handling a list of govc vms."
(setq tabulated-list-format [("Name" 40 t)
("Power state" 12 t)
("Boot time" 13 t)
("IP address" 15 t)
("Guest name" 20 t)
("Host" 20 t)
("CPU usage" 15 t)
("Host memory usage" 18 t)
("Guest memory usage" 19 t)
("Storage committed" 18 t)
("Storage" 10 t)
("Network" 10 t)]
tabulated-list-sort-key (cons "Name" nil)
tabulated-list-padding 2
tabulated-list-entries #'govc-vm-info)
(tabulated-list-init-header))
(magit-define-popup govc-vm-popup
"VM popup."
:actions (govc-keymap-popup govc-vm-mode-map))
(easy-menu-define govc-vm-mode-menu govc-vm-mode-map
"VM menu."
(cons "VM" (govc-keymap-menu govc-vm-mode-map)))
;;; govc device mode
(defun govc-device-ls ()
"Wrapper for govc device.ls -vm VM."
(govc-type-list-entries "device.ls"))
(defun govc-device-info ()
"Populate table with govc device.info output."
(govc-map-info "device.info" govc-args))
(defun govc-device-info-table ()
"Tabulated govc device.info."
(interactive)
(govc-map-info-table #'govc-device-info))
(defun govc-device-json-info ()
"JSON via govc device.info -json on current selection."
(interactive)
(govc-json-info-selection "device.info"))
(defvar govc-device-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "J") 'govc-device-json-info)
(define-key map (kbd "RET") 'govc-device-info-table)
map)
"Keymap for `govc-device-mode'.")
(defun govc-device (&optional vm session)
"List govc devices for VM. Optionally inherit SESSION."
(interactive)
(let ((buffer (get-buffer-create "*govc-device*")))
(pop-to-buffer buffer)
(govc-device-mode)
(if session
(govc-session-clone session)
(call-interactively 'govc-session))
(setq govc-args (list "-vm" (or vm (govc-vm-prompt "vm: "))))
(tabulated-list-print)))
(define-derived-mode govc-device-mode govc-tabulated-list-mode "Device"
"Major mode for handling a govc device."
(setq tabulated-list-format [("Name" 15 t)
("Type" 30 t)
("Summary" 40 t)]
tabulated-list-sort-key (cons "Name" nil)
tabulated-list-padding 2
tabulated-list-entries #'govc-device-ls)
(tabulated-list-init-header))
(magit-define-popup govc-popup
"govc popup."
:actions (govc-keymap-list govc-command-map))
(easy-menu-change
'("Tools") "govc"
(govc-keymap-menu govc-command-map)
"Search Files (Grep)...")
(provide 'govc)
;;; govc.el ends here
|