Skip to content

Commit 8cd5431

Browse files
committed
Add small comments to methods/class
Fix apply new ACTIVE_PROXIES when fail create new proxy (port maybe already used Fix stopping proxies when stopping application on ctrl+c
1 parent fb0e420 commit 8cd5431

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/docker_cli_wrapper.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
import subprocess, json
22

33
class DockerCLIWrapper(object):
4+
"""
5+
Docker cli wrapper for getting containers and node information
6+
Alternative implementation of docker client and general implementation for windows
7+
because default docker client when used ssh connection don't working in windows with ssh-agents
8+
"""
49
def containers(self, all=False):
10+
"""
11+
Getting containers info (id, ports, image, command and other)
12+
By default, return only running containers. For get all, set all=True when execute
13+
"""
514
ps_args = ['docker', 'ps', '--format', '"{{json .}}"', '--no-trunc']
615
if all:
716
ps_args.append('-a')
@@ -14,12 +23,18 @@ def containers(self, all=False):
1423
return containers
1524

1625
def info(self):
26+
"""
27+
Get information about docker host
28+
"""
1729
info_args = ['docker', 'info', '--format', '"{{json .}}"']
1830
info = subprocess.run(info_args, capture_output=True)
1931
return json.loads(info.stdout.decode()[1:-2])
2032

2133

2234
class DockerCliWrappedContainer(object):
35+
"""
36+
Docker container information model
37+
"""
2338
id = None
2439
image = None
2540
command = None

src/main.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@
1616
logging.getLogger("docker").setLevel(logging.INFO)
1717

1818
def getDockerHostFromContext():
19+
"""
20+
Get docker remote host uri from current docker context
21+
"""
1922
current_context_cmd = subprocess.run(["docker", "context", "inspect"], capture_output=True)
2023
current_context_json = json.loads(current_context_cmd.stdout.decode())
2124
return current_context_json[0]["Endpoints"]["docker"]["Host"]
2225

2326
def manageProxies(remote_docker_host_name, published_ports, listen_system_ports):
27+
"""
28+
Manage proxy to publisher containers.
29+
Getting new published ports, stopping proxies for stopped containers (published ports)
30+
starting new proxies to new published ports
31+
"""
2432
global ACTIVE_PROXIES
2533
# Get only tcp services because only tcp proxy implemented
2634
published_ports = list(x["published"] for x in published_ports if x["internal"].endswith("tcp"))
@@ -34,17 +42,22 @@ def manageProxies(remote_docker_host_name, published_ports, listen_system_ports)
3442
published_ports.remove(proxy.proxy_port)
3543
else:
3644
proxy.stop()
37-
# Add and start new proxies
38-
for port in published_ports:
39-
proxy = ProxyServer(remote_docker_host_name, port, ProxyType.TCP)
40-
proxy.start()
41-
new_active_proxies.append(proxy)
42-
# Update proxy list
43-
ACTIVE_PROXIES = new_active_proxies
45+
try:
46+
# Add and start new proxies
47+
for port in published_ports:
48+
proxy = ProxyServer(remote_docker_host_name, port, ProxyType.TCP)
49+
proxy.start()
50+
new_active_proxies.append(proxy)
51+
# Update proxy list
52+
finally:
53+
ACTIVE_PROXIES = new_active_proxies
4454

4555

4656

4757
def mainLoop(docker_host, listen_system_ports, use_docker_cli):
58+
"""
59+
Main loop where getting published ports and launch managing proxies with 60s intervals
60+
"""
4861
global ACTIVE_PROXIES
4962
logging.info("Initialize docker remote proxy to `%s`" %docker_host)
5063
docker_service = DockerService(docker_host, use_docker_cli)
@@ -59,10 +72,12 @@ def mainLoop(docker_host, listen_system_ports, use_docker_cli):
5972
logging.error(e)
6073
time.sleep(60)
6174
except KeyboardInterrupt:
75+
return
76+
finally:
6277
for proxy in ACTIVE_PROXIES:
6378
proxy.stop()
6479
logging.info('Docker remote proxy stopped!')
65-
return
80+
6681

6782
if __name__ == "__main__":
6883
logging.info('Starting docker remote proxy!')

src/proxy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class ThreadedTCPServer(ThreadingMixIn, TCPServer):
6969
pass
7070

7171
class ProxyServer():
72-
72+
7373
def __init__(self, proxy_host, proxy_port, type=ProxyType.TCP):
7474
if(type != ProxyType.TCP):
7575
raise NotImplementedError("Only TCP Proxy implemented")

0 commit comments

Comments
 (0)