-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unix socket #62
Unix socket #62
Changes from all commits
93f2981
a6afe2b
1c75389
06a5e13
419be89
ce0d989
fceba23
ef464bf
eb4c79c
2f0359b
117dbf3
daba983
68388e8
2b5ee58
30b7267
bdb8ecb
dfb2ba7
479a406
a77f766
856bcd2
48e601a
a160943
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,3 +52,5 @@ docs/_build/ | |
|
||
# PyBuilder | ||
target/ | ||
|
||
_trial_temp/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
import os | ||
import sys | ||
from twisted.application import service, internet | ||
#from twisted.protocols.policies import TrafficLoggingFactory | ||
from urlparse import urlparse | ||
|
||
from powerstrip.powerstrip import ServerProtocolFactory | ||
from powerstrip.resources import GetDockerHost,GetDockerAPICredentials | ||
|
||
TARGET_DOCKER_SOCKET = "/host-var-run/docker.sock" | ||
|
||
application = service.Application("Powerstrip") | ||
|
||
DOCKER_HOST = os.environ.get('DOCKER_HOST') | ||
if DOCKER_HOST is None: | ||
# Default to assuming we've got a Docker socket bind-mounted into a | ||
# container we're running in. | ||
DOCKER_HOST = "unix:///var/run/docker.sock" | ||
if "://" not in DOCKER_HOST: | ||
DOCKER_HOST = "tcp://" + DOCKER_HOST | ||
if DOCKER_HOST.startswith("tcp://"): | ||
parsed = urlparse(DOCKER_HOST) | ||
dockerAPI = ServerProtocolFactory(dockerAddr=parsed.hostname, | ||
dockerPort=parsed.port) | ||
elif DOCKER_HOST.startswith("unix://"): | ||
socketPath = DOCKER_HOST[len("unix://"):] | ||
dockerAPI = ServerProtocolFactory(dockerSocket=socketPath) | ||
#logged = TrafficLoggingFactory(dockerAPI, "api-") | ||
dockerServer = internet.TCPServer(2375, dockerAPI, interface='0.0.0.0') | ||
dockerServer.setServiceParent(application) | ||
# we create a connection to the Docker server based on DOCKER_HOST (can be tcp or unix socket) | ||
DOCKER_HOST = GetDockerHost(os.environ.get('DOCKER_HOST')) | ||
dockerAPICredentials = GetDockerAPICredentials(DOCKER_HOST) | ||
|
||
# check that /var/run is mounted from the host (so we can write docker.sock to it) | ||
if not os.path.isdir("/host-var-run"): | ||
sys.stderr.write("/var/run must be mounted as /host-var-run in the powerstrip container\n") | ||
sys.exit(1) | ||
|
||
print r'export DOCKER_HOST=tcp://localhost:2375' | ||
# check that the docker unix socket that we are trying to connect to actually exists | ||
if dockerAPICredentials['dockerSocket'] and not os.path.exists(dockerAPICredentials["dockerSocket"]): | ||
sys.stderr.write(dockerAPICredentials["dockerSocket"] + " does not exist as a docker unix socket to connect to\n") | ||
sys.exit(1) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super happy about removing this functionality - some people might be depending on it (for whatever "depending on" means for a hack project). I feel like we should make the effort to support it still, probably via an environment variable or by implying the user's intention from whether |
||
# check that the unix socket we want to listen on does not already exist | ||
if os.path.exists(TARGET_DOCKER_SOCKET): | ||
sys.stderr.write(TARGET_DOCKER_SOCKET + " already exists - we want to listen on this path\n") | ||
sys.exit(1) | ||
|
||
dockerAPI = ServerProtocolFactory(**dockerAPICredentials) | ||
dockerServer = internet.UNIXServer(TARGET_DOCKER_SOCKET, dockerAPI, mode=0660) | ||
dockerServer.setServiceParent(application) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright ClusterHQ Limited. See LICENSE file for details. | ||
# -*- test-case-name: powerstrip.test.test_utils -*- | ||
|
||
from twisted.trial.unittest import TestCase | ||
from ..resources import GetDockerHost,GetDockerAPICredentials | ||
|
||
""" | ||
Tests for the utils. | ||
""" | ||
|
||
class TestDockerHost(TestCase): | ||
|
||
def test_get_default_docker_host(self): | ||
""" | ||
Test that if nothing is supplied we get the default UNIX socket | ||
""" | ||
dockerHost = GetDockerHost() | ||
self.assertEqual(dockerHost, "unix:///host-var-run/docker.real.sock") | ||
|
||
def test_get_path_based_docker_host(self): | ||
""" | ||
Check that if a path with no scheme is supplied then we get a unix socket | ||
""" | ||
dockerHost = GetDockerHost('/var/run/my.sock') | ||
self.assertEqual(dockerHost, "unix:///var/run/my.sock") | ||
|
||
def test_get_tcp_based_docker_host(self): | ||
""" | ||
Check that if a path with no scheme is supplied then we get a unix socket | ||
""" | ||
dockerHost = GetDockerHost('localhost:2375') | ||
self.assertEqual(dockerHost, "tcp://localhost:2375") | ||
|
||
def test_get_path_based_docker_host_unchanged(self): | ||
""" | ||
Check that if a path with no scheme is supplied then we get a unix socket | ||
""" | ||
dockerHost = GetDockerHost('unix:///var/run/yobedisfileyo') | ||
self.assertEqual(dockerHost, "unix:///var/run/yobedisfileyo") | ||
|
||
def test_get_tcp_based_docker_host_unchanged(self): | ||
""" | ||
Check that if a path with no scheme is supplied then we get a unix socket | ||
""" | ||
dockerHost = GetDockerHost('tcp://127.0.0.1:2375') | ||
self.assertEqual(dockerHost, "tcp://127.0.0.1:2375") | ||
|
||
|
||
class TestDockerAPICredentials(TestCase): | ||
|
||
def test_get_default_dockerapi_credentials(self): | ||
""" | ||
Test that if nothing is supplied we get the default UNIX socket | ||
""" | ||
dockerAPICredentials = GetDockerAPICredentials() | ||
|
||
self.assertEqual(dockerAPICredentials['dockerSocket'], "/host-var-run/docker.real.sock") | ||
|
||
self.assertNotIn("dockerAddr", dockerAPICredentials) | ||
self.assertNotIn("dockerPort", dockerAPICredentials) | ||
|
||
def test_get_tcp_dockerapi_credentials(self): | ||
""" | ||
Test that if TCP is supplied we get the IP / port returned | ||
""" | ||
dockerAPICredentials = GetDockerAPICredentials('tcp://127.0.0.1:2375') | ||
|
||
self.assertEqual(dockerAPICredentials['dockerAddr'], "127.0.0.1") | ||
self.assertEqual(dockerAPICredentials['dockerPort'], 2375) | ||
self.assertNotIn("dockerSocket", dockerAPICredentials) | ||
|
||
def test_get_unixsocket_dockerapi_credentials(self): | ||
""" | ||
Test that if UNIX is supplied | ||
""" | ||
dockerAPICredentials = GetDockerAPICredentials('unix:///var/run/yobedisfileyo') | ||
|
||
self.assertEqual(dockerAPICredentials['dockerSocket'], "/var/run/yobedisfileyo") | ||
self.assertNotIn("dockerAddr", dockerAPICredentials) | ||
self.assertNotIn("dockerPort", dockerAPICredentials) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a major change in operation with this release. There should be big red text on here or something.