mirror of
https://github.com/devplayer0/docker-net-dhcp
synced 2024-10-30 09:20:28 +00:00
Use single-threaded WSGI server and fix mqissues
This commit is contained in:
parent
1c3c2ec5d5
commit
717d856e50
@ -7,8 +7,6 @@ RUN apk --no-cache add gcc musl-dev && \
|
||||
|
||||
RUN mkdir -p /opt/plugin /run/docker/plugins /var/run/docker/netns
|
||||
COPY net-dhcp/ /opt/plugin/net_dhcp
|
||||
COPY plugin.sh /opt/plugin/launch.sh
|
||||
|
||||
WORKDIR /opt/plugin
|
||||
ENV GUNICORN_OPTS="--log-level=DEBUG"
|
||||
ENTRYPOINT [ "/opt/plugin/launch.sh" ]
|
||||
ENTRYPOINT ["python", "-m", "net_dhcp"]
|
||||
|
16
config.json
16
config.json
@ -6,7 +6,7 @@
|
||||
"docker.networkdriver/1.0"
|
||||
]
|
||||
},
|
||||
"entrypoint": [ "/opt/plugin/launch.sh" ],
|
||||
"entrypoint": [ "python", "-m", "net_dhcp" ],
|
||||
"workdir": "/opt/plugin",
|
||||
"network": {
|
||||
"type": "host"
|
||||
@ -25,16 +25,8 @@
|
||||
"linux": {
|
||||
"capabilities": [
|
||||
"CAP_NET_ADMIN",
|
||||
"CAP_SYS_ADMIN"
|
||||
"CAP_SYS_ADMIN",
|
||||
"CAP_SYS_PTRACE"
|
||||
]
|
||||
},
|
||||
"env": [
|
||||
{
|
||||
"name": "GUNICORN_OPTS",
|
||||
"settable": [
|
||||
"value"
|
||||
],
|
||||
"value": "--log-level DEBUG --log-file /var/log/net-dhcp.log"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
14
net-dhcp/__main__.py
Normal file
14
net-dhcp/__main__.py
Normal file
@ -0,0 +1,14 @@
|
||||
import logging
|
||||
import socketserver
|
||||
from werkzeug.serving import run_simple
|
||||
from . import app
|
||||
|
||||
fh = logging.FileHandler('/var/log/net-dhcp.log')
|
||||
fh.setFormatter(logging.Formatter('%(asctime)s [%(levelname)s] %(message)s'))
|
||||
|
||||
logger = logging.getLogger('net-dhcp')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.addHandler(fh)
|
||||
|
||||
socketserver.TCPServer.allow_reuse_address = True
|
||||
run_simple('unix:///run/docker/plugins/net-dhcp.sock', 0, app)
|
@ -18,7 +18,7 @@ OPTS_KEY = 'com.docker.network.generic'
|
||||
OPT_BRIDGE = 'bridge'
|
||||
OPT_IPV6 = 'ipv6'
|
||||
|
||||
logger = logging.getLogger('gunicorn.error')
|
||||
logger = logging.getLogger('net-dhcp')
|
||||
|
||||
ndb = pyroute2.NDB()
|
||||
@atexit.register
|
||||
@ -147,6 +147,7 @@ def create_endpoint():
|
||||
.set('state', 'up')
|
||||
.commit())
|
||||
|
||||
time.sleep(0.5)
|
||||
if_container = (ndb.interfaces[if_container]
|
||||
.set('state', 'up')
|
||||
.commit())
|
||||
@ -372,14 +373,16 @@ class ContainerDHCPManager:
|
||||
if not self.dhcp:
|
||||
return
|
||||
|
||||
logger.info('Shutting down DHCPv4 client on %s in container namespace %s', \
|
||||
self.dhcp.iface['ifname'], self.dhcp.netns)
|
||||
self.dhcp.finish(timeout=1)
|
||||
|
||||
if self.ipv6:
|
||||
logger.info('Shutting down DHCPv6 client on %s in container namespace %s', \
|
||||
self.dhcp6.iface['ifname'], self.dhcp.netns)
|
||||
self.dhcp6.finish(timeout=1)
|
||||
|
||||
ndb.sources.remove(self.dhcp.netns)
|
||||
self._thread.join()
|
||||
try:
|
||||
logger.info('Shutting down DHCPv4 client on %s in container namespace %s', \
|
||||
self.dhcp.iface['ifname'], self.dhcp.netns)
|
||||
self.dhcp.finish(timeout=1)
|
||||
finally:
|
||||
try:
|
||||
if self.ipv6:
|
||||
logger.info('Shutting down DHCPv6 client on %s in container namespace %s', \
|
||||
self.dhcp6.iface['ifname'], self.dhcp.netns)
|
||||
self.dhcp6.finish(timeout=1)
|
||||
finally:
|
||||
ndb.sources.remove(self.dhcp.netns)
|
||||
self._thread.join()
|
||||
|
@ -24,7 +24,7 @@ class EventType(Enum):
|
||||
DECONFIG = 'deconfig'
|
||||
LEASEFAIL = 'leasefail'
|
||||
|
||||
logger = logging.getLogger('gunicorn.error')
|
||||
logger = logging.getLogger('net-dhcp')
|
||||
|
||||
class DHCPClientError(Exception):
|
||||
pass
|
||||
@ -66,7 +66,7 @@ class DHCPClient:
|
||||
|
||||
self._suffix = '6' if v6 else ''
|
||||
self._event_queue = posix_ipc.MessageQueue(f'/udhcpc{self._suffix}_{iface["address"].replace(":", "_")}', \
|
||||
flags=os.O_CREAT | os.O_EXCL)
|
||||
flags=os.O_CREAT | os.O_EXCL, max_messages=2, max_message_size=1024)
|
||||
self.proc = Popen(cmdline, env={'EVENT_QUEUE': self._event_queue.name})
|
||||
if hostname:
|
||||
logger.debug('[udhcpc%s#%d] using hostname "%s"', self._suffix, self.proc.pid, hostname)
|
||||
@ -77,6 +77,7 @@ class DHCPClient:
|
||||
self.domain = None
|
||||
|
||||
self._shutdown_event = EventFD()
|
||||
self.shutdown = False
|
||||
self._event_thread = threading.Thread(target=self._read_events)
|
||||
self._event_thread.start()
|
||||
|
||||
@ -115,6 +116,8 @@ class DHCPClient:
|
||||
listener(self, event['type'], event)
|
||||
except Exception as ex:
|
||||
logger.exception(ex)
|
||||
self.shutdown = True
|
||||
del self._shutdown_event
|
||||
|
||||
def await_ip(self, timeout=10):
|
||||
if not self._has_lease.wait(timeout=timeout):
|
||||
@ -123,7 +126,7 @@ class DHCPClient:
|
||||
return self.ip
|
||||
|
||||
def finish(self, timeout=5):
|
||||
if self._shutdown_event.is_set():
|
||||
if self.shutdown or self._shutdown_event.is_set():
|
||||
return
|
||||
|
||||
try:
|
||||
|
@ -1,2 +0,0 @@
|
||||
#!/bin/sh
|
||||
exec gunicorn $GUNICORN_OPTS --workers 1 --bind unix:/run/docker/plugins/net-dhcp.sock net_dhcp:app
|
@ -1,5 +1,4 @@
|
||||
flask==1.1.1
|
||||
gunicorn==19.9.0
|
||||
pyroute2==0.5.6
|
||||
docker==4.0.2
|
||||
eventfd==0.2
|
||||
|
Loading…
Reference in New Issue
Block a user