Python生成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 config
include /usr/local/redis/etc/redis-common.conf
# listen port
port %(port)s
# max memory
maxmemory 16G
pidfile /var/run/redis-%(port)s.pid
logfile /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 operations
maxmemory-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.close
def gen_Slave_Config(port,master_addr):
    data  = """################################ START ####################################
# include common config
include /usr/local/redis/etc/redis-common.conf
# listen port
port %(port)s
# max memory
maxmemory 16G
pidfile /var/run/redis-%(port)s.pid
logfile /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 operations
maxmemory-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.close
def  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)s
REDIS_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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章