Пример #1
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)
Пример #2
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")
Пример #3
0
def docker_worker(daemon: DockerDaemon):
    while daemon.running:
        if len(daemon.queue) <= 0:
            continue

        container_id = daemon.queue[0]
        container = DockerContainerAPI.status(container_id)
        #print(daemon.queue)
        for ct in container:
            if ct["Id"][:12] == container_id:
                db: wrappers.Collection = mongo.db.containers
                result: Container = deserialize_json(
                    Container, db.find_one({"short_id": container_id}))
                if result.status == "start" and ct["State"] == "stopped":
                    result.status = "stop"
                    db.update({"short_id": container_id}, result.__dict__)
                break
        daemon.queue = daemon.queue[1:] + [daemon.queue[0]]

        time.sleep(3)
Пример #4
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")
Пример #5
0
def docker_images():
    uid: uuid.UUID = session.get("uuid")
    db: wrappers.Collection = mongo.db.images
    result: list = deserialize_json(Image, db.find({"uid": uid}))

    return render_template("docker/images.html", images=result)
Пример #6
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))
Пример #7
0
 def getRoomDetail(self, seq):
     col: wrappers.Collection = mongo.db.room_detail
     return deserialize_json(Room, col.find_one({ "seq": seq }))
Пример #8
0
 def __getCoupon(self, coupon):
     db:wrappers.Collection = mongo.db.coupons
     cm:CouponModel = deserialize_json(CouponModel, db.find({ "coupon": coupon }))
     return cm
Пример #9
0
 def all():
     db: wrappers.Collection = mongo.db.users
     user: list = deserialize_json(User, db.find())
     return user
Пример #10
0
 def all(self):
     db:wrappers.Collection = mongo.db.coupons
     cm:list = deserialize_json(CouponModel, db.find())
     return cm
Пример #11
0
def dockerfile():
    db: wrappers.Collection = mongo.db.dockerfile
    uid = session.get("uuid")

    result: Dockerfile = deserialize_json(Dockerfile, db.find({ "uid": uid }))
    return render_template("dockerfile/list.html", dockerfiles=result)
Пример #12
0
 def find_by_shortid(self, short_id):
     db: wrappers.Collection = mongo.db.containers
     return deserialize_json(Container, db.find_one({"short_id": short_id}))
Пример #13
0
 def find_by_uuid(uid):
     db: wrappers.Collection = mongo.db.users
     user: User = deserialize_json(User, db.find_one({"uuid": str(uid)}))
     return user
Пример #14
0
 def find_by_name(username):
     db: wrappers.Collection = mongo.db.users
     user: User = deserialize_json(User, db.find_one({"username":
                                                      username}))
     return user
Пример #15
0
def docker_containers():
    uid = session.get("uuid")
    db: wrappers.Collection = mongo.db.containers
    result: list = deserialize_json(Container, db.find({"uid": uid}))

    return render_template("docker/containers.html", containers=result)
Пример #16
0
 def get(self, username):
     db:wrappers.Collection = mongo.db.coupons
     cm:list = deserialize_json(CouponModel, db.find({ "username": username }))
     if cm == None:
         return False
     return cm
Пример #17
0
 def find_by_tag(self, tag):
     db: wrappers.Collection = mongo.db.images
     image: Image = deserialize_json(Image, db.find_one({"tag": tag}))
     return image