Source

zpage / model / vps_host.py

Full commit
#coding:utf-8

#CREATE TABLE  `zpage`.`vps_host` (
#  `id` int(10) unsigned NOT NULL auto_increment,
#  `ip` int(10) unsigned NOT NULL,
#  `data_center` int(10) unsigned NOT NULL,
#  `ram_count` int(10) unsigned NOT NULL,
#  `ram_used` int(10) unsigned NOT NULL,
#  `state` tinyint(3) unsigned NOT NULL,
#  `hd_used` int(10) unsigned NOT NULL,
#  `hd_count` int(10) unsigned NOT NULL,
#  `vps_count` int(10) unsigned NOT NULL default '0',
#txt
#  PRIMARY KEY  (`id`),
#  KEY `index_2` USING BTREE (`data_center`,`state`)
#) ENGINE=MyISAM DEFAULT CHARSET=utf8;

from _db import McModel, Model, McNum, McCache, mc, redis, McCacheM

REDIS_VPS_REAL = 'VpsHost' #记录data_center的机器数目

VPS_REAL_STATE_STOP = 10 #未启用
VPS_REAL_STATE_RUN = 20


class VpsHost(Model):
    @property
    def ram_used(self):
        return self.ram_count - self.ram_remain

    @property
    def hd_used(self):
        return self.hd_count - self.hd_remain

def vps_host_new(ip, data_center, ram_count, hd_count, txt):
    data_center = int(data_center)
    ram = int(ram_count)
    real = VpsHost(
        ip=ip,
        data_center=data_center,
        ram_count=ram,
        ram_remain=ram,
        hd_count=hd_count,
        hd_remain=hd_count,
        state=VPS_REAL_STATE_RUN,
        txt=txt
    )
    real.save()
    redis.hincrby(REDIS_VPS_REAL, data_center, 1)
    return real.id

def vps_host_count():
    return redis.hgetall(REDIS_VPS_REAL)

def vps_host_list(data_center):
    return VpsHost.where(data_center=data_center)

def vps_host_id_bind(vps):
    if vps.host_id:
        return
    data_center = vps.data_center
    ram = vps.ram
    hd = vps.hd
    if data_center == 2:
        if ram < 1500:
            host_id = 9
        else:
            host_id = 6
        host = VpsHost.get(host_id)
    else:
        host = VpsHost.where(data_center=data_center).where(
            'ram_remain>%s and hd_remain>%s', ram, hd
        ).order_by('ram_remain')[:1]
        if host:
            host = host[0]

    if host:
        vps_host_mv(vps, host.id)

def vps_host_mv(vps, host_id):
    vps_host_rm(vps)


    host = VpsHost.get(host_id)

    if host:
        vps.host_id = host_id
        vps.save()

        host.vps_count += 1
        host.ram_remain -= vps.ram
        host.hd_remain -= vps.hd
        host.save()

def vps_host_rm(vps):
    host_id = vps.host_id
    if not host_id:
        return
    host = VpsHost.get(host_id)
    host.vps_count -= 1
    host.ram_remain += vps.ram
    host.hd_remain += vps.hd
    host.save()


def vps_host_rebuild():
    from model.vps_sell import VpsOne, VPS_STATE_RUN
    from collections import defaultdict

    host_dict = defaultdict(list)

    for i in VpsOne.where('host_id!=0').where(state=VPS_STATE_RUN):
        host_dict[i.host_id].append(i)


    for k, v in host_dict.iteritems():
        host = VpsHost.get(k)
        host.vps_count = len(v)
#        host.hd_remain = host.hd_count - sum(i.hd for i in v)
#        host.ram_remain = host.ram_count - sum(i.ram for i in v)
        host.save()


if __name__ == '__main__':
    #print v
    #print host
    #raise
    #    i.delete()
#    host = VpsHost.get(95)   
#    host.ram_remain
#    host.ram_count = 96*1024 
#    host.save()
#    print host.ram_count
#    print host.ram_remain 
#    
    #pass

    #host = VpsHost.get(5)
    #print host.hd_count
    #host.hd_count= 3550
    #host.save()

    vps_host_rebuild()
    #from model.vps_sell import VpsOne, VPS_STATE_RUN

    pass
    #print vps.host_id
    #total = 0
    #for i in VpsOne.where(data_center=1).where(state=VPS_STATE_RUN):
    #    total += i.ram
    #print total
    #host = VpsHost.get(2)
    #host.ram_count = total + 9035
    #host.ram_remain = 9035
    #host.save()
    #print host.ram_count