Beispiel #1
0
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
Beispiel #2
0
    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
Beispiel #3
0
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)
Beispiel #4
0
 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
Beispiel #5
0
 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
Beispiel #6
0
 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
Beispiel #7
0
 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
Beispiel #8
0
 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
Beispiel #9
0
 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
Beispiel #10
0
 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
Beispiel #11
0
 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)
Beispiel #12
0
 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)
Beispiel #13
0
 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
Beispiel #14
0
 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
Beispiel #15
0
 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
Beispiel #16
0
 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
Beispiel #17
0
 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
Beispiel #18
0
 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
Beispiel #19
0
 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
Beispiel #20
0
 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
Beispiel #21
0
    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:], "", [
Beispiel #22
0
            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()