123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- # -*- coding: utf-8 -*-
- from openerp.tools import config
- from openerp.exceptions import Warning
- from docker import DockerClient
- from docker.errors import DockerException, NotFound, APIError, ContainerError, ImageNotFound
- import time
- try:
- client = DockerClient(base_url='unix:/%s' % config['docker_sock'])
- except DockerException:
- raise Warning('Socket not found')
- def get_client():
- return client
- def run_watchdog(func):
- for e in client.events():
- func(e)
- def get_all_images():
- images = []
- for image in client.images.list():
- images.append({
- 'id': image.id,
- 'short_id': image.short_id,
- 'tags': image.tags
- })
- return images
- def get_all_containers():
- exclusion_list = map(lambda x: x.strip(), config['docker_exclusion'] or [])
- containers = []
- for container in client.containers.list(all=True):
- if container.name in exclusion_list:
- continue
- containers.append({
- 'id': container.id,
- 'short_id': container.short_id,
- 'name': container.name,
- 'image': container.image.tags,
- 'labels': container.labels,
- 'status': container.status,
- 'attrs': container.attrs
- })
- return containers
- def get_all_external_ports():
- port_dict = map(lambda x: x.get('attrs').get('NetworkSettings').get('Ports'), get_all_containers())
- ports = []
- for m in port_dict:
- if not m:
- continue
- for _, v in m.iteritems():
- if not v:
- continue
- ports.extend(map(lambda x: x['HostPort'], v))
- return ports
- def get_all_networks():
- networks = []
- for n in client.networks.list():
- networks.append({
- 'id': n.id,
- 'short_id': n.short_id,
- 'name': n.name,
- 'attrs': n.attrs
- })
- return networks
- def get_network(network_name_or_id):
- try:
- network = client.networks.get(network_name_or_id)
- return {
- 'id': network.id,
- 'short_id': network.short_id,
- 'name': network.name,
- 'attrs': network.attrs
- }
- except (NotFound, APIError):
- return None
- def get_internal_ip(container_name_or_id):
- network = get_network(config['docker_network'] or 'bridge')
- if not network:
- return None
- try:
- container = client.containers.get(container_name_or_id)
- internal_ip = container.attrs.get('NetworkSettings').get('Networks').get(network.get('name')).get('IPAddress')
- return str(internal_ip)
- except (NotFound, APIError):
- return None
- def run_container(image, name, ports=[], volumes=None, network=None, memory_limit=None, memory_swap_limit=None):
- try:
- client.containers.run(
- image,
- None,
- name=name,
- detach=True,
- mem_limit=memory_limit,
- memswap_limit=memory_swap_limit,
- network=network,
- ports=ports,
- volumes=volumes
- )
- return True
- except (ContainerError, ImageNotFound, APIError):
- return False
- def start_container(container_name_or_id):
- try:
- container = client.containers.get(container_name_or_id)
- container.start()
- time.sleep(0.3)
- container.reload()
- return {
- 'id': container.id,
- 'short_id': container.short_id,
- 'name': container.name,
- 'image': container.image,
- 'labels': container.labels,
- 'status': container.status
- }
- except (NotFound, APIError):
- return False
- def restart_container(container_name_or_id):
- try:
- container = client.containers.get(container_name_or_id)
- container.restart()
- time.sleep(0.3)
- container.reload()
- return {
- 'id': container.id,
- 'short_id': container.short_id,
- 'name': container.name,
- 'image': container.image,
- 'labels': container.labels,
- 'status': container.status
- }
- except (NotFound, APIError):
- return False
- def stop_container(container_name_or_id):
- try:
- container = client.containers.get(container_name_or_id)
- container.stop()
- time.sleep(0.3)
- container.reload()
- return {
- 'id': container.id,
- 'short_id': container.short_id,
- 'name': container.name,
- 'image': container.image,
- 'labels': container.labels,
- 'status': container.status
- }
- except (NotFound, APIError):
- return False
- def run_command(container_name_or_id, command):
- try:
- container = client.containers.get(container_name_or_id)
- (exit_code, _) = container.exec_run(command)
- return (False, True)[exit_code == 0]
- except (NotFound, APIError):
- return False
- def copy_in(container_name_or_id, path, tarfile):
- try:
- filestream = open(tarfile, 'r')
- container = client.containers.get(container_name_or_id)
- result = container.put_archive(path, filestream)
- filestream.close()
- return result
- except (NotFound, APIError, IOError):
- return False
- def get_status(container_name_or_id):
- try:
- container = client.containers.get(container_name_or_id)
- return (False, True)[container.status == 'running']
- except (NotFound, APIError):
- return False
|