def vm_migrate(): if request.method == 'POST': vm_name = request.form.get('vm_name') KVM_IP = request.form.get('kvm_ip') migrate_info = "migrate~" + KVM_IP + "~" + vm_name pub(migrate_info) sub = RedisHelper() redis_sub = sub.subscribe() status = "failure" while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "migrate": continue else: status = msg[2] break if status == "ok": response = make_response(redirect('/migrate_results')) result = vm_name + "~" + status response.set_cookie('result', value=result) return response else: response = make_response(redirect('/migrate_results')) result = vm_name + "~" + status response.set_cookie('result', value=result) return response else: username = request.cookies.get('username') if not username: return "please login!!!" islogin = session.get('islogin') return render_template('vm_migrate.html',username=username,islogin=islogin)
def disk_create(): if request.method == 'POST': disk_name = request.form.get('disk_name') disk_size = request.form.get('disk_size') disk_info = [disk_name,disk_size] pub(disk_info) sub = RedisHelper() redis_sub = sub.subscribe() status = "failure" while 1 < 2: msg = redis_sub.parse_response() if len(msg[2].split("[")[1].split("]")[0].split(", ")) == 2: continue else: status = msg[2].split("]")[0].split(", ")[2].split("'")[1] break if status == "ok": response = make_response(redirect('/disk_list')) return response else: return "%s is created %s!" % (disk_name, status) else: username = request.cookies.get('username') if not username: return "please login!!!" islogin = session.get('islogin') return render_template('disk_create.html', username=username, islogin=islogin)
# -*- coding: utf-8 -*- """ Created on Thu Dec 13 21:11:09 2018 @author: dingfei """ import redis #r = redis.Redis(host='127.0.0.1',port=6379,db=0) from RedisHelper import RedisHelper obj=RedisHelper() #obj.publish('hello') re_sub=obj.subscribe() while True: msg=re_sub.parse_response() print(msg)
import sys sys.path.append(r'/home/peng/learnredis') from RedisHelper import RedisHelper obj = RedisHelper() redis_sub = obj.subscribe() while True: msg = redis_sub.parse_response() print(msg)
#!/usr/bin/env python # -*- coding:utf-8 -*- #订阅 from RedisHelper import RedisHelper import sys sys.getdefaultencoding() reload(sys) sys.setdefaultencoding('UTF-8') sys.getdefaultencoding() obj = RedisHelper() redis_sub = obj.subscribe() #调用订阅方法 while True: msg = redis_sub.parse_response() print("收到订阅消息 {}".format((msg)[2])).decode('utf-8') #print "收到订阅消息 {}".format((msg)[2])
from RedisHelper import RedisHelper obj = RedisHelper()#订阅 i = 0 while True: redis_sub = obj.subscribe('mytopic%s'%i) # 调用订阅方法 msg= redis_sub.parse_response() print(msg) i += 1
print "Deploy operation successfully." write_vm_info_to_file = "echo %s %s %s %s %s running >> /vm_scripts/vm_info.list" % ( vm_name, cpu, mem, disk, os_type) run_command(write_vm_info_to_file) result = "ok" return result else: DEPLOY_END_TIME = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") print "Deploy operation failure." result = "failure" return result if __name__ == "__main__": sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() #info = msg[2].split("[")[1].split("]")[0].split(",|'") #vm_info = info[0].split(",") print msg[2] if msg[2] == "vm_list": run_command("/vm_scripts/get_running_vm_info.sh") f = open('/vm_scripts/vm_list', 'r') vm_list = [line.strip() for line in f.readlines()] f.close pub = RedisHelper() pub.publish(vm_list) elif msg[2].split("~")[0] == "delvm": vm_name = msg[2].split("~")[1] if vm_name == "#":
def vm_list(): url = request.args.get('url') global vm_list global vm_info vm_list = [] if url != None: response = make_response(redirect('/url')) response.set_cookie('url', value=url) return response else: if request.method == 'POST': vm_name = request.form.get('del_vm_name') username = request.cookies.get('username') delete = "delvm~" + vm_name pub(delete) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "delvm": continue else: vm_info = msg[2].split("[")[1].split("]")[0].split(",|'")[0].split(", ") break for f in vm_info: list = f.split("'")[1].split("|") if len(list[1]) > 3: port = "32"+list[1].split(".")[3] else: port = "0" list.append(port) vm_list.append(list) if not username: return "please login!!!" islogin = session.get('islogin') return render_template('vm_list.html', username=username, islogin=islogin, vm_info=vm_list) else: username = request.cookies.get('username') pub("vm_list") sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2] == "vm_list": continue else: vm_info = msg[2].split("[")[1].split("]")[0].split(",|'")[0].split(", ") break for f in vm_info: list = f.split("'")[1].split("|") if len(list[1]) > 3: port = "32" + list[1].split(".")[3] else: port = "0" list.append(port) vm_list.append(list) if not username: return "please login!!!" islogin = session.get('islogin') return render_template('vm_list.html',username=username,islogin=islogin,vm_info=vm_list)
def disk_list(): global disk_info disk_list = [] if request.method == 'POST': disk_mount_umount = request.form.get('mount_umount') if disk_mount_umount != None: username = request.cookies.get('username') if disk_mount_umount.split("~")[0] == "umount": pub(disk_mount_umount) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "umount": continue else: disk_info = msg[2].split("[")[1].split("]")[0].split(",|'")[0].split(", ") break for f in disk_info: list = f.split("'")[1].split("|") disk_list.append(list) if not username: return "please login!!!" islogin = session.get('islogin') return render_template('disk_list.html', username=username, islogin=islogin, disk_info=disk_list) elif disk_mount_umount.split("~")[0] == "deldisk": disk_del = disk_mount_umount pub(disk_del) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "deldisk": continue else: disk_info = msg[2].split("[")[1].split("]")[0].split(",|'")[0].split(", ") break for f in disk_info: list = f.split("'")[1].split("|") disk_list.append(list) if not username: return "please login!!!" islogin = session.get('islogin') return render_template('disk_list.html', username=username, islogin=islogin, disk_info=disk_list) else: disk_name = request.form.get('mount_disk_name') disk_mount = disk_name + "~" + disk_mount_umount pub(disk_mount) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "mount": continue else: disk_info = msg[2].split("[")[1].split("]")[0].split(",|'")[0].split(", ") break for f in disk_info: list = f.split("'")[1].split("|") disk_list.append(list) if not username: return "please login!!!" islogin = session.get('islogin') return render_template('disk_list.html', username=username, islogin=islogin, disk_info=disk_list) else: return "error." else: username = request.cookies.get('username') pub("disk_list") sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2] == "disk_list": continue else: disk_info = msg[2].split("[")[1].split("]")[0].split(",|'")[0].split(", ") break for f in disk_info: list = f.split("'")[1].split("|") disk_list.append(list) if not username: return "please login!!!" islogin = session.get('islogin') return render_template('disk_list.html', username=username, islogin=islogin, disk_info=disk_list)
def sub(): sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: global msg msg = redis_sub.parse_response()
def vm_detail(): username = request.cookies.get('username') vm_info = request.args.get('vm_name') vm_info = vm_info.split("[")[1].split("]")[0].split(",") if not username: return "please login!!!" islogin = session.get('islogin') if request.method == 'POST': cpu = request.form.get('cpu') ram = request.form.get('ram') if cpu == None and ram == None: oprate_vm = request.form.get('oprate_vm') oprate = oprate_vm.split("~")[0] if oprate == "start": pub(oprate_vm) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "start": continue else: if msg[2] == "ok": vm_info[3] = "running" else: return "error" break return render_template('vm_detail.html', username=username, islogin=islogin, vm=vm_info) elif oprate == "shutdown": pub(oprate_vm) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "shutdown": continue else: if msg[2] == "ok": vm_info[3] = "shut_off" else: return "error" break return render_template('vm_detail.html', username=username, islogin=islogin, vm=vm_info) elif oprate == "reboot": pub(oprate_vm) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "reboot": continue else: if msg[2] == "ok": vm_info[3] = "running" else: return "error" break return render_template('vm_detail.html', username=username, islogin=islogin, vm=vm_info) else: pub(oprate_vm) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "reboot_force": continue else: if msg[2] == "ok": vm_info[3] = "running" else: return "error" break return render_template('vm_detail.html', username=username, islogin=islogin, vm=vm_info) else: alter_name = request.form.get('alter_name') alter_args = "alter~"+alter_name+"~"+cpu+"~"+ram pub(alter_args) sub = RedisHelper() redis_sub = sub.subscribe() while 1 < 2: msg = redis_sub.parse_response() if msg[2].split("~")[0] == "alter": continue else: if msg[2] == "ok": vm_info[1] = cpu vm_info[2] = ram else: return "error" break return render_template('vm_detail.html', username=username, islogin=islogin, vm=vm_info) else: return render_template('vm_detail.html',username=username,islogin=islogin,vm=vm_info)
#!/usr/bin/env python # -*- coding:utf-8 -*- #订阅 from RedisHelper import RedisHelper obj = RedisHelper() redis_sub = obj.subscribe()#调用订阅方法 while True: msg= redis_sub.parse_response() print (msg)