def test_create_vps0(self): print "create vps00" logger = Log("test", config=conf) vpsops = VPSOps(logger) vps = XenVPS(0) try: vps.setup(os_id=10004, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfdsss", gateway="113.11.199.1") vps.add_netinf_ext({"113.11.199.18": "255.255.255.0"}) vps.add_netinf_int({"10.10.2.20": '255.255.255.0'}) #vps.add_extra_storage (disk_id=1, size_g=1, fs_type='ext3') #vps.add_extra_storage (disk_id=2, size_g=0.5, fs_type='ext4') #vps.setup (os_id=10001, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfd") #vps.setup (os_id=10002, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfd") #vps.setup (os_id=30001, vcpu=1, mem_m=512, disk_g=7, root_pw="root") #vps.setup (os_id=1, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfd") #vps.setup (os_id=10000, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfd") #vps.setup (os_id=20001, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfd") #vps.setup (os_id=10003, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfd") #vps.setup (os_id=20001, vcpu=1, mem_m=512, disk_g=7, root_pw="fdfdfd") print vps.gen_xenpv_config() #vpsops.create_vps (vps, vps_image='/data/vps/images/arch-2011.08.19-i386-fs-ext3.tar.gz') vpsops.create_vps(vps) except Exception, e: print str(e) logger.exception(e) raise e
def check_via_meta(client, vps_id, vps_info): meta = client.vpsops._meta_path(vps_id, is_trash=False) if os.path.exists(meta): xv = client.vpsops._load_vps_meta(meta) is_running = xv.is_running() and "(running)" or "(not running)" ip_ok = "ip %s " % (xv.ip) if vps_common.ping(xv.ip): ip_ok += "reachable " else: if scan_port_open(xv.ip): ip_ok += "filtered icmp" else: ip_ok += "timeout (filtered all port?)" print "vps %s %s %s" % (vps_id, is_running, ip_ok) return else: xv = XenVPS(vps_id) is_running = xv.is_running() and "(running)" or "(not running)" if not vps_info: print "vps %s %s has neither meta data or backend data" % ( vps_id, is_running) return else: client.setup_vps(xv, vps_info) ip_ok = "ip %s " % (xv.ip) + \ (vps_common.ping(xv.ip) and "reachable" or "timeout") print "vps %s %s %s has no meta_data" % (vps_id, is_running, ip_ok) return
def test_create_vps0 (self): print "create vps00" logger = Log ("test", config=conf) vpsops = VPSOps (logger) vps = XenVPS (0) try: vps.setup (os_id=20001, vcpu=1, mem_m=512, disk_g=7, ip="113.11.199.3", netmask="255.255.255.0", gateway="113.11.199.1", root_pw="fdfdfd") #vps.add_extra_storage (disk_id=1, size_g=1, fs_type='ext3') #vps.add_extra_storage (disk_id=2, size_g=0.5, fs_type='ext4') #vps.setup (os_id=10001, vcpu=1, mem_m=512, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") #vps.setup (os_id=10002, vcpu=1, mem_m=512, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") #vps.setup (os_id=30001, vcpu=1, mem_m=512, disk_g=7, ip="10.10.2.2", netmask="255.255.255.0", gateway="10.10.2.1", root_pw="root") #vps.setup (os_id=1, vcpu=1, mem_m=512, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") #vps.setup (os_id=10000, vcpu=1, mem_m=512, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") #vps.setup (os_id=20001, vcpu=1, mem_m=512, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") #vps.setup (os_id=10003, vcpu=1, mem_m=512, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") #vps.setup (os_id=20001, vcpu=1, mem_m=512, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") print vps.gen_xenpv_config () #vpsops.create_vps (vps, vps_image='/data/vps/images/arch-2011.08.19-i386-fs-ext3.tar.gz') vpsops.create_vps (vps) except Exception, e: print str(e) logger.exception (e) raise e
def vps_open (self, vps, vps_image=None, is_new=True): self.logger.info ("to open vps %s" % (vps.id)) if vps.host_id != self.host_id: msg = "vpsopen : vps %s host_id=%s != current host %s , abort" % (vps.id, vps.host_id, self.host_id) self.logger.error (msg) self.done_task (Cmd.OPEN, vps.id, False, msg) return if not vps.ipv4 or not vps.ipv4_gateway or vps.cpu <= 0 or vps.ram <= 0 or vps.hd <= 0 or not vps.password: self.logger.error ("vps open: invalid vps data received: %s" % (self.dump_vps_info (vps))) self.done_task (Cmd.OPEN, vps.id, False, "invalid vps data") return xv = XenVPS (vps.id) vpsops = VPSOps (self.logger) try: self.setup_vps (xv, vps) if xv.is_running (): msg = "vps %s is running" % (vps.id) self.logger_err.error (msg) self.done_task (Cmd.OPEN, vps.id, False, msg) return if vps.state in [vps_const.VPS_STATE_PAY, vps_const.VPS_STATE_RUN]: vpsops.create_vps (xv, vps_image, is_new) elif vps.state == vps_const.VPS_STATE_CLOSE: vpsops.reopen_vps (vps.id, xv) else: msg = "vps%s state is %s(%s)" % (str(vps.id), vps.state, vps_const.VPS_STATE2CN[vps.state]) self.logger_err.error (msg) self.done_task (Cmd.OPEN, vps.id, False, msg) return except Exception, e: self.logger_err.exception ("for %s: %s" % (str(vps.id), str(e))) self.done_task (Cmd.OPEN, vps.id, False, "error, " + str(e)) return
def check_via_meta(client, vps_id, vps_info): meta = client.vpsops._meta_path(vps_id, is_trash=False) if os.path.exists(meta): xv = client.vpsops._load_vps_meta(meta) is_running = xv.is_running() and "(running)" or "(not running)" ip_ok = "ip %s " % (xv.ip) if vps_common.ping(xv.ip): ip_ok += "reachable " else: if scan_port_open(xv.ip): ip_ok += "filtered icmp" else: ip_ok += "timeout (filtered all port?)" print "vps %s %s %s" % (vps_id, is_running, ip_ok) return else: xv = XenVPS(vps_id) is_running = xv.is_running() and "(running)" or "(not running)" if not vps_info: print "vps %s %s has neither meta data or backend data" % (vps_id, is_running) return else: client.setup_vps(xv, vps_info) ip_ok = "ip %s " % (xv.ip) + \ (vps_common.ping(xv.ip) and "reachable" or "timeout") print "vps %s %s %s has no meta_data" % (vps_id, is_running, ip_ok) return
def gen_meta(vps_mgr, domain_dict, vps_id): xv = XenVPS(vps_id) vps = vps_mgr.query_vps(vps_id) if not vps_mgr.vps_is_valid(vps): print "no backend data for %s " % vps_id return domain_id = domain_dict[xv.name] vif_datas = XenStore.get_vif_by_domain_id(domain_id) vps_mgr.setup_vps(xv, vps) #xv.add_extra_storage (1, 964) #TEST vpsops = VPSOps(vps_mgr.logger) if len(vif_datas.values()) == 1: vif_data = vif_datas.values()[0] if vif_data['online'] == '1': if vif_data.has_key('vifname'): xv.vifs[vif_data['vifname']].mac = vif_data['mac'] else: vif_id = vif_datas.items()[0][0] vif_name = 'vif%s.%s' % (domain_id, vif_id) del xv.vifs[xv.name] xv.add_netinf_ext(ip=xv.ip, netmask=xv.netmask, mac=vif_data['mac']) #vpsops.create_xen_config (xv) vpsops.save_vps_meta(xv, override=True)
def test_mem_too_big (self): print "test mem too big" vps = XenVPS (0) logger = Log ("test", config=conf) vps.setup (os_id=50001, vcpu=1, mem_m=500000, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") try: vps.check_resource_avail () except Exception, e: logger.exception (e) print "exception caught", type (e), str(e) return
def _load_vps_meta (self, meta_path): f = open (meta_path, "r") data = None try: data = json.load (f) finally: f.close () xv = XenVPS.from_meta (data) return xv
def _load_vps_meta(self, meta_path): f = open(meta_path, "r") data = None try: data = json.load(f) finally: f.close() xv = XenVPS.from_meta(data) return xv
def vps_open(self, vps_info, vps_image=None, is_new=True): vps_id = vps_info.id self.logger.info("to open vps %s" % (vps_id)) if vps_info.host_id != self.host_id: msg = "vpsopen : vps %s host_id=%s != current host %s , abort" % ( vps_id, vps_info.host_id, self.host_id) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return if not self.vpsinfo_check_ip(vps_info): msg = "no ip with vps %s" % (vps_id) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return xv = XenVPS(vps_id) try: domain_dict = XenStore.domain_name_id_map() limit = None if 'VPS_NUM_LIMIT' in dir(conf): limit = conf.VPS_NUM_LIMIT if limit and len(domain_dict.keys()) >= limit + 1: msg = "vps open: cannot open more than %d vps" % (limit) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return self.setup_vps(xv, vps_info) if xv.is_running(): msg = "vps %s is running" % (vps_id) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, True, msg) return if vps_info.state in [VM_STATE.PAY, VM_STATE.OPEN, VM_STATE.CLOSE]: if self.vpsops.is_normal_exists(vps_id): xv.check_storage_integrity() xv.check_xen_config() if not xv.is_running(): self.logger.info( "seems vps %s was not closed, try to boot" % (vps_id)) self.vpsops._boot_and_test(xv, is_new=False) elif self.vpsops.is_trash_exists(vps_id): self.vpsops.reopen_vps(vps_id, xv) else: self.vpsops.create_vps(xv, vps_image, is_new) else: msg = "vps%s state is %s(%s)" % (str(vps_id), vps_info.state, VM_STATE_CN[vps_info.state]) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return except Exception, e: self.logger.exception("vps %s: %s" % (str(vps_id), str(e))) self.done_task(CMD.OPEN, vps_id, False, "error, " + str(e)) return
def test_meta(self): print "test vps meta" logger = Log("test", config=conf) vpsops = VPSOps(logger) vps = XenVPS(0) vps.setup(os_id=50001, vcpu=1, mem_m=500000, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") vps.add_extra_storage(disk_id=1, size_g=1, fs_type='ext3') vps.add_netinf('vps0_inter', "10.10.3.2", '255.255.255.0', 'xenbr0', None) vps.data_disks['xvdc1']._set_expire_days(1) print "trash_date", vps.data_disks['xvdc1'].trash_date print "expire_date", vps.data_disks['xvdc1'].expire_date vpsops.save_vps_meta(vps) _vps = vpsops.load_vps_meta(0) self.assertEqual(_vps.vps_id, vps.vps_id) self.assertEqual(_vps.os_id, vps.os_id) self.assertEqual(_vps.vcpu, vps.vcpu) self.assertEqual(_vps.mem_m, vps.mem_m) self.assertEqual(_vps.ip, vps.ip) self.assertEqual(_vps.netmask, vps.netmask) self.assertEqual(_vps.gateway, vps.gateway) self.assertEqual(_vps.root_store.size_g, vps.root_store.size_g) self.assertEqual(_vps.swap_store.size_g, vps.swap_store.size_g) self.assertEqual(_vps.data_disks['xvdc1'].size_g, 1) self.assertEqual(_vps.data_disks['xvdc1'].fs_type, 'ext3') self.assertEqual(_vps.data_disks['xvdc1'].mount_point, '/mnt/data1') self.assertEqual(_vps.data_disks['xvdc1'].xen_dev, 'xvdc1') self.assertEqual(_vps.data_disks['xvdc1'].trash_date, vps.data_disks['xvdc1'].trash_date) self.assertEqual(_vps.data_disks['xvdc1'].expire_date, vps.data_disks['xvdc1'].expire_date) print _vps.data_disks['xvdc1'].__class__.__name__ self.assertEqual(len(_vps.vifs.values()), 2) self.assertEqual(_vps.vifs['vps0_inter'].ip, '10.10.3.2') self.assertEqual(_vps.vifs['vps0_inter'].netmask, '255.255.255.0') self.assertEqual(_vps.vifs['vps0_inter'].bridge, 'xenbr0') self.assertEqual(_vps.vifs['vps0_inter'].mac, vps.vifs['vps0_inter'].mac) print "test trash expire date None" vps.data_disks['xvdc1']._set_expire_days(None) self.assertEqual(vps.data_disks['xvdc1'].trash_date, None) self.assertEqual(vps.data_disks['xvdc1'].expire_date, None) vpsops.save_vps_meta(vps) _vps = vpsops.load_vps_meta(0) self.assertEqual(_vps.data_disks['xvdc1'].trash_date, None) self.assertEqual(_vps.data_disks['xvdc1'].expire_date, None)
def save_closed_vps(self, meta, origin_host_id): try: xv = XenVPS.from_meta(meta) self.logger.info("closed vps %s immigrate from host=%s" % (xv.vps_id, origin_host_id)) vpsops = VPSOps(self.logger) vpsops.save_vps_meta(xv, is_trash=True) except Exception, e: self.logger.exception(e) raise
def vps_reboot(self, vps_info): xv = XenVPS(vps_info.id) self.logger.info("to reboot vps %s" % (vps_info.id)) try: self.setup_vps(xv, vps_info) self.vpsops.reboot_vps(xv) except Exception, e: self.logger.exception(e) self.done_task(CMD.REBOOT, vps_info.id, False, "exception %s" % (str(e))) return
def _vps_delete(self, vps_id, vps=None): try: vpsops = VPSOps(self.logger) xv = None if vps: xv = XenVPS(vps_id) self.setup_vps(xv, vps) vpsops.delete_vps(vps_id, xv) except Exception, e: self.logger_err.exception(e) raise e
def vps_set_bandwidth(self, vps_info): xv = XenVPS(vps_info.id) self.logger.info("to modify bandwidth for vps %s" % (vps_info.id)) try: self.setup_vps(xv, vps_info) self.vpsops.change_qos(xv) except Exception, e: self.logger.exception(e) self.done_task(CMD.BANDWIDTH, vps_info.id, False, "exception %s" % (str(e))) return
def _vps_delete(self, vps_id, vps_info=None, check_date=False): try: xv = None if vps_info: xv = XenVPS(vps_id) self.setup_vps(xv, vps_info) self.vpsops.delete_vps(vps_id, xv, check_date=check_date) self.done_task(CMD.RM, vps_id, True) except Exception, e: self.logger.exception(e) raise e
def vps_close(self, vps): try: assert vps.state == vps_const.VPS_STATE_CLOSE vpsops = VPSOps(self.logger) xv = XenVPS(vps.id) self.setup_vps(xv, vps) vpsops.close_vps(vps.id, xv) except Exception, e: self.logger_err.exception(e) self.done_task(Cmd.CLOSE, vps.id, False, "exception %s" % (str(e))) return
def create_vps(self, meta, origin_host_id): try: xv = XenVPS.from_meta(meta) self.logger.info("vps %s immigrate from host=%s" % (xv.vps_id, origin_host_id)) vpsops = VPSOps(self.logger) # some setting various between different hosts vpsops.create_from_migrate(xv.clone()) except Exception, e: self.logger.exception(e) raise
def vps_reset_pw(self, vps_info): xv = XenVPS(vps_info.id) self.logger.info("to reset passwd for vps %s" % (vps_info.id)) try: self.setup_vps(xv, vps_info) self.vpsops.reset_pw(xv) except Exception, e: self.logger.exception(e) self.done_task(CMD.RESET_PW, vps_info.id, False, "exception %s" % (str(e))) return
def modify_vif_rate(self, vps): xv = XenVPS(vps.id) vpsops = VPSOps(self.logger) self.logger.info("to modify vif rate for vps %s" % (vps.id)) try: self.setup_vps(xv, vps) vpsops.create_xen_config(vps) except Exception, e: self.logger_err.exception(e) self.done_task(Cmd.BANDWIDTH, vps.id, False, "exception %s" % (str(e))) return
def vps_change_ip(self, vps_info): self.logger.info("to change ip vps %s" % (vps_info.id)) if not self.vpsinfo_check_ip(vps_info): msg = "no ip with vps %s" % (vps_info.id) self.logger.error(msg) return try: xv = XenVPS(vps_info.id) self.setup_vps(xv, vps_info) self.vpsops.change_ip(xv) except Exception, e: self.logger.error(e)
def _load_vps_meta(self, meta_path): f = open(meta_path, "r") data = None try: data = json.load(f) finally: f.close() try: xv = XenVPS.from_meta(data) return xv except Exception, e: raise Exception("file=%s, %s" % (meta_path, str(e)))
def vps_close(self, vps_info): try: if vps_info.state == VM_STATE.CLOSE or vps_info.host_id != self.host_id: xv = XenVPS(vps_info.id) self.setup_vps(xv, vps_info) self.vpsops.close_vps(vps_info.id, xv) else: raise Exception("state check not passed") except Exception, e: self.logger.exception(e) self.done_task(CMD.CLOSE, vps_info.id, False, "exception %s" % (str(e))) return
def vps_open(self, vps_info, vps_image=None, is_new=True): vps_id = vps_info.id self.logger.info("to open vps %s" % (vps_id)) if vps_info.host_id != self.host_id: msg = "vpsopen : vps %s host_id=%s != current host %s , abort" % ( vps_id, vps_info.host_id, self.host_id) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return if not self.vpsinfo_check_ip(vps_info): msg = "no ip with vps %s" % (vps_id) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return xv = XenVPS(vps_id) try: domain_dict = XenStore.domain_name_id_map() limit = None if 'VPS_NUM_LIMIT' in dir(conf): limit = conf.VPS_NUM_LIMIT if limit and len(domain_dict.keys()) >= limit + 1: msg = "vps open: cannot open more than %d vps" % (limit) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return self.setup_vps(xv, vps_info) if xv.is_running(): msg = "vps %s is running" % (vps_id) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, True, msg) return if vps_info.state in [VM_STATE.PAY, VM_STATE.OPEN, VM_STATE.CLOSE]: if self.vpsops.is_normal_exists(vps_id): xv.check_storage_integrity() xv.check_xen_config() if not xv.is_running(): self.logger.info( "seems vps %s was not closed, try to boot" % (vps_id)) self.vpsops._boot_and_test(xv, is_new=False) elif self.vpsops.is_trash_exists(vps_id): self.vpsops.reopen_vps(vps_id, xv) else: self.vpsops.create_vps(xv, vps_image, is_new) else: msg = "vps%s state is %s(%s)" % ( str(vps_id), vps_info.state, VM_STATE_CN[vps_info.state]) self.logger.error(msg) self.done_task(CMD.OPEN, vps_id, False, msg) return except Exception, e: self.logger.exception("vps %s: %s" % (str(vps_id), str(e))) self.done_task(CMD.OPEN, vps_id, False, "error, " + str(e)) return
def gen_meta (vps_mgr, domain_dict, vps_id): xv = XenVPS (vps_id) vps = vps_mgr.query_vps (vps_id) if not vps_mgr.vps_is_valid (vps): print "no backend data for %s " % vps_id return domain_id = domain_dict[xv.name] vif_datas = XenStore.get_vif_by_domain_id (domain_id) vps_mgr.setup_vps (xv, vps) #xv.add_extra_storage (1, 964) #TEST vpsops = VPSOps (vps_mgr.logger) if len (vif_datas.values ()) == 1: vif_data = vif_datas.values()[0] if vif_data['online'] == '1': if vif_data.has_key ('vifname'): xv.vifs[vif_data['vifname']].mac = vif_data['mac'] else: vif_id = vif_datas.items ()[0][0] vif_name = 'vif%s.%s' % (domain_id, vif_id) del xv.vifs[xv.name] xv.add_netinf (name=vif_name, ip=xv.ip, netmask=xv.netmask,bridge=vif_data['bridge'], mac=vif_data['mac']) #vpsops.create_xen_config (xv) vpsops.save_vps_meta (xv, override=False)
def test_vps0 (self): print "create vps00" logger = Log ("test", config=conf) vpsops = VPSOps (logger) xv = XenVPS (0) try: xv.setup (os_id=10001, vcpu=1, mem_m=512, disk_g=7, ip="10.10.2.2", netmask="255.255.255.0", gateway="10.10.2.1", root_pw="fdfdfd") xv.add_extra_storage (disk_id=1, size_g=1, fs_type='ext3') print xv.gen_xenpv_config () vpsops.create_vps (xv) except Exception, e: logger.exception (e) raise e
def vps_open(self, vps, vps_image=None, is_new=True): self.logger.info("to open vps %s" % (vps.id)) if vps.host_id != self.host_id: msg = "vpsopen : vps %s host_id=%s != current host %s , abort" % ( vps.id, vps.host_id, self.host_id) self.logger.error(msg) self.done_task(Cmd.OPEN, vps.id, False, msg) return if not vps.ipv4 or not vps.ipv4_gateway or vps.cpu <= 0 or vps.ram <= 0 or vps.hd <= 0 or not vps.password: self.logger.error("vps open: invalid vps data received: %s" % (self.dump_vps_info(vps))) self.done_task(Cmd.OPEN, vps.id, False, "invalid vps data") return xv = XenVPS(vps.id) vpsops = VPSOps(self.logger) try: self.setup_vps(xv, vps) if xv.is_running(): msg = "vps %s is running" % (vps.id) self.logger_err.error(msg) self.done_task(Cmd.OPEN, vps.id, False, msg) return if vps.state in [vps_const.VPS_STATE_PAY, vps_const.VPS_STATE_RUN]: vpsops.create_vps(xv, vps_image, is_new) elif vps.state == vps_const.VPS_STATE_CLOSE: vpsops.reopen_vps(vps.id, xv) else: msg = "vps%s state is %s(%s)" % (str( vps.id), vps.state, vps_const.VPS_STATE2CN[vps.state]) self.logger_err.error(msg) self.done_task(Cmd.OPEN, vps.id, False, msg) return except Exception, e: self.logger_err.exception("for %s: %s" % (str(vps.id), str(e))) self.done_task(Cmd.OPEN, vps.id, False, "error, " + str(e)) return
def _reinstall_os(self, vps_id, _vps=None, os_id=None, vps_image=None): try: vpsops = VPSOps(self.logger) xv = None if _vps: xv = XenVPS(_vps.id) self.setup_vps(xv, _vps) vpsops.reinstall_os(vps_id, xv, os_id, vps_image) if _vps: self.done_task(Cmd.OS, vps_id, True) return True except Exception, e: self.logger_err.exception("for %s: %s" % (str(vps_id), str(e))) if _vps: self.done_task(Cmd.OS, vps_id, False, "error," + str(e)) return False
def _reinstall_os(self, vps_id, vps_info=None, os_id=None, vps_image=None): try: xv = None if vps_info: xv = XenVPS(vps_info.id) self.setup_vps(xv, vps_info) self.vpsops.reinstall_os(vps_id, xv, os_id, vps_image) if vps_info: self.done_task(CMD.OS, vps_id, True, "os:%s" % (vps_info.os)) return True except Exception, e: self.logger.exception("vps %s: %s" % (str(vps_id), str(e))) if vps_info: self.done_task(CMD.OS, vps_id, False, "os:%s, error: %s " % (vps_info.os, str(e))) return False
def test_vps0(self): print "create vps00" logger = Log("test", config=conf) vpsops = VPSOps(logger) xv = XenVPS(0) try: xv.setup(os_id=10001, vcpu=1, mem_m=512, disk_g=7, ip="10.10.2.2", netmask="255.255.255.0", gateway="10.10.2.1", root_pw="fdfdfd") xv.add_extra_storage(disk_id=1, size_g=1, fs_type='ext3') print xv.gen_xenpv_config() vpsops.create_vps(xv) except Exception, e: logger.exception(e) raise e
def vps_upgrade(self, vps_info): self.logger.info("to upgrade vps %s" % (vps_info.id)) if not self.vpsinfo_check_ip(vps_info): msg = "no ip with vps %s" % (vps_info.id) self.logger.error(msg) self.done_task(CMD.UPGRADE, vps_info.id, False, msg) return try: xv = XenVPS(vps_info.id) self.setup_vps(xv, vps_info) self.vpsops.upgrade_vps(xv) self.refresh_host_space() self.done_task(CMD.UPGRADE, vps_info.id, True) return True except Exception, e: self.logger.exception("vps %s: %s" % (str(vps_info.id), str(e))) self.done_task(CMD.UPGRADE, vps_info.id, False, "exception %s" % str(e)) return False
def test_meta (self): print "test vps meta" logger = Log ("test", config=conf) vpsops = VPSOps (logger) vps = XenVPS (0) vps.setup (os_id=50001, vcpu=1, mem_m=500000, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") vps.add_extra_storage (disk_id=1, size_g=1, fs_type='ext3') vps.add_netinf ('vps0_inter', "10.10.3.2", '255.255.255.0', 'xenbr0', None) vps.data_disks['xvdc1']._set_expire_days (1) print "trash_date", vps.data_disks['xvdc1'].trash_date print "expire_date", vps.data_disks['xvdc1'].expire_date vpsops.save_vps_meta (vps) _vps = vpsops.load_vps_meta (0) self.assertEqual (_vps.vps_id, vps.vps_id) self.assertEqual (_vps.os_id, vps.os_id) self.assertEqual (_vps.vcpu, vps.vcpu) self.assertEqual (_vps.mem_m, vps.mem_m) self.assertEqual (_vps.ip, vps.ip) self.assertEqual (_vps.netmask, vps.netmask) self.assertEqual (_vps.gateway, vps.gateway) self.assertEqual (_vps.root_store.size_g, vps.root_store.size_g) self.assertEqual (_vps.swap_store.size_g, vps.swap_store.size_g) self.assertEqual (_vps.data_disks['xvdc1'].size_g, 1) self.assertEqual (_vps.data_disks['xvdc1'].fs_type, 'ext3') self.assertEqual (_vps.data_disks['xvdc1'].mount_point, '/mnt/data1') self.assertEqual (_vps.data_disks['xvdc1'].xen_dev, 'xvdc1') self.assertEqual (_vps.data_disks['xvdc1'].trash_date, vps.data_disks['xvdc1'].trash_date) self.assertEqual (_vps.data_disks['xvdc1'].expire_date, vps.data_disks['xvdc1'].expire_date) print _vps.data_disks['xvdc1'].__class__.__name__ self.assertEqual (len (_vps.vifs.values ()), 2) self.assertEqual (_vps.vifs['vps0_inter'].ip, '10.10.3.2') self.assertEqual (_vps.vifs['vps0_inter'].netmask, '255.255.255.0') self.assertEqual (_vps.vifs['vps0_inter'].bridge, 'xenbr0') self.assertEqual (_vps.vifs['vps0_inter'].mac, vps.vifs['vps0_inter'].mac) print "test trash expire date None" vps.data_disks['xvdc1']._set_expire_days (None) self.assertEqual (vps.data_disks['xvdc1'].trash_date, None) self.assertEqual (vps.data_disks['xvdc1'].expire_date, None) vpsops.save_vps_meta (vps) _vps = vpsops.load_vps_meta (0) self.assertEqual (_vps.data_disks['xvdc1'].trash_date, None) self.assertEqual (_vps.data_disks['xvdc1'].expire_date, None)
def test_mem_too_big(self): print "test mem too big" vps = XenVPS(0) logger = Log("test", config=conf) vps.setup(os_id=50001, vcpu=1, mem_m=500000, disk_g=7, ip="10.10.1.2", netmask="255.255.255.0", gateway="10.10.1.1", root_pw="fdfdfd") try: vps.check_resource_avail() except Exception, e: logger.exception(e) print "exception caught", type(e), str(e) return
from ops.saas_rpc import VM_STATE, VM_STATE_CN def _check_bandwidth(client, vps_id): meta = client.vpsops._meta_path(vps_id, is_trash=False) if not os.path.exists(meta): print >> sys.stderr, vps_id, "has no meta" return xv = client.vpsops._load_vps_meta(meta) vps_info = None try: vps_info = client.query_vps(vps_id) except Exception, e: print str(e) return _xv = XenVPS(vps_id) client.setup_vps(_xv, vps_info) if vps_info.host_id != conf.HOST_ID: is_running = xv.is_running() and "(running)" or "(not running)" print "vps %s %s host_id=%s not on this host" % (vps_id, is_running, vps_info.host_id) return elif vps_info.state != VM_STATE.OPEN: is_running = xv.is_running() and "(running)" or "(not running)" print "vps %s %s backend state=%s " % (vps_id, is_running, VM_STATE_CN[vps_info.state]) return if not _xv.vif_ext or not xv.vif_ext: return if xv.vif_ext.bandwidth != _xv.vif_ext.bandwidth: print "vps", vps_id, "bandwidth old:", xv.vif_ext.bandwidth, "new:", _xv.vif_ext.bandwidth
return def reopen_vps(vps_id): client = VPSMgr() vps_info = None try: vps_info = client.query_vps(vps_id) except Exception, e: print "failed to query vps state: %s %s " % (type(e), str(e)) return False if not vps_info: print "not backend data for vps %s" % (vps_id) return False try: xv = XenVPS(vps_info.id) client.setup_vps(xv, vps_info) client.vpsops.reopen_vps(vps_id, xv) client.done_task(CMD.OPEN, vps_id, True) return True except Exception, e: client.logger.exception("for %s: %s" % (str(vps_id), str(e))) client.done_task(CMD.OPEN, vps_id, False, "error, " + str(e)) return False if __name__ == '__main__': if len(sys.argv) <= 1: usage() os._exit(0) optlist, args = getopt.gnu_getopt(sys.argv[1:], "", [
ip_ok = "ip %s " % (xv.ip) + \ (vps_common.ping(xv.ip) and "reachable" or "timeout") print "vps %s %s %s has no meta_data" % (vps_id, is_running, ip_ok) return def check_via_backend(client, vps_id): vps_info = None try: vps_info = client.query_vps(vps_id) if not vps_info: return False, None except Exception, e: raise e xv = XenVPS(vps_id) if vps_info.host_id != conf.HOST_ID: is_running = xv.is_running() and "(running)" or "(not running)" print "vps %s %s host_id=%s not on this host" % (vps_id, is_running, vps_info.host_id) return True, None elif vps_info.state != VM_STATE.OPEN: is_running = xv.is_running() and "(running)" or "(not running)" print "vps %s %s backend state=%s " % (vps_id, is_running, VM_STATE_CN[vps_info.state]) return True, None return False, vps_info def check_all_vps(): client = VPSMgr() all_ids = client.vpsops.all_vpsid_from_config() print "xen_config: %d, running: %d" % (len(all_ids), client.vpsops.running_count)
ip_ok = "ip %s " % (xv.ip) + \ (vps_common.ping(xv.ip) and "reachable" or "timeout") print "vps %s %s %s has no meta_data" % (vps_id, is_running, ip_ok) return def check_via_backend(client, vps_id): vps_info = None try: vps_info = client.query_vps(vps_id) if not vps_info: return False, None except Exception, e: raise e xv = XenVPS(vps_id) if vps_info.host_id != conf.HOST_ID: is_running = xv.is_running() and "(running)" or "(not running)" print "vps %s %s host_id=%s not on this host" % (vps_id, is_running, vps_info.host_id) return True, None elif vps_info.state != VM_STATE.OPEN: is_running = xv.is_running() and "(running)" or "(not running)" print "vps %s %s backend state=%s " % (vps_id, is_running, VM_STATE_CN[vps_info.state]) return True, None return False, vps_info def check_all_vps(): client = VPSMgr()