Source

zpage / model / vps_ip.py

Full commit
#coding:utf-8

from _db import redis, Model, McNum
from zkit.ip import ip2int, int2ip

#CREATE TABLE `zpage`.`vps_ip` (
#  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
#  `gateway` INTEGER UNSIGNED NOT NULL,
#  `data_center` INTEGER UNSIGNED NOT NULL,
#  `netmask` INTEGER UNSIGNED NOT NULL,
#  `ip` INTEGER UNSIGNED NOT NULL,
#  `vps_one_id` INTEGER UNSIGNED NOT NULL,
#  PRIMARY KEY (`id`),
#  INDEX `index_2`(`data_center`)
#)
#ENGINE = MyISAM;
#
#
#CREATE TABLE `zpage`.`vps_ip_log` (
#  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
#  `ip` INTEGER UNSIGNED NOT NULL,
#  `vps_one_id` INTEGER UNSIGNED NOT NULL,
#  `time` INTEGER UNSIGNED NOT NULL,
#  PRIMARY KEY (`id`),
#  INDEX `index_2`(`ip`)
#)
#ENGINE = MyISAM;

REDIS_VPS_IP_TOTAL = 'VpsIpTotal'
REDIS_VPS_IP_USED = 'VpsIpUsed'

class VpsIp(Model):
    @property
    def ip_str(self):
        return int2ip(self.ip)

    @property
    def netmask_str(self):
        return int2ip(self.netmask)

    @property
    def gateway_str(self):
        return int2ip(self.gateway)


def vps_ip_unbind(ip, vps_one_id):
    ip = VpsIp.get(ip=ip)
    if not ip:
        return False
    print ip, ip.vps_one_id, ip.vps_one_id == vps_one_id, vps_one_id, '!!!'
    if ip.vps_one_id and ip.vps_one_id == vps_one_id:
        redis.hincrby(REDIS_VPS_IP_USED, ip.data_center, -1)
        ip.vps_one_id = 0
        ip.save()

def vps_ip_bind(ip, vps_one_id):
    ip = VpsIp.get(ip=ip)
    if not ip:
        return False
    if not ip.vps_one_id:
        redis.hincrby(REDIS_VPS_IP_USED, ip.data_center, 1)
    ip.vps_one_id = vps_one_id
    ip.save()
    return ip

def vps_ip_new(data_center, gateway, netmask, begin, end=None):
    gateway = int(gateway)
    netmask = int(netmask)
    total = 0
    if end is None:
        end = begin
    for ip in xrange(begin, end+1):
        if ip in (gateway, netmask):
            continue
        vps = VpsIp.get_or_create(ip=ip)
        if not vps.id:
            total += 1
        vps.netmask = netmask
        vps.gateway = gateway
        vps.data_center = data_center
        vps.save()
    redis.hincrby(REDIS_VPS_IP_TOTAL, data_center, total)
    return total

def vps_ip_used_total():
    return redis.hgetall(REDIS_VPS_IP_USED), redis.hgetall(REDIS_VPS_IP_TOTAL)

def vps_ip_rm(id):
    ip = VpsIp.get(id)
    if ip:
        data_center = ip.data_center
        redis.hincrby(REDIS_VPS_IP_TOTAL, data_center, -1)
        if ip.vps_one_id:
            redis.hincrby(REDIS_VPS_IP_USED, data_center, -1)
        ip.delete()

def vps_ip_count(data_center=0):
    r = VpsIp.where()
    if data_center:
        r = r.where(data_center=data_center) 
    return r.count()

def vps_ip_list(data_center, limit, offset):
    return VpsIp.where(data_center=data_center).order_by('id desc')[offset:limit+offset]

def vps_ip_autobind(id , data_center):
    ip = VpsIp.get(data_center=data_center, vps_one_id=0)
    if ip:
        from model.vps_sell import vps_one_ip_new
        return vps_one_ip_new(id, ip.ip)

def vps_ip_count_reset():
    for i in redis.hgetall(REDIS_VPS_IP_TOTAL):
        redis.hset(REDIS_VPS_IP_TOTAL, i, VpsIp.where(data_center=i).count())
        redis.hset(REDIS_VPS_IP_USED, i, VpsIp.where('vps_one_id!=0').where(data_center=i).count())

if __name__ == '__main__':

    #VpsIp.where().update(vps_one_id=0)
    from vps_sell import VpsOneIp, VpsOne
    for i in VpsIp.where():
        print i.data_center, i.ip, i.vps_one_id
    #    ip = i.ip
    #    vip = VpsIp.get(ip=i.ip)
    #    if not i.vps_one_id:
    #        i.delete()
    #        continue
    #    if vip:
    #        vps_ip_bind(ip, i.vps_one_id)
    #        one = VpsOne.get(i.vps_one_id)
    #        one.data_center = vip.data_center
    #        one.save()
    #    else:
    #        print i.vps_one_id, i.ip_str
    #        if i.ip_str.startswith('119.254.'):
    #            netmask = ip2int('255.255.255.240')
    #            gateway = ip2int('119.254.35.97')
    #            print netmask, gateway
    #            one = VpsOne.get(i.vps_one_id)
    #            vps_ip_new(one.data_center, gateway, netmask, i.ip)

    #vps_ip_count_reset()