本文整理汇总了Python中docker.APIClient类的典型用法代码示例。如果您正苦于以下问题:Python APIClient类的具体用法?Python APIClient怎么用?Python APIClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了APIClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: docker_abs_net_io
def docker_abs_net_io(container_id):
"""
Network traffic of all network interfaces within the controller.
:param container_id: The full ID of the docker container.
:type container_id: ``str``
:return: Returns the absolute network I/O till container startup, in bytes. The return dict also contains the
system time.
:rtype: ``dict``
"""
c = APIClient()
command = c.exec_create(container_id, 'ifconfig')
ifconfig = c.exec_start(command['Id'])
sys_time = int(time.time() * 1000000000)
in_bytes = 0
m = re.findall('RX bytes:(\d+)', str(ifconfig))
if m:
for number in m:
in_bytes += int(number)
else:
in_bytes = None
out_bytes = 0
m = re.findall('TX bytes:(\d+)', str(ifconfig))
if m:
for number in m:
out_bytes += int(number)
else:
out_bytes = None
return {'NET_in': in_bytes, 'NET_out': out_bytes, 'NET_systime': sys_time}
示例2: docker_client
def docker_client(environment, version=None, tls_config=None, host=None,
tls_version=None):
"""
Returns a docker-py client configured using environment variables
according to the same logic as the official Docker client.
"""
try:
kwargs = kwargs_from_env(environment=environment, ssl_version=tls_version)
except TLSParameterError:
raise UserError(
"TLS configuration is invalid - make sure your DOCKER_TLS_VERIFY "
"and DOCKER_CERT_PATH are set correctly.\n"
"You might need to run `eval \"$(docker-machine env default)\"`")
if host:
kwargs['base_url'] = host
if tls_config:
kwargs['tls'] = tls_config
if version:
kwargs['version'] = version
timeout = environment.get('COMPOSE_HTTP_TIMEOUT')
if timeout:
kwargs['timeout'] = int(timeout)
else:
kwargs['timeout'] = HTTP_TIMEOUT
kwargs['user_agent'] = generate_user_agent()
client = APIClient(**kwargs)
client._original_base_url = kwargs.get('base_url')
return client
示例3: get_server_containers
def get_server_containers(server: Server, client: docker.APIClient) -> tp.List[dict]:
containers = client.containers()
server_containers = []
for container in containers:
container = {
'command': filter_printable(container['Command']),
'containerId': container['Id'],
'image': container['Image'],
'labels': sorted([{
'containerId': container['Id'],
'name': l[0],
'value': l[1]} for l in container['Labels'].items()],
key=lambda label: label['name']),
'name': container['Names'][0],
'network': container['HostConfig']['NetworkMode'],
'ports': sorted([{
'destination': str(p['PrivatePort']),
'hostIp': p['IP'] if 'IP' in p else None,
'protocol': p['Type'],
'source': str(p['PublicPort']) if 'PublicPort' in p else None} for p in container['Ports']],
key=lambda port: (str(port['destination']), str(port['source']))),
'privileged': client.inspect_container(container['Id'])['HostConfig']['Privileged'],
'serverId': server.id,
'volumes': sorted([{
'containerId': container['Id'],
'destination': filter_printable(v['Destination']),
'source': filter_printable(v['Source'])} for v in container['Mounts']],
key=lambda volume: volume['destination'])
}
server_containers.append(container)
return server_containers
示例4: modify_random_containers
def modify_random_containers(client: docker.APIClient, amount: int, action: str = 'stop') -> tp.List[dict]:
server_containers = client.containers()
stopped_containers = []
for _ in range(amount):
container = random.choice(server_containers)
if action == 'delete':
client.remove_container(container, force=True)
elif action == 'stop':
client.stop(container)
stopped_containers.append(container)
server_containers.remove(container)
return stopped_containers
示例5: docker_container_id
def docker_container_id(container_name):
"""
Uses the container name to return the container ID.
:param container_name: The full name of the docker container.
:type container_name: ``str``
:return: Returns the container ID or None if the container is not running or could not be found.
:rtype: ``dict``
"""
c = APIClient()
detail = c.inspect_container(container_name)
if bool(detail["State"]["Running"]):
return detail['Id']
return None
示例6: __init__
def __init__(self,
build_job,
repo_path,
from_image,
copy_code=True,
build_steps=None,
env_vars=None,
dockerfile_name='Dockerfile'):
self.build_job = build_job
self.job_uuid = build_job.uuid.hex
self.job_name = build_job.unique_name
self.from_image = from_image
self.image_name = get_image_name(self.build_job)
self.image_tag = self.job_uuid
self.folder_name = repo_path.split('/')[-1]
self.repo_path = repo_path
self.copy_code = copy_code
self.build_path = '/'.join(self.repo_path.split('/')[:-1])
self.build_steps = get_list(build_steps)
self.env_vars = get_list(env_vars)
self.dockerfile_path = os.path.join(self.build_path, dockerfile_name)
self.polyaxon_requirements_path = self._get_requirements_path()
self.polyaxon_setup_path = self._get_setup_path()
self.docker = APIClient(version='auto')
self.registry_host = None
self.docker_url = None
示例7: execute
def execute(self, context):
self.log.info('Starting docker container from image %s', self.image)
tls_config = self.__get_tls_config()
if self.docker_conn_id:
self.cli = self.get_hook().get_conn()
else:
self.cli = APIClient(
base_url=self.docker_url,
version=self.api_version,
tls=tls_config
)
if ':' not in self.image:
image = self.image + ':latest'
else:
image = self.image
if self.force_pull or len(self.cli.images(name=image)) == 0:
self.log.info('Pulling docker image %s', image)
for l in self.cli.pull(image, stream=True):
output = json.loads(l.decode('utf-8'))
self.log.info("%s", output['status'])
cpu_shares = int(round(self.cpus * 1024))
with TemporaryDirectory(prefix='airflowtmp') as host_tmp_dir:
self.environment['AIRFLOW_TMP_DIR'] = self.tmp_dir
self.volumes.append('{0}:{1}'.format(host_tmp_dir, self.tmp_dir))
self.container = self.cli.create_container(
command=self.get_command(),
cpu_shares=cpu_shares,
environment=self.environment,
host_config=self.cli.create_host_config(
binds=self.volumes,
network_mode=self.network_mode,
shm_size=self.shm_size),
image=image,
mem_limit=self.mem_limit,
user=self.user,
working_dir=self.working_dir
)
self.cli.start(self.container['Id'])
line = ''
for line in self.cli.logs(container=self.container['Id'], stream=True):
line = line.strip()
if hasattr(line, 'decode'):
line = line.decode('utf-8')
self.log.info(line)
exit_code = self.cli.wait(self.container['Id'])
if exit_code != 0:
raise AirflowException('docker container failed')
if self.xcom_push_flag:
return self.cli.logs(container=self.container['Id']) \
if self.xcom_all else str(line)
示例8: __init__
def __init__(self,
repo_path,
from_image,
image_name,
image_tag,
copy_code=True,
in_tmp_repo=True,
build_steps=None,
env_vars=None,
dockerfile_name='Dockerfile'):
# This will help create a unique tmp folder for dockerizer in case of concurrent jobs
self.uuid = uuid.uuid4().hex
self.from_image = from_image
self.image_name = image_name
self.image_tag = image_tag
self.repo_path = repo_path
self.folder_name = repo_path.split('/')[-1]
self.copy_code = copy_code
self.in_tmp_repo = in_tmp_repo
if in_tmp_repo and copy_code:
self.build_repo_path = self.create_tmp_repo()
else:
self.build_repo_path = self.repo_path
self.build_path = '/'.join(self.build_repo_path.split('/')[:-1])
self.build_steps = get_list(build_steps)
self.env_vars = get_list(env_vars)
self.dockerfile_path = os.path.join(self.build_path, dockerfile_name)
self.polyaxon_requirements_path = self._get_requirements_path()
self.polyaxon_setup_path = self._get_setup_path()
self.docker = APIClient(version='auto')
self.registry_host = None
self.docker_url = None
示例9: execute
def execute(self, context):
logging.info('Starting docker container from image ' + self.image)
tls_config = None
if self.tls_ca_cert and self.tls_client_cert and self.tls_client_key:
tls_config = tls.TLSConfig(
ca_cert=self.tls_ca_cert,
client_cert=(self.tls_client_cert, self.tls_client_key),
verify=True,
ssl_version=self.tls_ssl_version,
assert_hostname=self.tls_hostname
)
self.docker_url = self.docker_url.replace('tcp://', 'https://')
self.cli = APIClient(base_url=self.docker_url, version=self.api_version, tls=tls_config)
if ':' not in self.image:
image = self.image + ':latest'
else:
image = self.image
if self.force_pull or len(self.cli.images(name=image)) == 0:
logging.info('Pulling docker image ' + image)
for l in self.cli.pull(image, stream=True):
output = json.loads(l.decode('utf-8'))
logging.info("{}".format(output['status']))
cpu_shares = int(round(self.cpus * 1024))
with TemporaryDirectory(prefix='airflowtmp') as host_tmp_dir:
self.environment['AIRFLOW_TMP_DIR'] = self.tmp_dir
self.volumes.append('{0}:{1}'.format(host_tmp_dir, self.tmp_dir))
self.container = self.cli.create_container(
command=self.get_command(),
cpu_shares=cpu_shares,
environment=self.environment,
host_config=self.cli.create_host_config(
binds=self.volumes,
network_mode=self.network_mode,
auto_remove=self.auto_remove),
image=image,
mem_limit=self.mem_limit,
user=self.user
)
self.cli.start(self.container['Id'])
line = ''
for line in self.cli.logs(container=self.container['Id'], stream=True):
line = line.strip()
if hasattr(line, 'decode'):
line = line.decode('utf-8')
logging.info(line)
exit_code = self.cli.wait(self.container['Id'])
if exit_code != 0:
raise AirflowException('docker container failed')
if self.xcom_push_flag:
return self.cli.logs(container=self.container['Id']) if self.xcom_all else str(line)
示例10: execute
def execute(self, context):
self.log.info('Starting docker container from image %s', self.image)
tls_config = self.__get_tls_config()
if self.docker_conn_id:
self.cli = self.get_hook().get_conn()
else:
self.cli = APIClient(
base_url=self.docker_url,
version=self.api_version,
tls=tls_config
)
if self.force_pull or len(self.cli.images(name=self.image)) == 0:
self.log.info('Pulling docker image %s', self.image)
for l in self.cli.pull(self.image, stream=True):
output = json.loads(l.decode('utf-8').strip())
if 'status' in output:
self.log.info("%s", output['status'])
with TemporaryDirectory(prefix='airflowtmp') as host_tmp_dir:
self.environment['AIRFLOW_TMP_DIR'] = self.tmp_dir
self.volumes.append('{0}:{1}'.format(host_tmp_dir, self.tmp_dir))
self.container = self.cli.create_container(
command=self.get_command(),
environment=self.environment,
host_config=self.cli.create_host_config(
auto_remove=self.auto_remove,
binds=self.volumes,
network_mode=self.network_mode,
shm_size=self.shm_size,
dns=self.dns,
dns_search=self.dns_search,
cpu_shares=int(round(self.cpus * 1024)),
mem_limit=self.mem_limit),
image=self.image,
user=self.user,
working_dir=self.working_dir
)
self.cli.start(self.container['Id'])
line = ''
for line in self.cli.logs(container=self.container['Id'], stream=True):
line = line.strip()
if hasattr(line, 'decode'):
line = line.decode('utf-8')
self.log.info(line)
result = self.cli.wait(self.container['Id'])
if result['StatusCode'] != 0:
raise AirflowException('docker container failed: ' + repr(result))
# duplicated conditional logic because of expensive operation
if self.do_xcom_push:
return self.cli.logs(container=self.container['Id']) \
if self.xcom_all else line.encode('utf-8')
示例11: _clean_network
def _clean_network(self):
client = Client(base_url=self._docker_host, version="auto", timeout=10)
networks = client.networks(names=["%s_default" % self._project_name])
id_removes = [e["Id"] for e in networks]
for network_id in id_removes:
client.remove_network(network_id)
LOG.debug("Remove network id {}".format(network_id))
示例12: detect_daemon_type
def detect_daemon_type(worker_api, timeout=5):
""" Try to detect the daemon type
Only wait for timeout seconds.
:param worker_api: Docker daemon url
:param timeout: Time to wait for the response
:return: host type info
"""
if not worker_api or not worker_api.startswith("tcp://"):
return None
segs = worker_api.split(":")
if len(segs) != 3:
logger.error("Invalid daemon url = ", worker_api)
return None
try:
client = Client(base_url=worker_api, version="auto", timeout=timeout)
info = client.info()
server_version = info['ServerVersion']
if not server_version:
logger.warning("info().ServerVersion cannot be empty")
return None
if server_version.startswith('swarm'):
return WORKER_TYPES[1]
try:
if info['Swarm']['Cluster']['ID'] != '':
return WORKER_TYPES[1]
except Exception as e:
logger.debug(e)
return WORKER_TYPES[0]
except Exception as e:
logger.error(e)
return None
示例13: cleanup_host
def cleanup_host(worker_api, timeout=5):
"""
Cleanup a container host when use removes the host
Maybe we will remove the networks?
:param worker_api: Docker daemon url
:param timeout: timeout to wait
:return:
"""
if not worker_api or not worker_api.startswith("tcp://"):
logger.error("Invalid worker_api={}".format(worker_api))
return False
try:
client = Client(base_url=worker_api, version="auto", timeout=timeout)
net_names = [x["Name"] for x in client.networks()]
for cs_type in CONSENSUS_PLUGINS_FABRIC_V1:
net_name = CLUSTER_NETWORK + "_{}".format(cs_type)
if net_name in net_names:
logger.debug("Remove network {}".format(net_name))
client.remove_network(net_name)
else:
logger.warning("Network {} not exists!".format(net_name))
except Exception as e:
logger.error("Exception happens!")
logger.error(e)
return False
return True
示例14: StopContainer
def StopContainer():
if GOT_DOCKERPY_API < 2:
cli = Client()
cli.stop('suri-buildbot')
else:
cli = DockerClient()
cli.containers.get('suri-buildbot').stop()
sys.exit(0)
示例15: StartContainer
def StartContainer():
suri_src_dir = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
print "Using base src dir: " + suri_src_dir
if GOT_DOCKERPY_API < 2:
cli = Client()
cli.start('suri-buildbot', port_bindings={8010:8010, 22:None}, binds={suri_src_dir: { 'bind': '/data/oisf', 'ro': True}, os.path.join(suri_src_dir,'qa','docker','buildbot.cfg'): { 'bind': '/data/buildbot/master/master.cfg', 'ro': True}} )
else:
cli = DockerClient()
cli.containers.get('suri-buildbot').start()
sys.exit(0)