本文整理汇总了Python中utils.dockerutil.DockerUtil.inspect_container方法的典型用法代码示例。如果您正苦于以下问题:Python DockerUtil.inspect_container方法的具体用法?Python DockerUtil.inspect_container怎么用?Python DockerUtil.inspect_container使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.dockerutil.DockerUtil
的用法示例。
在下文中一共展示了DockerUtil.inspect_container方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: agent_container_inspect
# 需要导入模块: from utils.dockerutil import DockerUtil [as 别名]
# 或者: from utils.dockerutil.DockerUtil import inspect_container [as 别名]
def agent_container_inspect():
# Self inspection based on cgroups
# On all platforms, the container ID is the last part of the path.
REGEX_PATTERN = '(.*/)+([a-z0-9]{64})$'
dockerutil = DockerUtil()
cgroup_path = '/proc/self/cgroup'
container_id = None
with open(cgroup_path, 'r') as f:
for ind in f:
id_match = re.search(REGEX_PATTERN, ind)
if id_match:
container_id = id_match.group(2)
break
if container_id is None:
print("The container_id could not be found. Refer to the docker log of the container running the agent")
return 1
try:
inspect = dockerutil.inspect_container(container_id)
key_indices = [i for i, k in enumerate(inspect['Config']['Env']) if 'API_KEY' in k]
for ind in key_indices:
inspect['Config']['Env'][ind] = '%s=%s' % (inspect['Config']['Env'][ind].split('=', 1)[0], 'redacted')
print json.dumps(inspect, indent=4)
return 0
except Exception as e:
print "Could not inspect container: %s" % e
示例2: __init__
# 需要导入模块: from utils.dockerutil import DockerUtil [as 别名]
# 或者: from utils.dockerutil.DockerUtil import inspect_container [as 别名]
class ECSUtil:
__metaclass__ = Singleton
def __init__(self):
self.docker_util = DockerUtil()
self.ecs_agent_local = None
self.ecs_tags = {}
self._populate_ecs_tags()
def _get_ecs_address(self):
"""Detect how to connect to the ecs-agent"""
ecs_config = self.docker_util.inspect_container('ecs-agent')
ip = ecs_config.get('NetworkSettings', {}).get('IPAddress')
ports = ecs_config.get('NetworkSettings', {}).get('Ports')
port = ports.keys()[0].split('/')[0] if ports else None
if not ip:
port = ECS_INTROSPECT_DEFAULT_PORT
if self._is_ecs_agent_local():
ip = "localhost"
elif Platform.is_containerized() and self.docker_gateway:
ip = self.docker_gateway
else:
raise Exception("Unable to determine ecs-agent IP address")
return ip, port
def _populate_ecs_tags(self, skip_known=False):
"""
Populate the cache of ecs tags. Can be called with skip_known=True
If we just want to update new containers quickly (single task api call)
(because we detected that a new task started for example)
"""
try:
ip, port = self._get_ecs_address()
except Exception as ex:
log.warning("Failed to connect to ecs-agent, skipping task tagging: %s" % ex)
return
try:
tasks = requests.get('http://%s:%s/v1/tasks' % (ip, port)).json()
for task in tasks.get('Tasks', []):
for container in task.get('Containers', []):
cid = container['DockerId']
if skip_known and cid in self.ecs_tags:
continue
tags = ['task_name:%s' % task['Family'], 'task_version:%s' % task['Version']]
self.ecs_tags[container['DockerId']] = tags
except requests.exceptions.HTTPError as ex:
log.warning("Unable to collect ECS task names: %s" % ex)
def _get_container_tags(self, cid):
"""
This method triggers a fast fill of the tag cache (useful when a new task starts
and we want the new containers to be cached with a single api call) and returns
the tags (or an empty list) from the fresh cache.
"""
self._populate_ecs_tags(skip_known=True)
if cid in self.ecs_tags:
return self.ecs_tags[cid]
else:
log.debug("Container %s doesn't seem to be an ECS task, skipping." % cid[:12])
self.ecs_tags[cid] = []
return []
def _is_ecs_agent_local(self):
"""Return True if we can reach the ecs-agent over localhost, False otherwise.
This is needed because if the ecs-agent is started with --net=host it won't have an IP address attached.
"""
if self.ecs_agent_local is not None:
return self.ecs_agent_local
self.ecs_agent_local = False
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
try:
result = sock.connect_ex(('localhost', ECS_INTROSPECT_DEFAULT_PORT))
except Exception as e:
log.debug("Unable to connect to ecs-agent. Exception: {0}".format(e))
else:
if result == 0:
self.ecs_agent_local = True
else:
log.debug("ecs-agent is not available locally, encountered error code: {0}".format(result))
sock.close()
return self.ecs_agent_local
def extract_container_tags(self, co):
"""
Queries the ecs-agent to get ECS tags (task and task version) for a containers.
As this is expensive, it is cached in the self.ecs_tags dict.
The cache invalidation goes through invalidate_ecs_cache, called by the docker_daemon check
:param co: container dict returned by docker-py
:return: tags as list<string>, cached
"""
co_id = co.get('Id', None)
#.........这里部分代码省略.........
示例3: __init__
# 需要导入模块: from utils.dockerutil import DockerUtil [as 别名]
# 或者: from utils.dockerutil.DockerUtil import inspect_container [as 别名]
class NomadUtil:
__metaclass__ = Singleton
def __init__(self):
self.docker_util = DockerUtil()
# Tags cache as a dict {co_id: (create_timestamp, [tags])}
self._container_tags_cache = {}
def extract_container_tags(self, co):
"""
Queries docker inspect to get nomad tags in the container's environment vars.
As this is expensive, it is cached in the self._nomad_tags_cache dict.
The cache invalidation goes through invalidate_nomad_cache, called by the docker_daemon check
:param co: container dict returned by docker-py
:return: tags as list<string>, cached
"""
co_id = co.get('Id', None)
if co_id is None:
log.warning("Invalid container object in extract_container_tags")
return
# Cache lookup on Id, verified on Created timestamp
if co_id in self._container_tags_cache:
created, tags = self._container_tags_cache[co_id]
if created == co.get('Created', -1):
return tags
tags = []
try:
inspect_info = self.docker_util.inspect_container(co_id)
envvars = inspect_info.get('Config', {}).get('Env', {})
for var in envvars:
if var.startswith(NOMAD_TASK_NAME):
tags.append('nomad_task:%s' % var[len(NOMAD_TASK_NAME) + 1:])
elif var.startswith(NOMAD_JOB_NAME):
tags.append('nomad_job:%s' % var[len(NOMAD_JOB_NAME) + 1:])
elif var.startswith(NOMAD_ALLOC_NAME):
try:
start = var.index('.', len(NOMAD_ALLOC_NAME)) + 1
end = var.index('[')
if end <= start:
raise ValueError("Error extracting group from %s, check format" % var)
tags.append('nomad_group:%s' % var[start:end])
except ValueError:
pass
self._container_tags_cache[co_id] = (co.get('Created'), tags)
except Exception as e:
log.warning("Error while parsing Nomad tags: %s" % str(e))
finally:
return tags
def invalidate_cache(self, events):
"""
Allows cache invalidation when containers dies
:param events from self.get_events
"""
try:
for ev in events:
if ev.get('status') == 'die' and ev.get('id') in self._container_tags_cache:
del self._container_tags_cache[ev.get('id')]
except Exception as e:
log.warning("Error when invalidating nomad cache: " + str(e))
示例4: SDDockerBackend
# 需要导入模块: from utils.dockerutil import DockerUtil [as 别名]
# 或者: from utils.dockerutil.DockerUtil import inspect_container [as 别名]
#.........这里部分代码省略.........
for image, cid, labels in containers:
try:
check_configs = self._get_check_configs(cid, image, trace_config=trace_config) or []
for conf in check_configs:
if trace_config and conf is not None:
source, conf = conf
check_name, init_config, instance = conf
# build instances list if needed
if configs.get(check_name) is None:
if trace_config:
configs[check_name] = (source, (init_config, [instance]))
else:
configs[check_name] = (init_config, [instance])
else:
conflict_init_msg = 'Different versions of `init_config` found for check {0}. ' \
'Keeping the first one found.'
if trace_config:
if configs[check_name][1][0] != init_config:
log.warning(conflict_init_msg.format(check_name))
configs[check_name][1][1].append(instance)
else:
if configs[check_name][0] != init_config:
log.warning(conflict_init_msg.format(check_name))
configs[check_name][1].append(instance)
except Exception:
log.exception('Building config for container %s based on image %s using service'
' discovery failed, leaving it alone.' % (cid[:12], image))
return configs
def _get_check_configs(self, c_id, image, trace_config=False):
"""Retrieve configuration templates and fill them with data pulled from docker and tags."""
inspect = self.docker_client.inspect_container(c_id)
config_templates = self._get_config_templates(image, trace_config=trace_config)
if not config_templates:
log.debug('No config template for container %s with image %s. '
'It will be left unconfigured.' % (c_id[:12], image))
return None
check_configs = []
tags = self.get_tags(inspect)
for config_tpl in config_templates:
if trace_config:
source, config_tpl = config_tpl
check_name, init_config_tpl, instance_tpl, variables = config_tpl
# insert tags in instance_tpl and process values for template variables
instance_tpl, var_values = self._fill_tpl(inspect, instance_tpl, variables, tags)
tpl = self._render_template(init_config_tpl or {}, instance_tpl or {}, var_values)
if tpl and len(tpl) == 2:
if trace_config and len(tpl[1]) == 2:
source, (init_config, instance) = tpl
check_configs.append((source, (check_name, init_config, instance)))
elif not trace_config:
init_config, instance = tpl
check_configs.append((check_name, init_config, instance))
return check_configs
def _get_config_templates(self, image_name, trace_config=False):
"""Extract config templates for an image from a K/V store and returns it as a dict object."""
config_backend = self.agentConfig.get('sd_config_backend')
templates = []
if config_backend is None: