# Copyright ClusterHQ Inc. See LICENSE file for details.

from twisted.web import server, resource
from twisted.application import service, internet

from flockerdockerplugin.adapter import (HandshakeResource, CreateResource,
    RemoveResource, PathResource, MountResource, UnmountResource)

def getAdapter():
    root = resource.Resource()
    root.putChild("Plugin.Activate", HandshakeResource())
    root.putChild("VolumeDriver.Create", CreateResource())
    root.putChild("VolumeDriver.Remove", RemoveResource())
    root.putChild("VolumeDriver.Path", PathResource())
    root.putChild("VolumeDriver.Mount", MountResource())
    root.putChild("VolumeDriver.Unmount", UnmountResource())

    site = server.Site(root)
    return site

application = service.Application("Flocker Docker Plugin")

adapterServer = internet.UNIXServer("/usr/share/docker/plugins/flocker.sock", getAdapter())
adapterServer.setServiceParent(application)

@@ -51,16 +51,26 @@
 tails.%0A%0A
+import os%0A
 from twi
@@ -296,16 +296,342 @@
 ource)%0A%0A
+%22%22%22%0ADocker 1.8 moved the plugins folder from %0A/usr/share/docker/plugins to%0A/run/docker/plugins%0AWe will listen on both paths for backwards compat reasons%0A%22%22%22%0AsocketPaths = %5B%0A    %22/usr/share/docker/plugins/flocker.sock%22,%0A    %22/run/docker/plugins/flocker.sock%22%0A%5D%0A%0A%22%22%22%0ACreate the handler that will deal with incoming requests%0A%22%22%22%0A
 def getA
@@ -1071,191 +1071,741 @@
 te%0A%0A
-application = service.Application(%22Flocker Docker Plugin%22)%0A%0AadapterServer = internet.UNIXServer(%22/usr/share/docker/plugins/f
+%22%22%22%0ACreate a UNIX socket on the given path and mount the %0Agiven adapter on the socket%0Amake the service parent of the server the given application%0A%22%22%22%0Adef mountAdapter(path, adapter, app):%0A    adapterServer = internet.UNIXServer(path, adapter)%0A    adapterServer.setServiceParent(app)%0A%0A%22%22%22%0AThe base application and adapter that will be mounted on each SocketPath%0A%22%22%22%0Aapplication = service.Application(%22F
 locker
-.sock%22, getAdapter())%0AadapterServer.setServiceParent(
+ Docker Plugin%22)%0Aadapter = getAdapter()%0A%0A%22%22%22%0ALoop over each socketPath - create the folder and then mount the adapter%0Aon the socket for that path%0A%22%22%22%0Afor socketPath in socketPaths:%0A    dirName = os.path.dirname(socketPath)%0A    if not os.path.exists(dirName):%0A        os.makedirs(dirName)%0A    mountAdapter(socketPath, adapter, 
 appl
@@ -1812,9 +1812,8 @@
 ication)
-%0A

enable support for multiple socketpaths for docker 1.8 compatability