当前位置: 首页 > 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/news/163075.html

相关文章:

  • 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”直播见!
  • 【游戏引擎 - C#脚本系统】6、C#端调用C++函数
  • 使用 Axios 进行网络请求的全面指南
  • echart中定义brush,默认状态,触发状态
  • MQTT协议对比TCP网络性能测试模拟弱网测试
  • Mybatis XML改查操作(结合上文)
  • SpringBoot集成WebSocket
  • Redis 基础—Redis Desktop Manager(Redis可视化工具)安装及使用教程
  • 如何使用玻璃材质制作3D钻石模型
  • 【conda】利用Conda创建虚拟环境,Pytorch各版本安装教程(Ubuntu)
  • IO多路转接之select