Exemple #1
0
def generate_backup():
    ''' 一天只备份一次, 在最早的一次发生(任何)配置变更后。

    每天的第一次配置改动后,触发 config reload_all,备份一次改动后的所有区的内容.

    每天第一次备份全部分区的全部配置时,可能会要花些时间,
    但实际测试显示是很快的, 基本在1秒内全备份完成。 

    如果 redis 重启了,要从mongo中调出所有的配置,再备份,则可能略花点时间。
    只要不是redis重启,实测速度基本可以接受。
    '''

    today_str = str(datetime.datetime.now().date())

    #need put into redis, or when program restarted, it will re-backup
    conf_backup_date = RedisTool.get('conf_backup_date')  #is a string

    if today_str == conf_backup_date:
        return

    #conf_with_multi_area = [ c['name'] for c in g_lConfig if c['use_subarea'] ]

    subarea_conf_obj = Config.get('subareas_conf_1')
    if not subarea_conf_obj:
        subarea_conf_obj = Config.create('subareas_conf_1')
    subarea_conf = eval(subarea_conf_obj.data)
    subarea_ids = subarea_conf.keys()

    #   print '#### in backup, subarea_config.data=', subarea_conf
    #   print '#### in backup, subarea_ids=', subarea_ids

    subarea_ids.sort()  #put 1 as first

    for area_id in subarea_ids:
        all_config_name_list = get_game_config_name_list(area_id)
        for conf_name in all_config_name_list:
            full_conf_name = conf_name + '_' + area_id
            print '##### in config backup,', full_conf_name, '@', datetime.datetime.now(
            )

            #need backup conf_obj.data, which has comments "# ... "
            conf_obj = Config.get(full_conf_name)
            if not conf_obj:
                continue

            #conf_bk = Config.get(full_conf_name + '_' + today_str)
            #if conf_bk is None:
            conf_bk = Config.create(full_conf_name + '_' + today_str)
            conf_bk.data = conf_obj.data
            conf_bk.put()

            #delete old backups
            old_conf_obj = Config.get(full_conf_name + '_' + conf_backup_date)
            if not old_conf_obj:
                continue
            old_conf_obj.delete()

    RedisTool.set('conf_backup_date', today_str)  # str
Exemple #2
0
def generate_backup():
    ''' 一天只备份一次, 在最早的一次发生(任何)配置变更后。

    每天的第一次配置改动后,触发 config reload_all,备份一次改动后的所有区的内容.

    每天第一次备份全部分区的全部配置时,可能会要花些时间,
    但实际测试显示是很快的, 基本在1秒内全备份完成。 

    如果 redis 重启了,要从mongo中调出所有的配置,再备份,则可能略花点时间。
    只要不是redis重启,实测速度基本可以接受。
    '''

    today_str = str( datetime.datetime.now().date() )

    #need put into redis, or when program restarted, it will re-backup
    conf_backup_date = RedisTool.get('conf_backup_date')  #is a string

    if today_str == conf_backup_date:
        return

    #conf_with_multi_area = [ c['name'] for c in g_lConfig if c['use_subarea'] ]

    subarea_conf_obj = Config.get('subareas_conf_1')
    if not subarea_conf_obj:
        subarea_conf_obj = Config.create('subareas_conf_1')
    subarea_conf = eval(subarea_conf_obj.data)
    subarea_ids = subarea_conf.keys()

#   print '#### in backup, subarea_config.data=', subarea_conf
#   print '#### in backup, subarea_ids=', subarea_ids

    subarea_ids.sort() #put 1 as first

    for area_id in subarea_ids:    
        all_config_name_list = get_game_config_name_list(area_id)
        for conf_name in all_config_name_list:
            full_conf_name = conf_name + '_' + area_id
            print '##### in config backup,', full_conf_name, '@', datetime.datetime.now()

            #need backup conf_obj.data, which has comments "# ... "
            conf_obj = Config.get(full_conf_name)
            if not conf_obj:
                continue

            #conf_bk = Config.get(full_conf_name + '_' + today_str)
            #if conf_bk is None:
            conf_bk = Config.create(full_conf_name + '_' + today_str)
            conf_bk.data = conf_obj.data
            conf_bk.put()
            
            
            #delete old backups
            old_conf_obj = Config.get(full_conf_name + '_' + conf_backup_date)
            if not old_conf_obj:
                continue
            old_conf_obj.delete()

    RedisTool.set('conf_backup_date', today_str) # str
def get_config_version(subarea, config_name):
    subarea = str(subarea)
    redis_config_name = config_version_key(subarea, config_name)
    config_update_time = RedisTool.get(redis_config_name)
    if not config_update_time:
        config_update_time = set_config_version(subarea=subarea, config_name=config_name)
    else:
        config_update_time = config_update_time
    return config_update_time
Exemple #4
0
def get_config_version(subarea, config_name):
    subarea = str(subarea)
    redis_config_name = config_version_key(subarea, config_name)
    config_update_time = RedisTool.get(redis_config_name)
    if not config_update_time:
        config_update_time = set_config_version(subarea=subarea,
                                                config_name=config_name)
    else:
        config_update_time = config_update_time
    return config_update_time
def set_config_version(subarea='1', config_name=''):
    subarea = str(subarea)
    redis_config_name = config_version_key(subarea, config_name)
    set_time = int(time.time())
    RedisTool.set(redis_config_name, set_time, 72*60*60)
    return set_time
Exemple #6
0
def game_setting(request,request_info=None, return_dict=False):
    """游戏配置
    """
    if not request_info:
        request_info = request.REQUEST
    data = {}
    subarea = request_info.get('subarea')
    categories = config_list.get_conflist_by_subarea(subarea)
    config_name = request_info.get('config_name')
    saved = False

    sconfig_name = config_list.get_configname_by_subarea(config_name,subarea)
    str_subareas_confname = config_list.get_configname_by_subarea('subareas_conf', '1')
    subareas_conf = Config.get(str_subareas_confname)
    if not subareas_conf:
        subareas_conf = Config.create(str_subareas_confname)

    subareas_conf_dict = eval(subareas_conf.data)

    return_subareas_conf = []
    for key in sorted(subareas_conf_dict.keys()):
        return_subareas_conf.append((key, subareas_conf_dict[key]))
    data['config_title'] = config_list.get_description(config_name)
    data['config_title'] += '__%s' % config_name
    if sconfig_name:
        #修改具体的一个
        config = Config.get(sconfig_name)
        if not config:
            config = Config.create(sconfig_name)
        config_value = config.data
        if request.method == 'POST':
            before_config = config_value
            if not return_dict:
                config_md5sum_from_web = request.POST['config_md5sum'] #before this update
                if type( config_value ) == unicode:
                    config_md5sum_calculated = hashlib.md5( config_value.strip().encode('utf-8') ).hexdigest()
                else:
                    config_md5sum_calculated = hashlib.md5( config_value.strip() ).hexdigest()

                if config_md5sum_from_web !=  config_md5sum_calculated:
                    return HttpResponse('<script>alert("配置内容, 已被别人修改过. 请重新刷新,再作修改。");</script>')

            config_value = request_info['config_value'].encode('utf-8').replace('\r','').strip()
            config.data = config_value
            if config_name != 'ruby_skill_params_config':
                verify_info = verify_game_config(config_name, eval(config_value))
            # 通过ruby的配置  简化技能的配置
            else:
                verify_info = _config_skill_params_by_ruby(config_value, request)
            if verify_info != '':
                return HttpResponse('<script>alert("%s");history.go(-1)</script>' % verify_info)
            config.put()
            config_update(subarea, config_name)

            moderator = apps.admin.auth.get_moderator_by_request(request)
            username,date,subarea,configname,REMOTE_ADDR = moderator.username,datetime.datetime.now(),subarea, data['config_title'], request.META["REMOTE_ADDR"]
            UpdateConfRecord.record(username, date, subarea, configname, REMOTE_ADDR)
            saved = True

            
            log_data = {}
            diff_result = diff_2_str(config_value, before_config)
            if diff_result:
                log_data['diff'] = diff_result
                log_data['subarea'] = subarea
                log_data['config_name'] = config_name
                log_data['before_config'] = before_config
                log_data['after_config'] = config_value
                log_data["username"] =  moderator.username
                ConfigHistory.set_log(**log_data)


            #多分区,保存配置后,要允许运营同学 同步到其它分区。
            config_use_subarea = False 
            for g in config_list.g_lConfig:
                if g['name'] == config_name and g['use_subarea']:
                    config_use_subarea = True 
                    break

            if config_use_subarea:
                #本配置, 不同的分区,有差别, 要同步到其它分区
                same_contents_subareas = {}
                diff_contents_subareas = {}
                for other_subarea_id, other_subarea_name in subareas_conf_dict.items():
                    if other_subarea_id == subarea:
                        continue

                    other_subarea = Config.get(config_name + '_' + other_subarea_id)
                    if not other_subarea:
                        other_subarea = Config.create(config_name + '_' + other_subarea_id)
                    #print '#### other_subarea.data=', other_subarea.data.encode('utf8')
                    #print '#### config_value=', config_value
                    #print '#### other_subarea.data == config_value ', other_subarea.data == config_value
                    if other_subarea.data == config_value or other_subarea.data.encode('utf8') == config_value:
                        same_contents_subareas[other_subarea_id] = other_subarea_name
                    else:
                        diff_contents_subareas[other_subarea_id] = other_subarea_name

                data = {'subarea': subarea, 'config_name': config_name}
                data['config_description'] = config_list.get_description(config_name)
                data['subarea_name'] = subareas_conf_dict[subarea]
                data['same_contents_subareas'] = same_contents_subareas
                data['diff_contents_subareas'] = diff_contents_subareas
                return 'admin/sync_conf_to_subarea.html', data

    else:
        #显示所有
        config_value = None
    data['subarea'] = subarea
    data['subareas_conf'] = return_subareas_conf
    #data['g_l_config'] = g_l_config
    data['categories'] = categories
    data['config_name'] = config_name
    data['config_value'] = beautify_config_value (config_value)
    data['saved'] = saved
    data['updateconfrecord'] = UpdateConfRecord.infos()['infos_list']
    if return_dict:
        return data

    data['config_error'] = ''
    if subarea == '1' and not config_name:
        data['config_error'] = check_config_error()

    #print '#### data[config_error]=', data['config_error']

    if config_value:
        if type( config_value ) == unicode:
            config_md5sum_calculated = hashlib.md5( config_value.strip().encode('utf-8') ).hexdigest()
        else:
            config_md5sum_calculated = hashlib.md5( config_value.strip() ).hexdigest()
        data['config_md5sum'] = config_md5sum_calculated

    #data['app_name'] = settings.APP_NAME
    #data['environment_type'] = settings.ENVIRONMENT_TYPE

    #show config backup
    conf_backup_date = RedisTool.get('conf_backup_date')

    data['conf_backup_date'] = conf_backup_date
    data['backup_diff_from_now'] = {}
    data['changed_from_today'] = []    #from today's early morning backup

    if config_name: #display one specific config (in certain subarea)
        old_conf_data = show_config_backup(config_name=config_name, 
               subarea=subarea, date_str=conf_backup_date)

        if old_conf_data in ('', None): #never backed up yet
            data['backup_diff_from_now'] = ''
        elif Config.get(config_name + '_' + subarea):
            if old_conf_data == Config.get(config_name + '_' + subarea).data:
                data['backup_diff_from_now'] = ''
            else:
                data['backup_diff_from_now'] = 'Yes'

    else: #display overview of all config
      if subarea:
        for conf_name in config_list.get_game_config_name_list(subarea=subarea):
            conf_obj = Config.get(conf_name + '_' + subarea)
            if not conf_obj:
                continue

            morning_conf_data = show_config_backup( config_name=conf_name, 
                                subarea=subarea, date_str=conf_backup_date)
            if morning_conf_data:   #if '', i.e. not exist/backed up yet, 
                                    #don't display
                if conf_obj.data != morning_conf_data:
                    data['changed_from_today'] += [
                         '<a href="/admin/game_setting/?config_name=' + 
                         conf_name + '&subarea=' + subarea + '">' + conf_name + '(' + subarea + ')</a>' ]
        data['changed_from_today'] = ', '.join(data['changed_from_today'])

    return 'admin/game_setting.html', data
Exemple #7
0
def set_config_version(subarea='1', config_name=''):
    subarea = str(subarea)
    redis_config_name = config_version_key(subarea, config_name)
    set_time = int(time.time())
    RedisTool.set(redis_config_name, set_time, 72 * 60 * 60)
    return set_time
Exemple #8
0
def game_setting(request, request_info=None, return_dict=False):
    """游戏配置
    """
    if not request_info:
        request_info = request.REQUEST
    data = {}
    subarea = request_info.get('subarea')
    categories = config_list.get_conflist_by_subarea(subarea)
    config_name = request_info.get('config_name')
    saved = False

    sconfig_name = config_list.get_configname_by_subarea(config_name, subarea)
    str_subareas_confname = config_list.get_configname_by_subarea(
        'subareas_conf', '1')
    subareas_conf = Config.get(str_subareas_confname)
    if not subareas_conf:
        subareas_conf = Config.create(str_subareas_confname)

    subareas_conf_dict = eval(subareas_conf.data)

    return_subareas_conf = []
    for key in sorted(subareas_conf_dict.keys()):
        return_subareas_conf.append((key, subareas_conf_dict[key]))
    data['config_title'] = config_list.get_description(config_name)
    data['config_title'] += '__%s' % config_name
    if sconfig_name:
        #修改具体的一个
        config = Config.get(sconfig_name)
        if not config:
            config = Config.create(sconfig_name)
        config_value = config.data
        if request.method == 'POST':
            before_config = config_value
            if not return_dict:
                config_md5sum_from_web = request.POST[
                    'config_md5sum']  #before this update
                if type(config_value) == unicode:
                    config_md5sum_calculated = hashlib.md5(
                        config_value.strip().encode('utf-8')).hexdigest()
                else:
                    config_md5sum_calculated = hashlib.md5(
                        config_value.strip()).hexdigest()

                if config_md5sum_from_web != config_md5sum_calculated:
                    return HttpResponse(
                        '<script>alert("配置内容, 已被别人修改过. 请重新刷新,再作修改。");</script>'
                    )

            config_value = request_info['config_value'].encode(
                'utf-8').replace('\r', '').strip()
            config.data = config_value
            if config_name != 'ruby_skill_params_config':
                verify_info = verify_game_config(config_name,
                                                 eval(config_value))
            # 通过ruby的配置  简化技能的配置
            else:
                verify_info = _config_skill_params_by_ruby(
                    config_value, request)
            if verify_info != '':
                return HttpResponse(
                    '<script>alert("%s");history.go(-1)</script>' %
                    verify_info)
            config.put()
            config_update(subarea, config_name)

            moderator = apps.admin.auth.get_moderator_by_request(request)
            username, date, subarea, configname, REMOTE_ADDR = moderator.username, datetime.datetime.now(
            ), subarea, data['config_title'], request.META["REMOTE_ADDR"]
            UpdateConfRecord.record(username, date, subarea, configname,
                                    REMOTE_ADDR)
            saved = True

            log_data = {}
            diff_result = diff_2_str(config_value, before_config)
            if diff_result:
                log_data['diff'] = diff_result
                log_data['subarea'] = subarea
                log_data['config_name'] = config_name
                log_data['before_config'] = before_config
                log_data['after_config'] = config_value
                log_data["username"] = moderator.username
                ConfigHistory.set_log(**log_data)

            #多分区,保存配置后,要允许运营同学 同步到其它分区。
            config_use_subarea = False
            for g in config_list.g_lConfig:
                if g['name'] == config_name and g['use_subarea']:
                    config_use_subarea = True
                    break

            if config_use_subarea:
                #本配置, 不同的分区,有差别, 要同步到其它分区
                same_contents_subareas = {}
                diff_contents_subareas = {}
                for other_subarea_id, other_subarea_name in subareas_conf_dict.items(
                ):
                    if other_subarea_id == subarea:
                        continue

                    other_subarea = Config.get(config_name + '_' +
                                               other_subarea_id)
                    if not other_subarea:
                        other_subarea = Config.create(config_name + '_' +
                                                      other_subarea_id)
                    #print '#### other_subarea.data=', other_subarea.data.encode('utf8')
                    #print '#### config_value=', config_value
                    #print '#### other_subarea.data == config_value ', other_subarea.data == config_value
                    if other_subarea.data == config_value or other_subarea.data.encode(
                            'utf8') == config_value:
                        same_contents_subareas[
                            other_subarea_id] = other_subarea_name
                    else:
                        diff_contents_subareas[
                            other_subarea_id] = other_subarea_name

                data = {'subarea': subarea, 'config_name': config_name}
                data['config_description'] = config_list.get_description(
                    config_name)
                data['subarea_name'] = subareas_conf_dict[subarea]
                data['same_contents_subareas'] = same_contents_subareas
                data['diff_contents_subareas'] = diff_contents_subareas
                return 'admin/sync_conf_to_subarea.html', data

    else:
        #显示所有
        config_value = None
    data['subarea'] = subarea
    data['subareas_conf'] = return_subareas_conf
    #data['g_l_config'] = g_l_config
    data['categories'] = categories
    data['config_name'] = config_name
    data['config_value'] = beautify_config_value(config_value)
    data['saved'] = saved
    data['updateconfrecord'] = UpdateConfRecord.infos()['infos_list']
    if return_dict:
        return data

    data['config_error'] = ''
    if subarea == '1' and not config_name:
        data['config_error'] = check_config_error()

    #print '#### data[config_error]=', data['config_error']

    if config_value:
        if type(config_value) == unicode:
            config_md5sum_calculated = hashlib.md5(
                config_value.strip().encode('utf-8')).hexdigest()
        else:
            config_md5sum_calculated = hashlib.md5(
                config_value.strip()).hexdigest()
        data['config_md5sum'] = config_md5sum_calculated

    #data['app_name'] = settings.APP_NAME
    #data['environment_type'] = settings.ENVIRONMENT_TYPE

    #show config backup
    conf_backup_date = RedisTool.get('conf_backup_date')

    data['conf_backup_date'] = conf_backup_date
    data['backup_diff_from_now'] = {}
    data['changed_from_today'] = []  #from today's early morning backup

    if config_name:  #display one specific config (in certain subarea)
        old_conf_data = show_config_backup(config_name=config_name,
                                           subarea=subarea,
                                           date_str=conf_backup_date)

        if old_conf_data in ('', None):  #never backed up yet
            data['backup_diff_from_now'] = ''
        elif Config.get(config_name + '_' + subarea):
            if old_conf_data == Config.get(config_name + '_' + subarea).data:
                data['backup_diff_from_now'] = ''
            else:
                data['backup_diff_from_now'] = 'Yes'

    else:  #display overview of all config
        if subarea:
            for conf_name in config_list.get_game_config_name_list(
                    subarea=subarea):
                conf_obj = Config.get(conf_name + '_' + subarea)
                if not conf_obj:
                    continue

                morning_conf_data = show_config_backup(
                    config_name=conf_name,
                    subarea=subarea,
                    date_str=conf_backup_date)
                if morning_conf_data:  #if '', i.e. not exist/backed up yet,
                    #don't display
                    if conf_obj.data != morning_conf_data:
                        data['changed_from_today'] += [
                            '<a href="/admin/game_setting/?config_name=' +
                            conf_name + '&subarea=' + subarea + '">' +
                            conf_name + '(' + subarea + ')</a>'
                        ]
            data['changed_from_today'] = ', '.join(data['changed_from_today'])

    return 'admin/game_setting.html', data
Exemple #9
0
def send_mobile_message():
    #"""发送手机短信
    #"""
    try:
        if sms_config.SEND_MOBILE_MESSAGE is False:
            return 
        error_msg = traceback.format_exc()
        if error_msg.find('com.zeptolab.ctrbonus') >= 0:
            return
        if error_msg.find('IndexError') < 0 and \
         error_msg.find('KeyError') < 0 and \
         error_msg.find('Mongo') < 0 and \
         error_msg.find('mongo') < 0 and \
         error_msg.find('redis') < 0 and \
         error_msg.find('Redis') < 0 and \
         error_msg.find('AttributeError') < 0:
            return
        last_time = RedisTool.get(settings.CACHE_PRE+'mobile_time', None)
        error_num = RedisTool.get(settings.CACHE_PRE+'mobile_error_num', 0)
        error_time = RedisTool.get(settings.CACHE_PRE+'mobile_error_time', None)
        now = int(time.time())
        if error_time is None:
            error_num = 0
            RedisTool.set(settings.CACHE_PRE+'mobile_error_time', now, 0)
        else:
            total_second = now - error_time
            if total_second > 65:
                error_num = 0
                RedisTool.set(settings.CACHE_PRE+'mobile_error_time', now, 0)
                return
        #错误15条以上才开始发
        error_num += 1
        if error_num < 30:
            RedisTool.set(settings.CACHE_PRE+'mobile_error_num', error_num, 0) 
            return  
        if last_time is not None:
            total_second = now - last_time
            if total_second < 60 * 3:
                return 

        urllib2.urlopen(sms_config.MOBILE_URL + socket.gethostname(), 
                timeout=12)
        RedisTool.set(settings.CACHE_PRE+'mobile_time', now, 0) 
        RedisTool.set(settings.CACHE_PRE+'mobile_error_num', 0, 0)
        RedisTool.set(settings.CACHE_PRE+'mobile_error_time', now, 0)
    except:
        print 'send_mobile_message error'
        print_err()
Exemple #10
0
def send_mobile_message():
    #"""发送手机短信
    #"""
    try:
        if sms_config.SEND_MOBILE_MESSAGE is False:
            return
        error_msg = traceback.format_exc()
        if error_msg.find('com.zeptolab.ctrbonus') >= 0:
            return
        if error_msg.find('IndexError') < 0 and \
         error_msg.find('KeyError') < 0 and \
         error_msg.find('Mongo') < 0 and \
         error_msg.find('mongo') < 0 and \
         error_msg.find('redis') < 0 and \
         error_msg.find('Redis') < 0 and \
         error_msg.find('AttributeError') < 0:
            return
        last_time = RedisTool.get(settings.CACHE_PRE + 'mobile_time', None)
        error_num = RedisTool.get(settings.CACHE_PRE + 'mobile_error_num', 0)
        error_time = RedisTool.get(settings.CACHE_PRE + 'mobile_error_time',
                                   None)
        now = int(time.time())
        if error_time is None:
            error_num = 0
            RedisTool.set(settings.CACHE_PRE + 'mobile_error_time', now, 0)
        else:
            total_second = now - error_time
            if total_second > 65:
                error_num = 0
                RedisTool.set(settings.CACHE_PRE + 'mobile_error_time', now, 0)
                return
        #错误15条以上才开始发
        error_num += 1
        if error_num < 30:
            RedisTool.set(settings.CACHE_PRE + 'mobile_error_num', error_num,
                          0)
            return
        if last_time is not None:
            total_second = now - last_time
            if total_second < 60 * 3:
                return

        urllib2.urlopen(sms_config.MOBILE_URL + socket.gethostname(),
                        timeout=12)
        RedisTool.set(settings.CACHE_PRE + 'mobile_time', now, 0)
        RedisTool.set(settings.CACHE_PRE + 'mobile_error_num', 0, 0)
        RedisTool.set(settings.CACHE_PRE + 'mobile_error_time', now, 0)
    except:
        print 'send_mobile_message error'
        print_err()