Exemplo n.º 1
0
def execute(cid, username, cmds):
    """在容器所在 Docker 主机上执行命令

    :param str cid: The container uuid
    :param list cmds: List of commands to execute
    """

    # 获取容器的详细信息
    s_inspect, m_inspect, r_inspect = inspect.inspect(cid)
    if s_inspect != 200:
        return (s_inspect, m_inspect, r_inspect)

    # 判断容器是否在当前主机上
    node = r_inspect["Node"]['IP']

    if node == NODE_IP:
        return execute_extend(cid, username, cmds)
    else:
        params = {
            "action": "Extend:HostExecuteExtend",
            "params": {
                "cid": cid,
                "cmds": cmds,
                "username": username
            }
        }
        r = requests.post(url="http://%s:8000/api/v1" % node,
                          headers=TOKEN_HEADERS,
                          data=json.dumps(params))
        s = r.status_code
        if s != 200:
            return (s, r.text, "")
        return (s, r.json()["message"], r.json()["data"])
Exemplo n.º 2
0
def delete(username, cid, reset=False):
    """删除容器

    在容器所在主机上删除 Redis 中的 URL 地址, 卸载用户容器存储, unmap rbd image

    :param str username: Fuvism user name
    :param str cid: The container uuid
    :param bool reset:
        reset is true, clear container data in ceph rbd
    """

    # 获取容器的详细信息
    s_inspect, m_inspect, r_inspect = inspect.inspect(cid)
    if s_inspect != 200:
        return (s_inspect, m_inspect, r_inspect)

    # 判断容器是否在当前主机上
    node = r_inspect["Node"]["IP"]

    if node == NODE_IP:
        return delete_extend(username, cid, reset)
    else:
        params = {"action": "Extend:ContainerDeleteExtend",
                  "params": {"cid": cid, "reset": reset, "username": username}}
        r = requests.post(url="http://%s:8000/api/v1" % node,
                          headers=TOKEN_HEADERS,
                          data=json.dumps(params))
        s = r.status_code
        if s != 200:
            return (s, r.text, "")
        return (s, r.json()["message"], r.json()["data"])
Exemplo n.º 3
0
def write_files(files, username, cid=None):
    """为 Docker 主机或容器里写入文件内

    :param dict files:
        e.g.: {"/etc/hosts": "",
               "/tmp/test.txt": "the test.txt default content."}
    :param str or None cid: The container uuid
        cid is str: 为容器里的文件写入内容
        cid is None: 为当前主机的文件写入内容
    """

    if cid:
        # 获取容器的详细信息
        s_inspect, m_inspect, r_inspect = inspect.inspect(cid)
        if s_inspect != 200:
            return (s_inspect, m_inspect, r_inspect)

        # 判断容器是否在当前主机上
        pid = r_inspect['State']['Pid']
        node = r_inspect["Node"]['IP']

        if node == NODE_IP:
            return write_files_extend(files, username, pid)
        else:
            params = {
                "action": "Extend:WriteFilesExtend",
                "params": {
                    "pid": pid,
                    "files": files,
                    "username": username
                }
            }
            r = requests.post(url="http://%s:8000/api/v1" % node,
                              headers=TOKEN_HEADERS,
                              data=json.dumps(params))
            s = r.status_code
            if s != 200:
                return (s, r.text, "")
            return (s, r.json()["message"], r.json()["data"])
    else:
        return write_files_extend(files)
Exemplo n.º 4
0
def fd_check(cid, fds):
    """在容器所在 Docker 主机上检测文件、目录是否创建

    :param str cid: The container uuid
    :param list fds: 要检测的数据结构
        e.g.: [
            {"type": "dir", "name": "/some/dirname"},
            {"type": "file", "name": "/some/filename"}
        ]
    """

    # 获取容器的详细信息
    s_inspect, m_inspect, r_inspect = inspect.inspect(cid)
    if s_inspect != 200:
        return (s_inspect, m_inspect, r_inspect)

    # 判断容器是否在当前主机上
    pid = r_inspect["State"]["Pid"]
    node = r_inspect["Node"]["IP"]

    if node == NODE_IP:
        return fd_check_extend(cid, fds, pid)
    else:
        params = {
            "action": "Extend:HostFdCheckExtend",
            "params": {
                "cid": cid,
                "fds": fds,
                "pid": pid
            }
        }
        r = requests.post(url="http://%s:8000/api/v1" % node,
                          headers=TOKEN_HEADERS,
                          data=json.dumps(params))
        s = r.status_code
        if s != 200:
            return (s, r.text, "")
        return (s, r.json()["message"], r.json()["data"])