Drop docker_utils
This was last used by fedora swarm driver, which was dropped in Change I81095221fa45f7070bde22e2a4415345080ed1f1 Change-Id: Id5e61029d54424e8f3462e5063b1beb2495ec305
This commit is contained in:
parent
76b056d771
commit
91ac8f411a
@ -1,114 +0,0 @@
|
||||
# Copyright 2015 Rackspace All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import contextlib
|
||||
|
||||
import docker
|
||||
from docker.utils import utils
|
||||
|
||||
from magnum.conductor.handlers.common import cert_manager
|
||||
from magnum.conductor import utils as conductor_utils
|
||||
import magnum.conf
|
||||
|
||||
|
||||
CONF = magnum.conf.CONF
|
||||
|
||||
|
||||
def parse_docker_image(image):
|
||||
image_parts = image.split(':', 1)
|
||||
|
||||
image_repo = image_parts[0]
|
||||
image_tag = None
|
||||
|
||||
if len(image_parts) > 1:
|
||||
image_tag = image_parts[1]
|
||||
|
||||
return image_repo, image_tag
|
||||
|
||||
|
||||
def is_docker_library_version_atleast(version):
|
||||
if utils.compare_version(docker.version, version) <= 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def is_docker_api_version_atleast(docker, version):
|
||||
if utils.compare_version(docker.version()['ApiVersion'], version) <= 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def docker_for_cluster(context, cluster):
|
||||
cluster_template = conductor_utils.retrieve_cluster_template(
|
||||
context, cluster)
|
||||
|
||||
ca_cert, magnum_key, magnum_cert = None, None, None
|
||||
client_kwargs = dict()
|
||||
if not cluster_template.tls_disabled:
|
||||
(ca_cert, magnum_key,
|
||||
magnum_cert) = cert_manager.create_client_files(cluster, context)
|
||||
client_kwargs['ca_cert'] = ca_cert.name
|
||||
client_kwargs['client_key'] = magnum_key.name
|
||||
client_kwargs['client_cert'] = magnum_cert.name
|
||||
|
||||
yield DockerHTTPClient(
|
||||
cluster.api_address,
|
||||
CONF.docker.docker_remote_api_version,
|
||||
CONF.docker.default_timeout,
|
||||
**client_kwargs
|
||||
)
|
||||
|
||||
if ca_cert:
|
||||
ca_cert.close()
|
||||
if magnum_key:
|
||||
magnum_key.close()
|
||||
if magnum_cert:
|
||||
magnum_cert.close()
|
||||
|
||||
|
||||
class DockerHTTPClient(docker.APIClient):
|
||||
def __init__(self, url='unix://var/run/docker.sock',
|
||||
ver=CONF.docker.docker_remote_api_version,
|
||||
timeout=CONF.docker.default_timeout,
|
||||
ca_cert=None,
|
||||
client_key=None,
|
||||
client_cert=None):
|
||||
|
||||
if ca_cert and client_key and client_cert:
|
||||
ssl_config = docker.tls.TLSConfig(
|
||||
client_cert=(client_cert, client_key),
|
||||
verify=ca_cert,
|
||||
assert_hostname=False,
|
||||
)
|
||||
else:
|
||||
ssl_config = False
|
||||
|
||||
super(DockerHTTPClient, self).__init__(
|
||||
base_url=url,
|
||||
version=ver,
|
||||
timeout=timeout,
|
||||
tls=ssl_config
|
||||
)
|
||||
|
||||
def list_instances(self, inspect=False):
|
||||
res = []
|
||||
for container in self.containers(all=True):
|
||||
info = self.inspect_container(container['Id'])
|
||||
if not info:
|
||||
continue
|
||||
if inspect:
|
||||
res.append(info)
|
||||
else:
|
||||
res.append(info['Config'].get('Hostname'))
|
||||
return res
|
@ -1,108 +0,0 @@
|
||||
# Copyright 2015 Huawei Technologies Co.,LTD.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import docker
|
||||
from unittest import mock
|
||||
|
||||
from magnum.common import docker_utils
|
||||
import magnum.conf
|
||||
from magnum.tests import base
|
||||
|
||||
|
||||
CONF = magnum.conf.CONF
|
||||
|
||||
|
||||
class TestDockerUtils(base.BaseTestCase):
|
||||
|
||||
def test_is_docker_api_version_atleast(self):
|
||||
|
||||
def fake_version():
|
||||
return {'ApiVersion': '1.18'}
|
||||
|
||||
docker_client = mock.MagicMock()
|
||||
docker_client.version.side_effect = fake_version
|
||||
res = docker_utils.is_docker_api_version_atleast(docker_client, '1.21')
|
||||
self.assertFalse(res)
|
||||
|
||||
|
||||
class DockerClientTestCase(base.BaseTestCase):
|
||||
def test_docker_client_init(self):
|
||||
client = docker_utils.DockerHTTPClient()
|
||||
|
||||
self.assertEqual(CONF.docker.docker_remote_api_version,
|
||||
client.api_version)
|
||||
self.assertEqual(CONF.docker.default_timeout,
|
||||
client.timeout)
|
||||
|
||||
def test_docker_client_init_timeout(self):
|
||||
expected_timeout = 300
|
||||
client = docker_utils.DockerHTTPClient(timeout=expected_timeout)
|
||||
|
||||
self.assertEqual(CONF.docker.docker_remote_api_version,
|
||||
client.api_version)
|
||||
self.assertEqual(expected_timeout, client.timeout)
|
||||
|
||||
def test_docker_client_init_url(self):
|
||||
expected_url = 'http://127.0.0.1:2375'
|
||||
client = docker_utils.DockerHTTPClient(url=expected_url)
|
||||
|
||||
self.assertEqual(expected_url,
|
||||
client.base_url)
|
||||
self.assertEqual(CONF.docker.docker_remote_api_version,
|
||||
client.api_version)
|
||||
self.assertEqual(CONF.docker.default_timeout,
|
||||
client.timeout)
|
||||
|
||||
def test_docker_client_init_version(self):
|
||||
expected_version = '1.21'
|
||||
client = docker_utils.DockerHTTPClient(ver=expected_version)
|
||||
|
||||
self.assertEqual(expected_version,
|
||||
client.api_version)
|
||||
self.assertEqual(CONF.docker.default_timeout,
|
||||
client.timeout)
|
||||
|
||||
@mock.patch.object(docker.APIClient, 'inspect_container')
|
||||
@mock.patch.object(docker.APIClient, 'containers')
|
||||
def test_list_instances(self, mock_containers, mock_inspect):
|
||||
client = docker_utils.DockerHTTPClient()
|
||||
|
||||
containers = [dict(Id=x) for x in range(0, 3)]
|
||||
inspect_results = [dict(Config=dict(Hostname=x)) for x in range(0, 3)]
|
||||
|
||||
mock_containers.return_value = containers
|
||||
mock_inspect.side_effect = inspect_results
|
||||
|
||||
instances = client.list_instances()
|
||||
|
||||
self.assertEqual([0, 1, 2], instances)
|
||||
mock_containers.assert_called_once_with(all=True)
|
||||
mock_inspect.assert_has_calls([mock.call(x) for x in range(0, 3)])
|
||||
|
||||
@mock.patch.object(docker.APIClient, 'inspect_container')
|
||||
@mock.patch.object(docker.APIClient, 'containers')
|
||||
def test_list_instances_inspect(self, mock_containers, mock_inspect):
|
||||
client = docker_utils.DockerHTTPClient()
|
||||
|
||||
containers = [dict(Id=x) for x in range(0, 3)]
|
||||
inspect_results = [dict(Config=dict(Hostname=x)) for x in range(0, 3)]
|
||||
|
||||
mock_containers.return_value = containers
|
||||
mock_inspect.side_effect = inspect_results
|
||||
|
||||
instances = client.list_instances(inspect=True)
|
||||
|
||||
self.assertEqual(inspect_results, instances)
|
||||
mock_containers.assert_called_once_with(all=True)
|
||||
mock_inspect.assert_has_calls([mock.call(x) for x in range(0, 3)])
|
@ -16,7 +16,6 @@ WebOb>=1.8.1 # MIT
|
||||
alembic>=0.9.6 # MIT
|
||||
cliff!=2.9.0,>=2.8.0 # Apache-2.0
|
||||
decorator>=3.4.0 # BSD
|
||||
docker>=4.3.0 # Apache-2.0
|
||||
eventlet>=0.28.0 # MIT
|
||||
iso8601>=0.1.11 # MIT
|
||||
jsonpatch!=1.20,>=1.16 # BSD
|
||||
|
Loading…
Reference in New Issue
Block a user