diff --git a/.github/workflows/upload_artifact.yml b/.github/workflows/upload_artifact.yml index 3d777435..d74b7d27 100644 --- a/.github/workflows/upload_artifact.yml +++ b/.github/workflows/upload_artifact.yml @@ -33,6 +33,7 @@ jobs: cp -r scripts websoft9 cp -r install websoft9 cp -r docs websoft9 + cp -r systemd websoft9 cp *.md websoft9 cp version.json websoft9 zip -r websoft9-$version.zip websoft9 diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 2592db98..a09fb20c 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -5,7 +5,7 @@ services: image: websoft9dev/apphub:$APPHUB_VERSION container_name: websoft9-apphub ports: - - 127.0.0.1:9001:8080 + - 9001-9999:8080 restart: always volumes: - apphub_logs:/websoft9/apphub/logs diff --git a/install/install_docker.sh b/install/install_docker.sh index 11140e41..341fe3d1 100644 --- a/install/install_docker.sh +++ b/install/install_docker.sh @@ -31,7 +31,7 @@ export PATH docker_packages="docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin" -docker_exist="command -v docker &> /dev/null && systemctl is-active --quiet docker" +echo_prefix_docker=$'\n[Docker] - ' # Function to check if apt is locked is_apt_locked(){ @@ -42,8 +42,27 @@ is_apt_locked(){ fi } +docker_exist() { + # 检查 `docker` 命令是否存在 + if ! command -v docker &> /dev/null; then + echo "false" + return 1 + fi + + # 检查 Docker 服务是否存在 + systemctl status docker &> /dev/null + if [ $? -ne 0 ]; then + echo "false" + return 1 + fi + + echo "true" + return 0 +} + + Install_Docker(){ - echo "Installing Docker for your system..." + echo "$echo_prefix_docker Installing Docker for your system" # For redhat family if [[ -f /etc/redhat-release ]]; then @@ -58,7 +77,7 @@ Install_Docker(){ fi # For Ubuntu, Debian, or Raspbian - if type apt >/dev/null; then + if type apt >/dev/null 2>&1; then apt update # Wait for apt to be unlocked while is_apt_locked; do @@ -72,7 +91,7 @@ Install_Docker(){ Upgrade_Docker(){ if eval "$docker_exist"; then - echo "Upgrading Docker for your system..." + echo "$echo_prefix_docker Upgrading Docker for your system..." dnf --version >/dev/null 2>&1 dnf_status=$? yum --version >/dev/null 2>&1 @@ -95,7 +114,7 @@ fi } Remove_Podman(){ - echo "Try to remove Podman" + echo "$echo_prefix_docker Try to remove Podman" podman pod stop --all # Remove Podman and its dependencies if [ -x "$(command -v dnf)" ]; then @@ -118,7 +137,7 @@ Remove_Podman(){ Set_Docker(){ # should have Docker server and Docker cli if eval $docker_exist; then - echo "Starting to Set docker..." + echo "$echo_prefix_docker Starting to Set docker..." sudo systemctl enable docker sudo systemctl start docker if ! docker network inspect websoft9 > /dev/null 2>&1; then diff --git a/install/install_new.sh b/install/install_new.sh index 3de9a530..a8845bf4 100644 --- a/install/install_new.sh +++ b/install/install_new.sh @@ -67,6 +67,7 @@ export https_port=443 export cockpit_port=$port export force_install=$force export install_path="/data/websoft9/source" +export systemd_path="/opt/websoft9/systemd" export source_zip="websoft9-latest.zip" export source_unzip="websoft9" export source_github_pages="https://websoft9.github.io/websoft9" @@ -79,7 +80,9 @@ echo Install from url: $artifact_url # Define common functions install_tools(){ - echo "Starting install necessary tool..." + echo_prefix_tools=$'\n[Tools] - ' + echo "$echo_prefix_tools Starting install necessary tool..." + dnf --version >/dev/null 2>&1 dnf_status=$? yum --version >/dev/null 2>&1 @@ -106,15 +109,17 @@ install_tools(){ download_source() { - echo "Download Websoft9 source code..." + echo_prefix_source=$'\n[Dowload Source] - ' + echo "$echo_prefix_source Download Websoft9 source code from $artifact_url/$source_zip" + rm -rf websoft9-latest.zip* if [ -d "$install_path" ]; then - echo "Directory $install_path already exists." + echo "Directory $install_path already exists and installation will cover it." else mkdir -p "$install_path" fi - wget "$urls/$source_zip" + wget "$artifact_url/$source_zip" if [ $? -ne 0 ]; then echo "Failed to download source package." exit 1 @@ -151,7 +156,9 @@ check_ports() { } install_backends() { - echo "Install backend docker services" + echo_prefix_backends=$'\n[Backend] - ' + echo "$echo_prefix_backends Install backend docker services" + cd "$install_path/docker" if [ $? -ne 0 ]; then echo "Failed to change directory." @@ -179,8 +186,15 @@ install_backends() { install_systemd() { - echo "Install Systemd service" - cp "$install_path/systemd/websoft9.service" /lib/systemd/system/ + echo_prefix_systemd=$'\n[Systemd] - ' + echo "$echo_prefix_systemdInstall Systemd service" + + if [ ! -d "$systemd_path" ]; then + sudo mkdir -p "$systemd_path" + fi + + sudo cp -r $install_path/systemd/* "$systemd_path" + sudo cp -f "$systemd_path/websoft9.service" /lib/systemd/system/ if [ $? -ne 0 ]; then echo "Failed to copy Systemd service file." exit 1 diff --git a/install/install_plugins.sh b/install/install_plugins.sh index edf5dfb8..227858fa 100644 --- a/install/install_plugins.sh +++ b/install/install_plugins.sh @@ -26,7 +26,7 @@ while [[ $# -gt 0 ]]; do esac done - +# channel,source_github_pages,install_path from install.sh priority if [ -z "$channel" ]; then channel="release" fi @@ -44,55 +44,65 @@ echo "Your installation parameters are as follows: " echo "--channel: $channel" artifact_url="https://w9artifact.blob.core.windows.net/$channel/websoft9/plugin" -echo_prefix_cockpit=$'\n[Plugins] - ' -mydata="" +echo_prefix_plugins=$'\n[Plugins] - ' -version_json(){ +versions_local_file="$install_path/version.json" +versions_url="$source_github_pages/version.json" +file_suffix=".zip" +plugin_path="/usr/share/cockpit" - if [ -f "$install_path/version.json" ]; then - echo "Find version file on your $install_path " - mydata=$(cat "$install_path/version.json") - echo $mydata - else - echo "Get version.json from $source_github_pages/version.json" - mydata=$(curl -s "$source_github_pages/version.json") - if [ $? -ne 0 ]; then - echo "URL does not exist or cannot be accessed." - exit 1 - else - echo "$mydata" - fi - fi -} +echo "$echo_prefix_plugins Starting dowload plugin and update it" +python3 - << END +import requests +import json +import queue +import os +import sys +import zipfile +import io -install_plugins() { - echo "$echo_prefix_cockpit Start to install plugins" - echo $mydata +def get_plugin_versions(versions_local_file, versions_url, artifact_url, file_suffix, plugin_path): + if os.path.exists(versions_local_file): + print("Get version file on your local install"+versions_local_file) + with open(versions_local_file) as f: + data = json.load(f) + else: + try: + print("Get version file from URL " + versions_url) + response = requests.get(versions_url, timeout=5) # Set timeout to 5 seconds + data = json.loads(response.text) + except (requests.exceptions.RequestException, json.JSONDecodeError) as e: + print("Error occurred while getting version file from URL: ", e) + sys.exit(1) # Exit the program if an error occurred - # 解析数据文件,获取 plugins 的子元素和对应的版本号 - plugins=$(echo "$data" | jq -r '.plugins | keys_unsorted[]') - versions=$(echo "$data" | jq -r '.plugins | .[]') + plugins = data.get('plugins', {}) - echo $plugins - echo $versions + q = queue.Queue() + for plugin, version in plugins.items(): + q.put(f'{artifact_url}/{plugin}/{plugin}-{version}{file_suffix}') - # 定义数组变量 - declare -a artifact_array + return q - # 构建数组内容 - readarray -t plugins_array <<<"$plugins" - readarray -t versions_array <<<"$versions" +# 使用函数 +q = get_plugin_versions("${versions_local_file}", "${versions_url}", "${artifact_url}", "${file_suffix}", "${plugin_path}") - for ((i=0; i<${#plugins_array[@]}; i++)); do - artifact_array+=("$artifact_url/${plugins_array[$i]}-${versions_array[$i]}") - done +# 下载并解压缩文件 +while not q.empty(): + try: + file_url = q.get() + print(f"Downloading {file_url}...") + response = requests.get(file_url, stream=True, timeout=120) # Set timeout to 120 seconds - # 打印数组元素 - for element in "${artifact_array[@]}"; do - echo "$element" - done -} + # Make sure the download was successful + response.raise_for_status() -version_json -install_plugins + with zipfile.ZipFile(io.BytesIO(response.content)) as z: + z.extractall("${plugin_path}") + print(f"Successfully extracted {file_url} to ${plugin_path}") + except Exception as e: + print(f"Error occurred while downloading or extracting file: {e}") + sys.exit(1) # Exit the program if an error occurred +END + +echo "Plugins install successfully..." diff --git a/systemd/README.md b/systemd/README.md index efcc8c0d..0345ffdc 100644 --- a/systemd/README.md +++ b/systemd/README.md @@ -2,4 +2,15 @@ This is the Websoft9 system service that run some proxy services on the host machine for Websoft9 to solve the problem that the API cannot handle. -- Copy credentials from one other containers to apphub container \ No newline at end of file +- Copy credentials from one other containers to apphub container + +## Test it + +``` +export install_path="/data/websoft9/source" +chmod +x $install_path/systemd/send_credentials.sh +cp $install_path/systemd/websoft9.service /lib/systemd/system/ +sudo systemctl daemon-reload +sudo systemctl enable websoft9.service +sudo systemctl start websoft9 +``` \ No newline at end of file diff --git a/systemd/send_credentials.sh b/systemd/script/send_credentials.sh similarity index 94% rename from systemd/send_credentials.sh rename to systemd/script/send_credentials.sh index d4c1d7b8..0490fff8 100644 --- a/systemd/send_credentials.sh +++ b/systemd/script/send_credentials.sh @@ -1,4 +1,7 @@ #!/bin/bash +# Define PATH +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin +# Export PATH set -e diff --git a/systemd/script/set_hosts.sh b/systemd/script/set_hosts.sh new file mode 100644 index 00000000..4be0a661 --- /dev/null +++ b/systemd/script/set_hosts.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Define PATH +PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin +# Export PATH + +set -e + +# 容器名称 +container_name="websoft9-apphub" + +# 最大尝试次数 +max_attempts=60 + +# 获取容器的内网IP +attempt=0 +while [ $attempt -lt $max_attempts ]; do + container_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $container_name) + + if [ -n "$container_ip" ]; then + break + fi + + attempt=$(( $attempt + 1 )) + sleep 1 +done + +# 检查获取IP是否成功 +if [ -z "$container_ip" ]; then + echo "Failed to get IP for container $container_name after $max_attempts attempts" + exit 1 +fi + +# 检查 /etc/hosts 文件中是否已经存在一个条目与容器名称相同 +if grep -q $container_name /etc/hosts; then + # 如果存在,使用 sed 命令更新这个条目 + sudo sed -i "/$container_name/d" /etc/hosts + echo "$container_ip $container_name" | sudo tee -a /etc/hosts > /dev/null + echo "Container $container_name IP updated to $container_ip in /etc/hosts" +else + # 如果不存在,添加一个新的条目 + echo "$container_ip $container_name" | sudo tee -a /etc/hosts > /dev/null + echo "Container $container_name IP $container_ip added to /etc/hosts" +fi diff --git a/systemd/script/start_up.sh b/systemd/script/start_up.sh new file mode 100644 index 00000000..f30741a6 --- /dev/null +++ b/systemd/script/start_up.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# 在这个脚本中,我们将运行两个脚本 +/bin/bash send_credentials.sh +/bin/bash set_hosts.sh diff --git a/systemd/test/install.sh b/systemd/test/install.sh deleted file mode 100644 index 11b54ab4..00000000 --- a/systemd/test/install.sh +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -# Define PATH -PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin -# Export PATH -export PATH -export install_path="/data/websoft9" - -chmod +x $install_path/systemd/send_credentials.sh -cp $install_path/systemd/websoft9.service /lib/systemd/system/ -sudo systemctl daemon-reload -sudo systemctl enable websoft9.service -sudo systemctl start websoft9 \ No newline at end of file diff --git a/systemd/websoft9.service b/systemd/websoft9.service index d262c546..a3e25fca 100644 --- a/systemd/websoft9.service +++ b/systemd/websoft9.service @@ -4,8 +4,8 @@ Requires=network.target docker After=network.target docker [Service] -WorkingDirectory=/data/websoft9/systemd -ExecStart=/bin/bash send_credentials.sh +WorkingDirectory=/opt/websoft9/systemd +ExecStart=/bin/bash /opt/websoft9/systemd/startup.sh Restart=always Type=simple NotifyAccess=all