From 7c3e072a900508983fe5d47f267cef1390f334b2 Mon Sep 17 00:00:00 2001 From: ObjectNotFound Date: Tue, 10 Dec 2024 16:46:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E9=83=A8=E7=BD=B2=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/chart/agents/values.yaml | 10 +- deploy/chart/authhub/values.yaml | 10 +- .../templates/pgsql/pgsql-deployment.yaml | 6 - .../templates/pgsql/pgsql-secret.yaml | 11 - deploy/chart/databases/values.yaml | 6 +- deploy/chart/euler_copilot/values.yaml | 52 +-- deploy/chart/witchaind/values.yaml | 26 +- deploy/chart_ssl/traefik-tlsstore.yaml | 4 +- deploy/scripts/check_env.sh | 24 +- deploy/scripts/download_file.sh | 121 ++++--- deploy/scripts/install_tools.sh | 327 ++++++++++-------- 11 files changed, 314 insertions(+), 283 deletions(-) delete mode 100644 deploy/chart/databases/templates/pgsql/pgsql-secret.yaml diff --git a/deploy/chart/agents/values.yaml b/deploy/chart/agents/values.yaml index 3d52d101..770c9f96 100644 --- a/deploy/chart/agents/values.yaml +++ b/deploy/chart/agents/values.yaml @@ -76,15 +76,15 @@ agents: # 大模型设置 llm: # 【必填】模型地址(需要包含v1后缀) - url: "http:/$IP:8001/v1" + url: # 【必填】模型名称 - name: "Qwen2.5-32B" + name: "" # 【必填】模型API Key - key: "sk-12345" + key: "" # 【必填】模型最大Token数 - max_tokens: 8192 + max_tokens: 8096 # 【必填】Embedding地址 - embedding: "http://vectorize-agent-service-service.euler-copilot.svc.cluster.local:8001/embedding" + embedding: "" # 待优化机器信息 machine: # 【必填】IP地址 diff --git a/deploy/chart/authhub/values.yaml b/deploy/chart/authhub/values.yaml index 5044de75..33016c8e 100644 --- a/deploy/chart/authhub/values.yaml +++ b/deploy/chart/authhub/values.yaml @@ -6,19 +6,19 @@ globals: imagePullPolicy: IfNotPresent # [必填] AuthHub部署域名 # 需要修改为AuthHub域名。单机部署时,服务基于Host进行区分,无法使用IP地址 - domain: "" + domain: # 数据库设置 databases: # [必填] database helm安装时的app name - app_name: "databases" + app_name: # [必填] database helm安装时的namespace - app_namespace: "euler-copilot" + app_namespace: # 数据库密码 passwords: # [必填] MySQL数据库的密码 - mysql: "8URM%HtCHQPxKe$u" + mysql: # [必填] Redis数据库的密码 - redis: "8FDk2rnhxVPvkSdb" + redis: # 部署AuthHub本地鉴权服务 authhub: diff --git a/deploy/chart/databases/templates/pgsql/pgsql-deployment.yaml b/deploy/chart/databases/templates/pgsql/pgsql-deployment.yaml index df8cee79..15fbff6f 100644 --- a/deploy/chart/databases/templates/pgsql/pgsql-deployment.yaml +++ b/deploy/chart/databases/templates/pgsql/pgsql-deployment.yaml @@ -44,16 +44,10 @@ spec: volumeMounts: - mountPath: /var/lib/postgresql/data name: pgsql-data - - mountPath: /docker-entrypoint-initdb.d/init.sql - name: pgsql-init - subPath: init.sql resources: {{- toYaml .Values.databases.pgsql.resources | nindent 12 }} volumes: - name: pgsql-data persistentVolumeClaim: claimName: pgsql-pvc-{{ .Release.Name }} - - name: pgsql-init - secret: - secretName: pgsql-secret-{{ .Release.Name }} {{- end }} diff --git a/deploy/chart/databases/templates/pgsql/pgsql-secret.yaml b/deploy/chart/databases/templates/pgsql/pgsql-secret.yaml deleted file mode 100644 index a3b3c45c..00000000 --- a/deploy/chart/databases/templates/pgsql/pgsql-secret.yaml +++ /dev/null @@ -1,11 +0,0 @@ -{{- if .Values.databases.pgsql.enabled }} -apiVersion: v1 -kind: Secret -metadata: - name: pgsql-secret-{{ .Release.Name }} - namespace: {{ .Release.Namespace }} -type: Opaque -stringData: - init.sql: | -{{ tpl (.Files.Get "configs/pgsql/init.sql") . | indent 4 }} -{{- end }} diff --git a/deploy/chart/databases/values.yaml b/deploy/chart/databases/values.yaml index 5a32f5f0..cb09f48b 100644 --- a/deploy/chart/databases/values.yaml +++ b/deploy/chart/databases/values.yaml @@ -28,7 +28,7 @@ databases: # [必填] PersistentVolume大小设置 persistentVolumeSize: 10Gi # [必填] 密码设置 - password: "8URM%HtCHQPxKe$u" + password: "" # Service设置 service: # [必填] Service类型,ClusterIP或NodePort @@ -53,7 +53,7 @@ databases: # [必填] 容器根目录只读 readOnly: false # 密码设置 - password: "8FDk2rnhxVPvkSdb" + password: "" # Service设置 service: # [必填] Service类型,ClusterIP或NodePort @@ -86,4 +86,4 @@ databases: # 当类型为nodePort时,填写主机的端口号 nodePort: # [必填] 密码设置 - password: "123456" + password: "" diff --git a/deploy/chart/euler_copilot/values.yaml b/deploy/chart/euler_copilot/values.yaml index c2d0afd5..f11d3a73 100644 --- a/deploy/chart/euler_copilot/values.yaml +++ b/deploy/chart/euler_copilot/values.yaml @@ -4,8 +4,8 @@ globals: imageRegistry: "hub.oepkgs.net/neocopilot" # [必填] 镜像拉取策略 imagePullPolicy: IfNotPresent - # [必填] 部署域名:需要修改为EulerCopilot域名。单节点部署时,服务基于Host进行区分,无法使用IP地 - domain: "" + # [必填] 部署域名 + domain: # 需要修改为EulerCopilot域名。单节点部署时,服务基于Host进行区分,无法使用IP地址 # 用于问答和推理的大模型 llm: # [必填] 模型类型 @@ -17,7 +17,7 @@ globals: # [必填] 模型 API Key key: "" # [必填] 模型名称 - model: "Qwen1.5-32B-chat-GPTQ-Int4" + model: "" # [必填] 模型最大token数 max_tokens: 8096 # 用于Function Call的模型 @@ -31,17 +31,17 @@ globals: # 数据库设置 databases: # [必填] database helm安装时的app name - app_name: "databases" + app_name: # [必填] database helm安装时的namespace - app_namespace: "euler-copilot" + app_namespace: # 数据库密码 passwords: # [必填] MySQL数据库的密码 - mysql: "8URM%HtCHQPxKe$u" + mysql: # [必填] Redis数据库的密码 - redis: "8FDk2rnhxVPvkSdb" + redis: # [必填] PostgreSQL数据库密码 - postgres: "123456" + postgres: euler_copilot: # 配置文件安全复制工具 @@ -76,7 +76,7 @@ euler_copilot: # Volume设置 volume: # [必填] Embedding模型的位置 - models: "/home/EulerCopilot/models" + models: /home/EulerCopilot/models # Service设置 service: # [必填] Service类型,ClusterIP或NodePort @@ -116,7 +116,7 @@ euler_copilot: nodePortDagster: # [必填] RAG知识库名 knowledgebaseID: default_test - # [必填] 待向量化的文档位置,需要手动创建目录 + # [必填] 待向量化的文档位置 docs_dir: "/home/EulerCopilot/docs" # 向量化服务设置 vectorize: @@ -149,20 +149,20 @@ euler_copilot: nodePort: # Volume设置 volume: - # [必填] 插件包路径,需要手动创建目录 - plugin_dir: "/home/EulerCopilot/plugin" + # [必填] 插件包路径 + plugin_dir: # 修改为实际的插件文件夹地址 # [必填] JWT Key - jwtKey: 13e46d8963c997814f996c3294ccc92d + jwtKey: "" # 加密密钥设置 half_keys: # [必填] 加密密钥1 - key1: aB3cDfGhI5JkLmN7O + key1: "" # [必填] 加密密钥2 - key2: pQ6rStUvWxYzA1B9C + key2: "" # [必填] 加密密钥3 - key3: D4eFgHjKlMnOpQrS3 - # [必填] Web前端地址,需要添加https前缀 - web_url: "" + key3: "" + # [必填] Web前端地址,需要添加http/https前缀 + web_url: # 登录设置 login: # [必填] 是否启用登录 @@ -172,19 +172,19 @@ euler_copilot: # OIDC 设置 oidc: # [enabled为true时必填] OIDC 客户端ID - client_id: "" + client_id: # [enabled为true时必填] OIDC 客户端密钥 - client_secret: "" + client_secret: # [enabled为true时必填] OIDC Token获取地址 - token_url: "http://authhub-backend-service-authhub.euler-copilot.svc.cluster.local:11120/oauth2/token" + token_url: http://authhub-backend-service-authhub.euler-copilot.svc.cluster.local:11120/oauth2/token # [enabled为true时必填] OIDC 用户信息地址 - user_url: "http://authhub-backend-service-authhub.euler-copilot.svc.cluster.local:11120/oauth2/introspect" + user_url: http://authhub-backend-service-authhub.euler-copilot.svc.cluster.local:11120/oauth2/introspect # [enabled为true时必填] OIDC 刷新Token地址 - refresh_token_url: "http://authhub-backend-service-authhub.euler-copilot.svc.cluster.local:11120/oauth2/refresh-token" - # [enabled为true时必填] EulerCopilot主页地址:请将替换为 EulerCopilot 实际的域名 - euler_copilot_front: "https:///api/auth/login" + refresh_token_url: http://authhub-backend-service-authhub.euler-copilot.svc.cluster.local:11120/oauth2/refresh-token + # [enabled为true时必填] EulerCopilot主页地址; 替换为 EulerCopilot 实际的域名 + euler_copilot_front: https:///api/auth/login # [enabled为true时必填] OIDC登录跳转地址:请将修改为authhub实际域名, 替换为实际的Client ID,将替换为EulerCopilot域名 - redirect: "https:///oauth2/authorize?client_id=&redirect_uri=https:///api/auth/login&scope=openid offline_access&access_type=offline&response_type=code&prompt=consent&state=235345&nonce=loser" + redirect: https:///oauth2/authorize?client_id=&redirect_uri=https:///api/auth/login&scope=openid offline_access&access_type=offline&response_type=code&prompt=consent&state=235345&nonce=loser web: # [必填] 是否部署Web前端用户界面 enabled: true diff --git a/deploy/chart/witchaind/values.yaml b/deploy/chart/witchaind/values.yaml index 91f87f48..2ec97d12 100644 --- a/deploy/chart/witchaind/values.yaml +++ b/deploy/chart/witchaind/values.yaml @@ -11,19 +11,19 @@ globals: # [必填] Postgresql设置 pgsql: # [必填] 主机 - host: "pgsql-db-databases.euler-copilot.svc.cluster.local" + host: "pgsql-service.euler-copilot.svc.cluster.local" # [必填] 端口 port: 5432 # [必填] 用户 user: "postgres" # [必填] 密码 - password: "123456" + password: "" # [必填] LLM设置 llm: # [必填] 模型名称 - model: "Qwen1.5-32B-chat-GPTQ-Int4" - url: "http://60.204.250.91:8009" - key: "sk-EulerCopilot1bT1WtG2ssG92pvOPTkpT3BlbkFJVruTv8oUe" + model: "" + url: "" + key: "" max_tokens: 8192 witchaind: @@ -47,7 +47,7 @@ witchaind: # [必填] PersistentVolume大小设置 persistentVolumeSize: 20Gi # [必填] 密码设置 - password: "2whty8pm7cdX8Fd" + password: "" # Service设置 service: # [必填] Service类型,ClusterIP或NodePort @@ -72,7 +72,7 @@ witchaind: # [必填] 容器根目录只读 readOnly: false # 密码设置 - password: "QJzoHi1GiobKzyd" + password: "" # Service设置 service: # [必填] Service类型,ClusterIP或NodePort @@ -90,7 +90,7 @@ witchaind: # [必填] 镜像名 name: "data_chain_web" # [必填] 镜像标签 - tag: "0.9.2" + tag: "1230" # 拉取策略。留空则使用全局设置。 imagePullPolicy: "" # 性能限制设置 @@ -134,14 +134,14 @@ witchaind: # 当类型为nodePort时,填写主机的端口号 nodePort: # [必填] Embedding模型URL - embedding: "http://vectorize-agent-service-service.euler-copilot.svc.cluster.local:8001/embedding" + embedding: "" # [必填] 密钥设置 security: # [必填] CSRF密钥 - csrf_key: 629e8bf67264dd350aadc5948ba6cb0c + csrf_key: "" # [必填] 工作密钥1 - half_key_1: 328d63b3ad399c1947bc5863fd28bf75 + half_key_1: "" # [必填] 工作密钥2 - half_key_2: 567ac95f82abb29ec00c2bedae49e192 + half_key_2: "" # [必填] 工作密钥3 - half_key_3: 3ac88e15e3dbfe501d1c62139e32e26a + half_key_3: "" diff --git a/deploy/chart_ssl/traefik-tlsstore.yaml b/deploy/chart_ssl/traefik-tlsstore.yaml index b27ca372..7e948f8e 100644 --- a/deploy/chart_ssl/traefik-tlsstore.yaml +++ b/deploy/chart_ssl/traefik-tlsstore.yaml @@ -1,9 +1,9 @@ apiVersion: traefik.containo.us/v1alpha1 kind: TLSStore metadata: - name: traefik-tlsstore + name: default namespace: default spec: defaultCertificate: - secretName: traefik-ssl-secret \ No newline at end of file + secretName: traefik-ssl-secret diff --git a/deploy/scripts/check_env.sh b/deploy/scripts/check_env.sh index 02fc422c..94c5a524 100644 --- a/deploy/scripts/check_env.sh +++ b/deploy/scripts/check_env.sh @@ -1,6 +1,4 @@ #!/bin/bash - - function check_user { if [[ $(id -u) -ne 0 ]]; then echo -e "\033[31m[Error]请以root权限运行该脚本!\033[0m"; @@ -67,14 +65,6 @@ 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; @@ -190,20 +180,16 @@ function check_disk { function check_network { echo -e "[Info]正在检查当前机器网络情况"; - # 检查curl是否已安装 - if ! command -v curl &> /dev/null; then + if [[ -x $(command -v curl) ]]; then echo -e "\033[31m[Error]Curl不存在,将进行安装\033[0m"; - # 尝试使用yum安装curl - if yum install -y curl; then - echo -e "\033[32m[Success]Curl安装成功\033[0m"; - else + yum install -y curl; + if [[ $? -ne 0 ]]; then echo -e "\033[31m[Error]Curl安装失败\033[0m"; return 1; fi - else - echo -e "\033[32m[Success]Curl已存在\033[0m"; fi - curl https://hub.oepkgs.net/neocopilot --connect-timeout 5 -s + + curl https://swr.cn-southwest-2.myhuaweicloud.com --connect-timeout 5 -s > /dev/null; if [[ $? -ne 0 ]]; then echo -e "\033[31m[Error]当前机器网络无法连接至镜像仓库,请检查网络配置,或使用离线部署方案\033[0m"; return 1; diff --git a/deploy/scripts/download_file.sh b/deploy/scripts/download_file.sh index 8bd11575..265972a6 100644 --- a/deploy/scripts/download_file.sh +++ b/deploy/scripts/download_file.sh @@ -1,49 +1,74 @@ #!/bin/bash - -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[@]}" +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"; + 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 + # 解压embedding + tar -xzf $EMBEDDING; + if [[ $? -ne 0 ]]; then + echo -e "[Error]解压模型权重失败:$EMBEDDING \033[0m"; + return 1; + fi + rm -f $EMBEDDING; + echo -e "\033[32m[Success]Reranker与Embedding模型配置成功\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 \ No newline at end of file diff --git a/deploy/scripts/install_tools.sh b/deploy/scripts/install_tools.sh index 58e4d4b6..3a11b3e8 100644 --- a/deploy/scripts/install_tools.sh +++ b/deploy/scripts/install_tools.sh @@ -1,173 +1,210 @@ -#/bin/bash - +#!/bin/bash GITHUB_MIRROR="https://gh-proxy.com"; ARCH=$(uname -m); -# 函数:显示帮助信息 + function help { - 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"; + echo -e "用法:./install_tools.sh [K3s版本] [Helm版本] [cn: 是否使用镜像站]"; + echo -e "示例:./install_tools.sh \"v1.30.2+k3s1\" \"v3.15.3\""; } function check_user { if [[ $(id -u) -ne 0 ]]; then - echo -e "\033[31m[Error]请以root权限运行该脚本!\033[0m" - exit 1; + echo -e "\033[31m[Error]请以root权限运行该脚本!\033[0m"; + return 1; fi + + return 0; } function check_arch { - case $ARCH in - x86_64) - ARCH=amd64 - ;; - aarch64) - ARCH=arm64 - ;; - *) - echo -e "\033[31m[Error]当前CPU架构不受支持\033[0m" - return 1; - ;; - esac - return 0 + 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; } -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_existing { + if [[ -x $(command -v k3s) ]] && [[ -x $(command -v helm) ]]; then + echo -e "[Info]K3s与Helm已经安装,无需再次安装"; + 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}" - - 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 - - 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 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_helm { - local helm_version=$1 - local use_mirror=$2 - local file_name="helm-$helm_version-linux-$ARCH.tar.gz" - local url="" - - 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 +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]下载Helm" - curl -L $url -o $file_name - if [[ $? -ne 0 ]]; then - echo -e "\033[31m[Error]Helm下载失败\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; +} - 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 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; } function main { - if [[ $# -lt 1 || $# -gt 3 ]]; then - help - exit 1; - fi - + echo -e "[Info]安装K3s与Helm"; + check_user - check_arch if [[ $? -ne 0 ]]; then - 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 + 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; } -main "$@" +if [[ $# -lt 2 ]]; then + help +else + main $1 $2 $3; +fi -- Gitee