Python產生Redis模板指令碼

來源:互聯網
上載者:User

標籤:redis設定檔

#!/usr/bin/env python#-*- encoding:utf8 -*-#---------------------------------------------------------------------------------------#           FILE:    Gen_Redis Config.py#   #          USAGE:    Python_Code_Style.txt [-h] [Copy PEP 8]##    DESCRIPTION:    Copy python style guide and coding standard .#                    The default copy example is the current text .#                    Dont.t find text on other directories .##        OPTIONS:    see fuction ‘usage‘ below #           BUGS:    ---#         AUTHOR:    Dr.-Vision. Simple(sp)#        VERSION:    1.0#        CREATED:    08.18.2014 - 10:15:30#       REVISION:    08.20.2014#        PROJECT:    PDE #      COPYRITHT:    Copyright(c)2002-2014 Python, All Rights Reserved#---------------------------------------------------------------------------------------# import python module#---------------------------------------------------------------------------------------#   define python import modules .#---------------------------------------------------------------------------------------import os#   Python Class comments#========   CLASS   ====================================================================#          NAME:        #   DESCRIPTION:    Display usage information for this script.#  PARAMETER  1:    ---#=======================================================================================def  gen_Master_Config(port):    data  = """################################ START ##################################### include common configinclude /usr/local/redis/etc/redis-common.conf# listen portport %(port)s# max memorymaxmemory 16Gpidfile /var/run/redis-%(port)s.pidlogfile /data/redis/logs/redis-%(port)s.log#記憶體耗盡時採用的淘汰策略:# volatile-lru -> remove the key with an expire set using an LRU algorithm# allkeys-lru -> remove any key accordingly to the LRU algorithm# volatile-random -> remove a random key with an expire set# allkeys-random -> remove a random key, any key# volatile-ttl -> remove the key with the nearest expire time (minor TTL)# noeviction -> don‘t expire at all, just return an error on write operationsmaxmemory-policy allkeys-lru#aof隱藏檔appendfilename "appendonly-%(port)s.aof"#rdb檔案,只用於動態添加slave過程dbfilename dump-%(port)s.rdb#cluster設定檔(啟動自動產生)cluster-config-file nodes-%(port)s.conf#部署在同一機器的redis執行個體,把auto-aof-rewrite搓開,防止瞬間fork所有redis進程做rewrite,佔用大量記憶體auto-aof-rewrite-percentage 80-100################################ END  #####################################"""    values = {"port":port}    #print(data % values)    with open((‘redis-%s.conf‘)%(port),"w") as f:        #f.write((data % values).encode(‘utf8‘))        f.write(data % values)    f.closedef gen_Slave_Config(port,master_addr):    data  = """################################ START ##################################### include common configinclude /usr/local/redis/etc/redis-common.conf# listen portport %(port)s# max memorymaxmemory 16Gpidfile /var/run/redis-%(port)s.pidlogfile /data/redis/logs/redis-%(port)s.log#slaveof %(master_addr)s#記憶體耗盡時採用的淘汰策略:# volatile-lru -> remove the key with an expire set using an LRU algorithm# allkeys-lru -> remove any key accordingly to the LRU algorithm# volatile-random -> remove a random key with an expire set# allkeys-random -> remove a random key, any key# volatile-ttl -> remove the key with the nearest expire time (minor TTL)# noeviction -> don‘t expire at all, just return an error on write operationsmaxmemory-policy allkeys-lru#aof隱藏檔appendfilename "appendonly-%(port)s.aof"#rdb檔案,只用於動態添加slave過程dbfilename dump-%(port)s.rdb#cluster設定檔(啟動自動產生)cluster-config-file nodes-%(port)s.conf#部署在同一機器的redis執行個體,把auto-aof-rewrite搓開,防止瞬間fork所有redis進程做rewrite,佔用大量記憶體auto-aof-rewrite-percentage 80-100################################ END  #####################################"""    values = {"port":port,"master_addr":master_addr}    #print(data % values)    with open((‘redis-%s.conf‘)%(port),"w") as f:        #f.write((data % values).encode(‘utf8‘))        f.write(data % values)    f.closedef  gen_Service_Config(port):    data = """#!/bin/bash#chkconfig: 2345 80 90## Simple Redis init.d script conceived to work on Linux systems# as it does use of the /proc filesystem.REDISPORT=%(port)sREDIS_PATH="/usr/local/redis"EXEC="/usr/local/redis/bin/redis-server"CLIEXEC="/usr/local/redis/bin/redis-cli"PIDFILE="/var/run/redis-%(port)s.pid"CONF="${REDIS_PATH}/etc/redis-%(port)s.conf"#PASSWD="superredis"case "$1" in    start)        if [ -f $PIDFILE ]        then                echo "$PIDFILE exists, process is already running or crashed"        else                echo "Starting Redis server..."                $EXEC $CONF &                                echo -e "$!">${PIDFILE}        fi        ;;    stop)        if [ ! -f $PIDFILE ]        then                echo "$PIDFILE does not exist, process is not running"        else                PID=$(cat $PIDFILE)                echo "Stopping ..."                #$CLIEXEC -p $REDISPORT -a ${PASSWD} shutdown                $CLIEXEC -p $REDISPORT shutdown                while [ -x /proc/${PID} ]                do                    echo "Waiting for Redis to shutdown ..."                    sleep 1                done                echo "Redis stopped"        fi        ;;        status)                if [[ -f ${PIDFILE} ]];then                echo "$PIDFILE exists, redis is already running"                else                echo "$PIDFILE is not exists"                fi                ;;    *)        echo "Please use start or stop as first argument"        ;;esac"""    values = {"port":port}    #print(data % values)    with open((‘redis-%s‘)%(port),"w") as f:        #f.write((data % values).encode(‘utf8‘))        f.write(data % values)    f.close#   Python main comments#---------------------------------------------------------------------------------------#   define python main .#---------------------------------------------------------------------------------------if __name__ == "__main__":    master_addr = "192.1678.10.130"    os.chdir(‘/usr/local/src/SsoRedis‘)    os.chdir(‘master/etc‘)    for i in range(6379,6387):        gen_Master_Config(str(i))    for i in range(6379,6387):        os.chdir(‘/usr/local/src/SsoRedis‘)        os.chdir(‘master/service‘)        gen_Service_Config(str(i))        os.chdir(‘/usr/local/src/SsoRedis‘)        os.chdir(‘slave/service‘)        gen_Service_Config(str(i))    for i in range(6379,6387):        os.chdir(‘/usr/local/src/SsoRedis‘)        os.chdir(‘slave/etc‘)        gen_Slave_Config(port=str(i),master_addr=master_addr)

本文出自 “關注後台設計” 部落格,請務必保留此出處http://xleft.blog.51cto.com/955567/1895598

Python產生Redis模板指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.