Пример #1
0
def get_container(username, project_name, service_name, container_name):
    url = database_update.container_ip(username, project_name, service_name, container_name)
    full_name = container_name + config.split_mark + project_name + config.split_mark + username

    cli = Client(url, config.c_version)
    con = Container.get_container_by_name(cli, full_name)

    srv_dict = {'name': container_name, 'ip': str(url).split(":")[0]}

    if con is None:
        srv_dict['status'] = 'not create'
        srv_dict['ports'] = '-'
        srv_dict['image'] = '-'
        srv_dict['create_time'] = '-'
        srv_dict['id'] = '-'
    else:
        srv_dict['status'] = con.status
        srv_dict['image'] = con.image
        srv_dict['create_time'] = con.create_time
        srv_dict['id'] = con.id
        if len(con.ports) == 0:
            srv_dict['shell'] = '-'
            ports = '-'
        else:
            ports = con.ports
            if '4200' in con.ports:
                srv_dict['shell'] = con.ports['4200']
                del ports['4200']

        srv_dict['ports'] = ports

    return srv_dict
Пример #2
0
def get_container_ip_port(username, project_name, service_name, container_name):
    ip = database_update.container_ip(username, project_name, service_name, container_name)
    port = database_update.container_port(username, project_name, service_name, container_name)
    client = Client(ip, config.c_version).client
    detail = client.inspect_container(container_name+config.split_mark+project_name+config.split_mark+username)
    host_ports = detail["NetworkSettings"]["Ports"][port][0]["HostPort"]
    return ip.split(":")[0] + ":" + host_port
Пример #3
0
def restart_container(username, project_name, service_name, container_name):
    ip = database_update.container_ip(username, project_name, service_name,
                                      container_name)
    client = Client(ip, config.c_version)
    full_name = container_name + config.split_mark + project_name + config.split_mark + username
    cont = Container.get_container_by_name(client, full_name)
    cont.restart()
Пример #4
0
def container_monitor(username, project_name, service_name, container_name):
    machine = database_update.container_ip(username, project_name, service_name, container_name)
    if machine is None:
        return 'no this project or service'

    full_name = container_name + config.split_mark + project_name + config.split_mark + username
    ip = machine.split(":")[0]
    url = 'http://' + ip + ":8080/api/v1.2/docker/" + full_name

    response = requests.get(url)

    if response.status_code == 500:
        return None

    true = True
    false = False

    di = eval(response.text)
    rel = []

    for item in di:
        for node in di[item]['stats']:
            dic = {'mem_usage': node['memory']['usage'],
                   'mem_total': di[item]['spec']['memory']['limit'],
                   'cpu_usage': node['cpu']['usage']['total'],
                   'cpu_total': di[item]['spec']['cpu']['limit'],
                   'timestamp': node['timestamp']}

            # file_usage = []
            # file_total = []
            # for files in node['filesystem']:
            #     file_usage.append(files['usage'])
            #     file_total.append(files['capacity'])
            #
            # dic['file_usage'] = file_usage
            # dic['file_total'] = file_total

            rel.append(dic)

    re = []

    for index in range(len(rel)):
        if not index == 0:
            pre = rel[index - 1]
            cur = rel[index]

            gap = time_gap(pre['timestamp'], cur['timestamp'])

            dic = {'timestamp': cur['timestamp'],
                   # 'file_usage': float(cur['file_usage'])/float(cur['file_total']),
                   # 'memory_usage': float(cur['memory_usage'])float(cur['memory_total']),
                   'mem_usage': float(cur['mem_usage']),
                   'cpu_usage': (float(cur['cpu_usage']) - float(pre['cpu_usage'])) / gap}

            re.append(dic)

    return re
Пример #5
0
def get_logs(username, project_name, service_name, container_name):
    cip = database_update.container_ip(username, project_name, service_name, container_name)

    print cip

    if cip == '-':
        return 'no such project or service'

    cli = Client(cip, config.c_version)
    full_name = container_name + config.split_mark + project_name + config.split_mark + username

    con = Container.get_container_by_name(cli, full_name)

    print con.id

    return con.client.logs(container=con.id, tail=100)
Пример #6
0
    def get_service_by_name(cls, username, project_name, service_name):
        service = Service(service_name, project_name, username)

        container_list = database_update.container_list(username, project_name, service_name)

        print 'container_list'
        print container_list

        for container_name in container_list:
            full_name = container_name[0] + config.split_mark + project_name + config.split_mark + username
            ip = database_update.container_ip(username, project_name, service_name, container_name[0])
            client = Client(ip, config.c_version)
            cont = Container.get_container_by_name(client, full_name)
            service.containers.append(cont)

        return service
Пример #7
0
def container_resource_usage(username, project_name, service_name,
                             container_name):
    machine = database_update.container_ip(username, project_name,
                                           service_name, container_name)
    if machine is None:
        return 'no this project or service'

    full_name = container_name + config.split_mark + project_name + config.split_mark + username
    ip = machine.split(":")[0]
    url = 'http://' + ip + ":8080/api/v1.2/docker/" + full_name

    response = requests.get(url)

    if response.status_code == 500:
        return None

    true = True
    false = False

    di = eval(response.text)
    rel = []

    for item in di:
        for node in di[item]['stats']:
            dic = {
                'mem_usage': node['memory']['usage'],
                'mem_total': di[item]['spec']['memory']['limit'],
                'cpu_usage': node['cpu']['usage']['total'],
                'cpu_total': di[item]['spec']['cpu']['limit'],
                'timestamp': node['timestamp']
            }

            rel.append(dic)

    if len(rel) < 2:
        return None

    gap = time_gap(rel[-2]['timestamp'], rel[-1]['timestamp'])

    dic = {
        "mem_usage":
        float(rel[-1]["mem_usage"] / rel[-1]["mem_total"]),
        "cpu_usage":
        (float(rel[-1]['cpu_usage']) - float(rel[-2]['cpu_usage'])) / gap
    }

    return dic
Пример #8
0
    def get_service_by_name(cls, username, project_name, service_name):
        service = Service(service_name, project_name, username)

        container_list = database_update.container_list(
            username, project_name, service_name)

        print 'container_list'
        print container_list

        for container_name in container_list:
            full_name = container_name[
                0] + config.split_mark + project_name + config.split_mark + username
            ip = database_update.container_ip(username, project_name,
                                              service_name, container_name[0])
            client = Client(ip, config.c_version)
            cont = Container.get_container_by_name(client, full_name)
            service.containers.append(cont)

        return service
Пример #9
0
def scale_down(username, project_name, service_name):
    service_scale, service_config = database_update.get_service_scale_config(username, project_name, service_name)

    service_scale = int(service_scale) - 1

    if service_scale < 1:
        return

    container_name = service_name + "_" + str(service_scale)
    full_name = container_name + config.split_mark + project_name + config.split_mark + username

    ip = database_update.container_ip(username, project_name, service_name, container_name)
    client = Client(ip, config.c_version)

    cont = Container.get_container_by_name(client, full_name)

    print ip, cont.name

    cont.stop()
    cont.remove()

    database_update.delete_container_by_name(username, project_name, service_name, container_name)
    database_update.set_service_scale(username, project_name, service_name, service_scale)
Пример #10
0
# if __name__ == '__main__':
# ip_list = ['114.212.189.147:2376', '114.212.189.140:2376']
# database_update.create_machine(ip_list)
# database_update.create_user('test', '*****@*****.**')
# database_update.create_project()
database_update.create_project('test', 'project', '*****@*****.**')
# print database_update.get_machine()
print database_update.project_list('test', 0, 2)
print database_update.project_exists('test', 'project')
print database_update.project_exists('test', 'projects')
database_update.create_service('test', 'project', 'service', config, 2)
print database_update.service_list('test', 'project')
database_update.create_container('test', 'project', 'service', 'service0',
                                 '114.212.189.147:2376')
print database_update.container_list('test', 'project', 'service')
print database_update.container_ip('test', 'project', 'service', 'service0')
# print database_update.delete_container_by_name('test', 'project', 'service', 'service0')
print database_update.container_list('test', 'project', 'service')
# print database_update.service_list('test', 'project')
# print database_update.project_list('test', 0, 2)
# database_update.delete_project('test', 'project')
print database_update.project_list('test', 0, 10)

# db = MySQLdb.connect(config.database_url, config.database_user, config.database_passwd, config.database)
# cursor = db.cursor()
# cursor.execute("select name from user")
#
# data = cursor.fetchall()
# for item in data:
#     print item[0]
#
Пример #11
0
config = {'name': 'hello', 'scale': 2}
# if __name__ == '__main__':
    # ip_list = ['114.212.189.147:2376', '114.212.189.140:2376']
    # database_update.create_machine(ip_list)
    # database_update.create_user('test', '*****@*****.**')
    # database_update.create_project()
database_update.create_project('test', 'project', '*****@*****.**')
    # print database_update.get_machine()
print database_update.project_list('test', 0, 2)
print database_update.project_exists('test', 'project')
print database_update.project_exists('test', 'projects')
database_update.create_service('test', 'project', 'service', config, 2)
print database_update.service_list('test', 'project')
database_update.create_container('test', 'project', 'service', 'service0', '114.212.189.147:2376')
print database_update.container_list('test', 'project', 'service')
print database_update.container_ip('test', 'project', 'service', 'service0')
# print database_update.delete_container_by_name('test', 'project', 'service', 'service0')
print database_update.container_list('test', 'project', 'service')
    # print database_update.service_list('test', 'project')
    # print database_update.project_list('test', 0, 2)
# database_update.delete_project('test', 'project')
print database_update.project_list('test', 0, 10)

# db = MySQLdb.connect(config.database_url, config.database_user, config.database_passwd, config.database)
# cursor = db.cursor()
# cursor.execute("select name from user")
#
# data = cursor.fetchall()
# for item in data:
#     print item[0]
#