summaryrefslogtreecommitdiffstats
path: root/utils/infra_setup/runner/docker_usage.py
blob: 63dc6e7a07b928f36874785af31420b62cc3e4ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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()