diff options
author | liyin <liyin11@huawei.com> | 2017-07-12 10:57:13 +0800 |
---|---|---|
committer | Yu Yang (Gabriel) <Gabriel.yuyang@huawei.com> | 2017-07-18 01:35:54 +0000 |
commit | d1fa84ed4774525e634cc7f402be62bfccf7c876 (patch) | |
tree | b28393fdd28fc799a3c0f07730f9616a63422c28 | |
parent | d31e40fdf62cecd272ec7811d9205ea6916594f9 (diff) |
Add docker container manage function
JIRA: BOTTLENECK-171
This patch will add a file to create some function,
Bottlenecks will use those function to manage docker.
for example: docker ip address , create docker ,remove docker.
Change-Id: I60e70ed9c6c7805e85b183ea3b19295fce68f657
Signed-off-by: liyin <liyin11@huawei.com>
(cherry picked from commit c821b792db011dbbe1c70d256fadbe9c745570ff)
-rw-r--r-- | utils/infra_setup/runner/docker_usage.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/utils/infra_setup/runner/docker_usage.py b/utils/infra_setup/runner/docker_usage.py new file mode 100644 index 00000000..63dc6e7a --- /dev/null +++ b/utils/infra_setup/runner/docker_usage.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +############################################################################## +# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +'''This file contain some function about docker API. +At present, This file contain the following function: +1.Ask Docker service to create a docker(yardstick or ELK). +2.get a docker ip. +3.Remove a docker.''' + +import docker + +yardstick_info = None +ELK_info = None + + +def get_client(): + client = docker.from_env() + return client + + +def docker_find(docker_name): + client = get_client() + docker_client = client.containers.get(docker_name) + return docker_client + + +def env_yardstick(docker_name): + client = get_client() + yardstick_info["name"] = docker_name + try: + env_docker = docker_find(docker_name) + yardstick_info["containner"] = env_docker + yardstick_info["ip"] = get_docker_ip(docker_name) + return env_docker + except docker.errors.NotFound: + pass + env_docker = client.containers.run(image="opnfv/yardstick:latest", + privileged=True, + detach=True, + ports={'8888': '5000'}, + volumes={'/var/run/docker.sock': + '/var/run/docker.sock'}, + name=docker_name) + yardstick_info["containner"] = env_docker + yardstick_info["ip"] = get_docker_ip(docker_name) + return env_docker + + +def env_elk(docker_name): + client = get_client() + ELK_info["name"] = docker_name + try: + env_docker = docker_find(docker_name) + ELK_info["container"] = env_docker + ELK_info["ip"] = get_docker_ip(docker_name) + return env_docker + except docker.errors.NotFound: + pass + env_docker = client.containers.run(image="sebp/elk:es241_l240_k461", + privileged=True, + detach=True, + ports={'5044': '5044', + '5601': '5601', + '9200': '9200'}, + name=docker_name) + ELK_info["container"] = env_docker + ELK_info["ip"] = get_docker_ip(docker_name) + return env_docker + + +def get_docker_ip(docker_name): + env_docker = docker_find(docker_name) + client = docker.APIClient(base_url='unix://var/run/docker.sock') + ip_address = client.inspect_container(env_docker.id) + return ip_address + + +def docker_exec_cmd(docker, cmd): + return docker.exec_cmd(cmd) + + +def remove_docker(docker_name): + docker = docker_find(docker_name) + docker.kill() + docker.remove() |