本文整理汇总了Python中docker.errors.APIError方法的典型用法代码示例。如果您正苦于以下问题:Python errors.APIError方法的具体用法?Python errors.APIError怎么用?Python errors.APIError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docker.errors
的用法示例。
在下文中一共展示了errors.APIError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: docker_client
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def docker_client():
client_kwargs = dict()
if not CONF.docker.api_insecure:
client_kwargs['ca_cert'] = CONF.docker.ca_file
client_kwargs['client_key'] = CONF.docker.key_file
client_kwargs['client_cert'] = CONF.docker.cert_file
try:
yield DockerHTTPClient(
CONF.docker.api_url,
CONF.docker.docker_remote_api_version,
CONF.docker.default_timeout,
**client_kwargs
)
except errors.APIError as e:
desired_exc = exception.DockerError(error_msg=str(e))
utils.reraise(type(desired_exc), desired_exc, sys.exc_info()[2])
示例2: delete
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def delete(self, context, container, force):
with docker_utils.docker_client() as docker:
try:
network_api = zun_network.api(context=context,
docker_api=docker)
self._cleanup_network_for_container(container, network_api)
self._cleanup_exposed_ports(network_api.neutron_api,
container)
if container.container_id:
docker.remove_container(container.container_id,
force=force)
except errors.APIError as api_error:
if is_not_found(api_error):
return
if is_not_connected(api_error):
return
raise
示例3: show
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def show(self, context, container):
with docker_utils.docker_client() as docker:
if container.container_id is None:
return container
response = None
try:
response = docker.inspect_container(container.container_id)
except errors.APIError as api_error:
if is_not_found(api_error):
handle_not_found(api_error, context, container,
do_not_raise=True)
return container
raise
self._populate_container(container, response)
return container
示例4: image_exists
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def image_exists(self, image_id):
"""
does provided image exists?
:param image_id: str or ImageName
:return: True if exists, False if not
"""
logger.info("checking whether image '%s' exists", image_id)
logger.debug("image_id = '%s'", image_id)
try:
response = self.d.inspect_image(image_id)
except APIError as ex:
logger.warning(str(ex))
response = False
else:
response = response is not None
logger.debug("image exists: %s", response)
return response
示例5: cleanup_containers
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def cleanup_containers(self, *container_ids):
"""
Removes specified containers and their volumes
:param container_ids: IDs of containers
"""
volumes = []
for container_id in container_ids:
volumes.extend(self.get_volumes_for_container(container_id))
try:
self.remove_container(container_id)
except APIError:
logger.warning(
"error removing container %s (ignored):",
container_id, exc_info=True)
for volume_name in volumes:
try:
self.remove_volume(volume_name)
except APIError:
logger.warning(
"error removing volume %s (ignored):",
volume_name, exc_info=True)
示例6: test_export
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def test_export(docker_tasker, no_container):
if MOCK:
mock_docker()
container_dict = docker_tasker.create_container(INPUT_IMAGE, command=["/bin/bash"])
container_id = container_dict['Id']
try:
if no_container:
with pytest.raises(docker.errors.APIError):
docker_tasker.export_container('NOT_THERE')
else:
export_generator = docker_tasker.export_container(container_id)
for _ in export_generator:
pass
finally:
docker_tasker.remove_container(container_id)
示例7: test_retry_method
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def test_retry_method(retry_times):
my_args = ('some', 'new')
my_kwargs = {'one': 'first', 'two': 'second'}
(flexmock(sys.modules[__name__])
.should_call('my_func')
.with_args(*my_args, **my_kwargs)
.times(retry_times + 1))
(flexmock(time)
.should_receive('sleep')
.and_return(None))
if retry_times >= 0:
with pytest.raises(docker.errors.APIError):
retry(my_func, *my_args, retry=retry_times, **my_kwargs)
else:
retry(my_func, *my_args, retry=retry_times, **my_kwargs)
示例8: get_service
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def get_service(self):
self.log.debug("Getting Docker service '%s'", self.service_name)
try:
service = yield self.docker(
'inspect_service', self.service_name
)
self.service_id = service['ID']
except APIError as err:
if err.response.status_code == 404:
self.log.info("Docker service '%s' is gone", self.service_name)
service = None
# Docker service is gone, remove service id
self.service_id = ''
elif err.response.status_code == 500:
self.log.info("Docker Swarm Server error")
service = None
# Docker service is unhealthy, remove the service_id
self.service_id = ''
else:
raise
return service
示例9: retry_call
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def retry_call(call, name, logger, retries, *args, **kwargs):
""" call a function call(), retries times, with *args and **kwargs, and with
exponential backoff on failure. if it fails after after retries time, raise
the last exception """
exc = None
for i in range(retries):
try:
logger.info("calling %r, attempt %d/%d", name, i+1, retries)
return call(*args, **kwargs)
except (APIError, DockerException, exceptions.DockerResultError) as exc:
logger.exception("error calling %r, retrying", call)
sleep(2 ** i)
logger.error("failed calling %r after %d tries, giving up", call,
retries)
raise exc
示例10: get_network_mode
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def get_network_mode(self, service_dict, networks):
network_mode = service_dict.pop('network_mode', None)
if not network_mode:
if self.networks.use_networking:
return NetworkMode(networks[0]) if networks else NetworkMode('none')
return NetworkMode(None)
service_name = get_service_name_from_network_mode(network_mode)
if service_name:
return ServiceNetworkMode(self.get_service(service_name))
container_name = get_container_name_from_network_mode(network_mode)
if container_name:
try:
return ContainerNetworkMode(Container.from_id(self.client, container_name))
except APIError:
raise ConfigurationError(
"Service '{name}' uses the network stack of container '{dep}' which "
"does not exist.".format(name=service_dict['name'], dep=container_name))
return NetworkMode(network_mode)
示例11: get_pid_mode
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def get_pid_mode(self, service_dict):
pid_mode = service_dict.pop('pid', None)
if not pid_mode:
return PidMode(None)
service_name = get_service_name_from_network_mode(pid_mode)
if service_name:
return ServicePidMode(self.get_service(service_name))
container_name = get_container_name_from_network_mode(pid_mode)
if container_name:
try:
return ContainerPidMode(Container.from_id(self.client, container_name))
except APIError:
raise ConfigurationError(
"Service '{name}' uses the PID namespace of container '{dep}' which "
"does not exist.".format(name=service_dict['name'], dep=container_name)
)
return PidMode(pid_mode)
示例12: handle_connection_errors
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def handle_connection_errors(client):
try:
yield
except SSLError as e:
log.error('SSL error: %s' % e)
raise ConnectionError()
except RequestsConnectionError as e:
if e.args and isinstance(e.args[0], ReadTimeoutError):
log_timeout_error(client.timeout)
raise ConnectionError()
exit_with_error(get_conn_error_message(client.base_url))
except APIError as e:
log_api_error(e, client.api_version)
raise ConnectionError()
except (ReadTimeout, socket.timeout):
log_timeout_error(client.timeout)
raise ConnectionError()
except Exception as e:
if is_windows():
import pywintypes
if isinstance(e, pywintypes.error):
log_windows_pipe_error(e)
raise ConnectionError()
raise
示例13: error_passthrough_test
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def error_passthrough_test(self, method_name):
"""
If the given method name on the underyling ``Docker`` client has a
non-404 error, that gets passed through to ``Docker.list()``.
:param str method_name: Method of a docker ``Client``.
:return: ``Deferred`` firing on test success.
"""
name = random_name(self)
client = DockerClient()
self.addCleanup(client.remove, name)
d = client.add(name, u"busybox:latest")
response = make_response(500, "Simulated error")
def error(name):
raise APIError("", response)
def added(_):
# Monekypatch cause triggering non-404 errors from
# inspect_container is hard.
self.patch(client._client, method_name, error)
return client.list()
d.addCallback(added)
return self.assertFailure(d, APIError)
示例14: test_get_container_info_404
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def test_get_container_info_404(self):
"""Test 404 error on API Access in _get_container_info
Method should raise SystemExit when API returns 404
"""
# Create mock side effect APIError
response = Mock()
response.status_code = 404
self.m_docker_client.inspect_container.side_effect = APIError(
'Error', response)
# Set up args
container_name = 'container_name'
# Call method under test
assert_raises(
SystemExit, self.plugin._get_container_info, container_name)
示例15: repeater
# 需要导入模块: from docker import errors [as 别名]
# 或者: from docker.errors import APIError [as 别名]
def repeater(call, args=None, kwargs=None, retries=4):
"""
repeat call x-times: docker API is just awesome
:param call: function
:param args: tuple, args for function
:param kwargs: dict, kwargs for function
:param retries: int, how many times we try?
:return: response of the call
"""
args = args or ()
kwargs = kwargs or {}
t = 1.0
for x in range(retries):
try:
return call(*args, **kwargs)
except APIError as ex:
logger.error("query #%d: docker returned an error: %r", x, ex)
except Exception as ex:
# this may be pretty bad
log_last_traceback()
logger.error("query #%d: generic error: %r", x, ex)
t *= 2
time.sleep(t)