当前位置: 首页 > article >正文

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()



http://www.kler.cn/a/163075.html

相关文章:

  • UniApp 应用、页面与组件的生命周期详解
  • qt QKeySequence详解
  • HarmonyOS 如何实现传输中的数据加密
  • 封装el-menu
  • FatLab:我的编程课程系列
  • 冗余连接2 hard题 代随C#写法
  • Navicat 技术指引 | 适用于 GaussDB 分布式的模型功能
  • Django的logging-日志模块的简单使用方法
  • 微信小程序网络请求二次封装
  • Java项目开发,业务比较复杂如何减少bug
  • 基于深度学习yolov5实现安全帽人体识别工地安全识别系统-反光衣识别系统
  • ArkTS快速入门
  • CentOS常用基础命令大全(linux命令)2
  • Pycharm Jupyter ModuleNotFoundError 问题解决
  • 【前端】CSS基础(学习笔记)
  • Python合并一个 Excel 里面的多张表
  • 虚幻学习笔记10—C++函数与蓝图的通信
  • django与数据库交互关于当前时间的坑
  • 2023.12.7 关于 MySQL 事务详解
  • C#云LIS系统源码 B/S架构,SaaS模式,可扩展性强
  • 数据结构——二叉树的链式结构
  • pcl-3 pcl结合opencv做svm分类(法向量特征数据)
  • 如何运用gpt改写出高质量的文章 (1)
  • 【计算机网络】应用层电子邮件协议
  • AWS Remote Control ( Wi-Fi ) on i.MX RT1060 EVK - 3 “编译 NXP i.MX RT1060”( 完 )
  • 奇点云2023数智科技大会来了,“双12”直播见!