Beispiel #1
0
def load_image_file(node, files_name, username):
    """
    Load the image file to the docker mirrored repository.
    :param node: Node ip.
    :param files_name: Filename list.
    :param username: The current login user name.
    :return: There is no return value.
    """
    Message.write_message('开始加载镜像文件,清稍候', username)
    connect_node = Tools.get_connect_node()
    connect_node.bool_flush = False
    while connect_node.flush_status:
        pass
    node_info = connect_node.get_ip_attr(node, 'info')
    exec_status = []
    cmd = 'docker load < {path}/{filename}'
    path = connect_node.get_ip_attr(node, 'dir')
    for file_name in files_name:
        load_cmd = cmd.format(path=path, filename=file_name)
        # exec load cmd
        result = connect_node.cmd(node_info, load_cmd)
        exec_status.append(result[1])
    success_num = len([x for x in exec_status if x == 'success'])
    fail_num = len(exec_status) - success_num
    message_info = '镜像加载完成:%d成功 %d失败' % (success_num, fail_num)
    if 'defeated' in exec_status:
        Message.write_message(message_info, username, grade='danger')
    else:
        Message.write_message(message_info, username)
        Event.write_event(username, '向{node}加载了 {number_file} 个镜像文件'.format(node=node, number_file=success_num),
                          datetime.datetime.now())
    connect_node.bool_flush = True
Beispiel #2
0
def send_files_download(node, files_name, username):
    global tar_export_status
    Message.write_message('开始打包镜像文件,请等待', username)
    connect_node = Tools.get_connect_node()
    connect_node.bool_flush = False
    while connect_node.flush_status:
        pass
    master_ip = Sys.query.filter().first().master_node
    master_ip_info = connect_node.get_ip_attr(master_ip, 'info')
    node_username = connect_node.get_ip_attr(node, 'username')
    sou_path = connect_node.get_ip_attr(node, 'dir')
    exec_status = []
    send_cmd = 'scp -r {username}@{node_ip}:{sou_path}/{file} {local_path}'
    for file_name in files_name:
        cmd = send_cmd.format(username=node_username,
                              node_ip=node,
                              sou_path=sou_path,
                              file=file_name,
                              local_path=DOWNLOAD_FOLDER)
        # exist error
        # result = connect_node.cmd(master_ip_info, cmd)
        # exec_status.append(result[1])
        status, _ = commands.getstatusoutput(cmd)
        if status == 0:
            status = 'success'
        else:
            status = 'defeated'
        exec_status.append(status)
    print os.listdir(DOWNLOAD_FOLDER)
    with tarfile.open('Download.tar', 'w') as tar:
        for root, dir_, files in os.walk(DOWNLOAD_FOLDER):
            for file_ in files:
                print file_
                full = os.path.join(root, file_)
                tar.add(full, arcname=file_)
    connect_node.bool_flush = True
    success_num = len([x for x in exec_status if x == 'success'])
    fail_num = len(exec_status) - success_num
    message_info = '镜像打包完成:%d成功 %d失败' % (success_num, fail_num)
    if 'defeated' in exec_status:
        Message.write_message(message_info, username, grade='danger')
    else:
        Message.write_message(message_info, username)
        Event.write_event(
            username,
            '从{node}导出了 {number_file} 个镜像文件'.format(node=node,
                                                    number_file=success_num),
            datetime.datetime.now())
    tar_export_status = True
Beispiel #3
0
def rm_tar_file(ip, files_name, username):
    """
    删除镜像文件
    :param ip:
    :param files_name:
    :param username:
    :return:
    """
    Message.write_message('开始删除镜像文件,清稍候', username)
    connect_node = Tools.get_connect_node()
    connect_node.bool_flush = False
    while connect_node.flush_status:
        pass
    exec_status = []
    cmd = 'rm -rf {path}/{file_name}'
    path = connect_node.get_ip_attr(ip, 'dir')
    # 删除
    for file_name in files_name:
        # 若镜像名中存在'/',则进行转义
        if '/' in file_name:
            file_name = file_name.replace('/', '\/')
        rm_cmd = cmd.format(path=path, file_name=file_name)
        # exec rm cmd
        result = connect_node.cmd(ip, rm_cmd)
        exec_status.append(result[1])
    # 统计执行结果
    success_num = len([x for x in exec_status if x == 'success'])
    fail_num = len(exec_status) - success_num
    message_info = '镜像删除完成:%d成功 %d失败' % (success_num, fail_num)
    if 'defeated' in exec_status:
        Message.write_message(message_info, username, grade='danger')
    else:
        Message.write_message(message_info, username)
        Event.write_event(
            username,
            '从{ip}删除了 {number_file} 个镜像文件'.format(ip=ip,
                                                  number_file=success_num),
            datetime.datetime.now())
    connect_node.bool_flush = True
Beispiel #4
0
def import_image_file(import_type, node_ip_list, files_name, username):
    """
    Sends the specified file to the specified IP and returns the execution in each node.
    :param node_ip_list: The node IP list to be sent, such as [10.42.0.41, ...]
    :param import_type: The request type.Value as 'port' or 'local'
    :param files_name: File name list
    :return: The execution of each node, such as ['success', 'defeated']
    """
    if import_type == 'port':
        Message.write_message('主节点镜像开始导入', username)
    else:
        Message.write_message('本地镜像开始导入', username)
    connect_node = Tools.get_connect_node()
    connect_node.bool_flush = False
    while connect_node.flush_status:
        pass
    master_ip = Sys.query.filter().first().master_node
    if import_type == 'port':
        sou_path = connect_node.get_ip_attr(master_ip, 'dir')
    else:
        sou_path = app.config['UPLOAD_FOLDER']
    exec_status = []
    # eg: scp -r /path/test.tar [email protected]:/path
    import_master_cmd = 'scp -r {sou_path}/{file} ' \
                        '{node_username}@{node_ip}:{node_ip_path}/'
    for node in node_ip_list:
        node_username = connect_node.get_ip_attr(node, 'username')
        node_ip_path = connect_node.get_ip_attr(node, 'dir')
        for file_name in files_name:
            # 转移'/'
            if '/' in file_name:
                file_name = file_name.replace('/', '_')
            cmd = import_master_cmd.format(sou_path=sou_path,
                                           file=file_name,
                                           node_username=node_username,
                                           node_ip_path=node_ip_path,
                                           node_ip=node)
            print cmd
            result = connect_node.cmd(master_ip, cmd)
            exec_status.append(result[1])
    connect_node.bool_flush = True
    if 'defeated' in exec_status:
        grade = 'danger'
    else:
        grade = 'success'
    if import_type == 'port':
        if grade == 'success':
            Message.write_message('主节点镜像导入成功', username)
            Event.write_event(
                username,
                '从主节点导入了 {number_file} 个镜像文件到 {number_node} 个节点'.format(
                    number_file=len(files_name),
                    number_node=len(node_ip_list)), datetime.datetime.now())
        else:
            Message.write_message('主节点镜像导入失败', username, grade=grade)
    else:
        if grade == 'success':
            Message.write_message('本地镜像导入成功', username)
            Event.write_event(
                username,
                '从本地导入了 {number_file} 个镜像文件到 {number_node} 个节点'.format(
                    number_file=len(files_name),
                    number_node=len(node_ip_list)), datetime.datetime.now())
        else:
            Message.write_message('本次镜像导入失败', username, grade=grade)
Beispiel #5
0
def rm_image_file(ip, files_name, files_status, username):
    """删除Docker镜像
    基本思路:
    1. 判断该镜像是否正在被使用
    2. 若该镜像正在被占用,先找出占用该镜像的容器并删除该容器
    3. 删除该镜像
    :param ip:
    :param files_name:
    :param files_status:
    :param username:
    :return:
    """
    # Message.write_message('开始删除Docker镜像,清稍候', username)
    connect_node = Tools.get_connect_node()
    connect_node.bool_flush = False
    while connect_node.flush_status:
        pass
    exec_status = []
    # 获取容器id
    get_container_id = "docker ps -a|awk '/{image_name}/ {{print $1}}'"
    # 停止并删除容器命令
    rm_container_cmd = 'docker kill {container_id} & docker rm {container_id}'
    # 删除镜像
    rm_image_cmd = 'docker rmi {docker_image}'
    # 删除
    for index in xrange(len(files_name)):
        # 若镜像中存在'/',则进行转义
        if '/' in files_name[index]:
            files_name[index] = files_name[index].replace('/', '\/')
        image_name = files_name[index].split(':')[0]
        image_tag = files_name[index].split(':')[1]
        exec_rm_image_cmd = rm_image_cmd.format(docker_image=files_name[index])
        # 如果镜像名为空,则拒绝操作,防止筛选出全部容器
        if len(image_name) == 0:
            return
        # 若镜像未被使用
        if files_status[index] == 'NoUse':
            result = connect_node.cmd(ip, exec_rm_image_cmd)
            exec_status.append(result[1])
        else:
            exec_tag_container_id = get_container_id.format(
                image_name=files_name[index])
            exec_no_tag_container_id = get_container_id.format(
                image_name=image_name)
            # 获取id时,首先通过镜像全名获取,若结果为空且本镜像的tag为latest,则以镜像名重新获取一遍
            container_id = connect_node.cmd(ip, exec_tag_container_id)
            container_id_list = container_id[2][0].readlines()
            if len(container_id_list) == 0:
                if image_tag == 'latest':
                    container_id = connect_node.cmd(ip,
                                                    exec_no_tag_container_id)
                    container_id_list = container_id[2][0].readlines()
            print container_id_list
            # 删除容器
            for container in container_id_list:
                container_id = container.split('\n')[0]
                exec_rm_container_cmd = rm_container_cmd.format(
                    container_id=container_id)
                connect_node.cmd(ip, exec_rm_container_cmd)
            # 删除镜像
            result = connect_node.cmd(ip, exec_rm_image_cmd)
            exec_status.append(result[1])
    # 统计执行结果
    success_num = len([x for x in exec_status if x == 'success'])
    fail_num = len(exec_status) - success_num
    message_info = '镜像删除完成:%d成功 %d失败' % (success_num, fail_num)
    if 'defeated' in exec_status:
        Message.write_message(message_info, username, grade='danger')
    else:
        Message.write_message(message_info, username)
        Event.write_event(
            username,
            '从{ip}删除了 {number_file} 个Docker镜像'.format(ip=ip,
                                                      number_file=success_num),
            datetime.datetime.now())
    connect_node.bool_flush = True