示例#1
0
from flask import Flask
from flask_restful import Resource, Api
from flask import jsonify
from flask import request
from threading import Thread
import docker
import signal
import time
import os
import re
import sys

docker_client = docker.DockerClient(base_url='unix://var/run/docker.sock', version='auto')
app = Flask(__name__)
api = Api(app)

class containers_get(Resource):
  def get(self):
    containers = {}
    try:
      for container in docker_client.containers.list(all=True):
        containers.update({container.attrs['Id']: container.attrs})
      return containers
    except Exception as e:
      return jsonify(type='danger', msg=e)

class container_get(Resource):
  def get(self, container_id):
    if container_id and container_id.isalnum():
      try:
        for container in docker_client.containers.list(all=True, filters={"id": container_id}):
示例#2
0
 def __init__(self, base_url):
     self.client = docker.DockerClient(base_url)
示例#3
0
 def dind_client(self, container):
     return docker.DockerClient('tcp://%s:%s' % (self.DIND_HOST, self.dind_port(container)),
                                version='auto')
示例#4
0
def build_mysqlClient(dockerAddr, sqlFilePath):
    print "{time} {fileName} {num} {func} {args} ".format(
        time=datetime.datetime.now(),
        fileName=os.path.basename(__file__),
        func=sys._getframe().f_code.co_name,
        num=sys._getframe().f_lineno,
        args="dockerAddr: {}, sqlFilePath: {}".format(dockerAddr, sqlFilePath))
    if not sqlFilePath:
        return False

    if not os.path.isfile(sqlFilePath):
        return False

    dockerfile = "Dockerfile"

    path = os.path.split(sqlFilePath)[0]
    fileName = os.path.split(sqlFilePath)[1]
    tag = "{}:{}".format(
        settings.MYSQLCLIENT_IMAGE_BASE.split(":")[0],
        fileName.split(".sql")[0])
    print "{time} {fileName} {num} {func} {args} ".format(
        time=datetime.datetime.now(),
        fileName=os.path.basename(__file__),
        func=sys._getframe().f_code.co_name,
        num=sys._getframe().f_lineno,
        args="path: {}, fileName: {}, tag:{} ".format(path, fileName, tag))
    if os.path.isfile("{}/Dockerfile".format(path)):
        os.remove("{}/Dockerfile".format(path))

    f = open("{}/Dockerfile".format(path), "w+")
    f.write(template.replace("template.sql", fileName))
    print "{time} {fileName} {num} {func} {args} ".format(
        time=datetime.datetime.now(),
        fileName=os.path.basename(__file__),
        func=sys._getframe().f_code.co_name,
        num=sys._getframe().f_lineno,
        args="f.read(): {}".format(f.read()))
    f.close()
    if dockerAddr:
        client = docker.DockerClient(base_url='tcp://{}'.format(dockerAddr),
                                     timeout=30,
                                     version='auto')
    else:
        client = docker.DockerClient(base_url='unix://var/run/docker.sock',
                                     version='auto')
    try:
        result = client.images.build(path=path, tag=tag, dockerfile=dockerfile)
        print "{time} {fileName} {num} {func} {args} ".format(
            time=datetime.datetime.now(),
            fileName=os.path.basename(__file__),
            func=sys._getframe().f_code.co_name,
            num=sys._getframe().f_lineno,
            args=" build result: {}".format(result))

        push_result = client.images.push(tag, auth_config=auth_config)
        print "{time} {fileName} {num} {func} {args} ".format(
            time=datetime.datetime.now(),
            fileName=os.path.basename(__file__),
            func=sys._getframe().f_code.co_name,
            num=sys._getframe().f_lineno,
            args=" push result: {}".format(push_result))

    except Exception as e:
        print "{time} {fileName} {num} {func} {args} ".format(
            time=datetime.datetime.now(),
            fileName=os.path.basename(__file__),
            func=sys._getframe().f_code.co_name,
            num=sys._getframe().f_lineno,
            args="Exception build & push image: %s\n" % e)
        return False

    print "{time} {fileName} {num} {func} {args} ".format(
        time=datetime.datetime.now(),
        fileName=os.path.basename(__file__),
        func=sys._getframe().f_code.co_name,
        num=sys._getframe().f_lineno,
        args="Successfully built image: %s" % tag)
    return True
示例#5
0
import docker

client = docker.DockerClient('unix://var/run/docker.sock')

container = client.containers.run("nginx", detach=True, ports={'80/tcp': 80})

for container in client.containers.list():
    print(container.name)

for image in client.images.list():
    print(image.tag)    
示例#6
0
 def __init__(self, host: str):
     self.client = docker.DockerClient(base_url=host)
示例#7
0
文件: app.py 项目: simhaonline/skep
 def docker(self):
     # Host Docker socket must be mounted in agent containers here:
     return docker.DockerClient(base_url='unix://var/run/docker.sock')
示例#8
0
def render_to_pdf(template_id, pdf_id, context=None):

    pdf_obj = Pdf.objects.get(id=pdf_id)

    if not context:
        context={}
    try:
        src = render_template(template_id, context)
    except Exception as e:
        pdf_obj.status = Pdf.ERROR
        pdf_obj.save()
        return {"err":repr(e),"context":context}
    try:
        import docker
        from docker.errors import ContainerError
        import socket
        import os
        from django.conf import settings
        import uuid
        from shutil import copyfile, rmtree

        client = docker.DockerClient(base_url='unix://var/run/docker.sock')
        this_container = client.containers.get(socket.gethostname())

        compose_proj = this_container.labels["com.docker.compose.project"]
        hostdir = [x["Source"] for x in this_container.attrs['Mounts'] if x["Destination"] == '/data/tex_share' and x["Type"] == "bind"][0]

        bdir = os.path.join(os.path.dirname(settings.BASE_DIR), 'tex_share')

        job = pdf_obj.task_id

        jobdir = os.path.join(bdir, "%s" % job)
        os.mkdir(jobdir)

        xi = client.images.get("%s_xelatex" % compose_proj)

        with open(os.path.join(jobdir, '%s.tex'%job), 'wb') as f:
            f.write(src.encode('UTF-8'))

        with open(os.path.join(jobdir,"make.sh"), 'w') as f:
            f.write("latexmk -xelatex -halt-on-error %s.tex\n"%job)
            f.write("echo $? > exit-code.int")

        os.mkdir(os.path.join(jobdir,"pdf"))

        for cpf in Template.objects.get(pk=template_id).files.all():
            #print(cpf.name)
            #print(cpf.name.split('/')[-1])
            #print(cpf.pure_name())
            destname = cpf.pure_name()
            if not destname.endswith(".pdf"):
                destname+=".pdf"
            with open(cpf.file.path,'rb') as fs, open(os.path.join(jobdir,"pdf",destname), 'wb') as fd:
                while True:
                    buf = fs.read(1024)
                    if buf:
                        n = fd.write(buf)
                    else:
                        break

        os.mkdir(os.path.join(jobdir, "flag"))

        for origin in Template.objects.get(pk=template_id).tournament.origin_set.all():
            if origin.flag_pdf:
                destname = origin.slug
                if not destname.endswith(".pdf"):
                    destname+=".pdf"
                with open(origin.flag_pdf.path,'rb') as fs, open(os.path.join(jobdir,"flag",destname), 'wb') as fd:
                    while True:
                        buf = fs.read(1024)
                        if buf:
                            n = fd.write(buf)
                        else:
                            break

        dfiles = {}
        for path, dirs, files in os.walk(jobdir):
            fs = []
            for f in files:
                fs.append(f)
            dfiles[path] = fs

        try:
            ct = client.containers.run(xi, '/bin/bash make.sh', working_dir="/data/src", detach=True, volumes={
                os.path.join(hostdir, "%s" % job): {'bind': '/data/src', 'mode': 'rw'}}, log_config={'type':'json-file'})
            ct.wait()
            err = ct.logs(stderr=True, stdout=False)
            out = ct.logs(stdout=True, stderr=False)
            #print("log output")
            #print(out)

        except ContainerError as e:

            pdf_obj.status = Pdf.ERROR
            pdf_obj.save()

            rmtree(jobdir)
            return {'err': repr(e), "context":context}

        try:
            ct.remove()
        except:
            pass

        try:
            with open(os.path.join(jobdir, 'exit-code.int'), 'rb') as f:
                exit_code = int(f.read().strip())
        except Exception as e:
            exit_code = 42

        if exit_code:
            pdf_obj.status = Pdf.FAILURE
            pdf_obj.save()
            rmtree(jobdir)
            return {'err': err.decode('UTF-8'), 'out': out.decode('UTF-8'), 'tex': src, "files":dfiles}

        with open(os.path.join(jobdir, '%s.pdf'%job), 'rb') as f:
            pdf = f.read()

            pdf_obj.file = ContentFile(pdf, pdf_obj.name)
            pdf_obj.status = Pdf.SUCCESS
            pdf_obj.save()

        rmtree(jobdir)
    except Exception as e:
        pdf_obj.status = Pdf.ERROR
        pdf_obj.save()

        return {'err': traceback.format_exc(), "context": context}
示例#9
0
from concurrent.futures import ThreadPoolExecutor
import docker
import random


def fun(message, client):
    container = client.containers.run("eon01/md5summer",
                                      environment=["var=%s" % message],
                                      detach=True)
    logs = container.logs()

    for line in container.logs(stream=True):
        print(line.strip())


client1 = docker.DockerClient(base_url='tcp://54.171.137.205:4243', tls=False)

messages = [
    "c4ca4238a0b923820dcc509a6f75849b", "c4ca4238a0b923820dcc509a6f758491",
    "c4ca4238a0b923820dcc509a6f75849c", "c4ca4238a0b923820dcc509a6f75849d",
    "c4ca4238a0b923820dcc509a6f75849b", "c4ca4238a0b923820dcc509a6f758491",
    "c4ca4238a0b923820dcc509a6f75849c", "c4ca4238a0b923820dcc509a6f75849d",
    "c4ca4238a0b923820dcc509a6f75849b", "c4ca4238a0b923820dcc509a6f758491",
    "c4ca4238a0b923820dcc509a6f75849c", "c4ca4238a0b923820dcc509a6f75849d"
]
pool = ThreadPoolExecutor(50)

for my_message in messages:
    future = pool.submit(fun, (my_message), client1)
示例#10
0
def run():
    form = ImageForm().validate_for_api()
    host = form.host.data
    image = form.image.data
    command = form.command.data
    name = form.name.data
    restart = form.restart.data
    links = form.links.data
    ports = form.ports.data
    volumes = form.volumes.data
    if links:
        links_dick = {}
        for link in links.split(','):
            links_dick[link.split(':')[0]] = link.split(':')[1]
        links = links_dick
    else:
        links = None

    if ports:
        ports_dick = {}
        for port in ports.split(','):
            ports_dick[port.split(':')[1]] = port.split(':')[0]
        ports = ports_dick
    else:
        ports = None

    if volumes:
        volumes_dick = {}
        for volume in volumes.split(','):
            volumes_dick[volume.split(':')[0]] = {'bind': volume.split(':')[1], 'mode': 'rw'}
        volumes = volumes_dick
    else:
        volumes = None

    if restart:
        restart_policy = {"Name": "always"}
    else:
        restart_policy = None

    certification = get_certification(host)
    # 判断是否鉴权
    if certification:
        cert,key = cert_file_path(host)
        tls_config = docker.tls.TLSConfig(client_cert=(cert, key),verify=False)
        client = docker.DockerClient(base_url='tcp://' + host + ':2376', tls=tls_config)
    else:
        client = docker.DockerClient(base_url='tcp://' + host + ':2375')
    try:
        container = client.containers.run(image, command, detach=True, name=name, links=links, ports=ports,
                                          restart_policy=restart_policy, volumes=volumes)
        id = container.short_id
        name = container.attrs['Name'][1:]
        container = {
            "id": id,
            "name": name
        }
        client.close()
        return jsonify(container)
    except Exception:
        client.close()
        return DockerRunFail()
示例#11
0
 def __init__(self, address=os.environ.get('DOCKER_ADDRESS')):
     self.client = docker.DockerClient(address, version='auto')
示例#12
0
 def new_client(self, user, addr):
     """
     Gets a new Docker client for a given address.
     """
     return docker.DockerClient(base_url=f'ssh://{user}@{addr}', )
示例#13
0
        result = new_result['cpu_stats']['system_cpu_usage'] - old_result[
            'cpu_stats']['system_cpu_usage']
    elif collect_item == 'cpu_percent':
        cpu_total_usage = new_result['cpu_stats']['cpu_usage'][
            'total_usage'] - old_result['cpu_stats']['cpu_usage']['total_usage']
        cpu_system_uasge = new_result['cpu_stats'][
            'system_cpu_usage'] - old_result['cpu_stats']['system_cpu_usage']
        cpu_num = len(old_result['cpu_stats']['cpu_usage']['percpu_usage'])
        result = round((float(cpu_total_usage) / float(cpu_system_uasge)) *
                       cpu_num * 100.0, 2)
    elif collect_item == 'mem_usage':
        result = new_result['memory_stats']['usage']
    elif collect_item == 'mem_limit':
        result = new_result['memory_stats']['limit']
    elif collect_item == 'network_rx_bytes':
        result = new_result['networks']['eth0']['rx_bytes']
    elif collect_item == 'network_tx_bytes':
        result = new_result['networks']['eth0']['tx_bytes']
    elif collect_item == 'mem_percent':
        mem_usage = new_result['memory_stats']['usage']
        mem_limit = new_result['memory_stats']['limit']
        result = round(float(mem_usage) / float(mem_limit) * 100.0, 2)
    return result


if __name__ == "__main__":
    docker_client = docker.DockerClient(base_url='unix://run/docker.sock',
                                        version='1.27')
    container_name = sys.argv[1]
    collect_item = sys.argv[2]
    print check_container_stats(container_name, collect_item)
示例#14
0
def create_container(self, name, image, nets=None, cmd=None, ports=[], volumes=[], environment=[], label=None):
    if self.host == '127.0.0.1':
        finalvolumes = {}
        if volumes is not None:
            for i, volume in enumerate(volumes):
                if isinstance(volume, str):
                    if len(volume.split(':')) == 2:
                        origin, destination = volume.split(':')
                        finalvolumes[origin] = {'bind': destination, 'mode': 'rw'}
                    else:
                        finalvolumes[volume] = {'bind': volume, 'mode': 'rw'}
                elif isinstance(volume, dict):
                    path = volume.get('path')
                    origin = volume.get('origin')
                    destination = volume.get('destination')
                    mode = volume.get('mode', 'rw')
                    if origin is None or destination is None:
                        if path is None:
                            continue
                        finalvolumes[origin] = {'bind': path, 'mode': mode}
                    else:
                        finalvolumes[origin] = {'bind': destination, 'mode': mode}
        if ports is not None:
            ports = {'%s/tcp' % k: k for k in ports}
        if label is not None and isinstance(label, str) and len(label.split('=')) == 2:
            key, value = label.split('=')
            labels = {key: value}
        else:
            labels = None
        base_url = 'unix://var/run/docker.sock'
        finalenv = {}
        if environment is not None:
            for env in enumerate(environment):
                if isinstance(env, str):
                    if len(env.split(':')) == 2:
                        key, value = env.split(':')
                        finalenv[key] = value
                    else:
                        continue
                elif isinstance(env, dict):
                    if len(env.keys()) == 1:
                        key = env.keys[0]
                        value = env[key]
                        finalenv[key] = value
                    else:
                        continue
        d = docker.DockerClient(base_url=base_url, version='1.22')
        if ':' not in image:
            image = '%s:latest' % image
        d.containers.run(image, name=name, command=cmd, detach=True, ports=ports, volumes=finalvolumes,
                         stdin_open=True, tty=True, labels=labels, environment=finalenv, stdout=True)
    else:
        portinfo = ''
        if ports is not None:
            for port in ports:
                if isinstance(port, int):
                    oriport = port
                    destport = port
                elif isinstance(port, str):
                    if len(port.split(':')) == 2:
                        oriport, destport = port.split(':')
                    else:
                        oriport = port
                        destport = port
                elif isinstance(port, dict) and 'origin' in port and 'destination' in port:
                    oriport = port['origin']
                    destport = port['destination']
                else:
                    continue
                portinfo = "%s -p %s:%s" % (portinfo, oriport, destport)
        volumeinfo = ''
        if volumes is not None:
            for volume in volumes:
                if isinstance(volume, str):
                    if len(volume.split(':')) == 2:
                        origin, destination = volume.split(':')
                    else:
                        origin = volume
                        destination = volume
                elif isinstance(volume, dict):
                    path = volume.get('path')
                    origin = volume.get('origin')
                    destination = volume.get('destination')
                    if origin is None or destination is None:
                        if path is None:
                            continue
                        origin = path
                        destination = path
                volumeinfo = "%s -v %s:%s" % (volumeinfo, origin, destination)
        envinfo = ''
        if environment is not None:
            for env in environment:
                if isinstance(env, str):
                    if len(env.split(':')) == 2:
                        key, value = env.split(':')
                    else:
                        continue
                elif isinstance(env, dict):
                    if len(env.keys()) == 1:
                        key = env.keys()[0]
                        value = env[key]
                    else:
                        continue
                envinfo = "%s -e %s=%s" % (envinfo, key, value)
        dockercommand = "docker run -it %s %s %s --name %s -l %s -d %s" % (volumeinfo, envinfo, portinfo, name, label,
                                                                           image)
        if cmd is not None:
            dockercommand = "%s %s" % (dockercommand, cmd)
        command = "ssh -p %s %s@%s %s" % (self.port, self.user, self.host, dockercommand)
        os.system(command)
    return {'result': 'success'}
示例#15
0
 def __init__(self) -> None:
     # self.client = docker.DockerClient(base_url='http://13.210.60.109:4243')
     self.client = docker.DockerClient(
         base_url='unix://var/run/docker.sock')
示例#16
0
def java_container():
    docker_compose = os.path.join(DOCKER_COMPOSE_PATH, 'docker_compose_mysql_java_client.yml')
    run_and_check(
        ['docker-compose', '--env-file', cluster.instances["node"].env_file, '-p', cluster.project_name, '-f', docker_compose, 'up', '--no-recreate', '-d', '--no-build'])
    yield docker.DockerClient(base_url='unix:///var/run/docker.sock', version=cluster.docker_api_version, timeout=600).containers.get(cluster.project_name + '_java1_1')
示例#17
0
def client():
    return docker.DockerClient()
示例#18
0
文件: swarm.py 项目: kallisti5/skep
 def __init__(self):
     socket_url = os.environ.get(
         'DOCKER_HOST',
         'unix://var/run/docker.sock'
     )
     self.client = docker.DockerClient(base_url=socket_url)
示例#19
0
 def __init__(self, config):
     super(RunJobAction, self).__init__(config)
     # TODO(dzimine): get URL from config
     self.client = docker.DockerClient(
         base_url='unix://var/run/docker.sock')
     self.pool_interval = 1
示例#20
0
def get_docker_client():
    if not get_docker_client._client:
        get_docker_client._client = docker.DockerClient(
            base_url='unix://var/run/docker.sock')
    return get_docker_client._client
示例#21
0
# api list workers
@app.route('/api/v1/worker/list', methods=['GET'])
def get_worker_list():
    pid_list = []
    for i in master_list:
        pid_list.append(p_client.inspect_container(i.name)['State']['Pid'])

    for i in slave_list:
        pid_list.append(p_client.inspect_container(i.name)['State']['Pid'])

    pid_list.sort()
    return make_response(str(pid_list), 200)


if __name__ == '__main__':
    client = docker.DockerClient(base_url='unix://var/run/docker.sock')
    p_client = docker.APIClient(base_url='unix://var/run/docker.sock')

    master_count += 1
    slave_count += 1

    container = client.containers.run(
        "workers:latest",
        detach=True,
        name="master_container" + str(master_count),
        network="orch-network",
        command=["sh", "-c", "service mongodb start; python3 worker.py 1"])

    master_list.append(container)

    container = client.containers.run(
示例#22
0
# import re
#
# pattern = '([0-9a-zA-Z]{8})-([0-9a-zA-Z]{4})-([0-9a-zA-Z]{4})-([0-9a-zA-Z]{4})-([0-9a-zA-Z]{12})'
# pattern2 = '([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}?)'
# string1 = 'bca8e3d6-5a54-11e7-addb-000c296cc8d9'
# string2 = 'bca481dd-5a54-11e7-addb-000c296cc8d9'
# string3 = '08e7fb09-83e7-11e7-95d2-000c293526b7'
# string4 = 'a39f47a6-5d32-11e7-bae2-000c2966be9f'
# a1 = re.match(pattern=pattern2,string=string3)
# print (a1)
import docker

client = docker.DockerClient(base_url='http://192.168.151.17:4243',
                             version='auto')
a1 = client.login(username='******',
                  password='******',
                  email='*****@*****.**',
                  registry='registry.allbright.local',
                  reauth=False)
print(a1)
import docker
DOCKER_CLIENT = docker.DockerClient(base_url='unix://var/run/docker.sock')
client = docker.from_env()
RUNNING = 'running'


def create_if_not_present(container_name):
    try:
        container = DOCKER_CLIENT.containers.get(container_name)
        container_state = container.attrs['State']
        container_is_running = container_state['Status'] == RUNNING
        return container_is_running
    except:
        client.containers.run("ubuntu:latest", "echo created container")
        #client.containers.run("bharadwaj1995/poc", detach=True)
        return True


my_container_name = "asdf"
print(create_if_not_present(my_container_name))
示例#24
0
# task 4
import docker
#1
base_url = input('URL to the Docker server in form "http://ip:port" : \n')
client = docker.DockerClient(base_url)
if not client:
    client = docker.from_env()
if client.containers.list('status=exited'):
    print ('Warning! There are dead or stoped containers: \n')
    cont = list(client.containers.list('status=exited'))
    for con in cont:
        print (con)
#2
print('All containers: \n')
allcont = list((client.containers.list(all=True)))
for allcon in allcont:
    print (allcon)
#3
print('Docker images: \n')
images = list(client.images.list())
for im in images:
    print (im)
#4
def inspect_func(): 
    cont_id = input('Container id: \n')
    if not cont_id:
        cont_id = 'a9488b0f59' #just for test on my machine
    print('Docker inspect: \n')
    print(client.api.inspect_container(cont_id))
inspect_func()
示例#25
0
文件: docker_es.py 项目: xwwy/xiaoxi
# _*_ coding: utf-8 _*_
"""
get data from es

"""
import json
import docker
from elasticsearch import Elasticsearch, RequestError

with open("D:\\APM\\Code\\xuyp\\docker\\docker_monitor\\config.json", "r") as files:
    CONFIG = json.load(files)
ES_URL = "{url}:{port}".format(url=CONFIG["es_ip"], port=CONFIG["es_port"])
DOCKER_URL = "{url}:{port}".format(url=CONFIG["docker_ip"], port=CONFIG["docker_port"])

CLIENT = docker.DockerClient(base_url='tcp://{url}'.format(url=DOCKER_URL))
ES = Elasticsearch([ES_URL])
MAP_SET = {
    "mappings": {
        "docker" : {
            "properties":{
                "container": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "cpu": {
                    "type": "float"
                },
                "mem": {
                    "type": "float"
                },
                "input": {
示例#26
0
def docker():
    return dockermod.DockerClient()
示例#27
0
import docker
import json
import click
import requests
import random

client = docker.DockerClient(base_url='tcp://127.0.0.1:2376')


@click.group()
def cli():
    pass


@cli.command('build')
@click.option("-p", "--path", default="./", help="Path to your Dockerfile")
@click.option("-t",
              "--tag",
              default="alpine-hello-world:latest",
              help="A tag to add to the final image")
def build(path, tag):
    """Bulding a container image."""
    print("*** BUILDING IS INITIATED")
    try:
        response = client.images.build(path=path, tag=tag, rm=True)
    except Exception as ex:
        template = "An exception of type {0} occured. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        click.echo(message)
    print("*** BUILD OUTPUT:")
    for object in response[1]:
示例#28
0
def client():
    return docker.DockerClient(base_url="tcp://127.0.0.1:2376")
示例#29
0
 def connect(self):
     client = docker.DockerClient(base_url=self._BASE_URL,
                                  version='auto',
                                  timeout=5)
     return client
示例#30
0
def getDockerOutsideClient():
    return docker.DockerClient(base_url='tcp://127.0.0.1:1234')