Esempio n. 1
0
def docker_run(sid: uuid.UUID):
    if request.method != "POST":
        return json_result(-1, "POST only")

    sid = str(sid)
    tag = request.form["tag"]
    sshport = int(request.form["sshport"])
    uid = session.get("uuid")

    db: wrappers.Collection = mongo.db.images
    image: Image = deserialize_json(Image,
                                    db.find_one({
                                        "uid": uid,
                                        "uuid": sid
                                    }))
    if not image:
        return json_result(-1, "Docker::Images::rmi failed")

    container_id = DockerImageAPI.run(tag, "",
                                      sshport)  # image.run(tag, port=sshport)
    container_uuid = str(uuid.uuid4())
    container = Container(uid, tag, "start", sid, sshport, container_id,
                          container_uuid)
    db: wrappers.Collection = mongo.db.containers
    db.insert_one(container.__dict__)

    DockerContainerAPI.start(container)
    docker_daemon.notify(container_id)

    return json_result(0, "Successfully run")
Esempio n. 2
0
def api_request_auth():
    username = request.form["username"]
    password = request.form["password"]
    result = authapi.signin(username, password)
    if result == "":
        return json_result(-1, "login failed")
    return json_result(0, result)
Esempio n. 3
0
def api_image_run(sid: uuid.UUID, port: int):
    user: User = xtoken_user(AuthAPI.getXToken())

    tag = randomString(15)
    db: wrappers.Collection = mongo.db.images
    image: Image = deserialize_json(
        Image, db.find_one({
            "uid": user.uuid,
            "uuid": str(sid)
        }))

    try:
        container_id = DockerImageAPI.run(image.tag, "", port)
        container_uuid = str(uuid.uuid4())
        container = Container(user.uuid, tag, "start", str(sid), port,
                              container_id, container_uuid)
        db: wrappers.Collection = mongo.db.containers
        db.insert_one(container.__dict__)

        DockerContainerAPI.start(container)
        docker_daemon.notify(container_id)

        return json_result(0, "image run")
    except:
        return json_result(-1, "image not found")
Esempio n. 4
0
def docker_rm(sid: uuid.UUID):
    sid = str(sid)
    uid = session.get("uuid")
    db: wrappers.Collection = mongo.db.containers
    container: Container = deserialize_json(
        Container, db.find_one({
            "uid": uid,
            "uuid": sid
        }))
    if not container:
        return render_template("fail.html")

    status = container.status
    container.status = "removing"
    db.update({"uid": uid, "uuid": sid}, container.__dict__)

    try:
        DockerContainerAPI.remove(container)
        docker_daemon.notify_remove(container.short_id)
    except:
        container.status = status
        db.update({"uid": uid, "uuid": sid}, container.__dict__)
        return json_result(-1, "Docker::Container::remove failed")

    return json_result(0, "Successfully container removed")
Esempio n. 5
0
def docker_rmi(sid: uuid.UUID):
    """
    :param uid: user uuid
    :param sid: image uuid
    """
    sid = str(sid)
    uid: uuid.UUID = session.get("uuid")
    db: wrappers.Collection = mongo.db.images
    image: Image = deserialize_json(Image,
                                    db.find_one({
                                        "uid": uid,
                                        "uuid": sid
                                    }))
    if not image:
        return json_result(-1, "Docker::Images::rmi failed")

    ct: wrappers.Collection = mongo.db.containers
    containers: list = deserialize_json(Container, ct.find({"image": sid}))
    for item in containers:
        container: Container = item
        status = docker_status(container.uuid)
        if status != "stop":
            return json_result(
                -1, "Docker::Images::rmi failed(container is alive)")

    # delete image
    DockerImageAPI.delete(image.tag)
    return json_result(0, "Successfully image removed")
Esempio n. 6
0
def api_images():
    user = xtoken_user(AuthAPI.getXToken())
    img_db: wrappers.Collection = mongo.db.images
    image: list = deserialize_json(Image, img_db.find({"uid": user.uuid}))
    if len(image) == 0:
        return json_result(-1, "image not found")
    return json_result(0, image)
Esempio n. 7
0
    def cp(self, src, dst):
        if "../" in src or "../" in dst:
            return json_result(-1, "invalid path")
        if not os.path.exists(src):
            return json_result(-1, "invalid path")

        shutil.copy(src, dst)
        return json_result(0, "success")
Esempio n. 8
0
def admin_users_setlevel(sid: uuid.UUID, level: int):
    if level not in User.LEVELS:
        return json_result(-1, "invalid level")

    user: User = user_api.find_by_uuid(sid)
    user.level = level
    user_api.update_by_uuid(sid, user)
    return json_result(0, "success")
Esempio n. 9
0
def api_containers():
    user = xtoken_user(AuthAPI.getXToken())
    con_db: wrappers.Collection = mongo.db.containers
    container: list = deserialize_json(Container,
                                       con_db.find({"uid": user.uuid}))
    if len(container) == 0:
        return json_result(-1, "container not found")
    return json_result(0, container)
Esempio n. 10
0
    def rmdir(self, username, path):
        path = self.base + username + "/" + path
        if "../" in path:
            return json_result(-1, "invalid path")
        if not os.path.exists(path):
            return json_result(-1, "path not exist")

        os.removedirs(path)
        return json_result(0, "success")
Esempio n. 11
0
def admin_images_remove(tag: str):
    idx = tag.rfind("-")
    tag = tag[:idx] + ":" + tag[idx + 1:]
    image: Image = docker_api.image.find_by_tag(tag)
    if image == None:
        return json_result(-1, "image not exists")

    docker_api.image.delete(image.tag)
    return json_result(0, "success")
Esempio n. 12
0
    def rm(self, path):
        if "../" in path:
            return json_result(-1, "invalid path")
        if not os.path.exists(path):
            return json_result(-1, "file not exist")
        if not os.path.isfile(path):
            return json_result(-1, "invalid type")

        os.remove(path)
        return json_result(0, "success")
Esempio n. 13
0
def userfile_upload():
    uid = session.get("uuid")
    username = session.get("username")
    path = request.form["path"]

    if "../" in path:
        return json_result(-1, "invalid path")

    f = request.files["file"]
    fn = userfile_base + username + "/" + path + "/" + f.filename
    f.save(fn)
    return json_result(0, "success")
Esempio n. 14
0
def api_image_rm(sid):
    user: User = xtoken_user(AuthAPI.getXToken())
    db: wrappers.Collection = mongo.db.images
    image: Image = deserialize_json(
        Image, db.find_one({
            "uid": user.uuid,
            "uuid": str(sid)
        }))
    try:
        DockerImageAPI.delete(image.tag)
        return json_result(0, "image removed")
    except:
        return json_result(-1, "image not found")
Esempio n. 15
0
def api_container_rm(sid):
    user: User = xtoken_user(AuthAPI.getXToken())
    db: wrappers.Collection = mongo.db.containers
    container: Container = deserialize_json(
        Container, db.find_one({
            "uid": user.uuid,
            "uuid": str(sid)
        }))
    try:
        DockerContainerAPI.remove(container)
        return json_result(0, "container removed")
    except:
        return json_result(-1, "container not found")
Esempio n. 16
0
def admin_users_add():
    username = request.form["username"]
    password = request.form["password"]
    level = int(request.form["level"])

    if "../" in username:
        return json_result(0, "invalid username")

    result = user_api.find_by_name(username)
    if result != None:
        return json_result(-1, "exist user")

    u_uuid = user_api.add(username, password, level)

    return json_result(0, u_uuid)
Esempio n. 17
0
 def read(self, username, path):
     rpath = self.base + username + "/" + path
     if not os.path.exists(rpath):
         return False
     with open(rpath, "rb") as f:
         result = f.read()
     return json_result(0, base64.b64encode(result).decode())
Esempio n. 18
0
    def purchase(self, paypal: Paypal, amount: int):
        if amount <= 0:
            return json_result(-1, "invalid amount")

        db: wrappers.Collection = mongo.db.paypal
        db.insert_one(paypal.__dict__)

        cdb: wrappers.Collection = mongo.db.credit
        result = cdb.find_one({"username": session["username"]})
        if not result:
            result = {"username": session["username"], "credit": 0}
            cdb.insert_one(result)

        result["credit"] += amount
        cdb.update({"username": session["username"]}, result)
        return json_result(0, "success")
Esempio n. 19
0
 def write(self, username, path, data):
     rpath = self.base + username + "/" + path
     if not os.path.exists(rpath):
         return False
     f = open(rpath, "wb")
     f.write(data)
     f.close()
     return json_result(0, "success")
Esempio n. 20
0
def api_image_detail(sid: uuid.UUID):
    user: User = xtoken_user(AuthAPI.getXToken())
    db: wrappers.Collection = mongo.db.images
    image: Image = deserialize_json(
        Image, db.find_one({
            "uid": user.uuid,
            "uuid": str(sid)
        }))
    return json_result(0, image)
Esempio n. 21
0
def api_container_detail(sid: uuid.UUID):
    user: User = xtoken_user(AuthAPI.getXToken())
    db: wrappers.Collection = mongo.db.images
    container: Container = deserialize_json(
        Container, db.find_one({
            "uid": user.uuid,
            "uuid": str(sid)
        }))
    return json_result(0, container)
Esempio n. 22
0
def admin_users_update(sid: uuid.UUID):
    password = request.form["password"]
    level = request.form["level"]

    user: User = user_api.find_by_uuid(sid)
    if user == None:
        return json_result(-1, "user not found")

    user.password = password
    try:
        user.level = int(level)
    except:
        return json_result(-1, "level is not integer")
    if user.level not in User.LEVELS:
        return json_result(-1, "invalid level")

    user_api.update_by_uuid(sid, user)
    return json_result(0, "success")
Esempio n. 23
0
def docker_stop(sid: uuid.UUID):
    sid = str(sid)
    uid = session.get("uuid")
    db: wrappers.Collection = mongo.db.containers
    container: Container = deserialize_json(
        Container, db.find_one({
            "uid": uid,
            "uuid": sid
        }))
    if not container:
        return json_result(-1, "container not found")

    # container stop & check
    try:
        DockerContainerAPI.stop(container)
    except:
        return json_result(-1, "Docker::Container::stop failed")

    return json_result(0, "container stop")
Esempio n. 24
0
def docker_start(sid: uuid.UUID):
    sid = str(sid)
    uid = session.get("uuid")
    db: wrappers.Collection = mongo.db.containers
    container: Container = deserialize_json(
        Container, db.find_one({
            "uid": uid,
            "uuid": sid
        }))
    if not container:
        return json_result(-1, "container not found")

    # container start & check
    try:
        DockerContainerAPI.start(container)
        return json_result(0, "container start")
    except:
        container.status = "failed"
        db.update({"uid": uid, "uuid": sid}, container.__dict__)

    return json_result(-1, "Docker::Container::start failed")
Esempio n. 25
0
 def listing(self, username, path):
     result = {
         "is_base": True,
         "path": "/file/" + path,
         "dir": [],
         "file": []
     }
     for f in glob.glob(self.base + username + "/" + path + "/*"):
         if os.path.isdir(f):
             result["dir"] += [os.path.basename(f)]
         else:
             result["file"] += [os.path.basename(f)]
     return json_result(0, result)
Esempio n. 26
0
def docker_images_search():
    tag = request.form["tag"]
    uid = session.get("uuid")
    db: wrappers.Collection = mongo.db.images
    result: list = deserialize_json(
        Image, db.find({
            "uid": uid,
            "tag": {
                "$regex": tag
            }
        }))
    r = []
    for image in result:
        r += [image.__dict__]
    return json_result(0, r)
Esempio n. 27
0
def docker_containers_search():
    tag = request.form["tag"]
    uid = session.get("uuid")
    db: wrappers.Collection = mongo.db.containers
    result: list = deserialize_json(
        Container, db.find({
            "uid": uid,
            "tag": {
                "$regex": tag
            }
        }))
    r = []
    for container in result:
        r += [container.__dict__]

    return json_result(0, result)
Esempio n. 28
0
def docker_build():
    """
    GET
    :param uid: user uuid

    POST
    :param tag: docker tag
    :parma dockfile: dockerfile uuid
    :param rootpass: root password for ssh
    :param sshport: ssh port forwarding

    build Dockerfile
    """
    if request.method != "POST":
        return json_result(-1, "POST only")

    uid = session.get("uuid")
    username = session.get("username")
    tag = request.form["tag"]
    dockfile = request.form["dockfile"]
    rootpass = request.form["rootpass"]
    sshport = int(request.form["sshport"])

    fn = "upload/{}/{}/Dockerfile".format(username, dockfile)
    with open(fn, "r") as f:
        df = f.read()

    name = tag.split(":")[0]
    ver = "latest"
    if len(tag.split(":")) == 1:
        ver = tag.split(":")[1]
    tag = randomString(20 - len(name)) + name + ":" + ver

    image_uuid = str(uuid.uuid4())
    container_uuid = str(uuid.uuid4())

    image = Image(uid, "", tag, "installing", sshport, "", image_uuid)
    db: wrappers.Collection = mongo.db.images
    db.insert_one(image.__dict__)

    # search Dockerfile
    df: wrappers.Collection = mongo.db.dockerfile
    result: Dockerfile = deserialize_json(Dockerfile,
                                          df.find_one({"uuid": dockfile}))
    if result == None:
        return json_result(-1, "Dockerfile is not exist")

    try:
        # image build
        image.status = "build"
        db.update({"uuid": image_uuid}, image.__dict__)
        result, imgs = DockerImageAPI.build(result.path, rootpass, tag)
        image.short_id = imgs[0]["Id"].split(":")[1]
        print(result)

        image.status = "done"
        db.update({"uuid": image_uuid}, image.__dict__)
    except:
        image.status = "fail"
        db.update({"uuid": image_uuid}, image.__dict__)
        return json_result(-1, "Dockerfile::Image::build fail")

    # container start
    container_id = DockerImageAPI.run(tag, "",
                                      sshport)  # image.run(tag, port=sshport)
    container = Container(uid, tag, "start", image_uuid, sshport, container_id,
                          container_uuid)
    container.start(container_id)
    db: wrappers.Collection = mongo.db.containers
    db.insert_one(container.__dict__)

    docker_daemon.notify(container_id)

    result_stream = []
    for item in result:
        try:
            result_stream += [item["stream"]]
        except:
            continue

    return json_result(0, "".join(result_stream))
Esempio n. 29
0
 def history(self, username):
     db: wrappers.Collection = mongo.db.paypal
     return json_result(0, db.find({"username": username}))
Esempio n. 30
0
def api_error():
    return json_result(-1, "error")