Source

zpage / model / vps_sell.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
# -*- coding: utf-8 -*-
from collections import defaultdict
from operator import itemgetter
from _db import McModel, Model, McNum, McCache, mc, redis, McCacheM
from days import today_days, ONE_DAY
from zkit.attrcache import attrcache
from zkit.algorithm.unique import unique
from model.user_mail import mail_by_user_id
from zkit.ip import int2ip
from zkit.password import password_random
from model.vps_ip import vps_ip_autobind, vps_ip_bind, vps_ip_unbind
from vps_host import VpsHost, vps_host_id_bind, vps_host_rm, vps_host_mv
from const.vps import *

from mail import mq_rendermail as rendermail, mq_sendmail
#from mail import rendermail

MAIL_VPS_ORDER = '42qu-vps-order@googlegroups.com'

'''
ALTER TABLE  `vps_one` ADD  `machine_type` INT UNSIGNED NULL ;
ALTER TABLE  `vps_type` ADD  `data_center` TINYINT UNSIGNED NULL ;
'''


#from mail import rendermail
#MAIL_VPS_ORDER = 'zsp042@gmail.com'


VPS_OS_DEFAULT = 2 #CentOS-6.2

VPS_TYPE_STATE_HIDE = 5
VPS_TYPE_STATE_FOR_SELL = 10


VPS_ORDER_STATE_NEW = 5
VPS_ORDER_STATE_PAY = 10
VPS_ORDER_STATE_OPEN = 15
VPS_ORDER_STATE_CANCEL = 20

VPS_ORDER_STATE2CN = {
    VPS_ORDER_STATE_NEW: '未支付',
    VPS_ORDER_STATE_PAY: '待开通',
    VPS_ORDER_STATE_OPEN: '已成交',
    VPS_ORDER_STATE_CANCEL:'被撤销',
}

VPS_ORDER_STATE_LIST = (
    VPS_ORDER_STATE_PAY,
    VPS_ORDER_STATE_OPEN,
    VPS_ORDER_STATE_CANCEL,
    VPS_ORDER_STATE_NEW,
)

vps_order_count = McNum(
    lambda state:VpsOrder.where(state=state).count(),
    'VpsOrderCount:%s'
)
vps_one_count = McNum(
    lambda state:VpsOne.where(state=state).count(),
    'VpsOneCount:%s'
)
vps_one_count_by_user_id = McNum(
    lambda user_id:VpsOne.where(user_id=user_id).where('state>%s'%VPS_STATE_RM).count(),
    'VpsOneCountByUserId.%s'
)

VPS_OS_ALL = unique([i.split('-', 1)[0] for i in VPS_OS_DICT.itervalues()])

VPS_PRICE_TIME_MONTH = 1
VPS_PRICE_TIME_SEASON = 2
VPS_PRICE_TIME_YEAR = 3


VPS_PRICE_NAME_DICT = {
    VPS_PRICE_TIME_MONTH: '月',
    VPS_PRICE_TIME_SEASON: '季',
    VPS_PRICE_TIME_YEAR: '年',
}

@property
def ram_human(self):
    ram = self.ram
    if ram < 1024:
        s = '%sM'
    else:
        s = '%sG'
        ram = ram//1024
    return s%ram

class VpsType(McModel):
    ram_human = ram_human

#@mc_vps_type_id_list('{country_id}')
#def vps_type_id_list(country_id):
#    return VpsType.where(country=country_id).order_by('level').col_list()




class VpsPrice(McModel):
    @attrcache
    def vps_type(self):
        return VpsType.mc_get(self.vps_type_id)


def vps_type_list():
    d = defaultdict(dict)
    for i in VpsType.where(state=VPS_TYPE_STATE_FOR_SELL):
        d[i.country][i.level] = i
    li = []
    for country, _d in sorted(d.iteritems(), key=itemgetter(0)):
        li.append((country, tuple(v for k, v in sorted(_d.iteritems(), key=itemgetter(0)))))
    return tuple(li)

VPS_TYPE_LIST = vps_type_list()


def vps_price_dict_for_sell():
    d = defaultdict(dict)
    r = []
    for j in VPS_TYPE_LIST:
        r.extend(j[1])
    for k in r:
        for i in VpsPrice.where(vps_type_id=k.id):
            d[i.vps_type_id][i.cid] = i
    return dict((k, tuple(sorted(v.iteritems(), key=itemgetter(0)))) for k, v in d.iteritems())

VPS_PRICE_DICT_FOR_SELL = vps_price_dict_for_sell()

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


class VpsOne(McModel):
    ram_human = ram_human

    @property
    def user(self):
        from model.zsite import Zsite
        return Zsite.mc_get(self.user_id)

    @property
    def ip_inter_str(self):
        return int2ip(self.ip_inter)

    def state_set(self, state):
        self_state = self.state
        if self_state != state:
            if state == VPS_STATE_CLOSE:
                from yun.vps.server.model.vps import vps_saas_cmd_close
                vps_saas_cmd_close(self.host_id, self.id)
            self.state = state
            self.save()
            for i in (self_state, state):
                vps_one_count.delete(i)

    @property
    def ip_list(self):
        result = []
        for i in self.vps_one_ip_list:
            result.append(int2ip(i.ip))
        return result

    @property
    def vps_one_ip_list(self):
        return list(VpsOneIp.where(vps_one_id=self.id))

    def ip_autobind(self):
        id = self.id
        for i in self.vps_one_ip_list:
            o = vps_ip_bind(i.ip, id)
            if o:
                return o
        return vps_ip_autobind(id, self.data_center)

    @property
    def price_list(self):
        return vps_price_list(self.vps_type_id)

mc_vps_price_list = McCacheM('VpsPriceList:%s')

@mc_vps_price_list('{vps_type_id}')
def vps_price_list(vps_type_id):
    r = []
    for i in VpsPrice.where(vps_type_id=vps_type_id).order_by('cid'):
        r.append(
            (i.id, VPS_PRICE_NAME_DICT[i.cid])
        )
    return tuple(r)

class VpsOneOrder(McModel):
    pass

class VpsOrder(Model):
    def state_set(self, state):
        self_state = self.state
        if self_state != state:
            self.state = state
            self.save()
            vps_order_count.delete(state)
            vps_order_count.delete(self_state)


def vps_one_list_by_vps_order(vps_order):
    vps_order_id = vps_order.id
    id_list = VpsOneOrder.where(vps_order_id=vps_order_id).order_by('id desc').col_list(col='vps_one_id')
    result = VpsOne.mc_get_list(id_list)

    diff = vps_order.count - len(result)
    if diff > 0 and vps_order.state == VPS_ORDER_STATE_PAY:
        data_center = vps_order.data_center
        user_id = vps_order.user_id
        os = vps_order.os

        vps_price = VpsPrice.mc_get(vps_order.vps_price_id)


        for i in xrange(diff):
            vps = _vps_by_vps_type(
                user_id, os, data_center, vps_price.days, vps_price.vps_type_id
            )
            VpsOneOrder(
                vps_one_id=vps.id,
                vps_order_id=vps_order_id,
            ).save()
            result.append(vps)
    #o.ip_detail = vps_ip_autobind(o)
    return result


def _vps_by_vps_type(user_id, os, data_center, days, vps_type_id):
    vps_type = VpsType.mc_get(vps_type_id)

    country = vps_type.country
    cpu = vps_type.cpu
    ram = vps_type.ram
    hd = vps_type.hd
    netflow = vps_type.netflow

    return _vps_one_pay(
        user_id, os, country, data_center,
        cpu, ram, hd, netflow, days,
        vps_type_id
    )
    

def _vps_one_pay(user_id, os, country, data_center, cpu, ram, hd, netflow, days, vps_type_id):
    today = today_days()
    o = VpsOne(
        user_id=user_id,
        os=os,
        country=country,
        data_center=data_center,
        cpu=cpu,
        ram=ram,
        hd=hd,
        netflow=netflow,
        password=password_random(12),
        date_open=0,
        date_close=days,
        state=VPS_STATE_PAY,
        txt='',
        vps_type_id=vps_type_id
    )
    vps_host_id_bind(o)
    o.save()
    return o

from zkit.ip import ip2int

def vps_one_ip_rm(id):
    ip = VpsOneIp.get(id)
    vps_one_id = ip.vps_one_id
    ip.vps_one_id = 0
    ip.save()
    vps_ip_unbind(ip.ip, vps_one_id)
    return vps_one_id


def vps_one_ip_new(id, ip):
    if type(ip) is str:
        if '.' in ip:
            ip = ip2int(ip)
        else:
            ip = int(ip)
    _ip = vps_ip_bind(ip, id)

    if _ip:
        vip = VpsOneIp.get_or_create(ip=ip)
        vip.vps_one_id = id
        vip.save()

        vps = VpsOne.mc_get(id)
        if vps:
            vps.data_center = _ip.data_center
            vps.country = VPS_DATA_CENTER_COUNTRY[vps.data_center]
            vps.save()
        return _ip

def vps_one_open(id, ip_inter, txt):
    o = VpsOne.mc_get(id)
    if o and o.state == VPS_STATE_PAY:

        if ip_inter:
            o.ip_inter = ip2int(ip_inter)


        if txt:
            o.txt = txt

        if o.date_open == 0:
            today = today_days()
            o.date_open = today
            o.date_close += today

        o.state_set(VPS_STATE_RUN)

        return o

def vps_order_cancel(id):
    order = VpsOrder.get(id)
    order.state = VPS_ORDER_STATE_CANCEL
    order.save()

    for i in ( VPS_ORDER_STATE_PAY, VPS_ORDER_STATE_CANCEL):
        vps_order_count.delete(i)

def vps_order_new(user_id, vps_price_id, os, data_center, count, txt):
    count = int(count)
    if count <= 0:
        return

    vp = VpsPrice.mc_get(vps_price_id)
    cent = vp.cent*count

    o = VpsOrder(
        vps_price_id=vps_price_id,
        user_id=user_id,
        count=count,
        cent=cent,
        state=VPS_ORDER_STATE_NEW,
        txt=txt,
        os=os,
        data_center=data_center,
    )
    o.save()
    vps_order_count.delete(VPS_ORDER_STATE_NEW)
    return o

def vps_one_renew_pay(id):
    from money import REDIS_RENEW_KEY
    vps_one = VpsOne.mc_get(id)

    days = int(redis.get(REDIS_RENEW_KEY%id))

    vps_one.date_close += days
    vps_one.save()
    if vps_one.state == VPS_STATE_CLOSE:
        from yun.vps.server.model.vps import vps_saas_cmd_open
        vps_saas_cmd_open(vps_one.host_id, vps_one.id)


def vps_order_pay(id):
    o = VpsOrder.get(id)
    if o and o.state == VPS_ORDER_STATE_NEW:
        o.state_set(VPS_ORDER_STATE_PAY)

        rendermail(
           '/mail/vps/selled.txt',
           MAIL_VPS_ORDER,
           vps_order_id=o.id,
           data_center=VPS_DATA_CENTER_CN.get(o.data_center, '???'),
        )
        vps_order_auto_open(o)

        vps_one_count_by_user_id.delete(o.user_id)
        return o

def vps_order_open_by_vps_id(vps_id):
    vps = VpsOne.mc_get(vps_id)
    if not vps:
        return 

    if vps.state == VPS_STATE_CLOSE:
        vps.state_set(VPS_STATE_RUN)
        return

    vps_one_open(vps_id, '0', '')
    o = VpsOneOrder.get(vps_one_id=vps_id)

    if o:
        vps_order_id = o.vps_order_id
        for i in VpsOneOrder.where(vps_order_id=vps_order_id).where('vps_one_id!=%s', vps_id).col_list(col='vps_order_id'):
            one = VpsOne.get(i.vps_one_id)
            if one.state != VPS_STATE_RUN:
                return

        vps_order = VpsOrder.get(vps_order_id)
        if vps_order:
            return vps_order_open(vps_order)
    else:
        subject = "手工开通 VPS %s 成功"% vps_id
        _vps_open_mail(subject, [vps_id])

def vps_order_open(vps_order, mail=True):
    from model.money import pay_vps_finish
    if vps_order:
        vps_order.state_set(VPS_ORDER_STATE_OPEN)
        pay_vps_finish(vps_order.id)
        if mail:
            id_list = VpsOneOrder.where(vps_order_id=vps_order.id).order_by('id desc').col_list(col='vps_one_id')

            subject = '订单.%s VPS主机开通'%vps_order.id
            _vps_open_mail(subject, id_list)

def _vps_open_mail(subject, id_list):
    vps_one_list = []

    for vps_one in VpsOne.mc_get_list(id_list):
        vps_one_list.append(
            (
                vps_one.id,
                vps_one.ip_list,
                vps_one.password,
                vps_one.txt
            )
        )
    if not vps_one_list:
        return
    from namecard import namecard_mail_and_user_mail
    mail_list = namecard_mail_and_user_mail(vps_one.user_id)
    mail_list.append(MAIL_VPS_ORDER)

    rendermail(
        '/mail/vps/order_open.htm',
        mail_list,
        format='html',
        subject=subject,
        vps_one_list=vps_one_list
    )



def vps_order_by_state(state, limit, offset):
    return VpsOrder.where(state=state).order_by('id desc')[offset:limit+offset]

def vps_one_by_state(state, limit, offset):
    return VpsOne.where(state=state).order_by('id desc')[offset:limit+offset]

def vps_one_list_by_user_id(user_id):
    return VpsOne.where(user_id=user_id).where('state>%s'%VPS_STATE_RM)[:]


def vps_one_upgrade_os(vps, os, reset=False):
    os = int(os)
    if os in VPS_OS_DICT:
        if reset or (os != vps.os):
            from yun.vps.server.model.vps import vps_saas_cmd_os
            vps_saas_cmd_os(vps.host_id, vps.id)

            vps.os = os
            vps.save()

def vps_one_upgrade(vps_id, vps_type, vps_host, vps_os, reset=False):
    one = VpsOne.get(vps_id)
    t = VpsType.get(vps_type)

    if one and t:
        one.cpu = t.cpu
        one.ram = t.ram
        one.hd = t.hd
        one.vps_type_id = vps_type
        one.netflow = t.netflow

        vps_one_upgrade_os(one, vps_os, reset)
        vps_host_mv(one, vps_host)
        one.save()

def vps_one_rm(vps_id):
    one = VpsOne.get(vps_id)
    if one and one.state > VPS_STATE_RM:
        vps_host_rm(one)
        one.state_set(VPS_STATE_RM)
        for ip in one.vps_one_ip_list:
            vps_one_ip_rm(ip.id)

def vps_mv(from_id, to_id):
    if to_id:
        VpsOne.where(id=from_id).update(id=to_id)
        VpsOneOrder.where(vps_one_id=from_id).update(vps_one_id=to_id)
        VpsOneIp.where(vps_one_id=from_id).update(vps_one_id=to_id)
        from model.vps_ip import VpsIp
        VpsIp.where(vps_one_id=from_id).update(vps_one_id=to_id)

MC_VPS_ONE_EXPIRE_COUNT = 'VpsOneExpireCount'

def vps_one_expire_count():
    count = mc.get(MC_VPS_ONE_EXPIRE_COUNT)
    if count is None:
        days = today_days() - 1
        count = VpsOne.where('date_close<%s'%days, state=VPS_STATE_RUN).count()
        mc.set(MC_VPS_ONE_EXPIRE_COUNT, count, ONE_DAY)
    return count


def vps_one_expire_list():
    days = today_days() - 1
    ol = list(VpsOne.where('date_close < %s'%days, state=VPS_STATE_RUN))
    count = len(ol)
    mc.set(MC_VPS_ONE_EXPIRE_COUNT, count, ONE_DAY)
    return ol



def vps_order_auto_open(order):

    id = order.id

    for vps in vps_one_list_by_vps_order(order):
        _vps_auto_open(vps)



def _vps_auto_open(vps):
    if vps.state != VPS_STATE_PAY:
        return 
    if not vps.ip_autobind():
        return

    host_id = vps.host_id
    from yun.vps.server.model.vps import vps_saas_cmd_open
    vps_saas_cmd_open(host_id , vps.id)


def vps_auto_open(admin_id, user_id, os, data_center, days, vps_type_id):
    vps = _vps_by_vps_type(user_id, os, data_center, days, vps_type_id)
    _vps_auto_open(vps)
    from model.zsite import Zsite 
    admin = Zsite.mc_get(admin_id)
    user = Zsite.mc_get(user_id) 
    subject = "%s%s 手工开通 VPS %s"%(admin.name, user.name, vps.id)

    from config import SITE_DOMAIN
    mq_sendmail(subject, "http://god.%s/vps/one/%s"%(SITE_DOMAIN, vps.id), MAIL_VPS_ORDER)

    return vps

def vps_close(id):
    vps = VpsOne.get(id)
    if vps:
        vps.state_set(VPS_STATE_CLOSE)

if __name__ == '__main__':
    pass
    user_id_list = []
    for i in VpsOne.where(host_id=4):
        user_id_list.append(i.user_id)
    user_id_list = set(user_id_list)
    print user_id_list
    from model.user_mail import mail_by_user_id 
    for i in user_id_list:
        print mail_by_user_id(i),

    #vps_auto_open(10000000, 10000000, 1, 1, 2, 11)

#    vps = VpsOne.get(200)
#    vps.state_set(VPS_STATE_CLOSE)
    
    #from yun.vps.server.model.vps import vps_saas_cmd_open
    #vps_one = VpsOne.get(152)
    #vps_saas_cmd_open(vps_one.host_id, vps_one.id)


#    b =VpsOne.get(152)
#    one =VpsOne.get(164)
#    print one.user_id
#    one.user_id = 10275581
#    one.date_close = b.date_close
#    one.save()
#    vps_close(b.id) 
#    i = VpsPrice.get(61)
#    i.cent = 9900
#    i.save()
#
#    i = VpsPrice.get(60)
#    i.cent = 9900*3
#    i.save()
#    
#    i = VpsPrice.get(39)
#    i.cent = 12600 
#    i.save()

#    from model.zsite import Zsite
#    from model.user_mail import mail_by_user_id
#    for i in VpsOne.where(state=VPS_STATE_RUN):
#        print i.user_id
#        print mail_by_user_id(i.user_id)
#        zsite = Zsite.mc_get(i.user_id)
#        print 'http:%s'%zsite.link
#        print '数据中心', i.data_center
#        print '内存', i.ram
#        print '期限', i.date_close - i.date_open
#        print "ip" , i.ip_list
#        print zsite.name
#
#
#        print '-'*10
#from yun.vps.server.model.vps import vps_saas_cmd_open
#vps_saas_cmd_open(3 , 116)
#one = VpsOne.get(11)
#one.date_close = one.date_open+70
#one.save()
#one = VpsOne.get()
#one.date_close = one.date_open + 30 
#one.save()
#print one.host_id
#from model.zsite import Zsite
#from model.user_mail import mail_by_user_id
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.