python3安装redis
2023-12-15 16:12:59
#!/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()
文章来源:https://blog.csdn.net/qq_42704442/article/details/134889382
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!