-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
117 lines (95 loc) · 3.75 KB
/
main.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
import os
import re
import signal
import subprocess
import sys
import traceback
import docker
from nginx_proxy.WebServer import WebServer
server = None
# Handle exit signal to respond to stop command.
def receiveSignal(signalNumber, frame):
global server
if signalNumber == 15:
print("\nShutdown Requested")
if server is not None:
server.cleanup()
server = None
sys.exit(0)
signal.signal(signal.SIGTERM, receiveSignal)
# Enable pydevd for debugging locally.
debug_config = {}
if "PYTHON_DEBUG_PORT" in os.environ:
if os.environ["PYTHON_DEBUG_PORT"].strip():
debug_config["port"] = int(os.environ["PYTHON_DEBUG_PORT"].strip())
if "PYTHON_DEBUG_HOST" in os.environ:
debug_config["host"] = os.environ["PYTHON_DEBUG_HOST"]
if "PYTHON_DEBUG_ENABLE" in os.environ:
if os.environ["PYTHON_DEBUG_ENABLE"].strip() == "true":
if "host" not in debug_config:
debug_config["host"] = re.findall("([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)+",
subprocess.run(["ip", "route"],
stdout=subprocess.PIPE).stdout.decode().split(
"\n")[0])[0]
if len(debug_config):
import pydevd
print("Starting nginx-proxy in debug mode. Trying to connect to debug server ", str(debug_config))
pydevd.settrace(stdoutToServer=True, stderrToServer=True, **debug_config)
# fix for https://trello.com/c/dMG5lcTZ
try:
client = docker.from_env()
client.version()
except Exception as e:
print(
"There was error connecting with the docker server \nHave you correctly mounted /var/run/docker.sock?\n" + str(
e.args), file=sys.stderr)
sys.exit(1)
def eventLoop():
for event in client.events(decode=True):
try:
eventType = event["Type"]
if eventType == "service":
process_service_event(event["Action"], event)
elif eventType == "network":
process_network_event(event["Action"], event)
elif eventType == "container":
process_container_event(event["Action"], event)
except (KeyboardInterrupt, SystemExit) as e:
raise e
except Exception as e:
print("Unexpected error :" + e.__class__.__name__ + ' -> ' + str(e), file=sys.stderr)
traceback.print_exc(limit=10)
def process_service_event(action, event):
if action == "create":
print("service created")
def process_container_event(action, event):
if action == "start":
# print("container started", event["id"])
server.update_container(event["id"])
elif action == "die":
# print("container died", event["id"])
server.remove_container(event["id"])
def process_network_event(action, event):
if action == "create":
# print("network created")
pass
elif "container" in event["Actor"]["Attributes"]:
if action == "disconnect":
# print("network disconnect")
server.disconnect(network=event["Actor"]["ID"], container=event["Actor"]["Attributes"]["container"],
scope=event["scope"])
elif action == "connect":
# print("network connect")
server.connect(network=event["Actor"]["ID"], container=event["Actor"]["Attributes"]["container"],
scope=event["scope"])
elif action == "destroy":
# print("network destryed")
pass
try:
server = WebServer(client)
eventLoop()
except (KeyboardInterrupt, SystemExit):
print("-------------------------------\nPerforming Graceful ShutDown !!")
if server is not None:
server.cleanup()
print("---- See You ----")