python类host_string()的实例源码

game.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def remove(self, remote_dir=None):
        """????????????????(nginx, backstage)"""

        if remote_dir is None:
            release_type = 'game_backup'
            remote_dir = '/app/opbak/{}_{}'.format(release_type, TIMESTAMP)

        self.stop()

        self.mkdir(remote_dir)

        with settings(host_string=self.int_ip): 
            with cd('/app'):
                run('mv {} {}/'.format(self.name, remote_dir))
            with cd(remote_dir):
                run('pandora --dump --opt -R {0}>{0}.sql.rb{1}'.format(self.name, TIMESTAMP))
                run('mv /app/nginx/conf/vhost/{}.conf ./'.format(self.name))

            reload_nginx()

            run('pandora --update -e "DROP DATABASE {}"'.format(self.name))

            with cd('/app/{}_backstage'.format(self.game)):
                run('cp socket_gameserver.ini {}/'.format(remote_dir))
                run("sudo -u agent sed -i '/\\b{}\\b/d' socket_gameserver.ini".format(self.name))
                run("set -m; sudo -u agent /bin/bash start.sh restart")
game.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def upload_log(self, logtype=None, date=None, logfile=None, ftp_ip=None):
        """
        :: Example
            pandora --ftp -r 30 -t 1200 -z -m 42.62.119.164 /tjmob_log/tjmob_37wan_1 /app/tjmob_37wan_1/backend/logs/game/dayreport/dayreport_2015-05-03.log.bz2*
        """
        from bible.utils import BZIP2

        ftp_log_path = '/{}_log/{}'.format(self.game, self.name)

        logtypes = ['dayreport', 'rtreport']

        date = date if date else time.strftime('%Y-%m-%d')
        ftp_ip = ftp_ip if ftp_ip else '42.62.119.164'

        if logfile:
            logfiles = [logfile]
        else:
            if logtype:
                logfiles = ['/app/{0}/backend/logs/game/{1}/{1}_{2}.log'.format(self.name, each_type, date) for each_type in logtype.split(',')]
            else:
                logfiles = ['/app/{0}/backend/logs/game/{1}/{1}_{2}.log'.format(self.name, each_logtype, date) for each_logtype in logtypes]

        for each_log in logfiles:
            dir, filename = os.path.split(each_log)
            file_bz2 = '{}.bz2'.format(filename)
            file_md5 = '{}.MD5'.format(file_bz2)
            with settings(host_string=self.int_ip), cd(dir): 
                run('[ -f {0} ] && echo "{0} already exists" || {1} {2}'.format(file_bz2, BZIP2, filename))
                run('[ -f {0} ] && echo "{0} already exists" || md5sum {1} >{0}'.format(file_md5, file_bz2))

            with settings(host_string=self.int_ip): 
                run('''pandora --ftp -r 30 -t 1200 -z -m {} {} {}.bz2*'''.format(ftp_ip, ftp_log_path, each_log) )
game.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def transform(self, gameServers, all_gameServer_info=None):
        """
        Transform funcion. 
        eg: it will transformat from 
            ['astd_37wan_2', 'astd_51wan_99', 'astd_uoyoo_90']
        to
            {
                '10.6.20.1':['astd_37wan_2', 'astd_51wan_99'], 
                '10.6.20.2':['astd_uoyoo_90']
            }
        """
        if not all_gameServer_info:
            all_gameServer_info = self.all_gameServer_info

        IPS = list(set([ all_gameServer_info[each] for each in gameServers ]))
        locate_game_servers = { each:[] for each in IPS }
        for each in gameServers:
            locate_game_servers[all_gameServer_info[each]].append(each)
        return locate_game_servers

#    def sql_content_exec(self, gameServers, sql_content, backup='Yes', remote_dir=REMOTE_DIR):
#        locate_game_servers = self.transform(gameServers)
#        ips = locate_game_servers.keys()
#
#        def _sql_content_exec(sql_content, locate_game_servers, backup):
#            for gameServer in locate_game_servers[env.host_string]:
#                backup_dir = '{}/{}'.format(remote_dir, gameServer)
#                run('[ -d {0} ] || mkdir -p {0}'.format(backup_dir))
#                if backup.lower() == 'yes':
#                    run('pandora --dump --opt -R {0} >{1}/rollback_{0}.sql'.format(gameServer, backup_dir))
#                run('''pandora --update {} -e '{}' '''.format(gameServer, sql_content))
#
#        execute(_sql_content_exec, sql_content, locate_game_servers, backup=backup, hosts=ips)
#
testEnvModifyConfig.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def modify_file(filename, operation, key, value, comment='null'):
    for gameServer in LOCATE_GAME_SRVS[env.host_string]:
        with cd('/app/{}/backend/apps'.format(gameServer)):
            conf = Config(filename)
            operate_method = getattr(conf, operation)
            operate_method(key, value, comment)
test_env_load_file.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def load_file(game_servers, local_file, remote_file, load_type='upload'):
    test_server_info = get_test_server_info(GAME)
    check_game_servers(game_servers, test_server_info)

    locate_game_servers = transform_gameservers(game_servers, test_server_info)

    ips = locate_game_servers.keys()

    @hosts(ips)
    def _upload_file():
        upload(local_file, REMOTE_DIR, env.host_string)
        for game_server in locate_game_servers[env.host_string]:
            replace_file(game_server, remote_file)

    @hosts(ips)
    def _download_file():
        for game_server in locate_game_servers[env.host_string]:
            local_path = '{}/{}/'.format(local_root_path, game_server)
            local('su - astd -c "mkdir -p {}"'.format(local_path))
            target_file = '/app/{}/{}'.format(game_server, remote_file)
            with quiet():
                target_file_exists = run('test -f {}'.format(target_file)).succeeded

            if target_file_exists:
                get(target_file, local_path)
            else:
                raise Exception('File {} NOT exists on {}'.format(target_file, game_server))

        local('chown -R astd.astd {}'.format(local_root_path))

    if load_type == 'upload':
        ftp_file_check(local_file)
        file_name_consistence_check(local_file, remote_file)
        execute(_upload_file)
        print('{} was uploaded to {} successfully.'.format(local_file, game_servers))

    elif load_type == 'download':
        ftp_path = 'download/{}/{}'.format(GAME, TIMESTAMP)
        local_root_path = '/app/online/{}'.format(ftp_path)
        execute(_download_file)
        print('Downloaded remote file: {} to FTP: {}/'.format(remote_file, ftp_path))
test_env_export_db.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def export_db(game_servers, export_type='data'):
    test_server_info = get_test_server_info(GAME)
    check_game_servers(game_servers, test_server_info)

    locate_game_servers = transform_gameservers(game_servers, test_server_info)

    ips = locate_game_servers.keys()

    ftp_path = 'download/{}/{}'.format(GAME, TIMESTAMP)
    local_root_path = '/app/online/{}'.format(ftp_path)

    @hosts(ips)
    def _export_db():
        for game_server in locate_game_servers[env.host_string]:
            local_path = '{}/{}/'.format(local_root_path, game_server)
            local('su - astd -c "mkdir -p {}"'.format(local_path))

            run('mkdir -p {}'.format(REMOTE_DIR))

            sql_name = '{}.sql.rb{}'.format(game_server, TIMESTAMP)

            if export_type == 'no-data':
                run('pandora --dump -R --opt -d {} >{}/{}'.format(game_server, REMOTE_DIR, sql_name))
            elif export_type == 'data':
                run('pandora --dump -R --opt {} >{}/{}'.format(game_server, REMOTE_DIR, sql_name))

            with cd(REMOTE_DIR):
                run('tar zcf {0}.tgz {0}'.format(sql_name))

            target_file = '{}/{}.tgz'.format(REMOTE_DIR, sql_name)
            get(target_file, local_path)

        local('chown -R astd.astd {}'.format(local_root_path))

    print('Start dumping db...')
    sys.stdout.flush()
    execute(_export_db)
    print('Downloaded db to FTP: {}/'.format(ftp_path))
flush_mem_data.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def upload_to_resource_server(game, file):
    dir, filename = os.path.split(file)
    resource_dir = '/app/www/{}/{}/{}'.format(game, RELEASE_TYPE, TIMESTAMP) 
    resource_ip = gameOption('www_ssh_ip')
    execute(mk_remote_dir, resource_dir, hosts=[resource_ip])
    with lcd(dir), settings(host_string=resource_ip):
        put(filename, resource_dir)
        put('md5.txt', resource_dir)
    #local('{} {}/{{{},md5.txt}} {}:{}/'.format(RSYNC, dir, filename, resource_ip, resource_dir))
testEnvPayProxyRelease.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def update(restart='No'):
    for gameServer in LOCATE_GAME_SRVS[env.host_string]:
        if restart == 'Yes':
            stop_gameServer(gameServer)
        if restart == 'Yes':
            start_gameServer(gameServer)
testEnvRelease.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def transfer(file):
    from bible.utils import RSYNC
    file_dir, filename = os.path.split(file)
    remote_dir = '''/app/opbak/{}'''.format(TIME)
    run(''' [ -d {0} ] || mkdir -p {0} '''.format(remote_dir))

    #cmd = ''' {rsync} {file_dir}/{{{filename},md5.txt}} {ssh_user}@{target_host}:{remote_dir}/ '''.format(file_dir=file_dir, target_host=env.host_string, remote_dir=remote_dir, ssh_user=env.user, filename=filename, rsync=RSYNC)
    #local(cmd)
    with lcd(file_dir):
        put(filename, remote_dir)
        put('md5.txt', remote_dir)

    with cd(remote_dir):
        run('dos2unix md5.txt && md5sum -c md5.txt')
testEnvRelease.py 文件源码 项目:astoptool 作者: zouliuyun 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def update(fVersion, bVersion, sql_file, maindland, restart='No'):
    if sql_file.lower() != '/app/online/{}/no'.format(GAME):
        transfer(sql_file)
    for gameServer in LOCATE_GAME_SRVS[env.host_string]:
        if restart in ['Yes', 'Restart', 'Stop']:
            stop_gameServer(gameServer)
        if bVersion.lower() != 'no':
            update_backend(gameServer, bVersion, maindland)
        if fVersion.lower() != 'no':
            update_frontend(gameServer, fVersion, maindland)
        if sql_file.lower() != '/app/online/{}/no'.format(GAME):
            sql_exec(gameServer, sql_file)
        if restart in ['Yes', 'Restart', 'Start']:
            start_gameServer(gameServer)
machine.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def disable_transparent_huge_pages(_host):
    env.host_string = _host

    run('echo "never" | sudo tee /sys/kernel/mm/transparent_hugepage/enabled')
    put('{}/bootstrap/scripts/disable-thp.sh'.format(os.getcwd()), '~/', use_sudo=True)
    sudo("chmod +x disable-thp.sh")
    sudo(". ~/disable-thp.sh")
machine.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def set_swapiness(_host, _swap_percent):
    env.host_string = _host
    sudo('sudo sysctl vm.swappiness={}'.format(_swap_percent))
    run('echo "vm.swappiness = {}" | sudo tee -a /etc/sysctl.conf'.format(_swap_percent))
machine.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def set_overcommit_memory(_host, _value):
    env.host_string = _host
    sudo('sysctl vm.overcommit_memory={}'.format(_value))
    run('echo "vm.overcommit_memory = {}" | sudo tee -a /etc/sysctl.conf'.format(_value))
crate.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def restart_all(config_file):
    """Restarts crate service on all hosts"""
    cfg = helper.get_config(config_file)

    for host_config in cfg['hosts']:
        env.host_string = helper.get_env_host_string(host_config)
        env.user = helper.get_env_user(host_config)
        env.key_filename = helper.get_env_key_filename(host_config)
        sudo('service crate restart')
kafka.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def broker_install(host_config):
    env.host_string = helper.get_env_host_string(host_config)
    env.user = helper.get_env_user(host_config)
    env.key_filename = helper.get_env_key_filename(host_config)

    java.v8_install(host_config)

    software_config = helper.get_software_config(host_config, 'kafka-broker')

    version = software_config.get('version', '0.10.0.1')

    put('{}/software/scripts/kafka-broker.sh'.format(os.getcwd()), '~/', use_sudo=True)
    sudo("chmod +x kafka-broker.sh")
    sudo(". ~/kafka-broker.sh {}".format(version))

    broker_id = software_config.get('broker-id', '0')
    zk_hosts = software_config.get('zookeeper-hosts', 'localhost:2181')
    log_directories = software_config.get('log-directories', '/var/lib/kafka-logs')

    tag = '## ---- CUSTOM CONFIGURATION ---'

    sudo('echo "{}" | sudo tee -a /srv/kafka/config/server.properties'.format(tag))
    sudo('echo "delete.topic.enable = true" | sudo tee -a /srv/kafka/config/server.properties')
    sudo('echo "broker.id={}" | sudo tee -a /srv/kafka/config/server.properties'.format(broker_id))
    sudo('echo "zookeeper.connect={}" | sudo tee -a /srv/kafka/config/server.properties'.format(zk_hosts))
    sudo('echo "log.dirs={}" | sudo tee -a /srv/kafka/config/server.properties'.format(log_directories))
    sudo('echo "listeners=PLAINTEXT://{}:9093" | sudo tee -a /srv/kafka/config/server.properties'.format(host_config['private-ip']))
    sudo('echo "{}" | sudo tee -a /srv/kafka/config/server.properties'.format(tag))

    sudo("service kafka restart")
kafka.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def manager_install(host_config):
    env.host_string = helper.get_env_host_string(host_config)
    env.user = helper.get_env_user(host_config)
    env.key_filename = helper.get_env_key_filename(host_config)

    software_config = helper.get_software_config(host_config, 'kafka-manager')
    zk_hosts = software_config.get('zookeeper-hosts', 'localhost:2181')

    put('{}/software/scripts/kafka-manager.sh'.format(os.getcwd()), '~/', use_sudo=True)
    sudo("chmod +x kafka-manager.sh")
    sudo(". ~/kafka-manager.sh {}".format(zk_hosts))
kafka.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def create_topic(config_file, topic, replication_factor=1, partitions=1):
    """Creates a Kafka topic | args: config_file, topic name, replication factor, partitions"""
    cfg = helper.get_config(config_file)
    host_config = get_kafka_host_cfg(cfg)

    cmd = "/srv/kafka/bin/kafka-topics.sh --create --zookeeper {} " \
          "--replication-factor {} --partitions {} --topic {}".format(get_zk_host(cfg),
                                                                      replication_factor,
                                                                      partitions,
                                                                      topic)
    env.host_string = helper.get_env_host_string(host_config)
    env.user = helper.get_env_user(host_config)
    env.key_filename = helper.get_env_key_filename(host_config)

    sudo(cmd)
kafka.py 文件源码 项目:bigdata-tools 作者: kylebush 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def delete_topic(config_file, topic):
    """Deletes a Kafka topic | args: config_file, topic name"""
    cfg = helper.get_config(config_file)
    host_config = get_kafka_host_cfg(cfg)

    cmd = "/srv/kafka/bin/kafka-topics.sh --delete --zookeeper {} --topic {}".format(get_zk_host(cfg), topic)

    env.host_string = helper.get_env_host_string(host_config)
    env.user = helper.get_env_user(host_config)
    env.key_filename = helper.get_env_key_filename(host_config)

    sudo(cmd)


问题


面经


文章

微信
公众号

扫码关注公众号