cloudpanel-script/install.sh
2024-12-03 11:47:27 +05:30

186 lines
No EOL
4.9 KiB
Bash

#!/bin/bash -x
# Create a custom directory for preserved temporary files
PRESERVED_TMP_DIR="/root/preserved_tmp"
mkdir -p "$PRESERVED_TMP_DIR"
# Set custom temporary directory
export TMPDIR="$PRESERVED_TMP_DIR"
export TMP="$PRESERVED_TMP_DIR"
export TEMP="$PRESERVED_TMP_DIR"
VERBOSE=0
OS_NAME=
OS_VERSION=
OS_CODE_NAME=
ARCH=
export IP=
export DEBIAN_FRONTEND=noninteractive
[[ -z "$CREATE_AMI" ]] && export CREATE_AMI
[[ -z "$DB_ENGINE" ]] && export DB_ENGINE="MYSQL_8.0"
export IS_LXC=0
if grep -q container=lxc "/proc/1/environ"; then
export IS_LXC=1
fi
RED_TEXT_COLOR=`tput setaf 1`
GREEN_TEXT_COLOR=`tput setaf 2`
YELLOW_TEXT_COLOR=`tput setaf 3`
RESET_TEXT_COLOR=`tput sgr0`
if [ -z "${SWAP}" ]; then
SWAP=true
fi
die()
{
/bin/echo -e "ERROR: $*" >&2
exit 1
}
verbose()
{
if [ "$VERBOSE" -eq "1" ]; then
echo "$@" >&2
fi
}
setOSInfo()
{
[ -e '/bin/uname' ] && uname='/bin/uname' || uname='/usr/bin/uname'
ARCH=`uname -m`
OPERATING_SYSTEM=`uname -s`
if [ "$OPERATING_SYSTEM" = 'Linux' ]; then
if [ -e '/etc/debian_version' ]; then
if [ -e '/etc/lsb-release' ]; then
. /etc/lsb-release
OS_NAME=$DISTRIB_ID
OS_CODE_NAME=$(awk -F'=' '/VERSION_CODENAME/ {print $2}' /etc/os-release)
OS_VERSION=$DISTRIB_RELEASE
else
OS_NAME='Debian'
OS_CODE_NAME=$(awk -F= '/VERSION_CODENAME/{print $2}' /etc/os-release)
DEBIAN_VERSION=$(cat /etc/debian_version)
OS_VERSION=`echo $DEBIAN_VERSION | cut -d "." -f -1`
fi
else
die "Unable to detect Debian or Ubuntu."
fi
else
die "Operating System needs to be Linux."
fi
verbose "Architecture: $ARCH"
verbose "OS Name: $OS_NAME"
verbose "OS Version: $OS_VERSION"
}
checkRequirements()
{
apt update
apt -y install lsof
checkOperatingSystem
checkPortConflicts
checkDatabaseEngine
checkIfHostnameResolves
checkRootPartitionSize
}
addAptSourceList()
{
curl -fsSL https://d17k9fuiwb52nc.cloudfront.net/key.gpg | sudo gpg --yes --dearmor -o "$PRESERVED_TMP_DIR/cloudpanel-keyring.gpg"
cp "$PRESERVED_TMP_DIR/cloudpanel-keyring.gpg" /etc/apt/trusted.gpg.d/cloudpanel-keyring.gpg
if [ "$ARCH" = "aarch64" ]; then
ORIGIN="d2xpdm4jldf31f.cloudfront.net"
else
ORIGIN="d17k9fuiwb52nc.cloudfront.net"
fi
CLOUDPANEL_SOURCE_LIST=$(cat <<-END
deb https://$ORIGIN/ $OS_CODE_NAME main
deb https://$ORIGIN/ $OS_CODE_NAME nginx
deb https://$ORIGIN/ $OS_CODE_NAME php-7.1
deb https://$ORIGIN/ $OS_CODE_NAME php-7.2
deb https://$ORIGIN/ $OS_CODE_NAME php-7.3
deb https://$ORIGIN/ $OS_CODE_NAME php-7.4
deb https://$ORIGIN/ $OS_CODE_NAME php-8.0
deb https://$ORIGIN/ $OS_CODE_NAME php-8.1
deb https://$ORIGIN/ $OS_CODE_NAME php-8.2
deb https://$ORIGIN/ $OS_CODE_NAME php-8.3
deb https://$ORIGIN/ $OS_CODE_NAME php-8.4
deb https://$ORIGIN/ $OS_CODE_NAME proftpd
deb https://$ORIGIN/ $OS_CODE_NAME varnish-7
END
)
CLOUDPANEL_APT_PREFERENCES=$(cat <<-END
Package: *
Pin: origin $ORIGIN
Pin-Priority: 1000
END
)
echo -e "$CLOUDPANEL_SOURCE_LIST" > /etc/apt/sources.list.d/packages.cloudpanel.io.list
echo -e "$CLOUDPANEL_APT_PREFERENCES" > /etc/apt/preferences.d/00packages.cloudpanel.io.pref
apt -y update
}
setupRequiredPackages()
{
apt -y upgrade --download-only
cp -r /var/cache/apt/archives/* "$PRESERVED_TMP_DIR/"
apt -y upgrade
apt -y install gnupg apt-transport-https debsums chrony redis-server
DEBIAN_FRONTEND=noninteractive apt-get install -y postfix
if [ "$SWAP" != false ] ; then
echo "CONF_SWAPFILE=/home/.swap" > /etc/dphys-swapfile
echo "CONF_SWAPSIZE=2048" >> /etc/dphys-swapfile
echo "CONF_MAXSWAP=2048" >> /etc/dphys-swapfile
DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confnew install dphys-swapfile
fi
}
installMySQL() {
addAptSourceList
if [ "$OS_NAME" = "Debian" ]; then
case $OS_VERSION in
"11")
case $DB_ENGINE in
"MYSQL_5.7")
echo "deb https://$ORIGIN/ $OS_CODE_NAME percona-server-server-5.7" > /etc/apt/sources.list.d/percona-mysql.list
apt -y update --download-only
cp -r /var/cache/apt/archives/* "$PRESERVED_TMP_DIR/"
DEBIAN_FRONTEND=noninteractive apt -y install percona-server-client-5.7 percona-server-server-5.7
;;
# [Rest of the MySQL installation cases remain the same, just add the download preservation]
*)
die "Database Engine $DB_ENGINE not supported."
;;
esac
;;
# [Rest of the OS version cases remain the same]
esac
fi
# [Rest of the installMySQL function remains the same]
}
# [Other functions remain the same: checkOperatingSystem, checkPortConflicts, etc.]
cleanUp()
{
echo "Temporary files have been preserved in: $PRESERVED_TMP_DIR"
echo "Downloaded packages have been preserved in: $PRESERVED_TMP_DIR"
}
setOSInfo
checkRequirements
setIp
setupRequiredPackages
generateLocales
removeUnnecessaryPackages
installMySQL
setupCloudPanel
cleanUp