2016-10-24 16:18:02 +02:00
#!/bin/bash
2016-10-25 12:00:54 -07:00
set -eu -o pipefail
2016-10-24 16:18:02 +02:00
2021-02-25 10:40:34 -08:00
function exitHandler( ) {
rm -f /etc/update-motd.d/91-cloudron-install-in-progress
}
trap exitHandler EXIT
2016-10-24 14:52:30 -07:00
# change this to a hash when we make a upgrade release
2016-10-25 14:01:35 +02:00
readonly LOG_FILE = "/var/log/cloudron-setup.log"
2017-03-22 14:21:40 +01:00
readonly MINIMUM_DISK_SIZE_GB = "18" # this is the size of "/" and required to fit in docker images 18 is a safe bet for different reporting on 20GB min
2017-03-13 13:05:59 -07:00
readonly MINIMUM_MEMORY = "974" # this is mostly reported for 1GB main memory (DO 992, EC2 990, Linode 989, Serverdiscounter.com 974)
2017-01-02 18:02:40 +01:00
2017-02-16 17:32:44 -08:00
readonly curl = "curl --fail --connect-timeout 20 --retry 10 --retry-delay 2 --max-time 2400"
2017-01-02 18:02:40 +01:00
# copied from cloudron-resize-fs.sh
2017-07-24 18:14:51 -07:00
readonly rootfs_type = $( LC_ALL = C df --output= fstype / | tail -n1)
2017-03-13 10:32:27 -07:00
readonly physical_memory = $( LC_ALL = C free -m | awk '/Mem:/ { print $2 }' )
2017-03-27 13:51:34 +02:00
readonly disk_size_bytes = $( LC_ALL = C df --output= size / | tail -n1)
2017-03-22 14:21:40 +01:00
readonly disk_size_gb = $(( ${ disk_size_bytes } / 1024 / 1024 ))
2017-01-02 18:02:40 +01:00
2018-03-07 15:48:01 -08:00
readonly RED = '\033[31m'
readonly GREEN = '\033[32m'
readonly DONE = '\033[m'
2017-01-02 18:02:40 +01:00
# verify the system has minimum requirements met
2017-07-24 18:14:51 -07:00
if [ [ " ${ rootfs_type } " != "ext4" ] ] ; then
echo "Error: Cloudron requires '/' to be ext4" # see #364
exit 1
fi
2017-01-02 18:02:40 +01:00
if [ [ " ${ physical_memory } " -lt " ${ MINIMUM_MEMORY } " ] ] ; then
2017-01-03 14:27:01 -08:00
echo "Error: Cloudron requires atleast 1GB physical memory"
2017-01-02 18:02:40 +01:00
exit 1
fi
if [ [ " ${ disk_size_gb } " -lt " ${ MINIMUM_DISK_SIZE_GB } " ] ] ; then
2017-03-27 13:51:34 +02:00
echo " Error: Cloudron requires atleast 20GB disk space (Disk space on / is ${ disk_size_gb } GB) "
2017-01-02 18:02:40 +01:00
exit 1
fi
2016-10-24 14:52:30 -07:00
2021-08-25 13:20:12 -07:00
if [ [ " $( uname -m) " != "x86_64" ] ] ; then
echo "Error: Cloudron only supports amd64/x86_64"
exit 1
fi
2021-05-24 19:05:49 -07:00
# do not use is-active in case box service is down and user attempts to re-install
2021-06-17 10:03:59 -07:00
if systemctl cat box.service >/dev/null 2>& 1; then
2018-07-30 10:08:00 -07:00
echo "Error: Cloudron is already installed. To reinstall, start afresh"
exit 1
fi
2016-12-26 11:37:50 -08:00
initBaseImage = "true"
2020-06-03 13:47:30 -07:00
provider = "generic"
2017-04-13 15:20:33 -07:00
requestedVersion = ""
2021-01-10 11:26:01 -08:00
installServerOrigin = "https://api.cloudron.io"
2017-01-11 12:35:31 -08:00
apiServerOrigin = "https://api.cloudron.io"
2017-06-08 10:30:23 -07:00
webServerOrigin = "https://cloudron.io"
2017-02-01 14:06:18 -08:00
sourceTarballUrl = ""
2017-02-07 12:42:32 +01:00
rebootServer = "true"
2020-12-21 22:36:10 -08:00
setupToken = ""
2016-10-24 16:18:02 +02:00
2020-12-21 22:36:10 -08:00
args = $( getopt -o "" -l "help,skip-baseimage-init,provider:,version:,env:,skip-reboot,generate-setup-token" -n " $0 " -- " $@ " )
2016-10-24 16:18:02 +02:00
eval set -- " ${ args } "
while true; do
case " $1 " in
2020-09-15 14:46:22 -07:00
--help) echo "See https://docs.cloudron.io/installation/ on how to install Cloudron" ; exit 0; ;
2016-10-24 16:18:02 +02:00
--provider) provider = " $2 " ; shift 2; ;
2016-12-30 13:12:22 -08:00
--version) requestedVersion = " $2 " ; shift 2; ;
2017-01-10 20:19:39 -08:00
--env)
if [ [ " $2 " = = "dev" ] ] ; then
2017-01-11 12:35:31 -08:00
apiServerOrigin = "https://api.dev.cloudron.io"
2017-06-08 10:30:23 -07:00
webServerOrigin = "https://dev.cloudron.io"
2021-01-10 11:31:25 -08:00
installServerOrigin = "https://api.dev.cloudron.io"
2017-01-10 20:19:39 -08:00
elif [ [ " $2 " = = "staging" ] ] ; then
2017-01-11 12:35:31 -08:00
apiServerOrigin = "https://api.staging.cloudron.io"
2017-06-08 10:30:23 -07:00
webServerOrigin = "https://staging.cloudron.io"
2021-01-10 11:31:25 -08:00
installServerOrigin = "https://api.staging.cloudron.io"
2021-01-10 11:26:01 -08:00
elif [ [ " $2 " = = "unstable" ] ] ; then
2021-01-10 11:31:25 -08:00
installServerOrigin = "https://api.dev.cloudron.io"
2017-01-10 20:19:39 -08:00
fi
shift 2; ;
2016-12-26 10:00:00 -08:00
--skip-baseimage-init) initBaseImage = "false" ; shift; ;
2017-02-07 12:42:32 +01:00
--skip-reboot) rebootServer = "false" ; shift; ;
2020-12-21 22:36:10 -08:00
--generate-setup-token) setupToken = " $( openssl rand -hex 10) " ; shift; ;
2016-10-24 16:18:02 +02:00
--) break; ;
*) echo " Unknown option $1 " ; exit 1; ;
esac
done
2018-03-09 10:37:18 -08:00
# Only --help works as non-root
if [ [ ${ EUID } -ne 0 ] ] ; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
# Only --help works with mismatched ubuntu
2018-10-05 10:27:34 -07:00
ubuntu_version = $( lsb_release -rs)
2020-09-15 21:53:16 -07:00
if [ [ " ${ ubuntu_version } " != "16.04" && " ${ ubuntu_version } " != "18.04" && " ${ ubuntu_version } " != "20.04" ] ] ; then
echo "Cloudron requires Ubuntu 16.04, 18.04 or 20.04" > /dev/stderr
2018-03-09 10:37:18 -08:00
exit 1
fi
2021-07-28 07:16:06 -07:00
if which nginx >/dev/null || which docker >/dev/null || which node > /dev/null; then
echo "Error: Some packages like nginx/docker/nodejs are already installed. Cloudron requires specific versions of these packages and will install them as part of it's installation. Please start with a fresh Ubuntu install and run this script again." > /dev/stderr
exit 1
fi
2021-02-25 10:40:34 -08:00
# Install MOTD file for stack script style installations. this is removed by the trap exit handler. Heredoc quotes prevents parameter expansion
cat > /etc/update-motd.d/91-cloudron-install-in-progress <<'EOF'
#!/bin/bash
printf "**********************************************************************\n\n"
printf "\t\t\tWELCOME TO CLOUDRON\n"
printf "\t\t\t-------------------\n"
2021-03-03 14:34:55 -08:00
printf '\n\e[1;32m%-6s\e[m\n\n' "Cloudron is installing. Run 'tail -f /var/log/cloudron-setup.log' to view progress."
2021-02-25 10:40:34 -08:00
printf "Cloudron overview - https://docs.cloudron.io/ \n"
printf "Cloudron setup - https://docs.cloudron.io/installation/#setup \n"
printf "\nFor help and more information, visit https://forum.cloudron.io\n\n"
printf "**********************************************************************\n"
EOF
chmod +x /etc/update-motd.d/91-cloudron-install-in-progress
2018-10-25 11:31:39 -07:00
# Can only write after we have confirmed script has root access
echo " Running cloudron-setup with args : $@ " > " ${ LOG_FILE } "
2016-10-25 15:28:26 +02:00
echo ""
2016-10-25 14:07:28 +02:00
echo "##############################################"
2017-04-13 15:20:33 -07:00
echo " Cloudron Setup ( ${ requestedVersion :- latest } ) "
2016-10-25 14:07:28 +02:00
echo "##############################################"
echo ""
echo " Follow setup logs in a second terminal with:"
echo " $ tail -f ${ LOG_FILE } "
echo ""
2018-03-08 10:50:18 -08:00
echo " Join us at https://forum.cloudron.io for any questions."
2016-12-22 13:28:04 -08:00
echo ""
2016-10-25 14:07:28 +02:00
2017-01-05 14:03:30 -08:00
if [ [ " ${ initBaseImage } " = = "true" ] ] ; then
2017-02-12 20:48:36 +01:00
echo "=> Updating apt and installing script dependencies"
2017-01-05 14:03:30 -08:00
if ! apt-get update & >> " ${ LOG_FILE } " ; then
2018-09-06 09:16:11 -07:00
echo " Could not update package repositories. See ${ LOG_FILE } "
2017-01-05 14:03:30 -08:00
exit 1
fi
2021-01-04 23:30:41 -08:00
if ! DEBIAN_FRONTEND = noninteractive apt-get -o Dpkg::Options::= "--force-confdef" -o Dpkg::Options::= "--force-confold" -y install --no-install-recommends curl python3 ubuntu-standard software-properties-common -y & >> " ${ LOG_FILE } " ; then
2018-09-06 09:16:11 -07:00
echo " Could not install setup dependencies (curl). See ${ LOG_FILE } "
2017-01-05 14:03:30 -08:00
exit 1
fi
fi
2016-10-25 13:05:24 -07:00
echo "=> Checking version"
2021-01-10 11:26:01 -08:00
if ! releaseJson = $( $curl -s " ${ installServerOrigin } /api/v1/releases?boxVersion= ${ requestedVersion } " ) ; then
2018-01-31 20:14:31 -08:00
echo "Failed to get release information"
exit 1
fi
2017-04-13 15:20:33 -07:00
2018-01-31 20:14:31 -08:00
if [ [ " $requestedVersion " = = "" ] ] ; then
version = $( echo " ${ releaseJson } " | python3 -c 'import json,sys;obj=json.load(sys.stdin);print(obj["version"])' )
else
version = " ${ requestedVersion } "
fi
2017-04-13 15:20:33 -07:00
2018-01-31 20:14:31 -08:00
if ! sourceTarballUrl = $( echo " ${ releaseJson } " | python3 -c 'import json,sys;obj=json.load(sys.stdin);print(obj["info"]["sourceTarballUrl"])' ) ; then
echo " No source code for version ' ${ requestedVersion :- latest } ' "
exit 1
2016-10-25 13:05:24 -07:00
fi
2017-01-03 14:48:37 -08:00
echo " => Downloading version ${ version } ... "
2016-12-23 18:28:18 -08:00
box_src_tmp_dir = $( mktemp -dt box-src-XXXXXX)
2017-02-16 17:32:44 -08:00
if ! $curl -sL " ${ sourceTarballUrl } " | tar -zxf - -C " ${ box_src_tmp_dir } " ; then
2016-12-23 18:28:18 -08:00
echo " Could not download source tarball. See ${ LOG_FILE } for details "
exit 1
fi
2017-01-03 14:48:37 -08:00
if [ [ " ${ initBaseImage } " = = "true" ] ] ; then
2017-01-19 09:58:31 -08:00
echo -n "=> Installing base dependencies and downloading docker images (this takes some time) ..."
2020-06-16 14:10:14 +02:00
# initializeBaseUbuntuImage.sh args (provider, infraversion path) are only to support installation of pre 5.3 Cloudrons
if ! /bin/bash " ${ box_src_tmp_dir } /baseimage/initializeBaseUbuntuImage.sh " "generic" "../src" & >> " ${ LOG_FILE } " ; then
2017-01-03 14:48:37 -08:00
echo " Init script failed. See ${ LOG_FILE } for details "
exit 1
fi
2017-01-19 09:58:31 -08:00
echo ""
2017-01-03 14:48:37 -08:00
fi
2020-06-25 11:07:49 -07:00
# The provider flag is still used for marketplace images
2017-01-03 14:48:37 -08:00
echo " => Installing version ${ version } (this takes some time) ... "
2019-04-25 19:38:24 -07:00
mkdir -p /etc/cloudron
2019-07-26 08:44:37 -07:00
echo " ${ provider } " > /etc/cloudron/PROVIDER
2020-12-21 22:36:10 -08:00
[ [ ! -z " ${ setupToken } " ] ] && echo " ${ setupToken } " > /etc/cloudron/SETUP_TOKEN
2018-10-26 09:49:52 -07:00
2019-04-25 19:38:24 -07:00
if ! /bin/bash " ${ box_src_tmp_dir } /scripts/installer.sh " & >> " ${ LOG_FILE } " ; then
echo " Failed to install cloudron. See ${ LOG_FILE } for details "
exit 1
2016-10-25 12:47:51 -07:00
fi
2016-10-25 11:27:58 +02:00
2019-07-26 22:35:44 -07:00
mysql -uroot -ppassword -e " REPLACE INTO box.settings (name, value) VALUES ('api_server_origin', ' ${ apiServerOrigin } '); " 2>/dev/null
mysql -uroot -ppassword -e " REPLACE INTO box.settings (name, value) VALUES ('web_server_origin', ' ${ webServerOrigin } '); " 2>/dev/null
2019-07-26 10:49:29 -07:00
2016-12-30 11:35:03 +01:00
echo -n "=> Waiting for cloudron to be ready (this takes some time) ..."
2016-10-24 14:52:30 -07:00
while true; do
echo -n "."
2020-06-05 13:50:40 -07:00
if status = $( $curl -s -f "http://localhost:3000/api/v1/cloudron/status" 2>/dev/null) ; then
2018-01-30 19:08:06 -08:00
break # we are up and running
2016-10-24 14:52:30 -07:00
fi
sleep 10
done
2020-06-05 13:50:40 -07:00
if ! ip = $( curl -s --fail --connect-timeout 2 --max-time 2 https://api.cloudron.io/api/v1/helper/public_ip | sed -n -e 's/.*"ip": "\(.*\)"/\1/p' ) ; then
2020-03-09 14:33:48 -07:00
ip = '<IP>'
fi
2020-12-21 22:36:10 -08:00
if [ [ -z " ${ setupToken } " ] ] ; then
url = " https:// ${ ip } "
else
url = " https:// ${ ip } /?setupToken= ${ setupToken } "
fi
echo -e " \n\n ${ GREEN } After reboot, visit ${ url } and accept the self-signed certificate to finish setup. ${ DONE } \n "
2017-01-05 20:18:57 -08:00
2017-02-07 12:42:32 +01:00
if [ [ " ${ rebootServer } " = = "true" ] ] ; then
2018-12-07 10:41:13 -08:00
systemctl stop box mysql # sometimes mysql ends up having corrupt privilege tables
2018-12-14 15:01:28 -08:00
read -p "The server has to be rebooted to apply all the settings. Reboot now ? [Y/n] " yn
2018-12-07 10:41:13 -08:00
yn = ${ yn :- y }
case $yn in
2021-03-02 13:01:19 -08:00
[ Yy] * ) exitHandler; systemctl reboot; ;
2018-12-07 10:41:13 -08:00
* ) exit; ;
esac
2017-02-07 12:42:32 +01:00
fi