[Chore] 添加DevStar安装使用脚本

* add install.sh
This commit is contained in:
孟宁
2024-12-19 09:54:21 +00:00
repo.diff.committed_by 戴明辰
repo.diff.parent a57b4af354
repo.diff.commit cd3bf834ac

185
public/assets/install.sh Executable file
repo.diff.view_file

@@ -0,0 +1,185 @@
#!/bin/bash
# Copyright 2024 Mengning Software All rights reserved.
# 默认值
NAME=DevStar-Studio
IMAGE_NAME=devstar.cn/devstar/devstar-studio
VERSION=latest # DevStar Studio的默认版本为最新版本
PORT=8080 # 设置端口默认值为 8080
DATA_DIR=~/devstar_data
# 错误处理函数
error_handler() {
devstar help
exit 1
}
# 捕获错误信号
trap 'error_handler' ERR
# Exit immediately if a command exits with a non-zero status
set -e
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to display success message
function success {
echo -e "${GREEN}$1${NC}"
}
# Function to display failure message
function failure {
echo -e "${RED}$1${NC}"
}
# Detect the OS type and install dependencies
function install_dependencies {
# Install dependencies based on the OS type
success "dependencies install begin: "
OS_ID=$(grep '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"')
case $OS_ID in
ubuntu|debian)
# 检查 Docker 是否已安装
if ! command -v docker &> /dev/null
then
echo "Installing Docker..."
# 更新包索引
sudo apt-get update
# 安装 Docker
sudo apt-get install -y docker.io
echo "Docker Installed"
else
echo "Docker Installed, $(docker --version)"
fi
;;
centos)
# sudo yum update -y
# sudo yum install -y epel-release
# sudo yum groupinstall -y "Development Tools"
# sudo yum install -y yaml-cpp yaml-cpp-devel
;;
fedora)
# sudo dnf update -y
# sudo dnf group install -y "Development Tools"
# sudo dnf install -y yaml-cpp yaml-cpp-devel
;;
*)
failure "Unsupported OS: $OS_ID"
exit 1
;;
esac
}
# Function to install
function install {
install_dependencies
sudo docker pull $IMAGE_NAME:$VERSION
}
# Function to start
function start {
install
# 创建devstar_data目录用于持久化存储DevStar相关的配置和用户数据
mkdir -p $DATA_DIR
# 启动devstar-studio容器
stop
sudo docker run --restart=always --name $NAME -d -p $PORT:3000 -v /var/run/docker.sock:/var/run/docker.sock -v ~/devstar_data:/var/lib/gitea -v ~/devstar_data:/etc/gitea $IMAGE_NAME:$VERSION
# 打开 `http://localhost:8080` 完成安装。
success "-------------------------------------------------------"
success "DevStar started in http://localhost:$PORT successfully!"
success "-------------------------------------------------------"
exit 0
}
# Function to stop
function stop {
if [ $(docker ps -a --filter "name=^/${NAME}$" -q | wc -l) -gt 0 ]; then
sudo docker stop $NAME && sudo docker rm -f $NAME
fi
}
# Function to logs
function logs {
# 查看devstar-studio容器的运行日志
sudo docker logs $NAME
}
# Function to clean
function clean {
stop
rm -rf $DATA_DIR
}
# Function to usage help
function usage {
success "------------------------------------------------------------------------"
success "DevStar usage help:"
success " help, -h, --help, Help information"
success " start Start DevStar Studio"
success " --port <arg> Specify the port number (default port is 8080)"
success " stop Stop the running DevStar Studio"
success " logs View the logs of the devstar-studio container"
failure " clean Clean up the running DevStar Studio, including deleting user data. Please use with caution."
success "------------------------------------------------------------------------"
exit 0
}
# Main script
case "$1" in
-h|--help|help)
usage
;;
start)
ARGS=$(getopt --long port:: -- "$@")
if [ $? -ne 0 ]; then
failure "ARGS ERROR!"
exit 1
fi
eval set -- "${ARGS}"
# 处理选项
while true; do
case "$1" in
--port)
PORT="$2"
echo "The Port is: $PORT"
shift 2 ;;
--)
shift
break ;;
*)
failure "Unrecognized option: $1"
exit 1 ;;
esac
done
start
;;
stop)
stop
;;
logs)
logs
;;
clean)
clean
;;
*)
# 获取当前脚本的文件名
script_name=$(basename "$0")
# 判断脚本名是否为 devstar
if [ "$script_name" != "devstar" ]; then
sudo mv ./install.sh /usr/bin/devstar
rm -rf install.sh
success "---------------------------------"
success "DevStar Studio installed successfully!"
success "---------------------------------"
success "Copyright 2024 Mengning Software All rights reserved."
devstar help
fi
;;
esac