redis的cluster集群模式python一键安装脚本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CleverCode/article/details/90299268
# -*- coding: utf-8 -*-
"""
    redisCluster
"""
# appConfig.py
#
# Copyright (c) 2019 by clevercode.com, P.R.China
#
# modification history:
# --------------------
# 2019/5/17, by clevercode, Create


import sys
import os
import getopt
import time

class redisCluster(object):

    CONF_DIR = "/opt/cachecloud/conf/";
    LOG_DIR = "/opt/cachecloud/logs/";
    REDIS_SRC_DIR = "/opt/cachecloud/redis/src/";
    REDIS_CLUSTER_TEMPLATE = "/opt/cachecloud/conf/redis-cluster-template.conf"

    SLOT_SIZE = 16384

    def __init__(self,ports,maxMemory):    
	"""
        ports array [6501,6502,6503,6504,6505,6506]
        maxMemory string 512
	"""
        self.ports = ports
        self.maxMemory = maxMemory
        self.__checkProperty(); 

    def __checkProperty(self):
        ""
        ""
        if len(self.ports) % 2 != 0:
            raise RuntimeError('Invalid ports,ports must be even number!')

        if self.maxMemory < 1:
            raise RuntimeError('Invalid maxMemory,maxMemory < 1!')

    def __createConfigFile(self):
        """
        create config file base on template
        """
	
	# config file is all Exists
        isAllExists = 1
	for port in self.ports:
            configFile = self.CONF_DIR + "redis-cluster-" + str(port) +".conf"

	    if False == os.path.isfile(configFile):
		isAllExists = 0
		break

	if isAllExists:
	    return "OK"

        #redis template
        f = open(self.REDIS_CLUSTER_TEMPLATE, 'r')
        template = f.read()
        f.close()

        #replace {:maxmemory} to maxMemory
        template = template.replace("{:maxmemory}",str(maxMemory))

        for port in self.ports:
            
            #replace {:port} to port 
            configContent = template.replace("{:port}",str(port))

            configFile = self.CONF_DIR + "redis-cluster-" + str(port) +".conf"
            f = open(configFile, 'w')
            f.write(configContent)
            f.close()

	return "OK"

    def __runRedisServer(self):
	"""
	run redis servers
	"""
	for port in self.ports:
            
            #replace {:port} to port 
	    redisServerCmd = self.REDIS_SRC_DIR + "redis-server" 
            configFile = self.CONF_DIR + "redis-cluster-" + str(port) +".conf"
            logFile = self.LOG_DIR + "redis-%s-%s.log" % (port,time.strftime('%Y%m%d%H%M', time.localtime(time.time())))
	    cmd = "nohup %s %s > %s 2>&1 &" % (redisServerCmd,configFile,logFile)
	    os.system(cmd)

	return "OK"

    def __meet(self):

	firstPort = self.ports[0]
 
    	for port in self.ports:
            cmd = self.REDIS_SRC_DIR + "redis-cli -p %s" % firstPort + " --raw cluster meet 127.0.0.1 %s > /dev/null 2>&1" % port 
	    os.system(cmd)

    def __slotadd(self):
        count = len(self.ports)
	count = 8
        slot_num = self.SLOT_SIZE / count * 2
        for i in xrange(count/2):
            begin = slot_num * i
            end = begin + slot_num
            
            # ×îºóÒ»¸öÖ÷½Úµã
            if i == count/2 - 1: 
                end = self.SLOT_SIZE 
            print begin, end
            #stdin, stdout, stderr = os.popen3("redis-cli -p %d"  % curr)
            #for index in xrange(begin, end):
                 #print index
            #break 
            #    stdin.write("cluster addslots %d\n" % index)
            #stdin.write("exit\n")
            #stdin.flush()

	
    def start(self):
        
        self.__createConfigFile()

	self.__runRedisServer()

	try:
	    self.__meet()
	except:
	    return "ERROR"

	self.__slotadd()

        return "OK"


    def stop(self):
	
	for port in self.ports:
	    cmd = self.REDIS_SRC_DIR + "redis-cli -p %s shutdown nosave " % (port)
	    os.system(cmd)
	return "OK"        

if __name__ == '__main__':
    #ports = "6501,6502,6503,6504,6505,6506"
    ports = "6701,6702,6703,6704,6705,6706"
    ports = ports.split(",")
    maxMemory = 100
    redis = redisCluster(ports,maxMemory)
    redis.start()
    #redis.stop()
    


展开阅读全文

没有更多推荐了,返回首页