From 7825965557c72e3d4c4dabd5d3e462aa452b2302 Mon Sep 17 00:00:00 2001 From: Darren <27513732@qq.com> Date: Wed, 20 Sep 2023 19:37:12 +0800 Subject: [PATCH] install --- docker/docker-compose.yml | 1 + install/install_cockpit.sh | 26 ++--- install/install_docker.sh | 189 ++++++++++++++++++++++++----------- install/install_new.sh | 200 ++++++++++++++++++++++++++++++++++--- install/install_plugins.sh | 9 +- install/install_tools.sh | 33 ------ 6 files changed, 327 insertions(+), 131 deletions(-) delete mode 100644 install/install_tools.sh diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index e8c73dfd..b07f199b 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -25,6 +25,7 @@ services: volumes: - portainer:/data - /var/run/docker.sock:/var/run/docker.sock + #- /run/podman/podman.sock:/var/run/docker.sock labels: com.docker.compose.w9_http.port: 9000 diff --git a/install/install_cockpit.sh b/install/install_cockpit.sh index cf608313..31c10d8f 100644 --- a/install/install_cockpit.sh +++ b/install/install_cockpit.sh @@ -51,14 +51,16 @@ function get_os_version() { echo $VERSION } + + os_type=$(get_os_type) os_version=$(get_os_version) -CheckEnvironment(){ -echo "---------------------------------- Welcome to install websoft9's appstore, it will take 3-5 minutes -------------------------------------------------------" -echo "Check environment ..." +Check_OS(){ + +echo "Check OS environment for install Cockpit..." echo os_type: $os_type echo os_version: $os_version if [ $(id -u) != "0" ]; then @@ -122,20 +124,11 @@ if [ "$os_type" == 'Debian' ];then echo "This app only supported on Debian 11" exit 1 fi -fi - -# Todo: need replaced it with vars -# Check port used -if netstat -tuln | grep -qE ':(80|443|9000)\s'; then - echo "Port 80,443,9000 is already in use." - exit 1 -else - echo "Port 80,443, 9000 are free." -fi +fi } -InstallCockpit(){ +Install_Cockpit(){ echo "Prepare to install Cockpit ..." if [ "${os_type}" == 'Debian' ]; then @@ -209,8 +202,7 @@ fi echo "Set cockpit port to 9000 ..." sudo sed -i 's/ListenStream=9090/ListenStream=9000/' /lib/systemd/system/cockpit.socket - } -CheckEnvironment -InstallCockpit \ No newline at end of file +Check_OS +Install_Cockpit \ No newline at end of file diff --git a/install/install_docker.sh b/install/install_docker.sh index d94ea08e..d741bb02 100644 --- a/install/install_docker.sh +++ b/install/install_docker.sh @@ -2,70 +2,145 @@ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH +# Install and Upgade Docker for mosts of Linux +# This script is intended from https://get.docker.com and add below: +# +# - remove Podman +# - support Redhat, CentOS-Stream, OracleLinux, AmazonLinux +# +# 1. download the script +# +# $ curl -fsSL https://websoft9.github.io/websoft9/install/install-docker.sh -o install-docker.sh +# +# 2. verify the script's content +# +# $ cat install-docker.sh +# +# 3. run the script with --dry-run to verify the steps it executes +# +# $ sh install-docker.sh --dry-run +# +# 4. run the script either as root, or using sudo to perform the installation. +# +# $ sudo sh install-docker.sh -InstallDocker(){ +############################################################ +# This script is +# $force_value is define at install.sh +############################################################ -if command -v docker &> /dev/null -then - echo "Docker is installed, update..." - if command -v apt > /dev/null;then - sudo apt -y install --only-upgrade docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin - elif command -v dnf > /dev/null;then - sudo dnf update -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin - elif command -v yum > /dev/null;then - sudo yum update -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin +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" + +# Function to check if apt is locked +is_apt_locked(){ + if [[ -f /var/lib/dpkg/lock-frontend || -f /var/lib/apt/lists/lock ]]; then + return 0 # Apt is locked + else + return 1 # Apt is not locked fi - sudo systemctl start docker - sudo systemctl enable docker - if ! docker network inspect websoft9 > /dev/null 2>&1; then - sudo docker network create websoft9 +} + +Install_Docker(){ + echo "Installing Docker for your system..." + + # For redhat family + if [[ -f /etc/redhat-release ]]; then + # For CentOS, Fedora, or RHEL(only s390x) + if [[ $(cat /etc/redhat-release) =~ "RHEL" ]] && [[ $(uname -m) == "s390x" ]] || [[ $(cat /etc/redhat-release) =~ "CentOS" ]] || [[ $(cat /etc/redhat-release) =~ "Fedora" ]]; then + curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh + else + # For other distributions + sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo + sudo yum install $docker_packages -y + fi + fi + + # For Ubuntu, Debian, or Raspbian + if type apt >/dev/null; then + apt update + # Wait for apt to be unlocked + while is_apt_locked; do + echo "Waiting for apt to be unlocked..." + sleep 5 + done + curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh + fi +} + + +Upgrade_Docker(){ +if eval "$docker_exist"; then + echo "Upgrading Docker for your system..." + dnf --version >/dev/null 2>&1 + dnf_status=$? + yum --version >/dev/null 2>&1 + yum_status=$? + apt --version >/dev/null 2>&1 + apt_status=$? + + if [ $dnf_status -eq 0 ]; then + sudo dnf update -y $docker_packages + elif [ $yum_status -eq 0 ]; then + sudo yum update -y $docker_packages + elif [ $apt_status -eq 0 ]; then + sudo apt -y install --only-upgrade $docker_packages + else + echo "Docker installed, but cannot upgrade" fi - return else - echo "Docker is not installed, start to install..." -fi -if [ "$os_type" == 'CentOS' ];then - curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh + Install_Docker fi +} -if [ "$os_type" == 'Ubuntu' ] || [ "$os_type" == 'Debian' ] ;then - apt-get update - while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do - echo "Waiting for other software managers to finish..." - sleep 5 - done - curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh - sleep 30 -fi - -if [ "$os_type" == 'OracleLinux' ] ;then - sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo - sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y -fi - -if [ "$os_type" == 'Fedora' ] ;then - wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/fedora/docker-ce.repo - sudo yum install device-mapper-persistent-data lvm2 docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-scan-plugin docker-ce-rootless-extras -y -fi - -if [ "$os_type" == 'Redhat' ] ;then - sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine podman runc -y 1>/dev/null 2>&1 - sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo - sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y -fi - -if [ "$os_type" == 'CentOS Stream' ] || [ "$os_type" == 'Rocky Linux' ];then - sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine podman runc -y 1>/dev/null 2>&1 - wget -O /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo - sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y -fi - -sudo systemctl start docker -sudo systemctl enable docker -if ! docker network inspect websoft9 > /dev/null 2>&1; then - sudo docker network create websoft9 -fi +Remove_Podman(){ + echo "Try to remove Podman" + podman pod stop --all + # Remove Podman and its dependencies + if [ -x "$(command -v dnf)" ]; then + sudo dnf remove podman -y + elif [ -x "$(command -v apt)" ]; then + sudo apt remove podman -y + elif [ -x "$(command -v zypper)" ]; then + sudo zypper remove podman -y + elif [ -x "$(command -v pacman)" ]; then + sudo pacman -Rs podman --noconfirm + else + echo "Unable to find a suitable package manager to remove Podman." + exit 1 + fi + echo "Podman has been stopped and removed." } -InstallDocker \ No newline at end of file + +Set_Docker(){ +# should have Docker server and Docker cli +if eval $docker_exist; then + echo "Starting to Set docker..." + sudo systemctl enable docker + sudo systemctl start docker + if ! docker network inspect websoft9 > /dev/null 2>&1; then + sudo docker network create websoft9 + fi +else + echo "Docker no installed, exit..." + exit +fi +} + +## This Script starting here .................................... + +if command -v podman &> /dev/null; then + if [ "$force_install" = "y" ]; then + Remove_Podman + else + read -p "Install Websoft9 will remove Podman and Install Docker for continue(y/n): " answer + if [ "$answer" = "y" ]; then + Remove_Podman + fi + fi +fi + +Upgrade_Docker +Set_Docker \ No newline at end of file diff --git a/install/install_new.sh b/install/install_new.sh index 8cd4765d..223ab934 100644 --- a/install/install_new.sh +++ b/install/install_new.sh @@ -5,32 +5,200 @@ PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH +# Command-line options +# ============================================================================== +# +# --force +# Use the --force option to ignore all interactive choices. default is n, for example: +# +# --port <9000> +# Use the --port option to set Websoft9 cosole port. default is 9000, for example: +# +# $ sudo sh install.sh --port 9001 +# ============================================================================== + +#!/bin/bash + +# 设置参数的默认值 +force="n" +port="9000" + +# 获取参数值 +while [[ $# -gt 0 ]]; do + case $1 in + --force) + force="$2" + shift 2 + ;; + --port) + port="$2" + shift 2 + ;; + *) + shift + ;; + esac +done + +# 输出参数值 +echo "Force: $force" +echo "Port: $port" + # Define global vars + export http_port=80 export https_port=443 -export cockpit_port=9000 -export install_path="/data/websoft9" +export cockpit_port=$port +export force_install=$force +export install_path="/data/websoft9/source" +export source_zip="websoft9-latest.zip" +export source_unzip="websoft9" +export tools_yum="git curl wget yum-utils jq bc unzip" +export tools_apt="git curl wget jq bc unzip" +export docker_network="websoft9" export urls="https://w9artifact.blob.core.windows.net/release/websoft9" + if [[ "$1" == "dev" ]]; then echo "update by dev artifact" export urls="https://w9artifact.blob.core.windows.net/dev/websoft9" fi -# Install runtime -bash install_tools.sh -bash install_docker.sh -# Install Cockpit and plugins +# Define common functions + +install_tools(){ + echo "Starting install necessary tool..." + dnf --version >/dev/null 2>&1 + dnf_status=$? + yum --version >/dev/null 2>&1 + yum_status=$? + apt --version >/dev/null 2>&1 + apt_status=$? + + if [ $dnf_status -eq 0 ]; then + dnf install $tools_yum -y + elif [ $yum_status -eq 0 ]; then + yum $tools_yum -y + elif [ $apt_status -eq 0 ]; then + while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do + echo "Waiting for other software managers to finish..." + sleep 5 + done + sudo apt update -y 1>/dev/null 2>&1 + apt $tools_apt -y --assume-yes + else + echo "None of the required package managers are installed." + fi +} + + + +download_source() { + echo "Download Websoft9 source code..." + if [ -d "$install_path" ]; then + echo "Directory $install_path already exists." + else + mkdir -p "$install_path" + fi + + wget "$urls/$source_package" + if [ $? -ne 0 ]; then + echo "Failed to download source package." + exit 1 + fi + + unzip -o "$source_zip" -d "$install_path" + if [ $? -ne 0 ]; then + echo "Failed to unzip source package." + exit 1 + fi + + mv -fn "$install_path/$source_unzip/*" "$install_path" + rm -rf "$source_package" "$install_path/$source_unzip" + +} + + +check_ports() { + local ports=("$@") + + for port in "${ports[@]}"; do + if netstat -tuln | grep ":$port " >/dev/null; then + echo "Port $port is in use, install failed" + exit + fi + done + + echo "All ports are available" +} + +install_compose() { + echo "Install backend docker services" + cd "$install_path/docker" + if [ $? -ne 0 ]; then + echo "Failed to change directory." + exit 1 + fi + + sudo docker network inspect $docker_network >/dev/null 2>&1 + if [ $? -eq 0 ]; then + echo "Docker network '$docker_network' already exists." + else + sudo docker network create $docker_network + if [ $? -ne 0 ]; then + echo "Failed to create docker network." + exit 1 + fi + fi + + sudo docker-compose -p websoft9 up -d + if [ $? -ne 0 ]; then + echo "Failed to start docker services." + exit 1 + fi +} + + +install_systemd() { + echo "Install Systemd service" + cp "$install_path/systemd/websoft9.service" /lib/systemd/system/ + if [ $? -ne 0 ]; then + echo "Failed to copy Systemd service file." + exit 1 + fi + + sudo systemctl daemon-reload + if [ $? -ne 0 ]; then + echo "Failed to reload Systemd daemon." + exit 1 + fi + + sudo systemctl enable websoft9.service + if [ $? -ne 0 ]; then + echo "Failed to enable Systemd service." + exit 1 + fi + + sudo systemctl start websoft9 + if [ $? -ne 0 ]; then + echo "Failed to start Systemd service." + exit 1 + fi +} + + + +#--------------- main----------------------------------------- + +echo "------ Welcome to install Websoft9, it will take 3-5 minutes ------" +check_ports $http_port $https_port $cockpit_port +install_tools +download_source + +bash install_docker.sh bash install_cockpit.sh bash install_plugins.sh -# Install backend services -cd $install_path/docker -sudo docker network create websoft9 -sudo docker compose -p websoft9 up -d - -# Install Systemd service -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 +install_compose +install_systemd +echo "-- Install success! Access Websoft9 console by: http://Internet IP:9000 and using Linux user for login ------" \ No newline at end of file diff --git a/install/install_plugins.sh b/install/install_plugins.sh index bc4f6d74..1647cc77 100644 --- a/install/install_plugins.sh +++ b/install/install_plugins.sh @@ -9,12 +9,7 @@ export PATH InstallPlugins(){ -# download apps -mkdir -p /data/apps && cd /data/apps -wget $urls/websoft9-latest.zip -unzip websoft9-latest.zip -cp -r /data/apps/websoft9/docker /data/apps/w9services -rm -f websoft9-latest.zip + # install plugins cd /usr/share/cockpit @@ -128,8 +123,6 @@ echo "Start to Edit Cockpit Menu ..." # uninstall plugins rm -rf /usr/share/cockpit/apps /usr/share/cockpit/selinux /usr/share/cockpit/kdump /usr/share/cockpit/sosreport /usr/share/cockpit/packagekit cp -r /data/apps/websoft9/cockpit/menu_override/* /etc/cockpit - -echo "---------------------------------- Install success! When installation completed, you can access it by: http://Internet IP:9000 and using Linux user for login to install a app by websoft9's appstore. -------------------------------------------------------" } InstallPlugins diff --git a/install/install_tools.sh b/install/install_tools.sh deleted file mode 100644 index dd1c5822..00000000 --- a/install/install_tools.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash -PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin -export PATH - -echo "Starting InstallTools..." - - -InstallTools(){ - -echo "Prepare to install Tools ..." - -#!/bin/bash - -dnf --version >/dev/null 2>&1 || yum --version >/dev/null 2>&1 || apt --version >/dev/null 2>&1 - -if [ $? -eq 0 ]; then - dnf install git curl wget yum-utils jq bc unzip -y -elif [ $? -eq 0 ]; then - yum install git curl wget yum-utils jq bc unzip -y -elif [ $? -eq 0 ]; then - while fuser /var/lib/dpkg/lock >/dev/null 2>&1 ; do - echo "Waiting for other software managers to finish..." - sleep 5 - done - sudo apt update -y 1>/dev/null 2>&1 - apt install curl wget yum-utils jq bc unzip -y --assume-yes -else - echo "None of the required package managers are installed." -fi - -} - -InstallTools \ No newline at end of file