修改devops部署流程
This commit is contained in:
parent
18b049d6c0
commit
9fa84cd26e
|
|
@ -43,37 +43,15 @@ def run_docker_compose(compose_dir, service_name):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 构建镜像
|
# 构建镜像
|
||||||
Logger.info(f"执行命令: docker-compose build --no-cache {service_name}")
|
if not Logger.run_command(f"docker-compose build --no-cache {service_name}", compose_dir):
|
||||||
result = subprocess.run(
|
|
||||||
f"docker-compose build --no-cache {service_name}",
|
|
||||||
shell=True,
|
|
||||||
cwd=compose_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error(f"镜像构建失败: {service_name}")
|
Logger.error(f"镜像构建失败: {service_name}")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info(f"镜像构建成功: {service_name}")
|
Logger.info(f"镜像构建成功: {service_name}")
|
||||||
|
|
||||||
# 启动服务
|
# 启动服务
|
||||||
Logger.info(f"执行命令: docker-compose up -d {service_name}")
|
if not Logger.run_command(f"docker-compose up -d {service_name}", compose_dir):
|
||||||
result = subprocess.run(
|
|
||||||
f"docker-compose up -d {service_name}",
|
|
||||||
shell=True,
|
|
||||||
cwd=compose_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error(f"服务启动失败: {service_name}")
|
Logger.error(f"服务启动失败: {service_name}")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info(f"服务启动成功: {service_name}")
|
Logger.info(f"服务启动成功: {service_name}")
|
||||||
|
|
|
||||||
|
|
@ -86,37 +86,15 @@ def init_mysql(project_root, pre_deploy_commands=None):
|
||||||
Logger.info("构建 MySQL 镜像")
|
Logger.info("构建 MySQL 镜像")
|
||||||
Logger.separator()
|
Logger.separator()
|
||||||
Logger.info(f"执行目录: {docker_dir}")
|
Logger.info(f"执行目录: {docker_dir}")
|
||||||
Logger.info("执行命令: docker-compose build --no-cache ruoyi-mysql")
|
|
||||||
|
|
||||||
result = subprocess.run(
|
if not Logger.run_command("docker-compose build --no-cache ruoyi-mysql", docker_dir):
|
||||||
"docker-compose build --no-cache ruoyi-mysql",
|
|
||||||
shell=True,
|
|
||||||
cwd=docker_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("MySQL 镜像构建失败")
|
Logger.error("MySQL 镜像构建失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("MySQL 镜像构建成功")
|
Logger.info("MySQL 镜像构建成功")
|
||||||
|
|
||||||
Logger.info("执行命令: docker-compose up -d ruoyi-mysql")
|
if not Logger.run_command("docker-compose up -d ruoyi-mysql", docker_dir):
|
||||||
result = subprocess.run(
|
|
||||||
"docker-compose up -d ruoyi-mysql",
|
|
||||||
shell=True,
|
|
||||||
cwd=docker_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("MySQL 容器启动失败")
|
Logger.error("MySQL 容器启动失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("MySQL 容器启动成功")
|
Logger.info("MySQL 容器启动成功")
|
||||||
|
|
|
||||||
|
|
@ -35,37 +35,15 @@ def init_nacos(project_root):
|
||||||
|
|
||||||
# 构建并启动 Nacos 容器
|
# 构建并启动 Nacos 容器
|
||||||
Logger.info(f"执行目录: {docker_dir}")
|
Logger.info(f"执行目录: {docker_dir}")
|
||||||
Logger.info("执行命令: docker-compose build --no-cache ruoyi-nacos")
|
|
||||||
|
|
||||||
result = subprocess.run(
|
if not Logger.run_command("docker-compose build --no-cache ruoyi-nacos", docker_dir):
|
||||||
"docker-compose build --no-cache ruoyi-nacos",
|
|
||||||
shell=True,
|
|
||||||
cwd=docker_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("Nacos 镜像构建失败")
|
Logger.error("Nacos 镜像构建失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("Nacos 镜像构建成功")
|
Logger.info("Nacos 镜像构建成功")
|
||||||
|
|
||||||
Logger.info("执行命令: docker-compose up -d ruoyi-nacos")
|
if not Logger.run_command("docker-compose up -d ruoyi-nacos", docker_dir):
|
||||||
result = subprocess.run(
|
|
||||||
"docker-compose up -d ruoyi-nacos",
|
|
||||||
shell=True,
|
|
||||||
cwd=docker_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("Nacos 容器启动失败")
|
Logger.error("Nacos 容器启动失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("Nacos 容器启动成功")
|
Logger.info("Nacos 容器启动成功")
|
||||||
|
|
|
||||||
|
|
@ -35,37 +35,15 @@ def init_redis(project_root):
|
||||||
|
|
||||||
# 构建并启动 Redis 容器
|
# 构建并启动 Redis 容器
|
||||||
Logger.info(f"执行目录: {docker_dir}")
|
Logger.info(f"执行目录: {docker_dir}")
|
||||||
Logger.info("执行命令: docker-compose build --no-cache ruoyi-redis")
|
|
||||||
|
|
||||||
result = subprocess.run(
|
if not Logger.run_command("docker-compose build --no-cache ruoyi-redis", docker_dir):
|
||||||
"docker-compose build --no-cache ruoyi-redis",
|
|
||||||
shell=True,
|
|
||||||
cwd=docker_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("Redis 镜像构建失败")
|
Logger.error("Redis 镜像构建失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("Redis 镜像构建成功")
|
Logger.info("Redis 镜像构建成功")
|
||||||
|
|
||||||
Logger.info("执行命令: docker-compose up -d ruoyi-redis")
|
if not Logger.run_command("docker-compose up -d ruoyi-redis", docker_dir):
|
||||||
result = subprocess.run(
|
|
||||||
"docker-compose up -d ruoyi-redis",
|
|
||||||
shell=True,
|
|
||||||
cwd=docker_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("Redis 容器启动失败")
|
Logger.error("Redis 容器启动失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("Redis 容器启动成功")
|
Logger.info("Redis 容器启动成功")
|
||||||
|
|
|
||||||
|
|
@ -103,3 +103,49 @@ class Logger:
|
||||||
def separator():
|
def separator():
|
||||||
"""输出分隔线"""
|
"""输出分隔线"""
|
||||||
print("=" * 60)
|
print("=" * 60)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def run_command(cls, cmd, cwd=None):
|
||||||
|
"""
|
||||||
|
执行命令并实时输出日志到控制台和文件
|
||||||
|
|
||||||
|
参数:
|
||||||
|
cmd: 要执行的命令
|
||||||
|
cwd: 工作目录
|
||||||
|
|
||||||
|
返回:
|
||||||
|
bool: 成功返回 True,失败返回 False
|
||||||
|
"""
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
cls.info(f"执行命令: {cmd}")
|
||||||
|
cls.info(f"工作目录: {cwd if cwd else '当前目录'}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
process = subprocess.Popen(
|
||||||
|
cmd,
|
||||||
|
shell=True,
|
||||||
|
cwd=cwd,
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.STDOUT,
|
||||||
|
text=True,
|
||||||
|
bufsize=1
|
||||||
|
)
|
||||||
|
|
||||||
|
# 实时读取并输出
|
||||||
|
for line in process.stdout:
|
||||||
|
line = line.rstrip()
|
||||||
|
if line:
|
||||||
|
# 直接打印到控制台和写入文件
|
||||||
|
print(line)
|
||||||
|
if cls._log_file:
|
||||||
|
cls._rotate_log()
|
||||||
|
with open(cls._log_file, 'a', encoding='utf-8') as f:
|
||||||
|
f.write(line + '\n')
|
||||||
|
|
||||||
|
process.wait()
|
||||||
|
return process.returncode == 0
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
cls.error(f"命令执行异常: {e}")
|
||||||
|
return False
|
||||||
|
|
|
||||||
|
|
@ -40,19 +40,8 @@ def run_maven(work_dir, maven_commands, source_path, target_dir):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 执行 Maven 命令
|
# 执行 Maven 命令
|
||||||
Logger.info(f"执行命令: {maven_commands}")
|
if not Logger.run_command(maven_commands, work_dir):
|
||||||
result = subprocess.run(
|
|
||||||
maven_commands,
|
|
||||||
shell=True,
|
|
||||||
cwd=work_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("Maven 打包失败")
|
Logger.error("Maven 打包失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("Maven 打包成功")
|
Logger.info("Maven 打包成功")
|
||||||
|
|
|
||||||
|
|
@ -40,19 +40,8 @@ def run_npm(work_dir, npm_commands, source_dir, target_dir):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# 执行 NPM 命令
|
# 执行 NPM 命令
|
||||||
Logger.info(f"执行命令: {npm_commands}")
|
if not Logger.run_command(npm_commands, work_dir):
|
||||||
result = subprocess.run(
|
|
||||||
npm_commands,
|
|
||||||
shell=True,
|
|
||||||
cwd=work_dir,
|
|
||||||
capture_output=True,
|
|
||||||
text=True
|
|
||||||
)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
Logger.error("NPM 打包失败")
|
Logger.error("NPM 打包失败")
|
||||||
if result.stderr:
|
|
||||||
Logger.error(f"错误信息: {result.stderr}")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
Logger.info("NPM 打包成功")
|
Logger.info("NPM 打包成功")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue