websoft9/install/install.sh

526 lines
15 KiB
Bash
Raw Normal View History

2023-03-21 04:06:44 +00:00
#!/bin/bash
2023-09-27 06:50:42 +00:00
# Define PATH
2023-03-21 04:06:44 +00:00
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
2023-09-27 06:50:42 +00:00
# Export PATH
2023-03-21 04:06:44 +00:00
export PATH
2023-04-12 09:07:36 +00:00
2023-09-27 06:50:42 +00:00
# Command-line options
# ==============================================================================
#
2023-09-28 09:31:16 +00:00
# --version
# Use the --version option to install a special version for installation. default is latest, for example:
2023-09-27 06:50:42 +00:00
#
2023-09-28 09:31:16 +00:00
# $ sudo bash install.sh --version "0.8.25"
2023-09-27 06:50:42 +00:00
#
# --port <9000>
# Use the --port option to set Websoft9 cosole port. default is 9000, for example:
#
2023-09-28 09:31:16 +00:00
# $ sudo bash install.sh --port 9001
2023-09-27 06:50:42 +00:00
#
# --channel <release|dev>
# Use the --channel option to install a release(production) or dev distribution. default is release, for example:
#
2023-09-28 09:31:16 +00:00
# $ sudo bash install.sh --channel release
2023-09-27 06:50:42 +00:00
#
# --path
# Use the --path option to for installation path for example:
#
2023-09-28 09:31:16 +00:00
# $ sudo bash install.sh --path "/data/websoft9/source"
2023-09-27 06:50:42 +00:00
#
2024-03-13 02:35:09 +00:00
# --apps <wordpress,gitlab>
# Use the --apps option to set Websoft9 appstore dispaly. default is null and display all applicaionts. If you set it, appstore only display the defined, for example:
#
# $ sudo bash install.sh --apps "wordpress,gitlab"
#
2024-06-24 03:46:18 +00:00
# --mirrors <https://docker.rainbond.cc,https://registry.inner.websoft9.cn>
# Use the --mirrors option to set docker image mirrors when can not pull image from docker-hub, for example:
#
# $ sudo bash install.sh --mirrors "https://docker.rainbond.cc,https://registry.inner.websoft9.cn"
#
2023-10-12 02:26:44 +00:00
# --devto
# Use the --devto option to developer mode, devto is the developer code path, for example:
#
# $ sudo bash install.sh --devto "/data/dev/mycode"
#
2023-09-27 06:50:42 +00:00
# ==============================================================================
# 设置参数的默认值
2023-09-28 09:31:16 +00:00
version="latest"
2023-09-27 06:50:42 +00:00
channel="release"
path="/data/websoft9/source"
2024-03-13 02:35:09 +00:00
apps=""
2024-10-21 03:05:34 +00:00
mirrors="https://dockerhub.websoft9.com"
2023-09-27 06:50:42 +00:00
# 获取参数值
while [[ $# -gt 0 ]]; do
case $1 in
2023-09-28 09:31:16 +00:00
--version)
2023-11-24 06:57:27 +00:00
shift
if [[ $1 == --* ]]; then
echo "Missing value for --version"
exit 1
fi
version="$1"
shift
2023-09-27 06:50:42 +00:00
;;
--port)
2023-11-24 06:57:27 +00:00
shift
if [[ $1 == --* ]]; then
echo "Missing value for --port"
exit 1
fi
port="$1"
shift
2023-09-27 06:50:42 +00:00
;;
--channel)
2023-11-24 06:57:27 +00:00
shift
if [[ $1 == --* ]]; then
echo "Missing value for --channel"
exit 1
fi
channel="$1"
shift
2023-09-27 06:50:42 +00:00
;;
--path)
2023-11-24 06:57:27 +00:00
shift
if [[ $1 == --* ]]; then
echo "Missing value for --path"
exit 1
fi
path="$1"
shift
2023-09-27 06:50:42 +00:00
;;
2024-03-13 02:35:09 +00:00
--apps)
shift
if [[ $1 == --* ]]; then
echo "Missing value for --apps"
exit 1
fi
apps="$1"
shift
;;
2024-06-24 03:46:18 +00:00
--mirrors)
shift
if [[ $1 == --* ]]; then
echo "Missing value for --mirrors"
exit 1
fi
mirrors="$1"
shift
;;
2023-10-12 02:19:27 +00:00
--devto)
2023-11-24 06:57:27 +00:00
shift
if [[ $1 == --* ]]; then
echo "Missing value for --devto"
exit 1
fi
devto="$1"
shift
2023-10-12 02:19:27 +00:00
;;
2023-09-27 06:50:42 +00:00
*)
2023-11-24 06:57:27 +00:00
echo "Unknown parameter: $1"
exit 1
2023-09-27 06:50:42 +00:00
;;
esac
done
2023-04-12 09:07:36 +00:00
2023-10-08 10:40:19 +00:00
if [ -n "$port" ]; then
export port
else
2023-10-16 05:49:17 +00:00
export port=9000
2023-10-08 10:40:19 +00:00
fi
2023-10-13 04:02:38 +00:00
starttime=$(date +%s)
2023-12-05 04:07:39 +00:00
# Check is install or upgrade
if systemctl cat websoft9 >/dev/null 2>&1 && systemctl cat cockpit >/dev/null 2>&1 && sudo docker ps -a --format '{{.Names}}' 2>/dev/null | grep -q '^websoft9-apphub'; then
echo "execute_mode=upgrade"
export execute_mode="upgrade"
else
echo "execute_mode=install"
export execute_mode="install"
fi
2023-09-27 06:50:42 +00:00
# 输出参数值
2023-10-08 03:27:06 +00:00
echo -e "\n------ Welcome to install Websoft9, it will take 3-5 minutes ------"
echo -e "\nYour installation parameters are as follows: "
2023-09-28 09:31:16 +00:00
echo "--version: $version"
2023-09-27 06:50:42 +00:00
echo "--port: $port"
echo "--channel: $channel"
echo "--path: $path"
2024-03-13 02:35:09 +00:00
echo "--apps: $apps"
2024-06-24 03:46:18 +00:00
echo "--mirrors: $mirrors"
2023-10-12 02:19:27 +00:00
echo "--devto: $devto"
2023-09-27 06:50:42 +00:00
2023-10-08 03:27:06 +00:00
echo -e "\nYour OS: "
cat /etc/os-release | head -n 3 2>/dev/null
2023-09-27 06:50:42 +00:00
# Define global vars
# export var can send it to subprocess
export http_port=80
export https_port=443
export install_path=$path
2023-09-27 07:28:02 +00:00
export channel
2023-09-28 09:31:16 +00:00
export version
2024-03-13 02:35:09 +00:00
export apps
2024-06-24 03:46:18 +00:00
export mirrors
2023-09-27 06:50:42 +00:00
export systemd_path="/opt/websoft9/systemd"
2023-09-28 09:31:16 +00:00
export source_zip="websoft9-$version.zip"
2023-09-27 06:50:42 +00:00
export source_unzip="websoft9"
export source_github_pages="https://websoft9.github.io/websoft9"
2023-10-17 10:16:52 +00:00
# inotify-tools is at epel-release
2023-11-09 02:36:20 +00:00
export repo_tools_yum="epel-release"
2023-11-10 01:59:14 +00:00
export tools_yum="git curl wget yum-utils jq bc unzip inotify-tools"
2023-10-09 02:49:18 +00:00
export tools_apt="git curl wget jq bc unzip inotify-tools"
2023-09-27 06:50:42 +00:00
export docker_network="websoft9"
2024-09-05 01:04:28 +00:00
export artifact_url="https://artifact.websoft9.com/$channel/websoft9"
2023-11-08 03:52:21 +00:00
# export OS release environments
if [ -f /etc/os-release ]; then
. /etc/os-release
else
echo "Can't judge your Linux distribution"
exit 1
fi
2023-09-27 06:50:42 +00:00
echo Install from url: $artifact_url
2024-02-01 03:11:41 +00:00
if [ -d "$install_path" ]; then
echo "Directory $install_path already exists and installation will cover it."
else
sudo mkdir -p "$install_path"
fi
2023-09-27 06:50:42 +00:00
# Define common functions
2023-10-06 11:54:23 +00:00
Wait_apt() {
# Function to check if apt is locked
local lock_files=("/var/lib/dpkg/lock" "/var/lib/apt/lists/lock")
for lock_file in "${lock_files[@]}"; do
while fuser "${lock_file}" >/dev/null 2>&1 ; do
echo "${lock_file} is locked by another process. Waiting..."
sleep 5
done
done
echo "APT locks are not held by any processes. You can proceed."
}
export -f Wait_apt
2023-09-27 06:50:42 +00:00
install_tools(){
echo_prefix_tools=$'\n[Tools] - '
echo "$echo_prefix_tools Starting install necessary tool..."
2023-11-09 09:18:07 +00:00
if [ "$ID" = "rhel" ] || [ "$ID" = "ol" ]; then
2023-11-08 03:52:21 +00:00
RHEL_VERSION=${VERSION_ID%%.*}
2023-11-10 01:23:53 +00:00
sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-${RHEL_VERSION}.noarch.rpm >/dev/null
if [ $? -ne 0 ]; then
exit 1
fi
2023-11-09 09:18:07 +00:00
elif [ "$ID" = "centos" ] || [ "$ID" = "rocky" ]; then
2023-11-10 01:23:53 +00:00
sudo yum install -y "$repo_tools_yum" >/dev/null
if [ $? -ne 0 ]; then
exit 1
fi
2023-11-09 09:18:07 +00:00
elif [ "$ID" = "amzn" ]; then
2023-11-10 01:23:53 +00:00
sudo amazon-linux-extras install epel -y >/dev/null
if [ $? -ne 0 ]; then
exit 1
fi
2023-11-07 08:41:48 +00:00
fi
2023-09-27 06:50:42 +00:00
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
2023-11-09 09:18:07 +00:00
for package in $tools_yum; do
echo "Start to install $package"
2023-11-10 01:03:44 +00:00
sudo dnf install -y $package > /dev/null
if [ $? -ne 0 ]; then
exit 1
fi
2023-11-09 09:18:07 +00:00
done
2023-09-27 06:50:42 +00:00
elif [ $yum_status -eq 0 ]; then
2023-11-09 09:18:07 +00:00
for package in $tools_yum; do
echo "Start to install $package"
2023-11-10 01:03:44 +00:00
sudo yum install -y $package > /dev/null
if [ $? -ne 0 ]; then
exit 1
fi
2023-11-09 09:18:07 +00:00
done
2023-09-27 06:50:42 +00:00
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
2023-11-10 01:06:47 +00:00
sudo apt-get update -y 1>/dev/null 2>&1
2023-11-09 09:18:07 +00:00
for package in $tools_apt; do
echo "Start to install $package"
2023-11-10 01:06:47 +00:00
sudo apt-get install $package -y > /dev/null
2023-11-10 01:03:44 +00:00
if [ $? -ne 0 ]; then
exit 1
fi
2023-11-09 09:18:07 +00:00
done
2023-04-12 09:07:36 +00:00
else
2023-11-09 02:36:20 +00:00
echo "You system can not install Websoft9 because not have available Linux Package Manager"
exit 1
2023-04-12 09:07:36 +00:00
fi
}
2024-10-23 04:04:38 +00:00
download_source_and_checkimage() {
2023-10-06 10:59:47 +00:00
echo_prefix_source=$'\n[Download Source] - '
2023-09-27 06:50:42 +00:00
echo "$echo_prefix_source Download Websoft9 source code from $artifact_url/$source_zip"
2023-09-28 09:31:16 +00:00
find . -type f -name "websoft9*.zip*" -exec rm -f {} \;
2024-10-23 06:34:57 +00:00
rm -rf /tmp/$source_unzip
2023-09-27 06:50:42 +00:00
2024-10-23 06:34:57 +00:00
wget -P /tmp "$artifact_url/$source_zip"
2023-09-27 06:50:42 +00:00
if [ $? -ne 0 ]; then
echo "Failed to download source package."
exit 1
2023-06-27 08:51:11 +00:00
fi
2024-10-23 04:04:38 +00:00
## unzip and check image
2024-10-23 06:34:57 +00:00
sudo unzip -o "/tmp/$source_zip" -d /tmp > /dev/null
2024-10-23 04:04:38 +00:00
if [ $? -ne 0 ]; then
echo "Failed to unzip source package."
exit 1
fi
2024-10-23 07:31:08 +00:00
# install docker
2024-10-23 07:18:21 +00:00
bash /tmp/$source_unzip/install/install_docker.sh
2024-10-23 07:14:19 +00:00
if [ $? -ne 0 ]; then
echo "install_docker failed with error $?. Exiting."
exit 1
fi
2024-10-23 07:31:08 +00:00
2024-10-23 04:04:38 +00:00
cd /tmp/$source_unzip/docker
2024-10-23 06:34:57 +00:00
docker compose pull
2024-10-23 04:04:38 +00:00
if [ $? -ne 0 ]; then
echo "Can not pull images from docker hub, set mirrors...."
if [ -f "/etc/docker/daemon.json" ]; then
if grep -q "registry-mirrors" "/etc/docker/daemon.json"; then
mv /etc/docker/daemon.json /etc/docker/daemon.json.bak
cp daemon.json /etc/docker/daemon.json
else
rm -f /etc/docker/daemon.json
cp daemon.json /etc/docker/daemon.json
fi
else
cp daemon.json /etc/docker/daemon.json
fi
sudo systemctl daemon-reload
sudo systemctl restart docker
# pull image by new mirrors
docker compose pull
if [ $? -ne 0 ]; then
echo "image pull failed again, exit install"
exit 1
else
echo "image pull success by new mirrors"
fi
else
echo "image pull success"
fi
2023-04-12 09:07:36 +00:00
2024-10-23 04:04:38 +00:00
rm -rf /tmp/$source_unzip
2024-10-23 06:34:57 +00:00
sudo unzip -o "/tmp/$source_zip" -d "$install_path" > /dev/null
2023-09-27 06:50:42 +00:00
if [ $? -ne 0 ]; then
echo "Failed to unzip source package."
exit 1
fi
2023-03-21 04:06:44 +00:00
2023-09-27 06:50:42 +00:00
cp -r $install_path/$source_unzip/* "$install_path"
if [ $? -ne 0 ]; then
echo "Move directory failed"
exit 1
fi
2023-04-12 09:07:36 +00:00
2024-10-23 06:34:57 +00:00
rm -rf "/tmp/$source_zip" "$install_path/$source_unzip"
2023-04-12 09:07:36 +00:00
}
2023-09-27 06:50:42 +00:00
check_ports() {
local ports=("$@")
2023-07-21 07:19:59 +00:00
2023-09-27 06:50:42 +00:00
echo "Stop Websoft9 Proxy and Cockpit service for reserve ports..."
2023-10-06 08:45:57 +00:00
sudo docker stop websoft9-proxy 2>/dev/null || echo "docker stop websoft9-proxy not need "
2023-11-24 02:46:53 +00:00
2023-09-27 06:50:42 +00:00
for port in "${ports[@]}"; do
2023-11-24 02:46:53 +00:00
if [[ $port =~ ^[0-9]+$ ]] && [ $port -ge 0 ] && [ $port -le 65535 ]; then
if ss -tuln | grep ":$port " >/dev/null && ! systemctl status cockpit.socket | grep "$port" >/dev/null; then
echo "Port $port is in use or not in cockpit.socket, install failed"
exit 1
fi
else
echo "Invalid port: $port"
exit 1
2023-09-27 06:50:42 +00:00
fi
2023-11-24 02:46:53 +00:00
2023-09-27 06:50:42 +00:00
done
2023-08-07 06:10:52 +00:00
2023-09-27 06:50:42 +00:00
echo "All ports are available"
}
2023-07-21 07:19:59 +00:00
2023-10-06 09:12:07 +00:00
set_docker(){
echo "Set Docker for Websoft9 backend service..."
2023-10-08 03:27:06 +00:00
if ! systemctl is-active --quiet firewalld; then
echo "firewalld is not running"
else
echo "Set firewall for Docker..."
sudo sudo firewall-cmd --permanent --new-zone=docker 2> /dev/null
sudo firewall-cmd --permanent --zone=docker --add-interface=docker0 2> /dev/null
sudo firewall-cmd --permanent --zone=docker --set-target=ACCEPT
sudo firewall-cmd --reload
2023-10-17 10:16:52 +00:00
sudo systemctl stop firewalld
2023-10-18 07:25:03 +00:00
sudo systemctl disable firewalld
2023-10-06 08:45:57 +00:00
fi
2023-12-07 09:18:32 +00:00
if [ "$execute_mode" = "install" ]; then
sudo systemctl restart docker
fi
2023-10-06 08:45:57 +00:00
}
2023-09-27 06:50:42 +00:00
install_backends() {
echo_prefix_backends=$'\n[Backend] - '
echo "$echo_prefix_backends Install backend docker services"
2023-10-06 08:45:57 +00:00
set_docker
2023-07-24 02:43:56 +00:00
2023-09-27 06:50:42 +00:00
cd "$install_path/docker"
if [ $? -ne 0 ]; then
echo "Failed to change directory."
exit 1
fi
2023-07-20 07:40:33 +00:00
2023-09-27 06:50:42 +00:00
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
2023-09-27 07:28:02 +00:00
2023-10-12 02:19:27 +00:00
# set to devloper mode
if [ -n "$devto" ]; then
2023-10-12 09:43:27 +00:00
sed -i "s|\(- \).*:/websoft9/apphub-dev|\1$devto:/websoft9/apphub-dev|g" docker-compose-dev.yml
2023-10-12 02:19:27 +00:00
composefile=docker-compose-dev.yml
else
composefile=docker-compose.yml
fi
2023-09-27 07:28:02 +00:00
container_names=$(docker ps -a --format "{{.Names}}" --filter "name=websoft9")
2023-10-12 02:19:27 +00:00
sudo docker compose -p websoft9 -f $composefile down
2023-09-27 07:28:02 +00:00
# delete some dead containers that docker compose cannot deleted
if [ ! -z "$container_names" ]; then
echo "Deleting containers:"
echo $container_names
2023-10-06 11:54:23 +00:00
docker rm -f $container_names 2>/dev/null
2023-09-27 07:28:02 +00:00
else
echo "No containers to delete."
fi
2024-10-23 04:04:38 +00:00
sudo docker compose -p websoft9 -f $composefile up -d
2023-09-27 06:50:42 +00:00
if [ $? -ne 0 ]; then
echo "Failed to start docker services."
exit 1
fi
2023-12-05 04:07:39 +00:00
if [ "$execute_mode" = "install" ]; then
sudo docker exec -i websoft9-apphub apphub setconfig --section domain --key wildcard_domain --value ""
2024-06-24 03:46:18 +00:00
if [ -n "$apps" ]; then
sudo docker exec -i websoft9-apphub apphub setconfig --section initial_apps --key keys --value "$apps"
fi
2023-12-05 04:07:39 +00:00
fi
2024-10-23 06:34:57 +00:00
if [ -f "/etc/docker/daemon.json.bak" ]; then
rm -rf /etc/docker/daemon.json
mv /etc/docker/daemon.json.bak /etc/docker/daemon.json
fi
2023-04-12 09:07:36 +00:00
}
2023-09-27 06:50:42 +00:00
install_systemd() {
2023-10-24 06:47:39 +00:00
echo -e "\n\n-------- Systemd --------"
2023-09-27 06:50:42 +00:00
echo_prefix_systemd=$'\n[Systemd] - '
2023-10-24 06:47:39 +00:00
echo "$echo_prefix_systemd Install Systemd service"
2023-05-18 09:15:59 +00:00
2023-09-27 06:50:42 +00:00
if [ ! -d "$systemd_path" ]; then
sudo mkdir -p "$systemd_path"
fi
2023-04-12 09:07:36 +00:00
2023-10-06 10:59:47 +00:00
sudo cp -r $install_path/systemd/script/* "$systemd_path"
2023-10-06 11:54:23 +00:00
sudo cp -f "$install_path/systemd/websoft9.service" /lib/systemd/system/
2023-09-27 06:50:42 +00:00
if [ $? -ne 0 ]; then
echo "Failed to copy Systemd service file."
exit 1
fi
2023-05-18 09:15:59 +00:00
2023-09-27 06:50:42 +00:00
sudo systemctl daemon-reload
if [ $? -ne 0 ]; then
echo "Failed to reload Systemd daemon."
exit 1
fi
2023-06-30 08:02:39 +00:00
2023-09-27 06:50:42 +00:00
sudo systemctl enable websoft9.service
if [ $? -ne 0 ]; then
echo "Failed to enable Systemd service."
exit 1
fi
2023-05-18 09:15:59 +00:00
2023-09-27 06:50:42 +00:00
sudo systemctl start websoft9
if [ $? -ne 0 ]; then
echo "Failed to start Systemd service."
exit 1
fi
2023-04-12 09:07:36 +00:00
}
2023-09-27 06:50:42 +00:00
#--------------- main-----------------------------------------
2024-02-01 03:11:41 +00:00
log_path="$install_path/install.log"
2024-01-31 05:59:46 +00:00
check_ports $http_port $https_port $port | tee -a $log_path
install_tools | tee -a $log_path
2023-09-27 06:50:42 +00:00
2024-10-23 04:04:38 +00:00
download_source_and_checkimage | tee -a $log_path
2024-01-31 05:59:46 +00:00
install_backends | tee -a $log_path
2023-10-13 02:58:01 +00:00
2024-01-31 05:59:46 +00:00
install_systemd | tee -a $log_path
2023-10-24 06:47:39 +00:00
2024-01-31 05:59:46 +00:00
bash $install_path/install/install_cockpit.sh | tee -a $log_path
2023-10-16 03:59:50 +00:00
if [ $? -ne 0 ]; then
echo "install_cockpit failed with error $?. Exiting."
exit 1
fi
2023-10-16 04:14:00 +00:00
2024-01-31 05:59:46 +00:00
bash $install_path/install/install_plugins.sh | tee -a $log_path
2023-10-07 08:00:55 +00:00
if [ $? -ne 0 ]; then
echo "install_plugins failed with error $?. Exiting."
exit 1
fi
2023-05-31 03:32:37 +00:00
2023-10-18 07:25:03 +00:00
echo "Restart Docker for Firewalld..."
2023-12-07 09:18:32 +00:00
if [ "$execute_mode" = "install" ]; then
sudo systemctl restart docker
fi
2023-10-18 07:25:03 +00:00
2023-10-13 04:02:38 +00:00
endtime=$(date +%s)
runtime=$((endtime-starttime))
echo "Script execution time: $runtime seconds"
2023-10-08 03:27:06 +00:00
echo -e "\n-- Install success! ------"
2024-06-24 03:46:18 +00:00
echo "Access Websoft9 console by: http://Internet IP:$(grep ListenStream /lib/systemd/system/cockpit.socket | cut -d= -f2) and using Linux user for login"