2 ##############################################################################
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10 '''This file contain some function about docker API.
11 At present, This file contain the following function:
12 1.Ask Docker service to create a docker(yardstick or ELK).
25 client = docker.from_env()
29 def docker_find(docker_name):
31 docker_client = client.containers.get(docker_name)
35 def env_yardstick(docker_name):
37 yardstick_info["name"] = docker_name
39 env_docker = docker_find(docker_name)
40 yardstick_info["container"] = env_docker
41 yardstick_info["ip"] = get_docker_ip(docker_name)
43 except docker.errors.NotFound:
45 volume = get_self_volume()
46 yardstick_tag = os.getenv("Yardstick_TAG")
47 if yardstick_tag is None:
48 yardstick_tag = "danube.3.1"
49 env_docker = client.containers.run(image="yardstick_pmd",
53 ports={'5000': '8888'},
56 yardstick_info["container"] = env_docker
57 yardstick_info["ip"] = get_docker_ip(docker_name)
61 def env_bottlenecks(docker_name):
63 volume = get_self_volume()
64 env_docker = client.containers.run(image="opnfv/bottlenecks:latest",
67 ports={'8888': '5000'},
73 def env_elk(docker_name):
75 ELK_info["name"] = docker_name
77 env_docker = docker_find(docker_name)
78 ELK_info["container"] = env_docker
79 ELK_info["ip"] = get_docker_ip(docker_name)
81 except docker.errors.NotFound:
83 env_docker = client.containers.run(image="sebp/elk:es241_l240_k461",
86 ports={'5044': '5044',
90 ELK_info["container"] = env_docker
91 ELK_info["ip"] = get_docker_ip(docker_name)
95 def get_docker_ip(docker_name):
96 env_docker = docker_find(docker_name)
97 client = docker.APIClient(base_url='unix://var/run/docker.sock')
98 docker_inspect = client.inspect_container(env_docker.id)
99 ip_address = docker_inspect["NetworkSettings"]["IPAddress"]
103 def docker_exec_cmd(docker, cmd):
104 return docker.exec_run(cmd)
107 def get_self_volume():
109 hostname = socket.gethostname()
110 client = docker.APIClient(base_url='unix://var/run/docker.sock')
111 volume = client.inspect_container(hostname)["Mounts"]
113 self_volume[i['Source']] = i['Destination']
117 def remove_docker(docker_name):
118 docker = docker_find(docker_name)