websoft9/install/install.sh

266 lines
6.8 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
# ==============================================================================
#
# --force <y|n>
# Use the --force option to ignore all interactive choices. default is n, for example:
#
# $ sudo sh install.sh --force n
#
# --port <9000>
# Use the --port option to set Websoft9 cosole port. default is 9000, for example:
#
# $ sudo sh install.sh --port 9001
#
# --channel <release|dev>
# Use the --channel option to install a release(production) or dev distribution. default is release, for example:
#
# $ sudo sh install.sh --channel release
#
# --path
# Use the --path option to for installation path for example:
#
# $ sudo sh install.sh --path "/data/websoft9/source"
#
# ==============================================================================
# 设置参数的默认值
force="n"
port="9000"
channel="release"
path="/data/websoft9/source"
# 获取参数值
while [[ $# -gt 0 ]]; do
case $1 in
--force)
force="$2"
shift 2
;;
--port)
port="$2"
shift 2
;;
--channel)
channel="$2"
shift 2
;;
--path)
path="$2"
shift 2
;;
*)
shift
;;
esac
done
2023-04-12 09:07:36 +00:00
2023-09-27 06:50:42 +00:00
# 输出参数值
echo "Your installation parameters are as follows: "
echo "--force: $force"
echo "--port: $port"
echo "--channel: $channel"
echo "--path: $path"
# Define global vars
# export var can send it to subprocess
export http_port=80
export https_port=443
export cockpit_port=$port
export force_install=$force
export install_path=$path
2023-09-27 07:28:02 +00:00
export channel
2023-09-27 06:50:42 +00:00
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"
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 artifact_url="https://w9artifact.blob.core.windows.net/$channel/websoft9"
echo Install from url: $artifact_url
# Define common functions
install_tools(){
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
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 install $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 install $tools_apt -y --assume-yes
2023-04-12 09:07:36 +00:00
else
2023-09-27 06:50:42 +00:00
echo "None of the required package managers are installed."
2023-04-12 09:07:36 +00:00
fi
}
2023-09-27 06:50:42 +00:00
download_source() {
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 and installation will cover it."
else
mkdir -p "$install_path"
2023-06-25 03:01:50 +00:00
fi
2023-09-27 06:50:42 +00:00
wget "$artifact_url/$source_zip"
if [ $? -ne 0 ]; then
echo "Failed to download source package."
exit 1
2023-06-27 08:51:11 +00:00
fi
2023-04-12 09:07:36 +00:00
2023-09-27 06:50:42 +00:00
sudo unzip -o "$source_zip" -d "$install_path" > /dev/null
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
2023-09-27 06:50:42 +00:00
rm -rf "$source_zip" "$install_path/$source_unzip"
2023-04-12 09:07:36 +00:00
}
2023-07-21 07:19:59 +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..."
sudo docker stop websoft9-proxy || echo "docker stop websoft9-proxy failed "
sudo systemctl stop cockpit || echo "systemctl stop cockpit failed"
sudo systemctl stop cockpit.socket || echo "systemctl stop cockpit.socket failed"
2023-07-21 07:19:59 +00:00
2023-09-27 06:50:42 +00:00
for port in "${ports[@]}"; do
if netstat -tuln | grep ":$port " >/dev/null; then
echo "Port $port is in use, install failed"
exit
fi
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-09-27 06:50:42 +00:00
install_backends() {
echo_prefix_backends=$'\n[Backend] - '
echo "$echo_prefix_backends Install backend docker services"
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
container_names=$(docker ps -a --format "{{.Names}}" --filter "name=websoft9")
2023-09-27 06:50:42 +00:00
sudo docker compose 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
docker rm $container_names
else
echo "No containers to delete."
fi
2023-09-27 06:50:42 +00:00
sudo docker compose -p websoft9 pull
sudo docker compose -p websoft9 up -d
if [ $? -ne 0 ]; then
echo "Failed to start docker services."
exit 1
fi
2023-04-12 09:07:36 +00:00
}
2023-09-27 06:50:42 +00:00
install_systemd() {
echo_prefix_systemd=$'\n[Systemd] - '
echo "$echo_prefix_systemdInstall 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-09-27 06:50:42 +00:00
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
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-05-31 03:32:37 +00:00
2023-07-21 07:19:59 +00:00
2023-09-27 06:50:42 +00:00
#--------------- main-----------------------------------------
2023-07-21 01:17:40 +00:00
2023-09-27 06:50:42 +00:00
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_path/install/install_docker.sh
bash $install_path/install/install_cockpit.sh
bash $install_path/install/install_plugins.sh
2023-05-31 03:32:37 +00:00
2023-09-27 06:50:42 +00:00
install_backends
install_systemd
echo "-- Install success! Access Websoft9 console by: http://Internet IP:$cockpit_port and using Linux user for login ------"