
#!/usr/bin/env python3
# ! -*- coding:utf-8 -*-
import fcntl
import os
import psutil
import re
import shutil
import socket
import struct
import tarfile
firdir = os.getcwd()
def get_ip_address():
try:
csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
csock.connect(('8.8.8.8', 80))
(addr, port) = csock.getsockname()
csock.close()
return addr
except socket.error:
return "127.0.0.1"
def copy_g(x, y, z, s=1):
with open(x, "r") as f:
lines = f.readlines()
with open(x, "w") as f_w:
n = 0
if s == 1:
for line in lines:
if y in line:
line = line.replace(y, z)
f_w.write(line)
n += 1
break
f_w.write(line)
n += 1
for i in range(n, len(lines)):
f_w.write(lines[i])
elif s == 'g':
for line in lines:
if y in line:
line = line.replace(y, z)
f_w.write(line)
def redis_install(port=6301):
basedir = '/data/redis'
workdir = os.getcwd()
os.makedirs('/data/redis/' + bytes(port) + '/run')
os.makedirs('/data/redis/' + bytes(port) + '/data')
os.makedirs('/data/redis/' + bytes(port) + '/tmp')
shutil.copyfile("6301.conf", "/data/redis/" + bytes(port) + "/run/" + bytes(port) + ".conf")
shutil.copyfile("switch.sh", "/data/redis/" + bytes(port) + "/run/switch.sh")
os.system('yum -y install tcl-8.5*')
ta = tarfile.open('redis-2.8.20.tar.gz', 'r')
ta.extractall('/usr/src')
ta.close()
os.chdir('/usr/src/redis-2.8.20')
os.system('make install PREFIX=/data/redis')
os.system('make test')
os.system('\cp /data/redis/bin/* /usr/local/sbin/')
os.chmod("/data/redis/" + bytes(port) + "/run/" + bytes(port) + ".conf", 755)
os.chmod("/data/redis/" + bytes(port) + "/run/switch.sh", 755)
def redis_install(MASIP, port=6301):
basedir = '/data/redis'
workdir = os.getcwd()
os.makedirs('/data/redis/' + bytes(port) + '/run')
os.makedirs('/data/redis/' + bytes(port) + '/data')
os.makedirs('/data/redis/' + bytes(port) + '/tmp')
shutil.copyfile("6301.conf", "/data/redis/" + bytes(port) + "/run/" + bytes(port) + ".conf")
shutil.copyfile("switch.sh", "/data/redis/" + bytes(port) + "/run/switch.sh")
os.system('yum -y install tcl-8.5*')
ta = tarfile.open('redis-2.8.20.tar.gz', 'r')
ta.extractall('/usr/src')
ta.close()
os.chdir('/usr/src/redis-2.8.20')
os.system('make install PREFIX=/data/redis')
os.system('make test')
os.system('\cp /data/redis/bin/* /usr/local/sbin/')
os.chmod("/data/redis/" + bytes(port) + "/run/" + bytes(port) + ".conf", 755)
os.chmod("/data/redis/" + bytes(port) + "/run/switch.sh", 755)
os.chdir('/data/redis/' + bytes(port) + '/run')
os.system('echo \'slaveof ' + bytes(MASIP) + ' 6301\' >> 6301.conf')
def redis_example(port):
os.chdir('/data/redis/')
shutil.copytree('6301', port)
os.chdir('/data/redis/' + bytes(port) + '/run')
if os.path.exists('6301.pid'):
os.remove('6301.log')
os.remove('6301.pid')
shutil.move("6301.conf", bytes(port) + ".conf")
copy_g(bytes(port) + ".conf", "6301", port, "g")
copy_g("switch.sh", "6301", port, "g")
os.chdir('/data/redis/' + bytes(port))
os.system('rm -rf data/*')
os.system('./run/switch.sh start')
def redis_shing(port, MASIP):
MYIP = get_ip_address()
os.chdir(firdir)
os.mkdir('/data/redis/' + bytes(port))
mbfile = '/data/redis/' + bytes(port)
shutil.copyfile("26302.conf", bytes(port) + ".conf")
copy_g(bytes(port) + ".conf", "127.0.0.1", MASIP, "g")
copy_g(bytes(port) + ".conf", "26301", port, "g")
os.chmod(bytes(port) + ".conf", 755)
shutil.move(bytes(port) + ".conf", mbfile)
os.system('redis-sentinel /data/redis/' + bytes(port) + '/' + bytes(port) + '.conf &')
def redis_status():
pids = psutil.pids()
for pid in pids:
str1 = 'redis'
p = psutil.Process(pid)
if str1 in p.name():
os.system('ps -ef | grep %s | grep -v grep ' % pid)
def redis_status_shing():
pids = psutil.pids()
L = []
for pid in pids:
str1 = 'redis-sentinel'
p = psutil.Process(pid)
if str1 in p.name():
if pid != 'None':
L.append(pid)
return L
def redis_start_redis(port):
os.chdir('/data/redis/' + bytes(port) + '/run/')
os.system('./switch.sh start')
def redis_start_shing(port):
os.chdir('/data/redis/bin')
os.system('redis-sentinel /data/redis/' + bytes(port) + '/' + bytes(port) + '.conf')
def redis_close_redis(port):
os.chdir('/data/redis/' + bytes(port) + '/run/')
os.system('./switch.sh stop')
def redis_close_shing():
ts = redis_status_shing()
for t in ts:
os.system('kill -9 %s' % t)
def redis_test_file(port=6301):
if not os.path.exists('/data/redis/' + bytes(port)):
return "redis未安装"
def redis_test_shing(port):
if not os.path.exists('/data/redis/' + bytes(port)):
print("未安装该端口的哨兵")
if __name__ == '__main__':
MYIP = get_ip_address()
if MYIP == "127.0.0.1":
print("IP地址获取失败!!!")
else:
print("=======欢迎使用redis多实例一键安装脚本=========")
print("1.主 2.从 3.单实例安装 4.管理")
manstr = raw_input("请输入:")
if manstr == "1":
print("1.安装redis 2.多实例安装")
md = raw_input("请输入:")
if md == "1":
redis_install()
elif md == "2":
redis_test_file()
redis_example(6379)
redis_example(6380)
redis_example(6390)
redis_example(6600)
redis_example(6800)
redis_example(6900)
redis_example(6700)
redis_shing(26301)
redis_shing(26700)
elif manstr == "2":
IP = raw_input("请输入主redis的IP:")
print("1.安装redis 2.多实例安装")
md = raw_input("请输入:")
if md == "1":
redis_install(IP)
elif md == "2":
redis_test_file()
redis_example(6700)
redis_shing(26301)
redis_shing(26700)
else:
print("Please input 1 or 2 ")
elif manstr == "3":
print("1.安装实例 2.安装哨兵")
md = raw_input("请输入:")
if md == "1":
port = raw_input("请输入实例端口:")
redis_example(port)
elif md == "2":
ip = raw_input("请输入主redis的ip:")
port = raw_input("请输入哨兵端口:")
redis_shing(port, ip)
else:
print("Please input 1 or 2 ")
elif manstr == "4":
print("1.查看redis 2.启动redis")
print("3.启动哨兵 4.关闭redis、哨兵")
md = raw_input("请输入:")
if md == "1":
redis_status()
elif md == "2":
port = raw_input("请输入redis的端口号:")
if not os.path.exists('/data/redis/' + bytes(port)):
print("redis未安装")
else:
redis_start_redis(port)
redis_status()
elif md == "3":
port = raw_input("请输入哨兵的端口号:")
if not os.path.exists('/data/redis/' + bytes(port)):
print("哨兵未安装")
else:
redis_start_shing(port)
redis_status()
elif md == "4":
redis_close_redis(6301)
redis_close_shing()
redis_status()
else:
print("Please input 1 or 2 or 3 or 4")
else:
print("输入错误!!!")