一起采坑redis(4)--redis安装python脚本

centos7以下由于没有systemctl,配置让redis自动启动如下:

echo "/usr/local/bin/redis-server /etc/redis/redis.conf &" >> /etc/rc.local

centos7 and later可以使用systemctl 配置守护进程,以下是python 安装脚本:

#!/usr/bin/python
#-*- conding:utf-8 -*-
#for redis comm
import getopt
import sys
import os
import tarfile
import socket
import commands
import logging
from tqdm import tqdm
logging.basicConfig(level=logging.DEBUG,
                    format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘,
                    datefmt=‘%a, %d %b %Y %H:%M:%S‘,
                    filename=‘/tmp/redis_install.log‘,
                    filemode=‘w‘)
def usage():
    filePath = os.path.abspath(__file__)
    print ‘‘‘
    -a Password \n
    -p Port Number  multiplea‘,‘separate \n
    -r Role m or s \n
    -h access ip address multiplea ‘,‘ separate  \n
    [ usage: %s -a password -p port1,port2,port3 -r m|s -h 127.0.0.1,x.x.x.x ] ‘‘‘ % filePath

class redisClusterWork:
    ‘‘‘ install before check ‘‘‘
    def __init__(self,port,password,role,host):
        self.port = port
        self.password = password
        self.role = role
        self.host = host
        self.master_config = ‘‘‘
bind {host}
protected-mode yes
port {port}
tcp-backlog 511
timeout 0
tcp-keepalive 0
daemonize yes
supervised no
pidfile "/data/redis{port}/redis{port}.pid"
loglevel notice
logfile "/data/redis{port}/redis{port}.log"
databases 16
always-show-logo yes
requirepass {pwd}
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis{port}/"
masterauth {pwd}
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 100000
maxmemory-policy allkeys-lfu
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
#appendonly yes
appendfilename "appendonly.aof"
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
#client-output-buffer-limit slave 2048mb 1500mb 60
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
repl-backlog-size 2500000kb
repl-timeout 5000
# Generated by CONFIG REWRITE
save ""
#save 900 1
#save 300 10
#save 60 10000
rename-command CONFIG "ADMIN_CONFIG"
rename-command FLUSHALL ""
rename-command FLUSHDB  ""
rename-command KEYS     ""
‘‘‘.format(host=self.host,port=self.port,pwd=self.password)
        self.slave_config = ‘‘‘
bind {host}
protected-mode yes
port {port}
tcp-backlog 511
timeout 0
tcp-keepalive 0
daemonize yes
supervised no
pidfile "/data/redis{port}/redis{port}.pid"
loglevel notice
logfile "/data/redis{port}/redis{port}.log"
databases 16
always-show-logo yes
requirepass {pwd}
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/data/redis{port}/"
masterauth {pwd}
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
maxclients 100000
maxmemory-policy allkeys-lfu
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync no
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
#client-output-buffer-limit slave 2048mb 1500mb 60
client-output-buffer-limit slave 0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
repl-backlog-size 2500000kb
repl-timeout 5000
# Generated by CONFIG REWRITE
save 900 1
save 300 10
save 60 10000
rename-command CONFIG "ADMIN_CONFIG"
rename-command FLUSHALL ""
rename-command FLUSHDB  ""
#rename-command KEYS     ""
‘‘‘.format(host=self.host,port=self.port,pwd=self.password)

    def portCheck(self):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            result = sock.connect_ex((‘127.0.0.1‘,int(self.port)))
            if 0 == result:
                print ‘%s Current port to survive‘ % self.port
                logging.error(‘%s Current port to survive‘ % self.port)
                sys.exit()
        except Exception as e:
                logging.error(e)

    def baseDirCheck(self):
        baseDir = ‘/usr/local/redis/‘
        if os.path.isdir(baseDir):
            logging.info(‘redis cluster basedir exists,continue‘)
        else:
            cmd = ‘wget http://download.redis.io/releases/redis-5.0.4.tar.gz -P /opt‘
            status, result = commands.getstatusoutput(cmd)
            if status == 0:
                filename=‘/opt/redis-5.0.4.tar.gz‘
                tar = tarfile.open(filename)
                names = tar.getnames()
                for name in names:
                    tar.extract(name,path=‘/opt/‘)
                tar.close()
                os.chdir(‘/opt/redis-5.0.4/‘)
                mcmd = ‘make  MALLOC=libc prefix=/usr/local/redis/bin‘
                try:
                    status, result = tqdm(commands.getstatusoutput(mcmd))
                    os.rename(‘/opt/redis-5.0.4‘,‘/usr/local/redis‘)
                    logging.info(‘redis packet make success‘)
                except Exception as e:
                        logging.error(e)

    def dataDirCheck(self):
        dataDir = ‘/data/redis%s/‘ % self.port
        if os.path.isdir(dataDir):
            print ‘[%s alerdy exists,please check]‘%dataDir
            logging.error(‘[%s alerdy exists,please check]‘%dataDir)
            sys.exit()
        else:
            os.mkdir(‘/data/redis%s‘ % self.port)
            if self.role == ‘m‘:
                configTxt = self.master_config
            elif self.role == ‘s‘:
                configTxt = self.slave_config
            else:
                print  ‘[ %s must is m or s]‘ % self.role
            fileName = ‘/data/redis{port}/redis{port}.conf‘.format(port=self.port)
            with open(fileName, ‘w‘) as f:
                f.write(configTxt)
            redisGroup = ‘cat /etc/group |grep redis‘
            redisUser=‘cat /etc/passwd |grep redis‘
            gstatus, gresult = commands.getstatusoutput(redisGroup)
            if gstatus == 0:
                pass
            else:
                gcmd = ‘groupadd redis‘
                commands.getstatusoutput(gcmd)
            ustatus, uresult = commands.getstatusoutput(redisUser)
            if ustatus == 0:
                pass
            else:
                ucmd = ‘useradd -M -g redis redis -s /sbin/nologin‘
                commands.getstatusoutput(ucmd)
            xcmd = ‘chown -R redis:redis %s‘% dataDir
            commands.getstatusoutput(xcmd)

    def serviceCheck(self):
        serviceFile = ‘/usr/lib/systemd/system/redis%s.service‘ % self.port
        if os.path.isfile(serviceFile):
            print ‘[%s service name alredy exists]‘% serviceFile
            logging.error(‘[%s service name alredy exists]‘% serviceFile)
        else:
            configTxt=‘‘‘[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/redis/src/redis-server /data/redis{port}/redis{port}.conf --supervised systemd
ExecStop=/usr/local/redis/src/redis-cli -h 127.0.0.1 -p {port} -a {password} shutdown

Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
‘‘‘.format(port=self.port,password=self.password)
        with open(serviceFile, ‘w‘) as f:
            f.write(configTxt)
        cmd = ‘systemctl daemon-reload‘
        commands.getstatusoutput(cmd)

    def redisStart(self):
        cmd = ‘systemctl start redis%s‘% self.port
        status, output = commands.getstatusoutput(cmd)
        if status == 0:
            print ‘%s start success!‘ % self.port
            logging.debug(‘%s start success!‘% self.port)
        else:
            print ‘%s start faild,please check log file‘%self.port
            logging.debug(‘%s start faild,please check log file‘ % self.port)

if __name__ == ‘__main__‘:
    if len(sys.argv) < 4:
        print " No Enter parameter!! "
        usage()
    else:
        try:
            options, args = getopt.getopt(sys.argv[1:], "a:p:r:h:", [ "pwd=", "port=","role=","host="])
            for name, value in options:
                if name in (‘-a‘, ‘-pwd‘):
                    redisPwd = value
                    #print redisPwd
                elif name in (‘-p‘, ‘-port‘):
                    redisPort = value
                elif name in (‘-r‘, ‘-role‘):
                    redisRole = value
                elif name in (‘-h‘, ‘-host‘):
                    redisHost = value.replace(‘,‘,‘ ‘)
                    #print redisPort
            print redisPwd,redisPort,redisRole,redisHost
            for port in tqdm(redisPort.split(‘,‘)):
                workflow = redisClusterWork(port=port,password=redisPwd,role=redisRole,host=redisHost)
                workflow.portCheck()
                workflow.baseDirCheck()
                workflow.dataDirCheck()
                workflow.serviceCheck()
                workflow.redisStart()
        except getopt.GetoptError:
            print ‘Please check if the parameters are incorrect.‘
            usage()
#!/usr/bin/python
#-*- conding:utf-8 -*-
#for redis cluster
import getopt
import sys
import os
import tarfile
import socket
import commands
import logging
from tqdm import tqdm
logging.basicConfig(level=logging.DEBUG,
                    format=‘%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s‘,
                    datefmt=‘%a, %d %b %Y %H:%M:%S‘,
                    filename=‘/tmp/rediscluster.log‘,
                    filemode=‘w‘)
def usage():
    filePath = os.path.abspath(__file__)
    print ‘‘‘
    -a Password \n
    -p Port Number  multiplea‘,‘separate \n
    [ usage: %s -a password -p port1,port2,port3] ‘‘‘ % filePath

class redisClusterWork:
    ‘‘‘ install before check ‘‘‘
    def __init__(self,port,password):
        self.port = port
        self.password = password

    def portCheck(self):
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            result = sock.connect_ex((‘127.0.0.1‘,int(self.port)))
            if 0 == result:
                print ‘%s Current port to survive‘ % self.port
                logging.error(‘%s Current port to survive‘ % self.port)
                sys.exit()
        except Exception as e:
                logging.error(e)

    def baseDirCheck(self):
        baseDir = ‘/usr/local/redis/‘
        if os.path.isdir(baseDir):
            logging.info(‘redis cluster basedir exists,continue‘)
        else:
            cmd = ‘wget http://download.redis.io/releases/redis-5.0.4.tar.gz -P /opt‘
            status, result = commands.getstatusoutput(cmd)
            if status == 0:
                filename=‘/opt/redis-5.0.4.tar.gz‘
                tar = tarfile.open(filename)
                names = tar.getnames()
                for name in names:
                    tar.extract(name,path=‘/opt/‘)
                tar.close()
                os.chdir(‘/opt/redis-5.0.4/‘)
                mcmd = ‘make  MALLOC=libc prefix=/usr/local/redis/bin‘
                try:
                    os.rename(‘/opt/redis-5.0.4‘,‘/usr/local/redis‘)
                    status, result = commands.getstatusoutput(mcmd)
                    logging.info(‘redis packet make success‘)
                except Exception as e:
                        logging.error(e)

    def dataDirCheck(self):
        dataDir = ‘/data/redis%s/‘ % self.port
        if os.path.isdir(dataDir):
            print ‘[%s alerdy exists,please check]‘%dataDir
            logging.error(‘[%s alerdy exists,please check]‘%dataDir)
            sys.exit()
        else:
            os.mkdir(‘/data/redis%s‘ % self.port)
            configTxt=‘‘‘port {port}
bind 0.0.0.0
cluster-enabled yes
cluster-config-file /data/redis{port}/redis_nodes.conf
cluster-node-timeout 15000
appendonly yes
maxclients 100000
maxmemory 0
dir /data/redis{port}
repl-backlog-size 200m
tcp-backlog 1024
logfile /data/redis{port}/redis_server_{port}.log
pidfile /data/redis{port}/redis_{port}.pid
daemonize yes
rename-command CONFIG "ADMIN_CONFIG"
rename-command FLUSHALL "ADMIN_FLUSHALL"
rename-command FLUSHDB "ADMIN_FLUSHDB"
rename-command KEYS ""

client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave  0 0 0
client-output-buffer-limit pubsub 32mb 8mb 60
masterauth {password}
requirepass {password}
‘‘‘.format(port=self.port,password=self.password)
            fileName = ‘/data/redis{port}/redis{port}.conf‘.format(port=self.port)
            with open(fileName, ‘w‘) as f:
                f.write(configTxt)
            redisGroup = ‘cat /etc/group |grep redis‘
            redisUser=‘cat /etc/passwd |grep redis‘
            gstatus, gresult = commands.getstatusoutput(redisGroup)
            if gstatus == 0:
                pass
            else:
                gcmd = ‘groupadd redis‘
                commands.getstatusoutput(gcmd)
            ustatus, uresult = commands.getstatusoutput(redisUser)
            if ustatus == 0:
                pass
            else:
                ucmd = ‘useradd -M -g redis redis -s /sbin/nologin‘
                commands.getstatusoutput(ucmd)
            xcmd = ‘chown -R redis:redis %s‘% dataDir
            commands.getstatusoutput(xcmd)

    def serviceCheck(self):
        serviceFile = ‘/usr/lib/systemd/system/redis%s.service‘ % self.port
        if os.path.isfile(serviceFile):
            print ‘[%s service name alredy exists]‘% serviceFile
            logging.error(‘[%s service name alredy exists]‘% serviceFile)
        else:
            configTxt=‘‘‘[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/local/redis/src/redis-server /data/redis{port}/redis{port}.conf --supervised systemd
ExecStop=/usr/local/redis/src/redis-cli -h 127.0.0.1 -p {port} -a {password} shutdown

Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
‘‘‘.format(port=self.port,password=self.password)
        with open(serviceFile, ‘w‘) as f:
            f.write(configTxt)
        cmd = ‘systemctl daemon-reload‘
        commands.getstatusoutput(cmd)

    def redisStart(self):
        cmd = ‘systemctl start redis%s‘% self.port
        status, output = commands.getstatusoutput(cmd)
        if status == 0:
            print ‘%s start success!‘ % self.port
            logging.debug(‘%s start success!‘% self.port)
        else:
            print ‘%s start faild,please check log file‘%self.port
            logging.debug(‘%s start faild,please check log file‘ % self.port)

if __name__ == ‘__main__‘:
    if len(sys.argv) < 2:
        print " No Enter parameter!! "
        usage()
    else:
        try:
            options, args = getopt.getopt(sys.argv[1:], "a:p:", [ "pwd=", "port="])
            for name, value in options:
                if name in (‘-a‘, ‘-pwd‘):
                    redisPwd = value
                    #print redisPwd
                elif name in (‘-p‘, ‘-port‘):
                    redisPort = value
                    #print redisPort
            #print redisPwd,redisPort
            for port in tqdm(redisPort.split(‘,‘)):
                workflow = redisClusterWork(port=port,password=redisPwd)
                workflow.portCheck()
                workflow.baseDirCheck()
                workflow.dataDirCheck()
                workflow.serviceCheck()
                workflow.redisStart()
        except getopt.GetoptError:
            print ‘Please check if the parameters are incorrect.‘
            usage()

原文地址:https://www.cnblogs.com/monkeybron/p/11026821.html

时间: 2024-10-04 15:51:48

一起采坑redis(4)--redis安装python脚本的相关文章

Redis缓存全自动安装shell脚本

我只是把命令放到shell文件中了,方便安装,代码如下: #!/bin/bash # shell的执行选项: # -n 只读取shell脚本,但不实际执行 # -x 进入跟踪方式,显示所执行的每一条命令 # -c "string" 从strings中读取命令 # 下载目录 downloadsDir=/root/Downloads # 安装目录 appDir=/usr/local/redis # 判断备份目录是否存在,不存时新建目录 [ ! -d $downloadsDir ] &

docker -安装Python 脚本

#! /bin/bashcd /tmpecho "current direction is $(pwd)"yum -y install wgetwget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgztar -zxvf Python-3.6.6.tgz##python dependencyyum install -y gcc patch libffi-devel python-devel zlib-devel bzip2

jumserver 3.0安装及采坑记录

参考  https://blog.51cto.com/14226596/2420441 以及官方文档 https://www.jishuchi.com/read/Jumpserver-148/2102 补充笔者采坑处理 1.安装python3环境 1>更新epel-release $yum install -y python36 python36-devel python36-pip 2>安装python后,还要修改以下两个文件内容: a)/usr/bin/yum 将第一行 #!/usr/bi

原!!win7-64 安装python的 redis客户端库

安装python的redis客户端库 本人系统已装python2.7 利用cmd命令行: 1.cmd-->python -->>>进入python命令下 >>>from urllib import urlopen >>>data = urlopen('http://peak.telecommunity.com/dist/ez_setup.py') >>>open('ez_setup.py','wb').write(data.re

linux下安装python、scrapy、redis、mysql

今天给线上服务器装爬虫环境,随便记录下安装过程,网上有很多类似的安装过程,我只是整理+验证,希望对需要安装的人有帮助 安装python 安装python wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz tar zxvf Python-2.7.11.tgz cd Python-2.7.11 ./configure --prefix=/usr/local make && make altinstall 检查Pyth

源码包安装(Python mysql redis)

一  源码包安装 (1)python3.6源码包安装 ./configure ------> 定制功能 make make install mysql 源码包 cmake make make install (2) redis 源码包安装 make 修改环境变量 vim /etc/profile 添加以下一行: export PATH=/opt/redis-3.2.10/src:$PATH 生效配置 source /etc/profile (3) 让python链接redis unzip red

一起采坑redis(2)--redis 配置

单机配置(slave的配置就多红色部分,slaveof,masterauth) bind 192.168.50.92 protected-mode yes port 6399 tcp-backlog 511 timeout 300 tcp-keepalive 300 daemonize yes supervised no pidfile /data/redis_data/run/redis_6399.pid loglevel notice logfile "/data/redis_data/re

redis应用之安装配置介绍

一.redis介绍: 1.redis定义: Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主持.redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).这些数据类型都

redis缓存的安装和使用

Redis介绍    Redis本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库,但是他的数据可以持久化的保存在磁盘上,解决了服务重启后数据不丢失的问题,他的值可以是string(字符串).list(列表).sets(集合)或者是ordered  sets(被排序的集合),所有的数据类型都具有push/pop.add/remove.执行服务端的并集.交集.两个sets集中的差别等等操作,这些操作都是具有原子性的,Redis还支持各种不同的排序能力    Redis