st0627 发表于 2018-8-15 11:56:15

Python生成Redis模板脚本

#!/usr/bin/env python  
#-*- encoding:utf8 -*-
  
#---------------------------------------------------------------------------------------
  
#         FILE:    Gen_Redis Config.py
  
#
  
#          USAGE:    Python_Code_Style.txt [-h]
  
#
  
#    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.
  
#PARAMETER1:    ---
  
#=======================================================================================
  
defgen_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
  
defgen_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)
页: [1]
查看完整版本: Python生成Redis模板脚本