def create_machine(): if request.method == 'GET': images = Image.list_all() return render_template('create_machine.html', images=images) name = request.form['name'] image_name = request.form['image_name'] if not (image_name and name): abort(400) image = Image.get_by_name(image_name) if not image: abort(400) machine = Machine.create(g.user, name) callback_url = url_for('machine.callback', machine_id=machine.id, _external=True) try: if DEPLOY_MODE == 'public': eru.deploy_public(PODNAME, image.appname, 1, image.version, image.entrypoint, image.env, [image.network], raw=True, image=image.addr, callback_url=callback_url) elif DEPLOY_MODE == 'private': eru.deploy_private(PODNAME, image.appname, 1, 1, image.version, image.entrypoint, image.env, [image.network], raw=True, image=image.addr, callback_url=callback_url) except EruException: pass return redirect(url_for('machine.index'))
def index(): machines, total = Machine.list_machines(start=g.start, limit=g.limit) return render_template( 'admin.html', machines=machines, total=total, endpoint='admin.index', )
def delete_machine(): machine = Machine.get(request.form['machine_id']) if not machine: return jsonify(r=0) if machine.container_id: try: eru.remove_containers([machine.container_id]) except EruException: pass machine.delete() return jsonify(r=0)
def add_public_key(): machine = Machine.get(request.form['machine_id']) if not (machine and machine.container_id): return jsonify(error=u'机器挂了') container = eru.get_container(machine.container_id) if not container['networks']: return jsonify(error=u'机器没有对外开放') netaddr = container['networks'][0]['address'] if add_pubkey(g.user.pubkey, netaddr): return jsonify(error=u'添加成功') return jsonify(error=u'因为一些奇怪的原因添加失败')
def callback(machine_id): machine = Machine.get(machine_id) if not machine: return 'not found' data = request.get_json() container_id = data['container_id'] status = data.get('status') netaddr = data['networks'][0]['address'] machine.set_alive(status == 'start') if not machine.container_id: machine.set_container_id(container_id) machine.set_netaddr(netaddr) return 'ok'
def index(): machines = Machine.get_by_user(g.user) return render_template('index.html', machines=machines)
def is_alive(): machine = Machine.get(request.form['machine_id']) if not machine: return jsonify(r=0) return jsonify(r=machine.is_alive)