sshkube 0.2.3__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {sshkube-0.2.3 → sshkube-0.3.0}/PKG-INFO +1 -1
- {sshkube-0.2.3 → sshkube-0.3.0}/pyproject.toml +1 -1
- {sshkube-0.2.3 → sshkube-0.3.0}/sshkube/__main__.py +32 -19
- {sshkube-0.2.3 → sshkube-0.3.0}/LICENSE +0 -0
- {sshkube-0.2.3 → sshkube-0.3.0}/README.md +0 -0
- {sshkube-0.2.3 → sshkube-0.3.0}/sshkube/__init__.py +0 -0
- {sshkube-0.2.3 → sshkube-0.3.0}/sshkube/socat.py +0 -0
|
@@ -60,17 +60,32 @@ def wait_for_port(port, timeout=1, backoff=1, retries=3):
|
|
|
60
60
|
sock.close()
|
|
61
61
|
raise RuntimeError(f"Proxy server didn't start!")
|
|
62
62
|
|
|
63
|
+
def kubectl_livez(port, timeout=1):
|
|
64
|
+
import ssl, urllib.request, urllib.error
|
|
65
|
+
try:
|
|
66
|
+
with urllib.request.urlopen(f"https://127.0.0.1:{port}/livez", timeout=timeout, context=ssl._create_unverified_context()) as res:
|
|
67
|
+
return res.getcode()
|
|
68
|
+
except urllib.error.HTTPError as e:
|
|
69
|
+
return e.getcode()
|
|
70
|
+
except:
|
|
71
|
+
return 600
|
|
72
|
+
|
|
63
73
|
class PidFile:
|
|
64
74
|
pidfile = workdir/'pid'
|
|
65
|
-
def __init__(self, *, pid, port):
|
|
75
|
+
def __init__(self, *, netloc, pid, port):
|
|
76
|
+
self.netloc = netloc
|
|
66
77
|
self.pid = pid
|
|
67
78
|
self.port = port
|
|
68
79
|
|
|
69
80
|
@staticmethod
|
|
70
81
|
def read():
|
|
71
82
|
if PidFile.pidfile.exists():
|
|
72
|
-
pid, _,
|
|
73
|
-
|
|
83
|
+
pid, _, netloc_port = PidFile.pidfile.read_text().partition(':')
|
|
84
|
+
netloc, _, port = netloc_port.partition(':')
|
|
85
|
+
if not _:
|
|
86
|
+
port = netloc
|
|
87
|
+
netloc = ''
|
|
88
|
+
pidfile = PidFile(netloc=netloc, pid=int(pid), port=int(port))
|
|
74
89
|
if pidfile.running:
|
|
75
90
|
return pidfile
|
|
76
91
|
else:
|
|
@@ -79,7 +94,7 @@ class PidFile:
|
|
|
79
94
|
def write(self):
|
|
80
95
|
if PidFile.pidfile.exists(): raise RuntimeError('PID file already exists')
|
|
81
96
|
PidFile.pidfile.parent.mkdir(parents=True, exist_ok=True)
|
|
82
|
-
PidFile.pidfile.write_text(f"{self.pid}:{self.port}")
|
|
97
|
+
PidFile.pidfile.write_text(f"{self.pid}:{self.netloc}:{self.port}")
|
|
83
98
|
|
|
84
99
|
@property
|
|
85
100
|
def running(self):
|
|
@@ -163,7 +178,10 @@ def start_server(*, server, force):
|
|
|
163
178
|
def _start_server(*, server, force):
|
|
164
179
|
pid = PidFile.read()
|
|
165
180
|
if pid:
|
|
166
|
-
if force:
|
|
181
|
+
if force or pid.netloc != server:
|
|
182
|
+
_kill_server()
|
|
183
|
+
elif kubectl_livez(pid.port) >= 500:
|
|
184
|
+
# permission denied error is also fine if connection is broken we get a 600
|
|
167
185
|
_kill_server()
|
|
168
186
|
else:
|
|
169
187
|
return
|
|
@@ -182,7 +200,9 @@ def _start_server(*, server, force):
|
|
|
182
200
|
proc = Popen(make_ssh_cmd(server=server, flags=[f"-NL{port}:{k8s_server_parsed.netloc}"]), start_new_session=True)
|
|
183
201
|
try:
|
|
184
202
|
wait_for_port(port)
|
|
185
|
-
|
|
203
|
+
if kubectl_livez(port) >= 500:
|
|
204
|
+
raise click.UsageError('Kubernetes not available')
|
|
205
|
+
PidFile(netloc=server, pid=proc.pid, port=port).write()
|
|
186
206
|
except RuntimeError as e:
|
|
187
207
|
proc.kill()
|
|
188
208
|
raise click.UsageError('Proxy server failed to start..') from e
|
|
@@ -212,11 +232,9 @@ def _init(*, server):
|
|
|
212
232
|
'''
|
|
213
233
|
Usage: eval "$(sshkube init)"
|
|
214
234
|
'''
|
|
235
|
+
_start_server(server=server, force=False)
|
|
215
236
|
pid = PidFile.read()
|
|
216
|
-
|
|
217
|
-
_start_server(server=server, force=False)
|
|
218
|
-
pid = PidFile.read()
|
|
219
|
-
assert pid is not None
|
|
237
|
+
assert pid is not None
|
|
220
238
|
#
|
|
221
239
|
if sys.platform == 'win32':
|
|
222
240
|
print(
|
|
@@ -243,10 +261,9 @@ def _run(*, server, args):
|
|
|
243
261
|
Usage: sshkube run kubectl help
|
|
244
262
|
'''
|
|
245
263
|
pid = PidFile.read()
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
assert pid is not None
|
|
264
|
+
_start_server(server=server, force=False)
|
|
265
|
+
pid = PidFile.read()
|
|
266
|
+
assert pid is not None
|
|
250
267
|
#
|
|
251
268
|
subprocess.run(args, env=dict(
|
|
252
269
|
os.environ,
|
|
@@ -260,11 +277,7 @@ def openssl(*, server, verify):
|
|
|
260
277
|
_openssl(server=server, verify=verify)
|
|
261
278
|
|
|
262
279
|
def _openssl(*, server, verify):
|
|
263
|
-
|
|
264
|
-
socat = shutil.which('socat')
|
|
265
|
-
if socat:
|
|
266
|
-
subprocess.run([socat, '-', f"openssl:{server}:443,verify={verify}"])
|
|
267
|
-
elif sys.platform == 'win32':
|
|
280
|
+
if sys.platform == 'win32':
|
|
268
281
|
import winloop
|
|
269
282
|
winloop.run(_async_openssl(server=server, verify=verify))
|
|
270
283
|
else:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|