#!/usr/bin/python3
# encoding: utf-8
#filename: gitlab-maintenance.py
#author: gaohaixiang
#writetime:202403041043
import time
import subprocess
import sys
'''
脚本使用示例:
# 显示所有 GitLab 服务的状态。
python3 gitlab-maintenance.py gitlabStatusAll
# 显示某个特定的服务状态
python3 gitlab-maintenance.py gitlabStatusSomeService nginx
# 启动所有 GitLab 服务。
python3 gitlab-maintenance.py gitlabStartAll
# 如果你只想启动特定的服务,例如 gitlab-workhorse:
python3 gitlab-maintenance.py gitlabStartSomeService gitlab-workhorse
# 停止所有 GitLab 服务
python3 gitlab-maintenance.py gitlabStopAll
# 停止特定服务,例如 unicorn:
python3 gitlab-maintenance.py gitlabStopSomeService unicorn
# 重启所有 GitLab 服务
python3 gitlab-maintenance.py gitlabRestartAll
# 仅重启某个特定服务,如 sidekiq
python3 gitlab-maintenance.py gitlabRestSomeService sidekiq
# 应用 /etc/gitlab/gitlab.rb 配置文件中的更改
python3 gitlab-maintenance.py gitlabReconfigure
# 显示当前已应用的配置
python3 gitlab-maintenance.py gitlabShowconfig
# 实时查看 GitLab 服务的日志输出
python3 gitlab-maintenance.py gitlabTailAll
# 查看特定服务日志,如 nginx 的日志:
python3 gitlab-maintenance.py gitlabTailSomeService nginx
# 创建 /etc/gitlab 目录的备份。这个目录包含了 GitLab 的所有配置文件。
# 示例:/etc/gitlab/config_backup/gitlab_config_1709605011_2024_03_05.tar
python3 gitlab-maintenance.py gitlabBackupetc
# 发送 HUP 信号到所有服务。这通常用于快速重载服务而不中断服务。
python3 gitlab-maintenance.py gitlabHup
# 运行此命令以进行无停机升级。
python3 gitlab-maintenance.py gitlabUpgrade
# 卸载 GitLab 服务。
python3 gitlab-maintenance.py gitlabUninstall
# 备份,默认存放路径 /var/opt/gitlab/backups/timestamp_YYYY_MM_DD_gitlab_backup.tar
# 备份示例:/var/opt/gitlab/backups/1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar
python3 gitlab-maintenance.py gitlabBackupCreate
# 简单的恢复备份
python3 gitlab-maintenance.py gitlabBackupRestore 1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar
# gitlab服务监测,当某个服务出现异常,则重启该服务
python3 gitlab-maintenance.py gitlabServiceMonitor
# 依据备份数据,对gitlab进行重新恢复构建
# 恢复gitlab,需要已经安装好了相同版本的gitlab
python3 gitlab-maintenance.py gitlabBackupRestoreReconfiguration 1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar
'''
"""
# gitlab命令展示
# 服务管理
# 显示所有 GitLab 服务或指定服务的状态。
gitlab-ctl status
# 启动所有或指定的 GitLab 服务。
gitlab-ctl start
# 如果你只想启动特定的服务,例如 gitlab-workhorse:
gitlab-ctl start gitlab-workhorse
# 停止所有或指定的 GitLab 服务
gitlab-ctl stop
# 停止特定服务,例如 unicorn:
gitlab-ctl stop unicorn
# 重启所有或指定的 GitLab 服务
gitlab-ctl restart
# 仅重启某个特定服务,如 sidekiq
gitlab-ctl restart sidekiq
# 配置和维护
# 应用 /etc/gitlab/gitlab.rb 配置文件中的更改
gitlab-ctl reconfigure
# 显示当前已应用的配置
gitlab-ctl show-config
# 日志管理
# 实时查看 GitLab 服务的日志输出
gitlab-ctl tail
# 查看特定服务,如 nginx 的日志:
gitlab-ctl tail nginx
# 备份和恢复
# 创建 /etc/gitlab 目录的备份。这个目录包含了 GitLab 的所有配置文件。
# 示例:/etc/gitlab/config_backup/gitlab_config_1709605011_2024_03_05.tar
gitlab-ctl backup-etc
# 安全性
# 发送 HUP 信号到所有服务。这通常用于快速重载服务而不中断服务。
gitlab-ctl hup
# 运行此命令以进行无停机升级。
gitlab-ctl upgrade
# 卸载 GitLab 服务。
gitlab-ctl uninstall
"""
'''
# 备份,默认存放路径 /var/opt/gitlab/backups/timestamp_YYYY_MM_DD_gitlab_backup.tar
# 备份示例:/var/opt/gitlab/backups/1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar
gitlab-backup create
# 恢复,新gitlab与旧gitlab需要相同版本
# 恢复步骤
停止服务:在恢复数据之前,停止所有 GitLab 相关的服务,以防止数据损坏。
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# 恢复备份
gitlab-backup restore BACKUP=1709602272_2024_03_05_16.7.2-ee
# 重新配置 GitLab
gitlab-ctl reconfigure
# 启动所有 GitLab 服务。
gitlab-ctl start
'''
"""
# gitlab各个服务作用
alertmanager:处理 GitLab 发出的警报。
gitaly:负责 GitLab 的 Git 存储。
gitlab-exporter:采集和导出 GitLab 性能指标。
gitlab-kas:GitLab Kubernetes Agent Server,用于 GitLab 与 Kubernetes 的集成。
gitlab-workhorse:轻量级反向代理服务器。
logrotate:负责日志文件的轮替。
nginx:Web 服务器和反向代理。
node-exporter:收集系统级指标。
postgres-exporter:收集 PostgreSQL 数据库的性能指标。
postgresql:GitLab 使用的数据库。
prometheus:监控系统,收集和存储指标。
puma:处理 HTTP 请求的应用服务器。
redis:缓存和消息队列。
redis-exporter:收集 Redis 的性能指标。
sidekiq:后台作业处理。
"""
# 执行shell命令
# 实时读取输出结果,实时将输出结果写入文件
def gitlabShellCommand(gitlabCommand,filewrite):
# 定义命令
#gitlabCommand = ["ping", "-c", "20", "www.baidu.com"]
gitlabcommand = gitlabCommand.split()
# 使用try-except结构来捕获可能的异常
try:
# 创建子进程,将标准输出和标准错误都重定向到管道
with subprocess.Popen(gitlabcommand,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
#text=True,
universal_newlines=True,
bufsize=1) as proc:
# 实时读取输出
while True:
line = proc.stdout.readline()
if not line:
break
print(line, end="") # 打印输出
filewrite.write(line) # 写入文件
filewrite.flush() # 刷新文件缓冲区,确保所有内容都写入文件
# 等待子进程结束,获取退出码
return_code = proc.wait()
if return_code != 0:
# 可以在这里处理非零退出码的情况
print(f"The command exited with return code {return_code}")
except FileNotFoundError as e:
print(f"Error: {e.strerror}. Is the command correct?")
except Exception as e:
print(f"An error occurred: {e}")
# 日期时间获取
def timestamp_time():
timestamp = int(time.time())
# 转换成localtime(格式和时间戳一样)
timelocal = time.localtime(timestamp)
# 转换成新的时间格式(3016-05-05 20:28:54)
datetime = time.strftime("%Y%m%d%H%M%S", timelocal)
return datetime
# 服务管理
# 显示所有 GitLab 服务或指定服务的状态。
def gitlabStatusAll(getdatetime, filewrite):
fileWriteLine(getdatetime,filewrite)
filewrite.writelines("gitlabStatusAll\n")
# #gitlab-ctl status
# getoutput = subprocess.getoutput("/usr/bin/gitlab-ctl status")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "/usr/bin/gitlab-ctl status"
gitlabShellCommand(gitlabCommand,filewrite)
def gitlabStatusSomeService(getdatetime, filewrite,someService):
fileWriteLine(getdatetime,filewrite)
filewrite.writelines("gitlabStatusSomeService\n")
# # gitlab-ctl status gitlab-workhorse
# getoutput = subprocess.getoutput("/usr/bin/gitlab-ctl status %s" % someService)
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "/usr/bin/gitlab-ctl status %s" % someService
gitlabShellCommand(gitlabCommand,filewrite)
# 启动所有或指定的 GitLab 服务。
def gitlabStartAll(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabStartAll\n")
# #gitlab-ctl start
# getoutput = subprocess.getoutput("/usr/bin/gitlab-ctl start")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "/usr/bin/gitlab-ctl start"
gitlabShellCommand(gitlabCommand,filewrite)
# 如果你只想启动特定的服务,例如 gitlab-workhorse:
def gitlabStartSomeService(getdatetime, filewrite,someService):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabStartSomeService\n")
# #gitlab-ctl start gitlab-workhorse
# getoutput = subprocess.getoutput("/usr/bin/gitlab-ctl start %s" % someService)
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "/usr/bin/gitlab-ctl start %s" % someService
gitlabShellCommand(gitlabCommand,filewrite)
# 停止所有或指定的 GitLab 服务
def gitlabStopAll(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabStopAll\n")
# #gitlab-ctl stop
# getoutput = subprocess.getoutput("gitlab-ctl stop")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl stop"
gitlabShellCommand(gitlabCommand,filewrite)
# 停止特定服务,例如 unicorn:
def gitlabStopSomeService(getdatetime, filewrite,someService):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabStopSomeService\n")
# # gitlab-ctl stop unicorn
# getoutput = subprocess.getoutput("gitlab-ctl stop %s" % someService)
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl stop %s" % someService
gitlabShellCommand(gitlabCommand,filewrite)
# 重启所有或指定的 GitLab 服务
def gitlabRestartAll(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabRestartAll\n")
# # gitlab-ctl restart
# getoutput = subprocess.getoutput("gitlab-ctl restart")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl restart"
gitlabShellCommand(gitlabCommand,filewrite)
# 仅重启某个特定服务,如 sidekiq
def gitlabRestSomeService(getdatetime, filewrite,someService):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabRestSomeService\n")
# # gitlab-ctl restart sidekiq
# getoutput = subprocess.getoutput("gitlab-ctl restart %s" % someService)
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl restart %s" % someService
gitlabShellCommand(gitlabCommand,filewrite)
# 配置和维护
# 应用 /etc/gitlab/gitlab.rb 配置文件中的更改
def gitlabReconfigure(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabReconfigure\n")
# #gitlab-ctl reconfigure
# getoutput = subprocess.getoutput("gitlab-ctl reconfigure")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl reconfigure"
gitlabShellCommand(gitlabCommand,filewrite)
# 显示当前已应用的配置
def gitlabShowconfig(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabShowconfig\n")
# #gitlab-ctl show-config
# getoutput = subprocess.getoutput("gitlab-ctl show-config")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl show-config"
gitlabShellCommand(gitlabCommand,filewrite)
# 日志管理
# 实时查看 GitLab 服务的日志输出
def gitlabTailAll(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabTailAll\n")
# #gitlab-ctl tail
# getoutput = subprocess.getoutput("gitlab-ctl tail")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl tail"
gitlabShellCommand(gitlabCommand,filewrite)
# 查看特定服务,如 nginx 的日志:
def gitlabTailSomeService(getdatetime, filewrite,someService):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabTailSomeService\n")
# # gitlab-ctl tail nginx
# getoutput = subprocess.getoutput("gitlab-ctl tail %s" % someService)
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl tail %s" % someService
gitlabShellCommand(gitlabCommand,filewrite)
# 备份和恢复
# 创建 /etc/gitlab 目录的备份。这个目录包含了 GitLab 的所有配置文件。
# 示例:/etc/gitlab/config_backup/gitlab_config_1709605011_2024_03_05.tar
def gitlabBackupetc(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabBackupetc\n")
#gitlab-ctl backup-etc
# getoutput = subprocess.getoutput("gitlab-ctl backup-etc")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl backup-etc"
gitlabShellCommand(gitlabCommand,filewrite)
# 安全性
# 发送 HUP 信号到所有服务。这通常用于快速重载服务而不中断服务。
def gitlabHup(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabHup\n")
# #gitlab-ctl hup
# getoutput = subprocess.getoutput("gitlab-ctl hup")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl hup"
gitlabShellCommand(gitlabCommand,filewrite)
# 运行此命令以进行无停机升级。
def gitlabUpgrade(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabUpgrade\n")
# #gitlab-ctl upgrade
# getoutput = subprocess.getoutput("gitlab-ctl upgrade")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl upgrade"
gitlabShellCommand(gitlabCommand,filewrite)
# 卸载 GitLab 服务。
def gitlabUninstall(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabUninstall\n")
# #gitlab-ctl uninstall
# getoutput = subprocess.getoutput("gitlab-ctl uninstall")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-ctl uninstall"
gitlabShellCommand(gitlabCommand,filewrite)
# 备份,默认存放路径 /var/opt/gitlab/backups/timestamp_YYYY_MM_DD_gitlab_backup.tar
# 备份示例:/var/opt/gitlab/backups/1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar
def gitlabBackupCreate(getdatetime, filewrite):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabBackupCreate\n")
# #gitlab-backup create
# getoutput = subprocess.getoutput("gitlab-backup create")
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-backup create"
gitlabShellCommand(gitlabCommand,filewrite)
# 恢复备份
def gitlabBackupRestore(getdatetime, filewrite,gitlabTarName):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabBackupRestore\n")
#备份文件示例:1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar
#gitlab-backup restore BACKUP=1709602272_2024_03_05_16.7.2-ee
#gitlabTarName = "1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar"
gitlabTarList = gitlabTarName.split("_gitlab_backup.tar")
gitlabtar = gitlabTarList[0]
# getoutput = subprocess.getoutput("gitlab-backup restore %s" % gitlabtar)
# filewrite.writelines(getoutput)
# return getoutput
gitlabCommand = "gitlab-backup restore %s" % gitlabtar
gitlabShellCommand(gitlabCommand,filewrite)
# gitlab服务监测,当某个服务出现异常,则重启该服务
def gitlabServiceMonitor():
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabServiceMonitor\n")
# 获取gitlab服务状态
# getoutput = gitlabStatusAll(getdatetime, filewrite)
getoutput = subprocess.getoutput("/usr/bin/gitlab-ctl status")
# 处理获取的服务状态数据
outputLists = getoutput.split("\n")
for outputLines in outputLists:
outputLine = outputLines.split()
if len(outputLine) > 8:
serviceStatus = outputLine[0]
serviceName = outputLine[1]
#print(serviceStatus,serviceName)
# 如何状态不是 run 则重启该服务
if serviceStatus != "run:":
# 重启服务
gitlabRestSomeService(getdatetime, filewrite,serviceName.replace(":",""))
# else:
# print(serviceStatus, serviceName.replace(":",""))
# 依据备份数据,对gitlab进行重新恢复构建
# 恢复gitlab,需要已经安装好了相同版本的gitlab
def gitlabBackupRestoreReconfiguration(getdatetime,filewrite,gitlabTarName):
fileWriteLine(getdatetime, filewrite)
filewrite.writelines("gitlabBackupRestoreReconfiguration\n")
# 停止服务,unicorn,sidekiq
# gitlab-ctl stop unicorn
# gitlab-ctl stop sidekiq
gitlabStopSomeService(getdatetime, filewrite,"unicorn")
gitlabStopSomeService(getdatetime, filewrite,"sidekiq")
# 恢复备份
# gitlabTarName = "1709602272_2024_03_05_16.7.2-ee_gitlab_backup.tar"
# gitlab-backup restore BACKUP=1709602272_2024_03_05_16.7.2-ee
gitlabBackupRestore(getdatetime, filewrite,gitlabTarName)
# 重新配置 GitLab
# gitlab-ctl reconfigure
gitlabReconfigure(getdatetime, filewrite)
# 启动所有 GitLab 服务。
# gitlab-ctl start
gitlabStartAll(getdatetime, filewrite)
# 日志文件写入换行间隔
def fileWriteLine(getdatetime,filewrite):
filewrite.writelines("\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_\n")
filewrite.writelines(getdatetime+"\n")
def main(getdatetime, filewrite):
# 确保至少有一个参数被传递(脚本名是第一个参数)
if len(sys.argv) < 2:
print("脚本没有携带任何参数")
return
gitlabOperateType = sys.argv[1]
# 参数验证,确保someService和gitlabTarName在需要的时候被提供
if gitlabOperateType in ["gitlabStatusSomeService", "gitlabStartSomeService",
"gitlabStopSomeService", "gitlabRestSomeService",
"gitlabTailSomeService", "gitlabBackupRestore",
"gitlabBackupRestoreReconfiguration"] and len(sys.argv) < 3:
print(f"Error: The operation '{gitlabOperateType}' requires an additional parameter.")
return
# 如果提供了额外的参数,则使用它们
someService = sys.argv[2] if len(sys.argv) > 2 else None
gitlabTarName = sys.argv[2] if len(sys.argv) > 2 else None
# 定义一个字典,将操作类型映射到对应的函数
operations = {
"gitlabStatusAll": lambda: gitlabStatusAll(getdatetime, filewrite),
"gitlabStatusSomeService": lambda: gitlabStatusSomeService(getdatetime, filewrite, someService),
"gitlabStartAll": lambda: gitlabStartAll(getdatetime, filewrite),
"gitlabStartSomeService": lambda: gitlabStartSomeService(getdatetime, filewrite, someService),
"gitlabStopAll": lambda: gitlabStopAll(getdatetime, filewrite),
"gitlabStopSomeService": lambda: gitlabStopSomeService(getdatetime, filewrite, someService),
"gitlabRestartAll": lambda: gitlabRestartAll(getdatetime, filewrite),
"gitlabRestSomeService": lambda: gitlabRestSomeService(getdatetime, filewrite, someService),
"gitlabReconfigure": lambda: gitlabReconfigure(getdatetime, filewrite),
"gitlabShowconfig": lambda: gitlabShowconfig(getdatetime, filewrite),
"gitlabTailAll": lambda: gitlabTailAll(getdatetime, filewrite),
"gitlabTailSomeService": lambda: gitlabTailSomeService(getdatetime, filewrite, someService),
"gitlabBackupetc": lambda: gitlabBackupetc(getdatetime, filewrite),
"gitlabHup": lambda: gitlabHup(getdatetime, filewrite),
"gitlabUpgrade": lambda: gitlabUpgrade(getdatetime, filewrite),
"gitlabUninstall": lambda: gitlabUninstall(getdatetime, filewrite),
"gitlabBackupCreate": lambda: gitlabBackupCreate(getdatetime, filewrite),
"gitlabBackupRestore": lambda: gitlabBackupRestore(getdatetime, filewrite, gitlabTarName),
"gitlabServiceMonitor": gitlabServiceMonitor,
"gitlabBackupRestoreReconfiguration": lambda: gitlabBackupRestoreReconfiguration(getdatetime, filewrite,
gitlabTarName),
}
# 执行对应的操作,如果操作不存在则打印错误信息
operation = operations.get(gitlabOperateType)
if operation:
operation()
else:
print(f"Unknown operation: {gitlabOperateType}")
if __name__ == '__main__':
# 脚本及日志存放路径
logdir = "/data/processlog/"
# 日志文件
gitlablog = "gitlablog.log"
gitlabfile = logdir + gitlablog
# 时间获取
getdatetime = timestamp_time()
filewrite = open(gitlabfile, "a+", encoding="UTF8")
# 主函数入口
main(getdatetime, filewrite)
filewrite.close()
08-11
2372
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-19
3137
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-01
1587
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-28
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交