def list_instances(): instances = [] with DockerClient() as docker: containers = docker.list_containers() for container in containers: try: instance = { "name": container.name, "short_id": container.short_id, "status": container.status, "prots": json.dumps(container.ports), "image": container.image.tags[0], "created": container.attrs.get("Created") } except: #todo fix instance = { "name": 'xxx', "short_id": container.short_id, "status": "xxx", "prots": "xxx", "image": "xxx", "created": "xxx" } instances.append(instance) return jsonify(instances), 200
def instance_create_view(): instance = request.get_json() image = instance.get("image") image_type = instance.get("type") ports = instance.get("ports") volumes = instance.get("volumes").split(",") if instance.get( "volumes") else [] environment = instance.get("environment") if not environment and image_type == "mysql": environment = ["MYSQL_ROOT_PASSWORD=dangerous"] if not ports: ports = IMAGE_TYPE_PROT_MAPPER.get(image_type, "") else: new_ports = {} for port in ports.split(','): new_ports[port.split(":")[0]] = port.split(":")[-1] ports = new_ports with DockerClient() as docker: container = docker.run(image, ports=ports, volumes=volumes, environment=environment) return jsonify({"short_id": container.short_id}), 200
def stop_instance(instance_id): with DockerClient() as docker: if instance_id == "Dangerous_All": docker.stop("", stop_all=True) flash("Stop All Instance Success.") elif docker.stop(instance_id): flash("Stop Success.") else: flash("Stop Failed.") return redirect(url_for('dashboard_manage.index'))
def stop_instance(instance_id): with DockerClient() as docker: if instance_id == "Dangerous_All": docker.stop("", stop_all=True) flash(f"Stop All Instance Success.") elif docker.stop(instance_id): flash(f"Stop {instance_id} Success.") else: flash(f"Stop Failed.") return jsonify({}), 200
def build_images(): data = request.get_json() dockerfile = data.get("dockerfile") name = data.get("name") if not dockerfile: raise ConflictException("dockerfile can't be none") with DockerClient() as docker: image = docker.build_image(dockerfile, name) LOG.info("build image result", image) return jsonify({}), 200
def create_instace(): form=CreateInstanceForm() if request.method=='POST' and form.validate_on_submit(): image=form.image.data port = {"3306/tcp": form.data.get("port")} volumes = form.volumes.data.split(",") print ('开始执行docker') with DockerClient() as docker: print(image, port, volumes) docker.run(image, ports=port, volumes=volumes) flash(u'you create a instance') return redirect(url_for('dashboard_manage.index')) return render_template('create_instance.html',form=form)
def docker_login_view(): login_data = request.get_json() username = login_data.get("username") password = login_data.get("password") registry = login_data.get("registry") with DockerClient() as docker: try: resp = docker.login(username, password, registry) setattr(g, "has_docker_login", True) except: resp = None setattr(g, "has_docker_login", False) return jsonify(resp if resp else {}), 200
def instance_create(): form = CreateInstanceForm() s = form.validate_on_submit() if request.method == 'POST' and form.validate_on_submit(): image = form.data.get("image") port = {"3306/tcp": form.data.get("port")} volumes = form.data.get("volumes").split(",") with DockerClient() as docker: container = docker.run(image, ports=port, volumes=volumes) flash('You have successfully create {}'.format(escape( form.image.data))) return redirect(url_for('dashboard.dashboard')) return render_template('create_instance.html', form=form)
def list_instance(): comments = [] with DockerClient() as docker: containers = docker.list_containers() for container in containers: try: instance = InstanceSchema().load( {"name": container.image.tags[0], "short_id": container.short_id, "status": container.status, "created": container.attrs.get("Created")}) except: # todo fix instance = InstanceSchema().load( {"name": 'xxx', "short_id": container.short_id, "status": "xxx", "created": "xxx"}) comments.append(instance.data) return render_template('list_instance.html',comments=comments)
def list_images(): result = [] with DockerClient() as docker: images = docker.list_images() for image in images: for tag in image.tags: result.append({ "short_id": image.short_id.split(":")[-1], "tag": tag.split(":")[-1], "repository": tag.split(":")[0], "created": image.attrs.get("Created"), "size": int(image.attrs.get("Size") / 1024 / 1024) }) return jsonify(result), 200
def delete_images(short_id): with DockerClient() as docker: if docker.delete_image(short_id): return jsonify({}), 200 else: return jsonify({}), 500
def update_instance_status(image_tag): with DockerClient() as docker: docker.exsit_container(image_tag)
def restart_instance(instance_id): with DockerClient() as docker: if docker.restart(instance_id): flash(f"Restart Instance {instance_id} Success.") return jsonify({}), 200
def show_dashboard(): with DockerClient() as docker: if not docker: flash("Error: Docker Service Is Not Available !") return render_template("index.html")
def list_image(): with DockerClient() as docker: pass