-
Notifications
You must be signed in to change notification settings - Fork 47
/
self_diagnostic.py
187 lines (152 loc) · 5.6 KB
/
self_diagnostic.py
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
from __future__ import annotations
import glob
import os
import shlex
import shutil
import socket
import ssl
import subprocess
import sys
from urllib.parse import urlparse
import click
from globus_compute_sdk.sdk._environments import (
get_amqp_service_host,
get_web_service_url,
)
from globus_compute_sdk.sdk.web_client import WebClient
def cat(path: str, wildcard: bool = False, max_bytes: int = 0):
max_bytes = max(0, max_bytes)
full_path = os.path.expanduser(path)
if wildcard:
files = glob.glob(full_path)
else:
files = [full_path]
def kernel() -> None:
for filename in files:
cat_cmd = "cat " + filename
hline = "=" * len(cat_cmd)
click.echo(cat_cmd + "\n" + hline)
if os.path.exists(filename):
with open(filename, "rb") as f:
if max_bytes:
file_size = os.fstat(f.fileno()).st_size
f.seek(max(file_size - max_bytes, 0))
content = f.read().replace(b"\n", b"\n | ")
click.echo(b" | " + content)
else:
click.secho(f"No file named {filename}\n", fg="red", bold=True)
hline = "-" * len(cat_cmd)
click.echo(hline + "\n")
kernel.display_name = f"cat({path})" # type: ignore
return kernel
def test_conn(host: str, port: int, timeout: int = 5):
def kernel():
try:
socket.create_connection((host, port), timeout=timeout)
click.echo(f"Connected successfully to {host} over port {port}!\n")
except OSError as e:
click.secho(
f"Connection failed to {host} over port {port}: {e}\n",
fg="red",
bold=True,
)
kernel.display_name = f"test_conn({host}, {port})" # type: ignore
return kernel
def test_ssl_conn(host: str, port: int, timeout: int = 5):
def kernel() -> None:
context = ssl.create_default_context()
try:
with socket.create_connection((host, port), timeout=timeout) as sock:
with context.wrap_socket(sock, server_hostname=host) as ssock:
conn_data = ssock.cipher()
cipher, version, _ = conn_data if conn_data else (None, None, None)
click.echo(
f"Successfully established SSL connection with {host}:{port}!\n"
f"Version: {version}\n"
f"Cipher: {cipher}\n"
)
except OSError as e:
click.secho(
f"Failed to establish SSL connection with {host}:{port} - {e}\n",
fg="red",
bold=True,
)
kernel.display_name = f"test_ssl_conn({host}, {port})" # type: ignore
return kernel
def get_service_versions(base_url: str):
def kernel():
wc = WebClient(base_url=base_url)
res = wc.get_version(service="all")
click.echo(f"{res}\n")
kernel.display_name = f"get_service_versions({base_url})" # type: ignore
return kernel
def get_executable_path(exec_name: str):
def kernel():
path = shutil.which(exec_name)
click.echo(f"{path}\n")
kernel.display_name = f"which({exec_name})" # type: ignore
return kernel
def which_python():
click.echo(f"{sys.executable}\n")
def get_openssl_version():
click.echo(f"{ssl.OPENSSL_VERSION}\n")
def get_python_version():
click.echo(f"Python version {sys.version}\n")
def _run_command(cmd: str):
cmd_list = shlex.split(cmd)
arg0 = cmd_list[0]
if not shutil.which(arg0):
click.secho(f"{arg0} was not found in the PATH\n", fg="red", bold=True)
return
try:
res = subprocess.run(cmd_list, timeout=30, capture_output=True)
if res.stdout:
click.echo(res.stdout)
if res.stderr:
click.secho(res.stderr.decode("utf-8"), fg="red", bold=True)
except subprocess.TimeoutExpired:
click.secho("Command timed out\n", fg="red", bold=True)
except Exception as e:
click.secho(f"Command failed: {e}\n", fg="red", bold=True)
def run_self_diagnostic(log_bytes: int = 0):
web_svc_url = get_web_service_url()
web_svc_host = urlparse(web_svc_url).netloc
amqp_svc_host = get_amqp_service_host()
commands = [
"uname -a",
cat("/etc/os-release"),
"whoami",
which_python,
get_python_version,
"pip freeze",
get_openssl_version,
test_conn(web_svc_host, 443),
test_conn(amqp_svc_host, 5671),
test_ssl_conn(web_svc_host, 443),
test_ssl_conn(amqp_svc_host, 5671),
get_service_versions(web_svc_url),
"ip addr",
"ifconfig",
"ip route",
"netstat -r",
get_executable_path("globus-compute-endpoint"),
get_executable_path("process_worker_pool.py"),
"globus-compute-endpoint whoami",
"globus-compute-endpoint list",
cat("~/.globus_compute/*/*.yaml", wildcard=True),
cat("~/.globus_compute/*/*.py", wildcard=True),
cat("~/.globus_compute/*/*.j2", wildcard=True),
cat("~/.globus_compute/*/*.json", wildcard=True),
cat("~/.globus_compute/*/*.log", wildcard=True, max_bytes=log_bytes),
]
for cmd in commands:
if callable(cmd):
display_name = getattr(cmd, "display_name", f"{cmd.__name__}()")
display_name = f"python:{display_name}"
else:
display_name = str(cmd)
click.secho(f"== Diagnostic: {display_name} ==", fg="yellow", bold=True)
if callable(cmd):
cmd()
else:
_run_command(cmd)