提交 8550b1e9 authored 作者: yoyoyo's avatar yoyoyo

1

上级 bcf1f133
#!/bin/bash
install_dir="/proxy_server_install"
production_run_dir="$install_dir/install_projects"
production_run_repo="git.wkwork.xyz/root/install_projects.git"
production_install_dir="$production_run_dir/proxy_server"
if [ ! -d "$install_dir" ]; then
mkdir -p "$install_dir"
fi
cd $install_dir
BRANCH_FILE_SUFFIX=".branch"
CREDENTIALS_FILE="$HOME/.git_credentials"
# 输出函数
echo_content() {
ECHO_TYPE="echo -e"
case $1 in
"red")
${ECHO_TYPE} "\033[31m$2\033[0m"
;;
"green")
${ECHO_TYPE} "\033[32m$2\033[0m"
;;
"yellow")
${ECHO_TYPE} "\033[33m$2\033[0m"
;;
"blue")
${ECHO_TYPE} "\033[34m$2\033[0m"
;;
"purple")
${ECHO_TYPE} "\033[35m$2\033[0m"
;;
"skyBlue")
${ECHO_TYPE} "\033[36m$2\033[0m"
;;
"white")
${ECHO_TYPE} "\033[37m$2\033[0m"
;;
esac
}
# 函数:检查并安装 Git
check_git() {
echo_content "green" "检查并安装 Git..."
if ! command -v git &> /dev/null; then
echo "Git 未安装,正在安装 git..."
if [ -f /etc/debian_version ]; then
sudo apt-get update
sudo apt-get install -y git
elif [ -f /etc/redhat-release ]; then
sudo yum install -y git
else
echo "不支持的系统"
exit 1
fi
fi
}
check_unzip(){
if ! command -v unzip &> /dev/null; then
if command -v apt &> /dev/null; then
sudo apt update
sudo apt install -y unzip
elif command -v yum &> /dev/null; then
sudo yum install -y unzip
fi
fi
}
# 函数:检查并安装 Docker
install_docker() {
echo_content "green" "检查并安装 Docker..."
if ! [[ $(docker -v 2>/dev/null) ]]; then
sh <(curl -sL https://get.docker.com)
fi
if ! [[ $(docker -v 2>/dev/null) ]]; then
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
fi
if ! [[ $(docker -v 2>/dev/null) ]]; then
curl -sSL https://get.daocloud.io/docker | sh
fi
#!/bin/bash
if ! command -v docker-compose &> /dev/null; then
# 下载 docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 设置执行权限
sudo chmod +x /usr/local/bin/docker-compose
# 检查是否安装成功
if ! command -v docker-compose &> /dev/null; then
echo "docker-compose 安装失败。"
exit 1
fi
fi
}
# 函数:解析项目信息
get_project_info() {
local project_info="$1"
local index="$2"
IFS=' ' read -r -a project_array <<< "$project_info"
echo "${project_array[$index]}"
}
# 函数:获取所有项目名称
get_project_names(){
local names=()
for project in "${projects[@]}"; do
names+=("$(get_project_info "$project" 0)")
done
echo "${names[@]}"
}
set_branches(){
repo_dir=$1
repo_name=$2
cd "$repo_dir" || exit 1
# 获取当前分支
current_branch=$(git branch --show-current)
if [ -z "$current_branch" ]; then
echo_content "red" "未能获取当前分支,可能项目没有被正确初始化为git仓库。"
exit 1
fi
# 获取所有远程分支
branches=$(git branch -r | sed 's/origin\///' | grep -v 'HEAD')
#打印当前分支名称
branch_count=$(echo "$branches" | wc -l)
if [ "$branch_count" -gt 1 ]; then
echo_content "yellow" "$repo_name 项目:分支选择 (如继续使用 '$current_branch' 直接回车):"
# 显示分支列表
i=1
for branch in $branches; do
echo "$i) $branch"
i=$((i + 1))
done
# 提示用户选择
read -p "请输入分支对应的数字 (回车保持当前分支: $current_branch): " input
# 如果用户直接按回车,保持当前分支
if [ -z "$input" ]; then
echo_content "yellow" "继续使用当前分支: $current_branch"
branch="$current_branch"
else
# 验证输入是否为有效的数字
if [[ "$input" =~ ^[0-9]+$ ]] && (( input >= 1 && input <= branch_count )); then
selected_branch=$(echo "$branches" | sed -n "${input}p" | xargs)
echo_content "green" "你选择了分支: $selected_branch"
# 切换到选定分支
if git show-ref --verify --quiet "refs/heads/$selected_branch"; then
git checkout "$selected_branch" # 切换到本地分支
else
git checkout -b "$selected_branch" "origin/$selected_branch" # 从远程分支创建并切换
fi
else
echo_content "red" "无效的输入,请重新运行脚本。"
exit 1
fi
fi
fi
}
updata_project(){
repo_dir=$1
repo_name=$2
set_branches $repo_dir $repo_name
cd "$repo_dir" || exit 1
# 检查工作区是否有未提交的更改
if [[ -n $(git status --porcelain) ]]; then
git add .
git commit -m "Auto-commit: 保存当前变动"
fi
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# git pull origin "$CURRENT_BRANCH"
git pull --rebase -X theirs origin "$CURRENT_BRANCH"
if [ $? -ne 0 ]; then
echo_content "red" "更新代码失败,请手动检查冲突。"
else
echo_content "green" "代码更新完成。"
fi
}
# 函数:拉取项目代码
pull_project() {
repo_url=$1
repo_dir=$2
repo_name=$3
# 检查凭据是否存在
if [ -f "$CREDENTIALS_FILE" ]; then
source "$CREDENTIALS_FILE"
else
echo "请输入 Git 用户名:"
read -r -p "用户名: " git_username
echo "请输入 Git 密码:"
read -s -p "密码: " git_password
echo # 换行
# 询问是否保存凭据
echo "是否保存凭据到 $CREDENTIALS_FILE 以便下次使用?"
read -p "请选择(y/n): " save_credentials
echo # 换行
if [ "$save_credentials" = "y" ]; then
echo "git_username=\"$git_username\"" > "$CREDENTIALS_FILE"
echo "git_password=\"$git_password\"" >> "$CREDENTIALS_FILE"
chmod 600 "$CREDENTIALS_FILE" # 设置文件权限,仅用户可读写
fi
fi
# 克隆私有仓库
repo="https://$git_username:$git_password@$repo_url"
echo_content "green" "克隆仓库 $repo ..."
git clone $repo "$repo_dir"
if [ $? -ne 0 ]; then
rm $CREDENTIALS_FILE
echo_content "red" "克隆仓库失败,请检查仓库地址和凭据"
pull_project $repo_url $repo_dir $repo_name
fi
set_branches $repo_dir $repo_name
}
# 函数:卸载项目
uninstall_project() {
if [ -d "$install_dir" ]; then
echo_content "yellow" "你确定要卸载项目吗?此操作将删除项目目录 '$install_dir' (y/n):"
read -r confirm_uninstall
if [ "$confirm_uninstall" = "y" ]; then
echo_content "green" "正在卸载项目..."
cd $production_install_dir
docker-compose down
docker network rm production_mall_network
rm -rf "$install_dir" # 删除项目目录
if [ $? -eq 0 ]; then
echo_content "green" "项目已成功卸载。"
else
echo_content "red" "卸载失败。"
fi
else
echo_content "yellow" "卸载操作已取消。"
fi
else
echo_content "red" "项目目录不存在,无法卸载。"
fi
}
# 函数:启动或重启项目
start_project() {
cd "$production_install_dir" || exit 1
echo_content "green" "是否要启动/重启 项目?(y/n):"
read -r restart
if [ "$restart" = "y" ]; then
if docker-compose ps | grep -q "Up"; then
docker-compose restart
echo "docker-compose restart ..."
sleep 10
else
docker-compose up -d
echo "docker-compose up -d ..."
sleep 10
fi
fi
}
# 函数:选择要执行的操作
select_operation() {
echo_content "yellow" "请选择要执行的操作:"
PS3="请输入操作对应的数字:"
select operation in "启动/重启【项目】" "卸载" "退出"; do
case $REPLY in
1)
echo_content "green" "你选择了操作: (启动/重启)"
start_project
break
;;
2)
echo_content "green" "你选择了操作: 卸载"
uninstall_project
exit
;;
3)
echo_content "green" "你选择了操作: 退出"
exit
;;
*)
echo_content "red" "无效的选择"
continue
;;
esac
done
}
# 函数: 初始化脚本 检查环境
init_environment_check(){
if [ ! -d "$production_run_dir/.git" ]; then
# 运行相关逻辑,找到匹配项后可立即处理
echo_content "green" "拉取项目: production_run"
echo_content "green" "项目仓库: $production_run_repo"
echo_content "green" "项目目录: $production_run_dir"
pull_project "$production_run_repo" "$production_run_dir" "production_run"
else
echo_content "green" "更新项目: production_run"
echo_content "green" "项目仓库: $production_run_repo"
echo_content "green" "项目目录: $production_run_dir"
updata_project "$production_run_dir" "production_run"
fi
}
main(){
if [ "$(id -u)" -ne 0 ]; then
echo_content "red" "请以 root 权限运行此脚本。"
exit 1
fi
echo_content "green" "安装目录:$install_dir"
echo_content "green" "初始化脚本 检查环境..."
check_git
check_unzip
install_docker
init_environment_check
select_operation
}
main
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论