Skip to content

Instantly share code, notes, and snippets.

@rlane
Last active August 29, 2015 14:25
Show Gist options
  • Save rlane/ff31b506399b915d9543 to your computer and use it in GitHub Desktop.
Save rlane/ff31b506399b915d9543 to your computer and use it in GitHub Desktop.
No-op Docker networking plugin
#!/usr/bin/env python
from flask import Flask
from flask import request
import json
import logging
import os
import errno
import argparse
import docker
from docker.utils import check_resource
FAIL = False
app = Flask(__name__)
docker_client = None
class ExtendedDockerClient(docker.Client):
@check_resource
def inspect_network(self, network):
return self._result(self._get(self._url("/networks/{0}".format(network))), True)
@check_resource
def inspect_service(self, service):
return self._result(self._get(self._url("/services/{0}".format(service))), True)
@check_resource
def inspect_service_backend(self, service):
return self._result(self._get(self._url("/services/{0}/backend".format(service))), True)
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
@app.route('/status')
def handle_status():
return "Status: %r" % request.get_data()
@app.route('/Plugin.Activate', methods=['POST'])
def handle_activate():
logging.debug("%s: %r", request.full_path, request.get_data())
return json.dumps({ 'Implements': ['NetworkDriver'] })
@app.route('/NetworkDriver.CreateNetwork', methods=['POST'])
def handle_create_network():
logging.debug("%s: %r", request.full_path, request.get_data())
data = json.loads(request.get_data())
if FAIL:
network_info = docker_client.inspect_network(data['NetworkID'])
logging.debug("Network: %r", network_info)
return "{}"
@app.route('/NetworkDriver.DeleteNetwork', methods=['POST'])
def handle_delete_network():
logging.debug("%s: %r", request.full_path, request.get_data())
data = json.loads(request.get_data())
if FAIL:
network_info = docker_client.inspect_network(data['NetworkID'])
logging.debug("Network: %r", network_info)
return "{}"
@app.route('/NetworkDriver.CreateEndpoint', methods=['POST'])
def handle_create_endpoint():
logging.debug("%s: %r", request.full_path, request.get_data())
data = json.loads(request.get_data())
network_info = docker_client.inspect_network(data['NetworkID'])
logging.debug("Network: %r", network_info)
service_info = docker_client.inspect_service(data['EndpointID'])
logging.debug("Service: %r", service_info)
return json.dumps({
"Interfaces": [
]
})
@app.route('/NetworkDriver.DeleteEndpoint', methods=['POST'])
def handle_delete_endpoint():
logging.debug("%s: %r", request.full_path, request.get_data())
data = json.loads(request.get_data())
network_info = docker_client.inspect_network(data['NetworkID'])
logging.debug("Network: %r", network_info)
if FAIL:
service_info = docker_client.inspect_service(data['EndpointID'])
logging.debug("Service: %r", service_info)
return "{}"
@app.route('/NetworkDriver.EndpointOperInfo', methods=['POST'])
def handle_endpoint_oper_info():
logging.debug("%s: %r", request.full_path, request.get_data())
data = json.loads(request.get_data())
network_info = docker_client.inspect_network(data['NetworkID'])
logging.debug("Network: %r", network_info)
service_info = docker_client.inspect_service(data['EndpointID'])
logging.debug("Service: %r", service_info)
return "{}"
@app.route('/NetworkDriver.Join', methods=['POST'])
def handle_join():
logging.debug("%s: %r", request.full_path, request.get_data())
data = json.loads(request.get_data())
network_info = docker_client.inspect_network(data['NetworkID'])
logging.debug("Network: %r", network_info)
service_info = docker_client.inspect_service(data['EndpointID'])
logging.debug("Service: %r", service_info)
backend_info = docker_client.inspect_service_backend(data['EndpointID'])
logging.debug("Backend: %r", backend_info)
if FAIL:
for backend in backend_info:
container_info = docker_client.inspect_container(backend['id'])
logging.debug("container: %r", container_info)
return json.dumps({
})
@app.route('/NetworkDriver.Leave', methods=['POST'])
def handle_leave():
logging.debug("%s: %r", request.full_path, request.get_data())
data = json.loads(request.get_data())
network_info = docker_client.inspect_network(data['NetworkID'])
logging.debug("Network: %r", network_info)
service_info = docker_client.inspect_service(data['EndpointID'])
logging.debug("Service: %r", service_info)
backend_info = docker_client.inspect_service_backend(data['EndpointID'])
logging.debug("Backend: %r", backend_info)
for backend in backend_info:
container_info = docker_client.inspect_container(backend['id'])
logging.debug("container: %r", container_info)
return "{}"
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='No-op Docker networking plugin')
parser.add_argument('--log-level', "-l", type=str, choices=['debug', 'info', 'warning', 'error' 'critical'], default="info", help="Log level")
args = parser.parse_args()
logging.basicConfig(level=getattr(logging, args.log_level.upper()),
format="%(asctime)s.%(msecs)03d %(name)-10s: %(levelname)-8s: %(message)s")
docker_client = ExtendedDockerClient(base_url='unix://var/run/docker.sock')
from waitress import serve
mkdir_p("/run/docker/plugins")
serve(app, unix_socket="/run/docker/plugins/nop.sock")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment