def create_vm(self): ''' return 0: created failed 1: created succeed 2: aleady created ''' network_dict = network2dict() occi = OCCI(self.order.cluster) sunstone = Sunstone(self.order.cluster) vminst = VMC() # if aleady created if self.order.cnt <= self.order.vm.__len__(): return 2 ds_info = sunstone.datastore_id(self.order.cluster.ds_id).data ds_mad = json.loads(ds_info)['DATASTORE']['DS_MAD'] # create vm ret = 1 for i in range(0, self.order.cnt - self.order.vm.__len__()): if not vminst.create(self.order, ds_mad): ret = 0 if ret: self.order.status = 1 else: self.order.status = 2 db.session.add(self.order) db.session.commit() return ret
def view(vm, **kvargs): vminst = VMC() vm_detail = vminst.detail(vm) network_dict = network2dict() cpu = [x.cpu for x in db.session.query(CPU_Mem.cpu.distinct().label('cpu')).order_by(CPU_Mem.cpu).all()] mem = CPU_Mem.query.filter(CPU_Mem.cpu==vm.cpu).order_by(CPU_Mem.mem).all() return render_template('vm/view.html', vm=vm, cpu=cpu, mem=mem, vm_detail=vm_detail, network_dict=network_dict)
def refresh(): vminst = VMC() if vminst.refresh(): flash(u'刷新成功', 'success') else: flash(u'刷新失败', 'error') return redirect(request.referrer)
def resize(vm, **kvargs): try: vminst = VMC() cpu = request.form['cpu'] or vm.cpu mem = request.form['mem'] or vm.mem vminst.resize(vm, cpu, mem) status = 1 except: status = 0 return jsonify(status=status)
def rename(vm, **kvargs): try: newname = request.form['data'] or vm.hostname vminst = VMC() vminst.rename(vm, newname) vm.hostname = newname db.session.add(vm) db.session.commit() status = 1 except: status = 0 return jsonify(status=status)
def detach_nic(vm, **kvargs): try: vminst = VMC() nic_id = request.form['nic_id'] vminst.detach_nic(vm, nic_id) flag = True sunstone = Sunstone(vm.cluster) while flag: resp = sunstone.vm_id(vm.vm_id) info = json.loads(resp.data)['VM'] if int(info['STATE']) == 3: flag = False status = 1 except: status = 0 return jsonify(status=status)
def action_mult(): status = 1 vminst = VMC() try: action = request.form['action'] except: action = None if request.form['vmlist'] == "": vmlist = [] else: vmlist = request.form['vmlist'].split(',') for x in vmlist: vm = VM.query.get(int(x)) if action == 'delete': if not vminst.delete(vm): status = 0 if action == 'start': if not vminst.start(vm): status = 0 if action == 'poweroff': if not vminst.poweroff(vm): status = 0 if action == 'poweroff_hard': if not vminst.poweroff_hard(vm): status = 0 if action == 'reboot': if not vminst.reboot(vm): status = 0 if action == 'reboot_hard': if not vminst.reboot_hard(vm): status = 0 if action == 'recreate': if not vminst.recreate(vm): status = 0 vminst.refresh() if status: flash(u'操作成功', 'success') else: flash(u'操作失败', 'error') return redirect(request.referrer)
def delete(vm, **kvargs): vminst = VMC() status = 1 if vminst.delete(vm) else 0 return jsonify(status=status)