From ded2bbb77304825c4b6fe83b691aca7bef08c5f3 Mon Sep 17 00:00:00 2001 From: ylzhangah <1194926515@qq.com> Date: Wed, 25 Sep 2024 17:15:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=8E=AF=E5=A2=83=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E3=80=81=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=E5=92=8C?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E9=83=A8=E7=BD=B2=E5=B7=A5=E5=85=B7=E8=84=9A?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- euler-copilot-helm/chart/values.yaml | 2 +- euler-copilot-helm/scripts/check_env.sh | 24 +- euler-copilot-helm/scripts/download_file.sh | 133 +++----- euler-copilot-helm/scripts/install_tools.sh | 327 +++++++++----------- 4 files changed, 213 insertions(+), 273 deletions(-) diff --git a/euler-copilot-helm/chart/values.yaml b/euler-copilot-helm/chart/values.yaml index d1817d39..3bf72ed7 100644 --- a/euler-copilot-helm/chart/values.yaml +++ b/euler-copilot-helm/chart/values.yaml @@ -196,7 +196,7 @@ euler_copilot: nodePort: # Volume设置 volume: - text2vec: /home/EulerCopilot/text2vec/text2vec-base-chinese-paraphrase # 需要根据实际下载路径修改 + text2vec: /home/EulerCopilot/models/text2vec-base-chinese-paraphrase # 需要根据实际下载路径修改 # JWT Key jwtKey: 6vJZbyFlfJgXFAuNlQaUdOChVLm5aLTC diff --git a/euler-copilot-helm/scripts/check_env.sh b/euler-copilot-helm/scripts/check_env.sh index 94c5a524..02fc422c 100644 --- a/euler-copilot-helm/scripts/check_env.sh +++ b/euler-copilot-helm/scripts/check_env.sh @@ -1,4 +1,6 @@ #!/bin/bash + + function check_user { if [[ $(id -u) -ne 0 ]]; then echo -e "\033[31m[Error]请以root权限运行该脚本!\033[0m"; @@ -65,6 +67,14 @@ function check_os_version { check_version $version "${supported_version[@]}" return $?; fi + + if [[ $id =~ "HopeOS" ]]; then + supported_version=( + "V22" + ) + check_version $version "${supported_version[@]}" + return $?; + fi echo -e "\033[31m[Error]发行版不受支持,脚本将退出\033[0m"; return 1; @@ -180,16 +190,20 @@ function check_disk { function check_network { echo -e "[Info]正在检查当前机器网络情况"; - if [[ -x $(command -v curl) ]]; then + # 检查curl是否已安装 + if ! command -v curl &> /dev/null; then echo -e "\033[31m[Error]Curl不存在,将进行安装\033[0m"; - yum install -y curl; - if [[ $? -ne 0 ]]; then + # 尝试使用yum安装curl + if yum install -y curl; then + echo -e "\033[32m[Success]Curl安装成功\033[0m"; + else echo -e "\033[31m[Error]Curl安装失败\033[0m"; return 1; fi + else + echo -e "\033[32m[Success]Curl已存在\033[0m"; fi - - curl https://swr.cn-southwest-2.myhuaweicloud.com --connect-timeout 5 -s > /dev/null; + curl https://hub.oepkgs.net/neocopilot --connect-timeout 5 -s if [[ $? -ne 0 ]]; then echo -e "\033[31m[Error]当前机器网络无法连接至镜像仓库,请检查网络配置,或使用离线部署方案\033[0m"; return 1; diff --git a/euler-copilot-helm/scripts/download_file.sh b/euler-copilot-helm/scripts/download_file.sh index 071cb7d7..8bd11575 100644 --- a/euler-copilot-helm/scripts/download_file.sh +++ b/euler-copilot-helm/scripts/download_file.sh @@ -1,86 +1,49 @@ #!/bin/bash -function check_pip { - echo -e "[Info]检查pip3"; - if ! [[ -x "$(command -v pip3)" ]]; then - echo -e "\033[31m[Error]未找到pip3,将进行安装\033[0m"; - yum install -y python3 python3-pip; - if [[ $? -ne 0 ]]; then - echo -e "[Error]安装python3和pip失败"; - return 1; - fi - echo -e "\033[32m[Success]python3与pip安装成功\033[0m"; - fi - echo -e "\033[32m[Success]python3与pip已存在\033[0m"; - return 0; -} - -function check_huggingface { - echo -e "[Info]下载与安装最新huggingface_hub库"; - pip3 install -U huggingface_hub -i https://pypi.tuna.tsinghua.edu.cn/simple; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]huggingface_hub安装失败\033[0m"; - return 1; - fi - echo -e "\033[32m[Success]huggingface_hub安装成功\033[0m"; - - curl https://hf-mirror.com --connect-timeout 5 -s > /dev/null; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]HuggingFace镜像站无法连接,无法自动下载模型\033[0m"; - return 1; - fi - return 0; -} - -function download_small_model { - RERANKER="BAAI/bge-reranker-large"; - text2vec="shibing624/text2vec-base-chinese-paraphrase"; - EMBEDDING="bge-mixed-model.tar.gz"; - - export HF_ENDPOINT=https://hf-mirror.com; - - # 下载reranker - huggingface-cli download --resume-download $RERANKER --local-dir $(echo $RERANKER | cut -d "/" -f 2); - if [[ $? -ne 0 ]]; then - echo -e "[Error]下载模型权重失败:$RERANKER \033[0m"; - return 1; - fi - - # 下载bge-mixed-model - wget https://repo.oepkgs.net/openEuler/rpm/openEuler-22.03-LTS/contrib/EulerCopilot/bge-mixed-model.tar.gz; - tar -xzf $EMBEDDING; - if [[ $? -ne 0 ]]; then - echo -e "[Error]下载bge-mixed-model失败 \033[0m"; - return 1; - fi - - # 下载分词工具text2vec-base-chinese-paraphrase - huggingface-cli download --resume-download $text2vec --local-dir $(echo $text2vec | cut -d "/" -f 2); - if [[ $? -ne 0 ]]; then - echo -e "[Error]下载分词工具失败:$text2vec \033[0m"; - return 1; - fi - - rm -f $EMBEDDING; - echo -e "\033[32m[Success]模型配置和分词工具下载成功\033[0m"; - return 0; -} - - -function main { - check_pip - if [[ $? -ne 0 ]]; then - return 1; - fi - - check_huggingface - if [[ $? -ne 0 ]]; then - return 1; - fi - - download_small_model - if [[ $? -ne 0 ]]; then - return 1; - fi -} - -main + +download_and_extract_files() { + local BASE_URL=$1 + local TARGET_DIR="/home/EulerCopilot/models" + shift + local FILES=("$@") + + yum -y install tar wget + + if [ ! -d "${TARGET_DIR}" ]; then + echo "Creating directory ${TARGET_DIR}..." + mkdir -p "${TARGET_DIR}" + fi + + for FILE in "${FILES[@]}"; do + FULL_URL="${BASE_URL}${FILE}" + + if [ ! -f "${FILE}" ]; then + echo "Downloading ${FULL_URL}..." + wget -O "${FILE}" "${FULL_URL}" + if [ $? -ne 0 ]; then + echo "Failed to download ${FILE}." + continue + fi + else + echo "${FILE} already exists, skipping download." + fi + + echo "Extracting ${FILE} to ${TARGET_DIR}..." + if [[ "${FILE}" == *.tar.gz ]]; then + if ! tar -xzvf "${FILE}" -C "${TARGET_DIR}" 2>&1 | grep -q 'Error is not recoverable'; then + echo "${FILE} extracted successfully." + rm "${FILE}" + else + echo "Failed to extract ${FILE}: it may be corrupt or not a tar.gz file." + rm "${FILE}" + fi + else + echo "Unsupported file format: ${FILE}" + continue + fi + done +} + +BASE_URL="https://repo.oepkgs.net/openEuler/rpm/openEuler-22.03-LTS/contrib/EulerCopilot/" +FILES=("bge-mixed-model.tar.gz" "text2vec-base-chinese-paraphrase.tar.gz" "bge-reranker-large.tar.gz") + +download_and_extract_files "${BASE_URL}" "${FILES[@]}" diff --git a/euler-copilot-helm/scripts/install_tools.sh b/euler-copilot-helm/scripts/install_tools.sh index 3a11b3e8..58e4d4b6 100644 --- a/euler-copilot-helm/scripts/install_tools.sh +++ b/euler-copilot-helm/scripts/install_tools.sh @@ -1,210 +1,173 @@ -#!/bin/bash +#/bin/bash + GITHUB_MIRROR="https://gh-proxy.com"; ARCH=$(uname -m); - +# 函数:显示帮助信息 function help { - echo -e "用法:./install_tools.sh [K3s版本] [Helm版本] [cn: 是否使用镜像站]"; - echo -e "示例:./install_tools.sh \"v1.30.2+k3s1\" \"v3.15.3\""; + echo -e "用法:bash install_tools.sh [K3s版本] [Helm版本] [cn: 是否使用镜像站]"; + echo -e "示例:bash install_tools.sh v1.30.2+k3s1 仅安装K3s"; + echo -e " bash install_tools.sh v3.15.3 仅安装Helm"; + echo -e " bash install_tools.sh v1.30.2+k3s1 v3.15.3 安装K3s和Helm"; } function check_user { if [[ $(id -u) -ne 0 ]]; then - echo -e "\033[31m[Error]请以root权限运行该脚本!\033[0m"; - return 1; + echo -e "\033[31m[Error]请以root权限运行该脚本!\033[0m" + exit 1; fi - - return 0; } function check_arch { - if [[ $ARCH != "x86_64" ]] && [[ $ARCH != "aarch64" ]]; then - echo -e "\033[31m[Error]当前CPU架构不受支持\033[0m"; - return 1; - fi - - if [[ $ARCH = "x86_64" ]]; then - ARCH="amd64"; - elif [[ $ARCH = "aarch64" ]]; then - ARCH="arm64"; - fi - - return 0; + case $ARCH in + x86_64) + ARCH=amd64 + ;; + aarch64) + ARCH=arm64 + ;; + *) + echo -e "\033[31m[Error]当前CPU架构不受支持\033[0m" + return 1; + ;; + esac + return 0 } -function check_existing { - if [[ -x $(command -v k3s) ]] && [[ -x $(command -v helm) ]]; then - echo -e "[Info]K3s与Helm已经安装,无需再次安装"; - return 1; - fi - - return 0; +function check_helm { + echo -e "[Info]测试与Helm官方网站之间的网络连通性" + curl https://get.helm.sh --connect-timeout 5 -s > /dev/null + if [[ $? -ne 0 ]]; then + echo -e "\033[31m[Error]无法连接至get.helm.sh\033[0m" + return 1 + fi + return 0 } -function check_github { - if [[ $1 = "cn" ]]; then - echo -e "[Info]测试与GitHub镜像站之间的网络连通性"; - curl $GITHUB_MIRROR --connect-timeout 5 -s > /dev/null; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]无法连接至GitHub镜像站\033[0m"; - else - return 0; - fi - fi - - echo -e "[Info]测试与GitHub之间的网络连通性"; - curl https://github.com --connect-timeout 5 -s > /dev/null; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]无法连接至GitHub\033[0m"; - return 1; - fi - - return 0; -} +function install_k3s { + local image_name="k3s-airgap-images-$ARCH.tar.zst" + if [[ $ARCH = "amd64" ]]; then + local bin_name="k3s" + elif [[ $ARCH = "arm64" ]]; then + local bin_name="k3s-arm64" + fi + local k3s_bin_url="$GITHUB_MIRROR/https://github.com/k3s-io/k3s/releases/download/$k3s_version/${bin_name}" + local k3s_image_url="$GITHUB_MIRROR/https://github.com/k3s-io/k3s/releases/download/$k3s_version/${image_name}" -function check_helm { - echo -e "[Info]测试与Helm官方网站之间的网络连通性"; - curl https://get.helm.sh --connect-timeout 5 -s > /dev/null; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]无法连接至get.helm.sh\033[0m"; - return 1; - fi - return 0; -} + echo -e "[Info]下载K3s二进制文件" + curl -L ${k3s_bin_url} -o /usr/local/bin/k3s + if [[ $? -ne 0 ]]; then + echo -e "\033[31m[Error]K3s二进制文件下载失败\033[0m" + return 1; + fi + chmod +x /usr/local/bin/k3s + + echo -e "[Info]下载K3s依赖" + mkdir -p /var/lib/rancher/k3s/agent/images + curl -L ${k3s_image_url} -o /var/lib/rancher/k3s/agent/images/${image_name} + if [[ $? -ne 0 ]]; then + echo -e "\033[31m[Error]K3s依赖下载失败\033[0m" + return 1; + fi -function download_k3s { - if [[ $ARCH = "amd64" ]]; then - bin_name="k3s"; - elif [[ $ARCH = "arm64" ]]; then - bin_name="k3s-arm64"; - fi - - image_name="k3s-airgap-images-$ARCH.tar.zst"; - - if [[ $1 = "cn" ]]; then - bin_url="$GITHUB_MIRROR/https://github.com/k3s-io/k3s/releases/download/$2/$bin_name"; - image_url="$GITHUB_MIRROR/https://github.com/k3s-io/k3s/releases/download/$2/$image_name"; - else - bin_url="https://github.com/k3s-io/k3s/releases/download/$1/$bin_name"; - image_url="https://github.com/k3s-io/k3s/releases/download/$1/$image_name"; - fi - - echo -e "[Info]下载K3s"; - curl $bin_url -o k3s -L; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]K3s下载失败\033[0m"; - return 1; - fi - - mv k3s /usr/local/bin; - chmod +x /usr/local/bin/k3s; - - echo -e "[Info]下载K3s依赖"; - curl $image_url -o $image_name -L; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]K3s依赖下载失败\033[0m"; - return 1; - fi - - mkdir -p /var/lib/rancher/k3s/agent/images; - mv $image_name /var/lib/rancher/k3s/agent/images/; - - echo -e "\033[32m[Success]K3s及其依赖下载成功\033[0m"; - - mkdir -p /etc/rancher/k3s; - echo -e "[Info]请输入Docker私仓登录信息:"; - read -p "私仓地址:" repo_url; - read -p "用户名:" repo_user; - read -p "密码:" repo_pass; - cat > /etc/rancher/k3s/registries.yaml <<-EOF - mirrors: - "docker.io": - endpoint: - - "https://docker.anyhub.us.kg" - - "https://docker.1panel.live" - - "https://dockerhub.icu" - - "https://docker.ckyl.me" - - "https://docker.awsl9527.cn" - - "https://dhub.kubesre.xyz" - configs: - "$repo_url": - auth: - username: $repo_user - password: $repo_pass - EOF - - bash -c "curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_SKIP_DOWNLOAD=true sh -"; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]K3s安装失败\033[0m"; - return 1; - fi - echo -e "\033[32m[Success]K3s安装成功\033[0m"; - - return 0; + echo -e "\033[32m[Success]K3s及其依赖下载成功\033[0m" + + bash -c "curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_SKIP_DOWNLOAD=true sh -" + if [[ $? -ne 0 ]]; then + echo -e "\033[31m[Error]K3s安装失败\033[0m"; + return 1; + fi + echo -e "\033[32m[Success]K3s安装成功\033[0m" + return 0; } +function install_helm { + local helm_version=$1 + local use_mirror=$2 + local file_name="helm-$helm_version-linux-$ARCH.tar.gz" + local url="" -function download_helm { - file_name="helm-$1-linux-$ARCH.tar.gz"; - url="https://get.helm.sh/$file_name"; - - curl $url -o $file_name -L; - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]Helm下载失败\033[0m"; - return 1; - fi - - tar -zxvf $file_name linux-$ARCH/helm --strip-components 1; - mv helm /usr/local/bin; - chmod +x /usr/local/bin/helm; - - echo -e "\033[32m[Success]Helm安装成功\033[0m"; - return 0; + if [[ $use_mirror == "cn" ]]; then + url="https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts/helm/v$helm_version/$file_name" + else + url="${use_mirror:+$GITHUB_MIRROR/}https://get.helm.sh/$file_name" + fi + + echo -e "[Info]下载Helm" + curl -L $url -o $file_name + if [[ $? -ne 0 ]]; then + echo -e "\033[31m[Error]Helm下载失败\033[0m"; + return 1; + fi + + tar -zxvf $file_name --strip-components 1 -C /usr/local/bin linux-$ARCH/helm + chmod +x /usr/local/bin/helm + + echo -e "\033[32m[Success]Helm安装成功\033[0m" + return 0; } function main { - echo -e "[Info]安装K3s与Helm"; - + if [[ $# -lt 1 || $# -gt 3 ]]; then + help + exit 1; + fi + check_user + check_arch if [[ $? -ne 0 ]]; then - return 1; - fi - - check_existing - if [[ $? -ne 0 ]]; then - return 1; - fi - - check_arch - if [[ $? -ne 0 ]]; then - return 1; - fi - - check_github $3 - if [[ $? -ne 0 ]]; then - return 1; - fi - - check_helm - if [[ $? -ne 0 ]]; then - return 1; - fi - - download_k3s $3 $1 - if [[ $? -ne 0 ]]; then - return 1; - fi - - download_helm $2 - if [[ $? -ne 0 ]]; then - return 1; - fi - - return 0; + exit 1; + fi + + local k3s_version="" + local helm_version="" + local use_mirror="" + + for arg in "$@"; do + if [[ $arg == v*+k3s1 ]]; then + k3s_version=$arg + elif [[ $arg =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + helm_version=$arg + else + echo "未知的参数: $arg" + exit 1 + fi + done + # 检查 K3s 是否已安装 + if [[ -n $k3s_version ]]; then + if command -v k3s &>/dev/null; then + echo -e "[Info]K3s 已经安装,无需再次安装"; + else + install_k3s "$k3s_version" "$use_mirror" + if [[ $? -ne 0 ]]; then + return 1; + fi + fi + fi + + # 检查 Helm 是否已安装 + if [[ -n $helm_version ]]; then + if command -v helm &>/dev/null; then + echo -e "[Info]Helm 已经安装,无需再次安装"; + else + check_helm + if [[ $? -ne 0 ]]; then + return 1; + fi + install_helm "$helm_version" "$use_mirror" + if [[ $? -ne 0 ]]; then + return 1; + fi + fi + fi + + # 如果两个都需要安装,但至少有一个已安装,则不执行任何操作 + if [[ -n $k3s_version && -n $helm_version ]]; then + if ! (command -v k3s &>/dev/null) || ! (command -v helm &>/dev/null); then + echo -e "[Info]K3s 或 Helm 已经安装,无需再次安装"; + return 1; + fi + fi } -if [[ $# -lt 2 ]]; then - help -else - main $1 $2 $3; -fi +main "$@" -- Gitee