Skip to content

Commit 180414d

Browse files
author
aiordache
committed
Shell out to SSH client for an ssh connection
Signed-off-by: aiordache <[email protected]>
1 parent 9d8cd02 commit 180414d

File tree

16 files changed

+1006
-61
lines changed

16 files changed

+1006
-61
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ ARG PYTHON_VERSION=2.7
22

33
FROM python:${PYTHON_VERSION}
44

5+
# Add SSH keys and set permissions
6+
COPY tests/ssh-keys /root/.ssh
7+
RUN chmod -R 600 /root/.ssh
8+
59
RUN mkdir /src
610
WORKDIR /src
711

Jenkinsfile

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
def imageNameBase = "dockerbuildbot/docker-py"
44
def imageNamePy2
55
def imageNamePy3
6+
def imageDindSSH
67
def images = [:]
78

89
def buildImage = { name, buildargs, pyTag ->
@@ -13,7 +14,7 @@ def buildImage = { name, buildargs, pyTag ->
1314
img = docker.build(name, buildargs)
1415
img.push()
1516
}
16-
images[pyTag] = img.id
17+
if (pyTag?.trim()) images[pyTag] = img.id
1718
}
1819

1920
def buildImages = { ->
@@ -23,7 +24,9 @@ def buildImages = { ->
2324

2425
imageNamePy2 = "${imageNameBase}:py2-${gitCommit()}"
2526
imageNamePy3 = "${imageNameBase}:py3-${gitCommit()}"
27+
imageDindSSH = "${imageNameBase}:sshdind-${gitCommit()}"
2628

29+
buildImage(imageDindSSH, "-f tests/Dockerfile-ssh-dind .", "")
2730
buildImage(imageNamePy2, "-f tests/Dockerfile --build-arg PYTHON_VERSION=2.7 .", "py2.7")
2831
buildImage(imageNamePy3, "-f tests/Dockerfile --build-arg PYTHON_VERSION=3.7 .", "py3.7")
2932
}
@@ -81,22 +84,37 @@ def runTests = { Map settings ->
8184
def testNetwork = "dpy-testnet-\$BUILD_NUMBER-\$EXECUTOR_NUMBER-${pythonVersion}-${dockerVersion}"
8285
try {
8386
sh """docker network create ${testNetwork}"""
84-
sh """docker run -d --name ${dindContainerName} -v /tmp --privileged --network ${testNetwork} \\
85-
docker:${dockerVersion}-dind dockerd -H tcp://0.0.0.0:2375
87+
sh """docker run --rm -d --name ${dindContainerName} -v /tmp --privileged --network ${testNetwork} \\
88+
${imageDindSSH} dockerd -H tcp://0.0.0.0:2375
8689
"""
87-
sh """docker run \\
90+
sh """docker run --rm \\
8891
--name ${testContainerName} \\
8992
-e "DOCKER_HOST=tcp://${dindContainerName}:2375" \\
9093
-e 'DOCKER_TEST_API_VERSION=${apiVersion}' \\
9194
--network ${testNetwork} \\
9295
--volumes-from ${dindContainerName} \\
9396
${testImage} \\
94-
py.test -v -rxs --cov=docker tests/
97+
py.test -v -rxs --cov=docker --ignore=tests/ssh tests/
98+
"""
99+
sh """docker stop ${dindContainerName}"""
100+
101+
// start DIND container with SSH
102+
sh """docker run --rm -d --name ${dindContainerName} -v /tmp --privileged --network ${testNetwork} \\
103+
${imageDindSSH} dockerd --experimental"""
104+
sh """docker exec ${dindContainerName} sh -c /usr/sbin/sshd """
105+
// run SSH tests only
106+
sh """docker run --rm \\
107+
--name ${testContainerName} \\
108+
-e "DOCKER_HOST=ssh://${dindContainerName}:22" \\
109+
-e 'DOCKER_TEST_API_VERSION=${apiVersion}' \\
110+
--network ${testNetwork} \\
111+
--volumes-from ${dindContainerName} \\
112+
${testImage} \\
113+
py.test -v -rxs --cov=docker tests/ssh
95114
"""
96115
} finally {
97116
sh """
98-
docker stop ${dindContainerName} ${testContainerName}
99-
docker rm -vf ${dindContainerName} ${testContainerName}
117+
docker stop ${dindContainerName}
100118
docker network rm ${testNetwork}
101119
"""
102120
}

Makefile

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
TEST_API_VERSION ?= 1.39
2+
TEST_ENGINE_VERSION ?= 19.03.13
3+
14
.PHONY: all
25
all: test
36

@@ -10,6 +13,10 @@ clean:
1013
build:
1114
docker build -t docker-sdk-python -f tests/Dockerfile --build-arg PYTHON_VERSION=2.7 --build-arg APT_MIRROR .
1215

16+
.PHONY: build-dind-ssh
17+
build-dind-ssh:
18+
docker build -t docker-dind-ssh -f tests/Dockerfile-ssh-dind --build-arg ENGINE_VERSION=${TEST_ENGINE_VERSION} --build-arg API_VERSION=${TEST_API_VERSION} --build-arg APT_MIRROR .
19+
1320
.PHONY: build-py3
1421
build-py3:
1522
docker build -t docker-sdk-python3 -f tests/Dockerfile --build-arg APT_MIRROR .
@@ -41,9 +48,6 @@ integration-test: build
4148
integration-test-py3: build-py3
4249
docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock docker-sdk-python3 py.test -v tests/integration/${file}
4350

44-
TEST_API_VERSION ?= 1.39
45-
TEST_ENGINE_VERSION ?= 19.03.12
46-
4751
.PHONY: setup-network
4852
setup-network:
4953
docker network inspect dpy-tests || docker network create dpy-tests
@@ -69,6 +73,29 @@ integration-dind-py3: build-py3 setup-network
6973
--network dpy-tests docker-sdk-python3 py.test tests/integration/${file}
7074
docker rm -vf dpy-dind-py3
7175

76+
.PHONY: integration-ssh-py2
77+
integration-ssh-py2: build-dind-ssh build setup-network
78+
docker rm -vf dpy-dind-py2 || :
79+
docker run -d --network dpy-tests --name dpy-dind-py2 --privileged\
80+
docker-dind-ssh dockerd --experimental
81+
# start SSH daemon
82+
docker exec dpy-dind-py2 sh -c "/usr/sbin/sshd"
83+
docker run -t --rm --env="DOCKER_HOST=ssh://dpy-dind-py2" --env="DOCKER_TEST_API_VERSION=${TEST_API_VERSION}"\
84+
--network dpy-tests docker-sdk-python py.test tests/ssh/${file}
85+
docker rm -vf dpy-dind-py2
86+
87+
.PHONY: integration-ssh-py3
88+
integration-ssh-py3: build-dind-ssh build-py3 setup-network
89+
docker rm -vf dpy-dind-py3 || :
90+
docker run -d --network dpy-tests --name dpy-dind-py3 --privileged\
91+
docker-dind-ssh dockerd --experimental
92+
# start SSH daemon
93+
docker exec dpy-dind-py3 sh -c "/usr/sbin/sshd"
94+
docker run -t --rm --env="DOCKER_HOST=ssh://dpy-dind-py3" --env="DOCKER_TEST_API_VERSION=${TEST_API_VERSION}"\
95+
--network dpy-tests docker-sdk-python3 py.test tests/ssh/${file}
96+
docker rm -vf dpy-dind-py3
97+
98+
7299
.PHONY: integration-dind-ssl
73100
integration-dind-ssl: build-dind-certs build build-py3
74101
docker rm -vf dpy-dind-certs dpy-dind-ssl || :

docker/api/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class APIClient(
8989
user_agent (str): Set a custom user agent for requests to the server.
9090
credstore_env (dict): Override environment variables when calling the
9191
credential store process.
92+
use_ssh_client (bool): If set to `True`, an ssh connection is made
93+
via shelling out to the ssh client. Ensure the ssh client is
94+
installed and configured on the host.
9295
"""
9396

9497
__attrs__ = requests.Session.__attrs__ + ['_auth_configs',
@@ -100,7 +103,7 @@ class APIClient(
100103
def __init__(self, base_url=None, version=None,
101104
timeout=DEFAULT_TIMEOUT_SECONDS, tls=False,
102105
user_agent=DEFAULT_USER_AGENT, num_pools=None,
103-
credstore_env=None):
106+
credstore_env=None, use_ssh_client=False):
104107
super(APIClient, self).__init__()
105108

106109
if tls and not base_url:
@@ -161,7 +164,8 @@ def __init__(self, base_url=None, version=None,
161164
elif base_url.startswith('ssh://'):
162165
try:
163166
self._custom_adapter = SSHHTTPAdapter(
164-
base_url, timeout, pool_connections=num_pools
167+
base_url, timeout, pool_connections=num_pools,
168+
shell_out=use_ssh_client
165169
)
166170
except NameError:
167171
raise DockerException(

docker/client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class DockerClient(object):
3535
user_agent (str): Set a custom user agent for requests to the server.
3636
credstore_env (dict): Override environment variables when calling the
3737
credential store process.
38+
use_ssh_client (bool): If set to `True`, an ssh connection is made
39+
via shelling out to the ssh client. Ensure the ssh client is
40+
installed and configured on the host.
3841
"""
3942
def __init__(self, *args, **kwargs):
4043
self.api = APIClient(*args, **kwargs)
@@ -70,6 +73,9 @@ def from_env(cls, **kwargs):
7073
from. Default: the value of ``os.environ``
7174
credstore_env (dict): Override environment variables when calling
7275
the credential store process.
76+
use_ssh_client (bool): If set to `True`, an ssh connection is
77+
made via shelling out to the ssh client. Ensure the ssh
78+
client is installed and configured on the host.
7379
7480
Example:
7581
@@ -81,8 +87,12 @@ def from_env(cls, **kwargs):
8187
"""
8288
timeout = kwargs.pop('timeout', DEFAULT_TIMEOUT_SECONDS)
8389
version = kwargs.pop('version', None)
90+
use_ssh_client = kwargs.pop('use_ssh_client', False)
8491
return cls(
85-
timeout=timeout, version=version, **kwargs_from_env(**kwargs)
92+
timeout=timeout,
93+
version=version,
94+
use_ssh_client=use_ssh_client,
95+
**kwargs_from_env(**kwargs)
8696
)
8797

8898
# Resources

0 commit comments

Comments
 (0)