python3安装redis

#!/usr/bin/python3

import os
import platform
import argparse
import shutil

# 自定义变量
default_system = "ubuntu"
default_redis_version = "6.2.6"
default_install_path = "/usr/local/redis"
default_local_package_dir = os.path.dirname(os.path.abspath(__file__))

def stop_firewall():
    if platform.system() == "Linux":
        os.system("systemctl stop firewalld")

def disable_selinux(system):
    if system == "centos":
        os.system("setenforce 0")

def install_redis_dependencies(system):
    if system == "ubuntu":
        os.system("apt-get -y update")
        os.system("apt-get install -y build-essential tcl wget")
    elif system == "centos":
        os.system("yum -y  update")
        os.system("yum install -y build-essential tcl wget")
    else:
        print(f"Unsupported system: {system}")    

def download_redis(version, package_path):
    if not os.path.exists(os.path.join(package_path, f"redis-{version}.tar.gz")):
        os.system(f"wget http://download.redis.io/releases/redis-{version}.tar.gz -P {package_path}")

def extract_redis(version):
    os.system(f"tar xvf redis-{version}.tar.gz")

def compile_and_install_redis(package_path,version,install_path):
    os.chdir(f"redis-{version}")
    os.system(f"make PREFIX={install_path} install")
    shutil.copy(os.path.join(package_path, f"redis-{version}","redis.conf"),os.path.join(install_path, "redis.conf"))

def create_redis_service_file(install_path):
    redis_service_content = f"""[Unit]
Description=Redis In-Memory Data Store
After=network.target

[Service]
Type=forking
ExecStart={install_path}/bin/redis-server {install_path}/redis.conf
ExecStop={install_path}/bin/redis-cli shutdown
Restart=always

[Install]
WantedBy=multi-user.target
"""
    with open("/etc/systemd/system/redis.service", "w") as service_file:
        service_file.write(redis_service_content)
    os.system("sudo systemctl daemon-reload")

def set_redis_autostart(system):
    if system == "ubuntu":
        os.system(f"sudo systemctl enable redis")
    elif system == "centos":
        os.system(f"sudo chkconfig redis on")
        os.system(f"sudo systemctl enable redis")
    else:
        print(f"Unsupported system: {system}")
def modify_redis_config(install_path):
    config_file_path = os.path.join(install_path, "redis.conf")
    with open(config_file_path, "r") as config_file:
        config_lines = config_file.readlines()

    updated_config_lines = []
    for line in config_lines:
        if line.startswith("bind"):
            updated_config_lines.append("bind 0.0.0.0\n")  # Change bind option to bind 0.0.0.0
        else:
            updated_config_lines.append(line)

    with open(config_file_path, "w") as config_file:
        config_file.writelines(updated_config_lines)

def print_installation_info(redis_version, install_path):
    print("Redis installation completed successfully!")
    print("Installation details:")
    print(f"- Redis version: {redis_version}")
    print(f"- Install path: {install_path}")
    print(f"- Redis configuration file: {os.path.join(install_path, 'redis.conf')}")
    print(f"- Redis start command: redis-server")
    print("Remember to start Redis using the above command.")

def main():
    parser = argparse.ArgumentParser(description="Compile and install Redis with custom options")
    parser.add_argument("-s", "--system", choices=["ubuntu", "centos"], default=default_system, help="Choose the system (ubuntu or centos)")
    parser.add_argument("-v", "--version", help="Specify Redis version")
    parser.add_argument("-p", "--path", help="Specify install path")
    parser.add_argument("-l", "--local-package", help="Specify local package path")

    args = parser.parse_args()
    
    system = args.system if args.system else default_system
    redis_version = args.version if args.version else default_redis_version
    install_path = args.path if args.path else default_install_path
    local_package_path = args.local_package if args.local_package else default_local_package_dir

    stop_firewall()
    disable_selinux(system)
    install_redis_dependencies(system)
    download_redis(redis_version, local_package_path)
    extract_redis(redis_version)
    compile_and_install_redis(local_package_path,redis_version,install_path)
    create_redis_service_file(install_path)
    set_redis_autostart(system)
    modify_redis_config(install_path)
    print_installation_info(redis_version, install_path)

if __name__ == "__main__":
    main()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

(~ ̄▽ ̄)~凤凰涅槃

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值