|
25 | 25 | import logging |
26 | 26 | import os |
27 | 27 | from monplugin import Status |
28 | | -from pyVmomi import vim |
29 | 28 | from ..tools import cli, service_instance |
30 | 29 |
|
31 | 30 |
|
32 | 31 | def run(): |
33 | | - try: |
34 | | - parser = cli.Parser() |
35 | | - parser.add_optional_arguments({ |
36 | | - 'name_or_flags': ['--skip-permission'], |
37 | | - 'options': { |
38 | | - 'action': 'store_true', |
39 | | - 'default': False, |
40 | | - 'help': 'skips the System.View permission check', |
41 | | - } |
42 | | - }) |
| 32 | + parser = cli.Parser() |
| 33 | + parser.add_optional_arguments({ |
| 34 | + 'name_or_flags': ['--skip-permission'], |
| 35 | + 'options': { |
| 36 | + 'action': 'store_true', |
| 37 | + 'default': False, |
| 38 | + 'help': 'skips the System.View permission check', |
| 39 | + } |
| 40 | + }) |
43 | 41 |
|
44 | | - args = parser.get_args() |
45 | | - si = service_instance.connect(args) |
46 | | - about = si.content.about |
47 | | - status = Status.OK |
48 | | - clock = True |
49 | | - if not args.skip_permission: |
50 | | - try: |
51 | | - clock = si.serverClock |
52 | | - except Exception: |
53 | | - logging.debug("no server clock", exc_info=1) |
54 | | - status = Status.CRITICAL |
55 | | - clock = None |
56 | | - if args.sessionfile: |
57 | | - try: |
58 | | - logging.debug(f"deleting {args.sessionfile}") |
59 | | - os.unlink(args.sessionfile) |
60 | | - except Exception: |
61 | | - logging.debug(f"unlink {args.sessionfile} failed", exc_info=1) |
| 42 | + args = parser.get_args() |
| 43 | + si = service_instance.connect(args) |
| 44 | + about = si.content.about |
| 45 | + status = Status.OK |
| 46 | + clock = True |
| 47 | + if not args.skip_permission: |
| 48 | + try: |
| 49 | + clock = si.serverClock |
| 50 | + except Exception: |
| 51 | + logging.debug("no server clock", exc_info=1) |
| 52 | + status = Status.CRITICAL |
| 53 | + clock = None |
| 54 | + if args.sessionfile: |
| 55 | + try: |
| 56 | + logging.debug(f"deleting {args.sessionfile}") |
| 57 | + os.unlink(args.sessionfile) |
| 58 | + except Exception: |
| 59 | + logging.debug(f"unlink {args.sessionfile} failed", exc_info=1) |
62 | 60 |
|
63 | | - out = ( |
64 | | - f'{status.name}: ' |
65 | | - f'{ "No System.View permission, " if not clock else "" }' |
66 | | - f'{ about.fullName }, ' |
67 | | - f'api: { about.apiType }/{ about.apiVersion }, ' |
68 | | - f'product: { about.licenseProductName } { about.licenseProductVersion }' |
69 | | - ) |
70 | | - print(out) |
71 | | - raise SystemExit(status.value) |
72 | | - except vim.fault.VimFault as e: |
73 | | - if hasattr(e, 'msg'): |
74 | | - print(f"ERROR: {e.msg}") |
75 | | - else: |
76 | | - print(f"ERROR: {e}") |
77 | | - raise SystemExit(2) |
78 | | - except Exception as e: |
79 | | - print(f"ERROR: {e}") |
80 | | - raise SystemExit(2) |
| 61 | + out = ( |
| 62 | + f'{status.name}: ' |
| 63 | + f'{ "No System.View permission, " if not clock else "" }' |
| 64 | + f'{ about.fullName }, ' |
| 65 | + f'api: { about.apiType }/{ about.apiVersion }, ' |
| 66 | + f'product: { about.licenseProductName } { about.licenseProductVersion }' |
| 67 | + ) |
| 68 | + print(out) |
| 69 | + raise SystemExit(status.value) |
81 | 70 |
|
82 | 71 | if __name__ == "__main__": |
83 | | - run() |
| 72 | + try: |
| 73 | + run() |
| 74 | + except SystemExit as e: |
| 75 | + if not isinstance(e.code, int) or e.code > 3 or e.code < 0: |
| 76 | + print("UNKNOWN EXIT CODE") |
| 77 | + raise SystemExit(Status.UNKNOWN) |
| 78 | + raise |
| 79 | + except Exception as e: |
| 80 | + print("UNKNOWN - " + str(e)) |
| 81 | + raise SystemExit(Status.UNKNOWN) |
0 commit comments