vm/lib.sh

1103 строки
38 KiB
Bash
Исходник Обычный вид История

2017-04-09 23:43:30 +03:00
#!/bin/bash
# shellcheck disable=2034,2059
true
# see https://github.com/koalaman/shellcheck/wiki/Directive
## variables
# Dirs
SCRIPTS=/var/scripts
NCPATH=/var/www/nextcloud
HTML=/var/www
2018-05-06 19:08:18 +03:00
NCDATA=/mnt/ncdata
2017-04-09 23:43:30 +03:00
SNAPDIR=/var/snap/spreedme
GPGDIR=/tmp/gpg
BACKUP=/mnt/NCBACKUP
2018-08-05 18:20:42 +03:00
RORDIR=/opt/es/
2019-03-17 15:22:19 +03:00
NC_APPS_PATH=$NCPATH/apps
2018-08-05 18:20:42 +03:00
2017-04-09 23:43:30 +03:00
# Ubuntu OS
DISTRO=$(lsb_release -sd | cut -d ' ' -f 2)
# Network
[ ! -z "$FIRST_IFACE" ] && IFACE=$(lshw -c network | grep "logical name" | awk '{print $3; exit}')
IFACE2=$(ip -o link show | awk '{print $2,$9}' | grep 'UP' | cut -d ':' -f 1)
[ ! -z "$CHECK_CURRENT_REPO" ] && REPO=$(apt-get update | grep -m 1 Hit | awk '{ print $2}')
ADDRESS=$(hostname -I | cut -d ' ' -f 1)
# WANIP4=$(dig +short myip.opendns.com @resolver1.opendns.com) # as an alternative
2019-01-17 15:24:55 +03:00
WANIP4=$(curl -s -k -m 5 https://ipv4bot.whatismyipaddress.com)
2019-01-04 20:21:03 +03:00
[ ! -z "$LOAD_IP6" ] && WANIP6=$(curl -s -k -m 5 https://ipv6bot.whatismyipaddress.com)
2018-05-06 19:08:18 +03:00
INTERFACES="/etc/netplan/01-netcfg.yaml"
2018-09-05 10:39:12 +03:00
GATEWAY=$(ip route | grep default | awk '{print $3}')
2018-05-06 19:08:18 +03:00
DNS1="9.9.9.9"
DNS2="149.112.112.112"
2017-04-09 23:43:30 +03:00
# Repo
2018-08-09 14:25:11 +03:00
GITHUB_REPO="https://raw.githubusercontent.com/nextcloud/vm/master"
2017-04-09 23:43:30 +03:00
STATIC="$GITHUB_REPO/static"
LETS_ENC="$GITHUB_REPO/lets-encrypt"
APP="$GITHUB_REPO/apps"
NCREPO="https://download.nextcloud.com/server/releases"
ISSUES="https://github.com/nextcloud/vm/issues"
# User information
NCPASS=nextcloud
NCUSER=ncadmin
UNIXUSER=$SUDO_USER
2017-04-09 23:43:30 +03:00
UNIXUSER_PROFILE="/home/$UNIXUSER/.bash_profile"
ROOT_PROFILE="/root/.bash_profile"
2018-05-06 19:08:18 +03:00
# Database
2017-04-09 23:43:30 +03:00
SHUF=$(shuf -i 25-29 -n 1)
2017-06-20 22:52:25 +03:00
MARIADB_PASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
NEWMARIADBPASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
[ ! -z "$NCDB" ] && NCCONFIGDB=$(grep "dbname" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g")
ETCMYCNF=/etc/mysql/my.cnf
2017-05-22 20:15:04 +03:00
MYCNF=/root/.my.cnf
2017-06-20 22:52:25 +03:00
[ ! -z "$MYCNFPW" ] && MARIADBMYCNFPASS=$(grep "password" $MYCNF | sed -n "/password/s/^password='\(.*\)'$/\1/p")
2018-05-06 19:08:18 +03:00
PGDB_PASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
NEWPGPASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
[ ! -z "$NCDB" ] && NCCONFIGDB=$(grep "dbname" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g")
[ ! -z "$NCDBPASS" ] && NCCONFIGDBPASS=$(grep "dbpassword" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g")
2017-04-09 23:43:30 +03:00
# Path to specific files
SECURE="$SCRIPTS/setup_secure_permissions_nextcloud.sh"
SSL_CONF="/etc/apache2/sites-available/nextcloud_ssl_domain_self_signed.conf"
HTTP_CONF="/etc/apache2/sites-available/nextcloud_http_domain_self_signed.conf"
# Nextcloud version
2018-03-02 01:25:11 +03:00
[ ! -z "$NC_UPDATE" ] && CURRENTVERSION=$(sudo -u www-data php $NCPATH/occ status | grep "versionstring" | awk '{print $3}')
2019-05-23 21:14:20 +03:00
[ ! -z "$NC_UPDATE" ] && NCVERSION=$(curl -s -m 900 $NCREPO/ | sed --silent 's/.*href="nextcloud-\([^"]\+\).zip.asc".*/\1/p' | sort --version-sort | tail -1)
[ ! -z "$NC_UPDATE" ] && STABLEVERSION="nextcloud-$NCVERSION"
[ ! -z "$NC_UPDATE" ] && NCMAJOR="${NCVERSION%%.*}"
[ ! -z "$NC_UPDATE" ] && NCBAD=$((NCMAJOR-2))
2017-04-09 23:43:30 +03:00
# Keys
OpenPGP_fingerprint='28806A878AE423A28372792ED75899B9A724937A'
2017-06-01 20:36:16 +03:00
# OnlyOffice URL
2018-10-19 10:44:32 +03:00
[ ! -z "$OO_INSTALL" ] && SUBDOMAIN=$(whiptail --title "T&M Hansson IT OnlyOffice" --inputbox "OnlyOffice subdomain eg: office.yourdomain.com" "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3)
2017-06-01 20:36:16 +03:00
# Nextcloud Main Domain
2018-10-19 10:44:32 +03:00
[ ! -z "$OO_INSTALL" ] && NCDOMAIN=$(whiptail --title "T&M Hansson IT OnlyOffice" --inputbox "Nextcloud url, make sure it looks like this: cloud\\.yourdomain\\.com" "$WT_HEIGHT" "$WT_WIDTH" cloud\\.yourdomain\\.com 3>&1 1>&2 2>&3)
2017-04-09 23:43:30 +03:00
# Collabora Docker URL
2018-10-19 10:44:32 +03:00
[ ! -z "$COLLABORA_INSTALL" ] && SUBDOMAIN=$(whiptail --title "T&M Hansson IT Collabora" --inputbox "Collabora subdomain eg: office.yourdomain.com" "$WT_HEIGHT" "$WT_WIDTH" 3>&1 1>&2 2>&3)
2017-04-09 23:43:30 +03:00
# Nextcloud Main Domain
2018-10-19 10:44:32 +03:00
[ ! -z "$COLLABORA_INSTALL" ] && NCDOMAIN=$(whiptail --title "T&M Hansson IT Collabora" --inputbox "Nextcloud url, make sure it looks like this: cloud\\.yourdomain\\.com" "$WT_HEIGHT" "$WT_WIDTH" cloud\\.yourdomain\\.com 3>&1 1>&2 2>&3)
2017-04-09 23:43:30 +03:00
# Letsencrypt
LETSENCRYPTPATH="/etc/letsencrypt"
CERTFILES="$LETSENCRYPTPATH/live"
DHPARAMS="$CERTFILES/$SUBDOMAIN/dhparam.pem"
# Collabora App
HTTPS_CONF="/etc/apache2/sites-available/$SUBDOMAIN.conf"
2018-05-06 19:08:18 +03:00
HTTP2_CONF="/etc/apache2/mods-available/http2.conf"
2018-08-09 02:23:48 +03:00
# PHP-FPM
2019-05-17 13:51:36 +03:00
PHPVER=7.2
PHP_INI=/etc/php/$PHPVER/fpm/php.ini
PHP_POOL_DIR=/etc/php/$PHPVER/fpm/pool.d
2018-08-06 21:33:39 +03:00
# Adminer
ADMINERDIR=/usr/share/adminer
ADMINER_CONF=/etc/apache2/conf-available/adminer.conf
2017-04-09 23:43:30 +03:00
# Redis
REDIS_CONF=/etc/redis/redis.conf
2018-05-06 19:08:18 +03:00
REDIS_SOCK=/var/run/redis/redis-server.sock
2017-04-09 23:43:30 +03:00
RSHUF=$(shuf -i 30-35 -n 1)
REDIS_PASS=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$RSHUF" | head -n 1)
# Extra security
SPAMHAUS=/etc/spamhaus.wl
ENVASIVE=/etc/apache2/mods-available/mod-evasive.load
APACHE2=/etc/apache2/apache2.conf
2018-03-02 00:18:53 +03:00
# Full text Search
2018-09-29 00:14:01 +03:00
[ ! -z "$ES_INSTALL" ] && INDEX_USER=$(tr -dc '[:lower:]' < /dev/urandom | fold -w "$SHUF" | head -n 1)
[ ! -z "$ES_INSTALL" ] && ROREST=$(tr -dc "A-Za-z0-9" < /dev/urandom | fold -w "$SHUF" | head -n 1)
2018-08-05 18:20:42 +03:00
[ ! -z "$ES_INSTALL" ] && DOCKER_INS=$(dpkg -l | grep ^ii | awk '{print $2}' | grep docker)
2018-09-03 13:59:00 +03:00
[ ! -z "$ES_INSTALL" ] && nc_fts="ark74/nc_fts"
2018-08-06 22:59:50 +03:00
[ ! -z "$ES_INSTALL" ] && fts_es_name="fts_esror"
# Talk
[ ! -z "$TURN_INSTALL" ] && TURN_CONF="/etc/turnserver.conf"
2018-07-08 19:01:52 +03:00
[ ! -z "$TURN_INSTALL" ] && TURN_PORT=5349
[ ! -z "$TURN_INSTALL" ] && SHUF=$(shuf -i 25-29 -n 1)
[ ! -z "$TURN_INSTALL" ] && TURN_SECRET=$(tr -dc "a-zA-Z0-9@#*=" < /dev/urandom | fold -w "$SHUF" | head -n 1)
[ ! -z "$TURN_INSTALL" ] && TURN_DOMAIN=$(sudo -u www-data /var/www/nextcloud/occ config:system:get overwrite.cli.url | sed 's#https://##;s#/##')
2018-12-02 23:14:32 +03:00
# Migrate Docker to overlay2
[ ! -z "$DOCKEROVERLAY2" ] && DOCKERBACKUP=$(grep "datadir" $NCPATH/config/config.php | awk '{print $3}' | sed "s/[',]//g")/DOCKERBACKUP
2017-04-09 23:43:30 +03:00
## functions
# If script is running as root?
#
# Example:
# if is_root
# then
# # do stuff
# else
# print_text_in_color "$Red" "You are not root..."
2017-04-09 23:43:30 +03:00
# exit 1
# fi
#
is_root() {
if [[ "$EUID" -ne 0 ]]
then
return 1
else
return 0
fi
}
# Check if root
root_check() {
if ! is_root
then
2017-12-06 00:02:51 +03:00
msg_box "Sorry, you are not root. You now have two options:
2017-12-05 23:36:37 +03:00
2017-12-06 00:02:51 +03:00
1. With SUDO directly:
a) :~$ sudo bash $SCRIPTS/name-of-script.sh
2. Become ROOT and then type your command:
a) :~$ sudo -i
b) :~# $SCRIPTS/name-of-script.sh
2017-12-06 00:05:36 +03:00
In both cases above you can leave out $SCRIPTS/ if the script
2017-12-06 00:02:51 +03:00
is directly in your PATH.
2017-12-06 00:05:36 +03:00
More information can be found here: https://unix.stackexchange.com/a/3064"
exit 1
fi
}
2017-04-09 23:43:30 +03:00
debug_mode() {
if [ "$DEBUG" -eq 1 ]
then
set -ex
fi
}
ask_yes_or_no() {
read -r -p "$1 ([y]es or [N]o): "
case ${REPLY,,} in
y|yes)
echo "yes"
;;
*)
echo "no"
;;
esac
}
msg_box() {
local PROMPT="$1"
whiptail --msgbox "${PROMPT}" "$WT_HEIGHT" "$WT_WIDTH"
}
2017-08-20 23:36:37 +03:00
# Check if process is runnnig: is_process_running dpkg
is_process_running() {
PROCESS="$1"
while :
do
RESULT=$(pgrep "${PROCESS}")
if [ "${RESULT:-null}" = null ]; then
break
else
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "${PROCESS} is running. Waiting for it to stop..."
2017-08-20 23:36:37 +03:00
sleep 10
fi
done
}
# Checks if site is reachable with a HTTP 200 status
site_200() {
2019-04-26 13:37:43 +03:00
print_text_in_color "$ICyan" "Checking connection..."
2019-04-11 22:02:05 +03:00
CURL_STATUS="$(curl -sSL -w "%{http_code}" "${1}" | tail -1)"
if [[ "$CURL_STATUS" = "200" ]]
then
return 0
else
print_text_in_color "$IRed" "curl didn't produce a 200 status, is the site reachable?"
return 1
fi
}
2019-05-10 16:23:23 +03:00
# Do a DNS lookup and compare the WAN address with the A record
2019-04-11 22:02:05 +03:00
domain_check_200() {
print_text_in_color "$ICyan" "Doing a DNS lookup for ${1}..."
install_if_not dnsutils
2019-05-10 17:09:06 +03:00
# Try to resolve the domain with nslookup using $DNS as resolver
2019-04-14 16:11:47 +03:00
if nslookup "${1}" $DNS1 >/dev/null 2>&1
then
2019-05-10 16:23:23 +03:00
print_text_in_color "$IGreen" "DNS seems correct when checking with nslookup!"
else
2019-05-10 17:09:06 +03:00
print_text_in_color "$IRed" "DNS lookup failed with nslookup."
print_text_in_color "$IRed" "Please check your DNS settings! Maybe the domain isn't propagated?"
nslookup "${1}" $DNS1
return 1
fi
2019-05-10 17:09:06 +03:00
# Is the DNS record same as the external IP address of the server?
if [[ "$(dig +short "${1}" @resolver1.opendns.com)" = "$WANIP4" ]]
2019-05-10 16:23:23 +03:00
then
print_text_in_color "$IGreen" "DNS seems correct when checking with dig!"
else
2019-05-10 17:09:06 +03:00
print_text_in_color "$IRed" "DNS lookup failed with dig. The external IP ($WANIP4) address of this server is not the same as the A-record."
print_text_in_color "$IRed" "Please check your DNS settings! Maybe the domain isn't propagated?"
return 1
fi
}
# A function to fetch a file with curl to a directory
# 1 = https://example.com
# 2 = name of file
# 3 = directory that the file should end up in
curl_to_dir() {
2019-04-10 23:22:53 +03:00
if [ ! -d "$3" ]
then
mkdir -p "$3"
fi
2019-04-11 23:23:28 +03:00
curl -sfL "$1"/"$2" -o "$3"/"$2"
}
2019-01-24 00:38:38 +03:00
start_if_stopped() {
if ! pgrep "$1"
then
print_text_in_color "$ICyan" "Starting $1..."
check_command service "$1" start
fi
}
2018-08-09 02:23:48 +03:00
# Warn user that HTTP/2 will be disabled if installing app that use Apache2 PHP instead of PHP-FPM
# E.g: http2_warn Modsecurity
http2_warn() {
msg_box "This VM has HTTP/2 enabled by default.
If you continue with installing $1, HTTP/2 will be disabled since it's not compatible with the mpm module used by $1.
This is what Apache will say in the error.log if you enable $1 anyway:
'The mpm module (prefork.c) is not supported by mod_http2.
2018-08-09 02:23:48 +03:00
The mpm determines how things are processed in your server.
HTTP/2 has more demands in this regard and the currently selected mpm will just not do.
This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.'"
2018-08-09 02:23:48 +03:00
if [[ "no" == $(ask_yes_or_no "Do you really want to enable $1 anyway?") ]]
then
exit 1
fi
}
caulculate_php_fpm() {
# Minimum amount of max children (lower than this won't work with 2 GB RAM)
min_max_children=8
# If start servers are lower than this then it's likely that there are room for max_spare_servers
min_start_servers=20
# Maximum amount of children is only set if the min_start_servers value are met
min_max_spare_servers=35
# Calculate the sum of the current values
CURRENT_START="$(grep pm.start_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')"
CURRENT_MAX="$(grep pm.max_spare_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')"
CURRENT_MIN="$(grep pm.min_spare_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')"
CURRENT_SUM="$((CURRENT_START + CURRENT_MAX + CURRENT_MIN))"
# Calculate max_children depending on RAM
# Tends to be between 30-50MB per children
average_php_memory_requirement=50
available_memory=$(awk '/MemAvailable/ {printf "%d", $2/1024}' /proc/meminfo)
PHP_FPM_MAX_CHILDREN=$((available_memory/average_php_memory_requirement))
# Lowest possible value is 8
print_text_in_color "$ICyan" "Automatically configures pm.max_children for php-fpm..."
if [ $PHP_FPM_MAX_CHILDREN -lt $min_max_children ]
then
msg_box "The current max_children value available to set is $PHP_FPM_MAX_CHILDREN, and with that value PHP-FPM won't function properly.
The minimum value is 8, and the value is calculated depening on how much RAM you have left to use in the system.
The absolute minimum amount of RAM required to run the VM is 2 GB, but we recomend 4 GB.
You now have two choices:
1. Import this VM again, raise the amount of RAM with at least 1 GB, and then run this script again,
installing it in the same way as you did before.
2. Import this VM again without raising the RAM, but don't install any of the following apps:
1) Collabora
2) OnlyOffice
3) Full Text Search
This script will now exit.
The installation was not successful, sorry for the inconvenience.
If you think this is a bug, please report it to $ISSUES"
exit 1
else
check_command sed -i "s|pm.max_children.*|pm.max_children = $PHP_FPM_MAX_CHILDREN|g" $PHP_POOL_DIR/nextcloud.conf
restart_webserver
print_text_in_color "$IGreen" "pm.max_children was set to $PHP_FPM_MAX_CHILDREN"
# Check if the sum of all the current values are more than $PHP_FPM_MAX_CHILDREN and only continue it is
if [ $PHP_FPM_MAX_CHILDREN -gt $CURRENT_SUM ]
then
# Set pm.max_spare_servers
if [ $PHP_FPM_MAX_CHILDREN -ge $min_max_spare_servers ]
then
if [ "$(grep pm.start_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')" -lt $min_start_servers ]
then
check_command sed -i "s|pm.max_spare_servers.*|pm.max_spare_servers = $((PHP_FPM_MAX_CHILDREN - 30))|g" $PHP_POOL_DIR/nextcloud.conf
restart_webserver
print_text_in_color "$IGreen" "pm.max_spare_servers was set to $((PHP_FPM_MAX_CHILDREN - 30))"
fi
fi
fi
fi
# If $PHP_FPM_MAX_CHILDREN is lower than the current sum of all values, revert to default settings
if [ $PHP_FPM_MAX_CHILDREN -lt $CURRENT_SUM ]
then
check_command sed -i "s|pm.max_children.*|pm.max_children = $PHP_FPM_MAX_CHILDREN|g" $PHP_POOL_DIR/nextcloud.conf
check_command sed -i "s|pm.start_servers.*|pm.start_servers = 3|g" $PHP_POOL_DIR/nextcloud.conf
check_command sed -i "s|pm.min_spare_servers.*|pm.min_spare_servers = 2|g" $PHP_POOL_DIR/nextcloud.conf
check_command sed -i "s|pm.max_spare_servers.*|pm.max_spare_servers = 3|g" $PHP_POOL_DIR/nextcloud.conf
print_text_in_color "$ICyan" "All PHP-INI values were set back to default values as the value for pm.max_children ($PHP_FPM_MAX_CHILDREN) was lower than the sum of all the current values ($CURRENT_SUM)"
print_text_in_color "$ICyan" "Please run this again to set optimal values"
restart_webserver
fi
}
calculate_php_fpm() {
# Minimum amount of max children (lower than this won't work with 2 GB RAM)
min_max_children=8
# If start servers are lower than this then it's likely that there are room for max_spare_servers
min_start_servers=20
# Maximum amount of children is only set if the min_start_servers value are met
min_max_spare_servers=35
# Calculate the sum of the current values
CURRENT_START="$(grep pm.start_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')"
CURRENT_MAX="$(grep pm.max_spare_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')"
CURRENT_MIN="$(grep pm.min_spare_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')"
CURRENT_SUM="$((CURRENT_START + CURRENT_MAX + CURRENT_MIN))"
2018-08-09 02:23:48 +03:00
# Calculate max_children depending on RAM
# Tends to be between 30-50MB per children
2018-08-09 02:23:48 +03:00
average_php_memory_requirement=50
available_memory=$(awk '/MemAvailable/ {printf "%d", $2/1024}' /proc/meminfo)
PHP_FPM_MAX_CHILDREN=$((available_memory/average_php_memory_requirement))
2018-08-09 02:23:48 +03:00
# Lowest possible value is 8
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Automatically configures pm.max_children for php-fpm..."
if [ $PHP_FPM_MAX_CHILDREN -lt $min_max_children ]
2018-08-09 02:23:48 +03:00
then
msg_box "The current max_children value available to set is $PHP_FPM_MAX_CHILDREN, and with that value PHP-FPM won't function properly.
The minimum value is 8, and the value is calculated depening on how much RAM you have left to use in the system.
The absolute minimum amount of RAM required to run the VM is 2 GB, but we recomend 4 GB.
You now have two choices:
1. Import this VM again, raise the amount of RAM with at least 1 GB, and then run this script again,
installing it in the same way as you did before.
2. Import this VM again without raising the RAM, but don't install any of the following apps:
1) Collabora
2) OnlyOffice
3) Full Text Search
This script will now exit.
The installation was not successful, sorry for the inconvenience.
If you think this is a bug, please report it to $ISSUES"
exit 1
else
check_command sed -i "s|pm.max_children.*|pm.max_children = $PHP_FPM_MAX_CHILDREN|g" $PHP_POOL_DIR/nextcloud.conf
restart_webserver
2018-12-18 01:27:01 +03:00
print_text_in_color "$IGreen" "pm.max_children was set to $PHP_FPM_MAX_CHILDREN"
# Check if the sum of all the current values are more than $PHP_FPM_MAX_CHILDREN and only continue it is
if [ $PHP_FPM_MAX_CHILDREN -gt $CURRENT_SUM ]
then
# Set pm.max_spare_servers
if [ $PHP_FPM_MAX_CHILDREN -ge $min_max_spare_servers ]
then
if [ "$(grep pm.start_servers $PHP_POOL_DIR/nextcloud.conf | awk '{ print $3}')" -lt $min_start_servers ]
then
check_command sed -i "s|pm.max_spare_servers.*|pm.max_spare_servers = $((PHP_FPM_MAX_CHILDREN - 30))|g" $PHP_POOL_DIR/nextcloud.conf
restart_webserver
print_text_in_color "$IGreen" "pm.max_spare_servers was set to $((PHP_FPM_MAX_CHILDREN - 30))"
fi
fi
fi
fi
# If $PHP_FPM_MAX_CHILDREN is lower than the current sum of all values, revert to default settings
if [ $PHP_FPM_MAX_CHILDREN -lt $CURRENT_SUM ]
then
check_command sed -i "s|pm.max_children.*|pm.max_children = $PHP_FPM_MAX_CHILDREN|g" $PHP_POOL_DIR/nextcloud.conf
check_command sed -i "s|pm.start_servers.*|pm.start_servers = 3|g" $PHP_POOL_DIR/nextcloud.conf
check_command sed -i "s|pm.min_spare_servers.*|pm.min_spare_servers = 2|g" $PHP_POOL_DIR/nextcloud.conf
check_command sed -i "s|pm.max_spare_servers.*|pm.max_spare_servers = 3|g" $PHP_POOL_DIR/nextcloud.conf
print_text_in_color "$ICyan" "All PHP-INI values were set back to default values as the value for pm.max_children ($PHP_FPM_MAX_CHILDREN) was lower than the sum of all the current values ($CURRENT_SUM)"
print_text_in_color "$ICyan" "Please run this again to set optimal values"
restart_webserver
2018-08-09 02:23:48 +03:00
fi
}
2018-05-06 19:08:18 +03:00
test_connection() {
# Install dnsutils if not existing
2019-01-17 15:43:00 +03:00
if ! dpkg-query -W -f='${Status}' "dnsutils" | grep -q "ok installed"
then
2019-01-17 15:43:00 +03:00
apt update -q4 & spinner_loading && apt install dnsutils -y
fi
# Install network-manager if not existing
2019-01-17 15:43:00 +03:00
if ! dpkg-query -W -f='${Status}' "network-manager" | grep -q "ok installed"
then
2019-01-17 15:43:00 +03:00
apt update -q4 & spinner_loading && apt install network-manager -y
fi
2018-05-06 19:08:18 +03:00
check_command service network-manager restart
ip link set "$IFACE" down
2018-05-30 22:39:40 +03:00
sleep 2
2018-05-06 19:08:18 +03:00
ip link set "$IFACE" up
2018-05-30 22:39:40 +03:00
sleep 2
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Checking connection..."
2018-05-22 23:15:18 +03:00
check_command service network-manager restart
sleep 2
if nslookup github.com
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$IGreen" "Online!"
2018-05-22 23:15:18 +03:00
elif ! nslookup github.com
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Trying to restart networking service..."
2018-05-30 22:39:40 +03:00
check_command service networking restart && sleep 2
2018-05-22 23:15:18 +03:00
if nslookup github.com
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$IGreen" "Online!"
2018-05-22 23:15:18 +03:00
fi
else
if ! nslookup github.com
then
2018-05-06 19:08:18 +03:00
msg_box "Network NOT OK. You must have a working network connection to run this script
If you think that this is a bug, please report it to https://github.com/nextcloud/vm/issues."
exit 1
2018-05-22 23:15:18 +03:00
fi
2018-05-06 19:08:18 +03:00
fi
}
2018-08-09 02:23:48 +03:00
restart_webserver() {
check_command systemctl restart apache2
2019-05-17 13:51:36 +03:00
if which php"$PHPVER"-fpm > /dev/null
then
2019-05-17 13:51:36 +03:00
check_command systemctl restart php"$PHPVER"-fpm.service
fi
2018-08-09 02:23:48 +03:00
}
2017-07-23 00:28:35 +03:00
# Install certbot (Let's Encrypt)
install_certbot() {
certbot --version 2> /dev/null
LE_IS_AVAILABLE=$?
if [ $LE_IS_AVAILABLE -eq 0 ]
then
2018-09-28 17:36:57 +03:00
certbot --version 2> /dev/null
2017-07-23 00:28:35 +03:00
else
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Installing certbot (Let's Encrypt)..."
2017-07-23 00:28:35 +03:00
apt update -q4 & spinner_loading
2018-09-28 17:36:57 +03:00
install_if_not software-properties-common
2017-07-23 00:28:35 +03:00
add-apt-repository ppa:certbot/certbot -y
apt update -q4 & spinner_loading
2018-09-28 17:36:57 +03:00
install_if_not certbot
2017-07-23 00:28:35 +03:00
apt update -q4 & spinner_loading
apt dist-upgrade -y
fi
}
2017-08-31 08:24:08 +03:00
# Let's Encrypt for subdomains
le_subdomain() {
a2dissite 000-default.conf
service apache2 reload
certbot certonly --standalone --pre-hook "service apache2 stop" --post-hook "service apache2 start" --agree-tos --rsa-key-size 4096 -d "$SUBDOMAIN"
}
2018-05-06 19:08:18 +03:00
# Check if port is open # check_open_port 443 domain.example.com
2017-10-04 21:23:13 +03:00
check_open_port() {
# Check to see if user already has nmap installed on their system
if [ "$(dpkg-query -s nmap 2> /dev/null | grep -c "ok installed")" == "1" ]
then
NMAPSTATUS=preinstalled
fi
apt update -q4 & spinner_loading
if [ "$NMAPSTATUS" = "preinstalled" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "nmap is already installed..."
2017-10-04 21:23:13 +03:00
else
apt install nmap -y
fi
# Check if $1 is open using nmap, if not notify the user
if [ "$(nmap -sS -p "$1" "$WANIP4" | grep -c "open")" == "1" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "${IGreen}" "Port $1 is open on $WANIP4!"
2017-10-04 21:23:13 +03:00
if [ "$NMAPSTATUS" = "preinstalled" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "nmap was previously installed, not removing."
2017-10-04 21:23:13 +03:00
else
apt remove --purge nmap -y
fi
else
2017-10-04 21:43:23 +03:00
whiptail --msgbox "Port $1 is not open on $WANIP4. We will do a second try on $2 instead." "$WT_HEIGHT" "$WT_WIDTH"
2017-10-04 21:23:13 +03:00
if [[ "$(nmap -sS -PN -p "$1" "$2" | grep -m 1 "open" | awk '{print $2}')" = "open" ]]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "${IGreen}" "Port $1 is open on $2!"
2017-10-04 21:23:13 +03:00
if [ "$NMAPSTATUS" = "preinstalled" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "nmap was previously installed, not removing."
2017-10-04 21:23:13 +03:00
else
apt remove --purge nmap -y
fi
else
whiptail --msgbox "Port $1 is not open on $2. Please follow this guide to open ports in your router: https://www.techandme.se/open-port-80-443/" "$WT_HEIGHT" "$WT_WIDTH"
any_key "Press any key to exit... "
if [ "$NMAPSTATUS" = "preinstalled" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "nmap was previously installed, not removing."
2017-10-04 21:23:13 +03:00
else
apt remove --purge nmap -y
fi
exit 1
fi
fi
}
2018-05-06 19:08:18 +03:00
check_distro_version() {
# Check Ubuntu version
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Checking server OS and version..."
2018-11-16 22:31:55 +03:00
if lsb_release -c | grep -ic "bionic" &> /dev/null
then
OS=1
elif lsb_release -i | grep -ic "Ubuntu" &> /dev/null
then
OS=1
elif uname -a | grep -ic "bionic" &> /dev/null
2018-07-09 16:23:46 +03:00
then
OS=1
2018-08-09 02:23:48 +03:00
elif uname -v | grep -ic "Ubuntu" &> /dev/null
2018-07-09 16:23:46 +03:00
then
OS=1
fi
2018-05-06 19:08:18 +03:00
if [ "$OS" != 1 ]
then
msg_box "Ubuntu Server is required to run this script.
Please install that distro and try again.
You can find the download link here: https://www.ubuntu.com/download/server"
exit 1
fi
if ! version 18.04 "$DISTRO" 18.04.4; then
msg_box "Ubuntu version $DISTRO must be between 18.04 - 18.04.4"
exit 1
fi
}
2017-06-11 20:26:36 +03:00
configure_max_upload() {
2018-08-09 02:23:48 +03:00
# Increase max filesize (expects that changes are made in $PHP_INI)
2017-06-11 20:26:36 +03:00
# Here is a guide: https://www.techandme.se/increase-max-file-size/
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Setting max_upload size in PHP..."
2018-08-09 02:23:48 +03:00
# Copy settings from .htaccess to user.ini. beacuse we run php-fpm. Documented here: https://docs.nextcloud.com/server/13/admin_manual/installation/source_installation.html#php-fpm-configuration-notes
cp -fv "$NCPATH/.htaccess" "$NCPATH/.user.ini"
# Do the acutal change
sed -i 's/ php_value upload_max_filesize.*/# php_value upload_max_filesize 511M/g' "$NCPATH"/.user.ini
sed -i 's/ php_value post_max_size.*/# php_value post_max_size 511M/g' "$NCPATH"/.user.ini
sed -i 's/ php_value memory_limit.*/# php_value memory_limit 512M/g' "$NCPATH"/.user.ini
2017-06-11 20:26:36 +03:00
}
2017-06-23 16:09:40 +03:00
# Check if program is installed (is_this_installed apache2)
is_this_installed() {
if [ "$(dpkg-query -W -f='${Status}' "${1}" 2>/dev/null | grep -c "ok installed")" == "1" ]
then
print_text_in_color "$Red" "${1} is installed, it must be a clean server."
2017-06-23 16:09:40 +03:00
exit 1
fi
}
# Install_if_not program
2019-01-17 15:21:23 +03:00
install_if_not() {
if ! dpkg-query -W -f='${Status}' "${1}" | grep -q "ok installed"
then
apt update -q4 & spinner_loading && apt install "${1}" -y
fi
}
2018-12-18 01:27:01 +03:00
# Test RAM size
# Call it like this: ram_check [amount of min RAM in GB] [for which program]
# Example: ram_check 2 Nextcloud
ram_check() {
mem_available="$(awk '/MemTotal/{print $2}' /proc/meminfo)"
if [ "${mem_available}" -lt "$((${1}*1002400))" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "${Red}" "Error: ${1} GB RAM required to install ${2}!" >&2
print_text_in_color "${Red}" "Current RAM is: ("$((mem_available/1002400))" GB)" >&2
sleep 3
2018-02-06 19:09:44 +03:00
msg_box "If you want to bypass this check you could do so by commenting out (# before the line) 'ram_check X' in the script that you are trying to run.
2018-12-18 01:27:01 +03:00
In nextcloud_install_production.sh you can find the check somewhere around line #34.
2018-02-06 19:09:44 +03:00
Please notice that things may be veery slow and not work as expeced. YOU HAVE BEEN WARNED!"
exit 1
else
2018-12-18 01:27:01 +03:00
print_text_in_color "${IGreen}" "RAM for ${2} OK! ($((mem_available/1002400)) GB)"
fi
}
# Test number of CPU
# Call it like this: cpu_check [amount of min CPU] [for which program]
# Example: cpu_check 2 Nextcloud
cpu_check() {
nr_cpu="$(nproc)"
if [ "${nr_cpu}" -lt "${1}" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "${Red}" "Error: ${1} CPU required to install ${2}!" >&2
print_text_in_color "${Red}" "Current CPU: ($((nr_cpu)))" >&2
sleep 3
exit 1
else
2018-12-18 01:27:01 +03:00
print_text_in_color "${IGreen}" "CPU for ${2} OK! ($((nr_cpu)))"
fi
}
2017-04-09 23:43:30 +03:00
check_command() {
2018-03-02 00:18:53 +03:00
if ! "$@";
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Sorry but something went wrong. Please report this issue to $ISSUES and include the output of the error message. Thank you!"
print_text_in_color "$Red" "$* failed"
2017-04-09 23:43:30 +03:00
exit 1
fi
}
2018-03-07 02:25:24 +03:00
# Example: occ_command 'maintenance:mode --on'
occ_command() {
check_command sudo -u www-data php "$NCPATH"/occ "$@";
}
2017-04-09 23:43:30 +03:00
network_ok() {
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Testing if network is OK..."
install_if_not network-manager
2018-06-09 16:00:26 +03:00
if ! service network-manager restart > /dev/null
then
service networking restart > /dev/null
fi
sleep 5 && site_200 github.com
2017-04-09 23:43:30 +03:00
}
# Whiptail auto-size
calc_wt_size() {
WT_HEIGHT=17
WT_WIDTH=$(tput cols)
if [ -z "$WT_WIDTH" ] || [ "$WT_WIDTH" -lt 60 ]; then
WT_WIDTH=80
fi
if [ "$WT_WIDTH" -gt 178 ]; then
WT_WIDTH=120
fi
WT_MENU_HEIGHT=$((WT_HEIGHT-7))
export WT_MENU_HEIGHT
}
2018-02-06 19:09:44 +03:00
install_and_enable_app() {
# Download and install $1
if [ ! -d "$NC_APPS_PATH/$1" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Installing $1..."
# occ_command not possible here because it uses check_command and will exit if occ_command fails
result=$(sudo -u www-data php ${NCPATH}/occ app:install "$1")
if [ "$result" = "Error: Could not download app $1" ]
then
msg_box "The $1 app could not be installed.
Probably it's not compatible with $(occ_command -V).
You can try to install the app manually after the script has finished,
or when a new version of the app is released with the following command:
'sudo -u www-data php ${NCPATH}/occ app:install $1'"
fi
2018-02-06 19:09:44 +03:00
fi
# Enable $1
if [ -d "$NC_APPS_PATH/$1" ]
then
2018-03-02 00:18:53 +03:00
occ_command app:enable "$1"
2018-02-06 19:09:44 +03:00
chown -R www-data:www-data "$NC_APPS_PATH"
fi
}
2017-04-09 23:43:30 +03:00
download_verify_nextcloud_stable() {
2019-04-30 18:36:11 +03:00
install_if_not gnupg
rm -f "$HTML/$STABLEVERSION.tar.bz2"
2019-05-23 21:14:20 +03:00
cd $HTML
curl -fSLO --retry 3 "$NCREPO/$STABLEVERSION.tar.bz2"
2017-04-09 23:43:30 +03:00
mkdir -p "$GPGDIR"
curl_to_dir "$NCREPO" "$STABLEVERSION.tar.bz2.asc" "$GPGDIR"
2017-04-09 23:43:30 +03:00
chmod -R 600 "$GPGDIR"
gpg --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys "$OpenPGP_fingerprint"
gpg --verify "$GPGDIR/$STABLEVERSION.tar.bz2.asc" "$HTML/$STABLEVERSION.tar.bz2"
rm -r "$GPGDIR"
2017-09-23 19:32:38 +03:00
rm -f releases
2017-04-09 23:43:30 +03:00
}
# Initial download of script in ../static
# call like: download_static_script name_of_script
download_static_script() {
# Get ${1} script
rm -f "${SCRIPTS}/${1}.sh" "${SCRIPTS}/${1}.php" "${SCRIPTS}/${1}.py"
if ! { curl_to_dir "${STATIC}" "${1}.sh" "$SCRIPTS" || curl_to_dir "${STATIC}" "${1}.php" "$SCRIPTS" || curl_to_dir "${STATIC}" "${1}.py" "$SCRIPTS"; }
2017-04-09 23:43:30 +03:00
then
print_text_in_color "$Red" "{$1} failed to download. Please run: 'sudo curl -sLO ${STATIC}/${1}.sh|.php|.py' again."
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "If you get this error when running the nextcloud-startup-script then just re-run it with:"
print_text_in_color "$ICyan" "'sudo bash $SCRIPTS/nextcloud-startup-script.sh' and all the scripts will be downloaded again"
2017-04-09 23:43:30 +03:00
exit 1
fi
}
# Initial download of script in ../lets-encrypt
# call like: download_le_script name_of_script
download_le_script() {
# Get ${1} script
rm -f "${SCRIPTS}/${1}.sh" "${SCRIPTS}/${1}.php" "${SCRIPTS}/${1}.py"
if ! { curl_to_dir "${LETS_ENC}" "${1}.sh" "$SCRIPTS" || curl_to_dir "${LETS_ENC}" "${1}.php" "$SCRIPTS" || curl_to_dir "${LETS_ENC}" "${1}.py" "$SCRIPTS"; }
2017-04-09 23:43:30 +03:00
then
print_text_in_color "$Red" "{$1} failed to download. Please run: 'sudo curl -sLO ${STATIC}/${1}.sh|.php|.py' again."
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "If you get this error when running the nextcloud-startup-script then just re-run it with:"
print_text_in_color "$ICyan" "'sudo bash $SCRIPTS/nextcloud-startup-script.sh' and all the scripts will be downloaded again"
2017-04-09 23:43:30 +03:00
exit 1
fi
}
# Run any script in ../master
# call like: run_main_script name_of_script
run_main_script() {
rm -f "${SCRIPTS}/${1}.sh" "${SCRIPTS}/${1}.php" "${SCRIPTS}/${1}.py"
if curl_to_dir "${GITHUB_REPO}" "${1}.sh" "$SCRIPTS"
then
bash "${SCRIPTS}/${1}.sh"
rm -f "${SCRIPTS}/${1}.sh"
elif curl_to_dir "${GITHUB_REPO}" "${1}.php" "$SCRIPTS"
then
php "${SCRIPTS}/${1}.php"
rm -f "${SCRIPTS}/${1}.php"
elif curl_to_dir "${GITHUB_REPO}" "${1}.py" "$SCRIPTS"
then
python "${SCRIPTS}/${1}.py"
rm -f "${SCRIPTS}/${1}.py"
else
print_text_in_color "$Red" "Downloading ${1} failed"
print_text_in_color "$ICyan" "Script failed to download. Please run: 'sudo curl -sLO ${GITHUB_REPO}/${1}.sh|php|py' again."
exit 1
fi
}
2017-04-09 23:43:30 +03:00
# Run any script in ../static
# call like: run_static_script name_of_script
run_static_script() {
# Get ${1} script
rm -f "${SCRIPTS}/${1}.sh" "${SCRIPTS}/${1}.php" "${SCRIPTS}/${1}.py"
if curl_to_dir "${STATIC}" "${1}.sh" "$SCRIPTS"
2017-04-09 23:43:30 +03:00
then
bash "${SCRIPTS}/${1}.sh"
rm -f "${SCRIPTS}/${1}.sh"
elif curl_to_dir "${STATIC}" "${1}.php" "$SCRIPTS"
2017-04-09 23:43:30 +03:00
then
php "${SCRIPTS}/${1}.php"
rm -f "${SCRIPTS}/${1}.php"
elif curl_to_dir "${STATIC}" "${1}.py" "$SCRIPTS"
2017-04-09 23:43:30 +03:00
then
python "${SCRIPTS}/${1}.py"
rm -f "${SCRIPTS}/${1}.py"
else
print_text_in_color "$Red" "Downloading ${1} failed"
print_text_in_color "$ICyan" "Script failed to download. Please run: 'sudo curl -sLO ${STATIC}/${1}.sh|php|py' again."
exit 1
2017-04-09 23:43:30 +03:00
fi
}
# Run any script in ../apps
2017-05-25 14:37:44 +03:00
# call like: run_app_script collabora|nextant|passman|spreedme|contacts|calendar|webmin|previewgenerator
2017-04-09 23:43:30 +03:00
run_app_script() {
rm -f "${SCRIPTS}/${1}.sh" "${SCRIPTS}/${1}.php" "${SCRIPTS}/${1}.py"
if curl_to_dir "${APP}" "${1}.sh" "$SCRIPTS"
2017-04-09 23:43:30 +03:00
then
bash "${SCRIPTS}/${1}.sh"
rm -f "${SCRIPTS}/${1}.sh"
elif curl_to_dir "${APP}" "${1}.php" "$SCRIPTS"
2017-04-09 23:43:30 +03:00
then
php "${SCRIPTS}/${1}.php"
rm -f "${SCRIPTS}/${1}.php"
elif curl_to_dir "${APP}" "${1}.py" "$SCRIPTS"
2017-04-09 23:43:30 +03:00
then
python "${SCRIPTS}/${1}.py"
rm -f "${SCRIPTS}/${1}.py"
else
print_text_in_color "$Red" "Downloading ${1} failed"
print_text_in_color "$ICyan" "Script failed to download. Please run: 'sudo curl -sLO ${APP}/${1}.sh|php|py' again."
exit
2017-04-09 23:43:30 +03:00
fi
}
version(){
local h t v
[[ $2 = "$1" || $2 = "$3" ]] && return 0
v=$(printf '%s\n' "$@" | sort -V)
h=$(head -n1 <<<"$v")
t=$(tail -n1 <<<"$v")
[[ $2 != "$h" && $2 != "$t" ]]
}
version_gt() {
local v1 v2 IFS=.
read -ra v1 <<< "$1"
read -ra v2 <<< "$2"
printf -v v1 %03d "${v1[@]}"
printf -v v2 %03d "${v2[@]}"
[[ $v1 > $v2 ]]
}
spinner_loading() {
pid=$!
spin='-\|/'
i=0
while kill -0 $pid 2>/dev/null
do
i=$(( (i+1) %4 ))
printf "\r[${spin:$i:1}] " # Add text here, something like "Please be paitent..." maybe?
sleep .1
done
}
any_key() {
local PROMPT="$1"
2018-12-18 00:24:50 +03:00
read -r -sn 1 -p "$(printf "%b" "${IGreen}${PROMPT}${Color_Off}")";echo
2017-04-09 23:43:30 +03:00
}
2018-03-02 00:18:53 +03:00
lowest_compatible_nc() {
2018-03-02 02:12:52 +03:00
if [ "${CURRENTVERSION%%.*}" -lt "$1" ]
2018-03-02 00:18:53 +03:00
then
2018-03-07 02:25:24 +03:00
msg_box "This script is developed to work with Nextcloud $1 and later.
This means we can't use our own script for now. But don't worry,
we automated the update process and we will now use Nextclouds updater instead.
Press [OK] to continue the update, or press [CTRL+C] to abort.
2018-03-02 00:18:53 +03:00
If you are using Nextcloud $1 and later and still see this message,
2018-03-07 02:25:24 +03:00
or experience other issues then please report this to $ISSUES"
2018-03-31 15:12:33 +03:00
# Download the latest updater
# cd $NCPATH
# curl sLO https://github.com/nextcloud/updater/archive/master.zip
2018-03-31 15:12:33 +03:00
# install_if_not unzip
# unzip -q master.zip
# rm master.zip*
# rm updater/ -R
# mv updater-master/ updater/
# download_static_script setup_secure_permissions_nextcloud -P $SCRIPTS
# bash $SECURE
# cd
2018-03-07 02:25:24 +03:00
# Do the upgrade
chown -R www-data:www-data "$NCPATH"
rm -rf "$NCPATH"/assets
yes | sudo -u www-data php /var/www/nextcloud/updater/updater.phar
2018-03-31 15:12:33 +03:00
download_static_script setup_secure_permissions_nextcloud -P $SCRIPTS
bash $SECURE
2018-03-07 02:25:24 +03:00
occ_command maintenance:mode --off
2018-03-02 00:18:53 +03:00
fi
2018-03-02 02:07:54 +03:00
2018-03-07 02:25:24 +03:00
# Check new version
# shellcheck source=lib.sh
NC_UPDATE=1 . <(curl -sL $GITHUB_REPO/lib.sh)
unset NC_UPDATE
if [ "${CURRENTVERSION%%.*}" -ge "$1" ]
then
sleep 1
else
msg_box "It appears that something went wrong with the update.
Please report this to $ISSUES"
occ_command -V
exit
fi
}
2018-08-08 22:47:47 +03:00
# Check universe reposiroty
check_universe() {
UNIV=$(apt-cache policy | grep http | awk '{print $3}' | grep universe | head -n 1 | cut -d "/" -f 2)
if [ "$UNIV" != "universe" ]
2018-08-08 22:47:47 +03:00
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Adding required repo (universe)."
add-apt-repository universe
fi
}
# Check universe reposiroty
check_multiverse() {
MULTIV=$(apt-cache policy | grep http | awk '{print $3}' | grep multiverse | head -n 1 | cut -d "/" -f 2)
if [ "$MULTIV" != "multiverse" ]
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Adding required repo (multiverse)."
add-apt-repository multiverse
2018-08-08 22:47:47 +03:00
fi
}
2018-08-05 18:20:42 +03:00
set_max_count() {
if grep -F 'vm.max_map_count=262144' /etc/sysctl.conf ; then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Max map count already set, skipping..."
2018-08-05 18:20:42 +03:00
else
sysctl -w vm.max_map_count=262144
{
echo "###################################################################"
echo "# Docker ES max virtual memory"
echo "vm.max_map_count=262144"
} >> /etc/sysctl.conf
fi
}
install_docker() {
2018-08-09 02:23:48 +03:00
if ! docker -v &> /dev/null
then
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Installing Docker CE..."
2019-06-06 23:46:08 +03:00
apt update -q4 & spinner_loading
2018-08-09 02:23:48 +03:00
install_if_not curl
curl -fsSL get.docker.com | sh
2018-08-05 18:20:42 +03:00
fi
2018-11-26 22:54:59 +03:00
# Set overlay2
cat << OVERLAY2 > /etc/docker/daemon.json
{
"storage-driver": "overlay2"
}
OVERLAY2
systemctl daemon-reload
systemctl restart docker
2018-08-05 18:20:42 +03:00
}
2019-06-06 23:30:50 +03:00
# Check if old docker exists
# FULL NAME e.g. ark74/nc_fts or containrrr/watchtower or collabora/code
does_this_docker_exist() {
if [ "$(docker ps -a >/dev/null 2>&1 && echo yes || echo no)" == "yes" ]
then
[[ "$(docker images "$1" | awk '{print $1}' | tail -1)" == "$1" ]] && return
2019-06-06 23:30:50 +03:00
fi
# If either of above statments are false return:
false
2019-06-06 23:30:50 +03:00
}
# Remove all dockers excluding one
# docker_prune_except_this fts_esror 'Full Text Search'
docker_prune_except_this() {
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "Checking if there are any old images and removing them..."
DOCKERPS=$(docker ps -a | grep -v "$1" | awk 'NR>1 {print $1}')
if [ "$DOCKERPS" != "" ]
then
msg_box "Removing old Docker instance(s)... ($DOCKERPS)
Please note that we will not remove $1 ($2).
You will be given the option to abort when you hit OK."
any_key "Press any key to continue. Press CTRL+C to abort"
docker stop "$(docker ps -a | grep -v "$1" | awk 'NR>1 {print $1}')"
docker container prune -f
docker image prune -a -f
docker volume prune -f
fi
}
# Remove selected Docker image
2019-06-06 23:46:08 +03:00
# docker_prune_this 'collabora/code' 'onlyoffice/documentserver' 'ark74/nc_fts'
docker_prune_this() {
2019-06-06 23:46:08 +03:00
if [ "$(docker images "$1" | awk '{print $1}' | tail -1)" == "$1" ]
then
2019-06-06 23:46:08 +03:00
msg_box "Removing old Docker image: $1
You will be given the option to abort when you hit OK."
any_key "Press any key to continue. Press CTRL+C to abort"
docker stop "$(docker container ls | grep "$1" | awk '{print $1}' | tail -1)"
docker container prune -f
docker image prune -a -f
docker volume prune -f
fi
}
2018-10-15 23:04:27 +03:00
# countdown 'message looks like this' 10
countdown() {
2018-12-18 01:27:01 +03:00
print_text_in_color "$ICyan" "$1"
2018-10-15 23:04:27 +03:00
secs="$(($2))"
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
}
print_text_in_color() {
printf "%b%s%b\n" "$1" "$2" "$Color_Off"
}
2017-04-09 23:43:30 +03:00
## bash colors
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Underline
UBlack='\e[4;30m' # Black
URed='\e[4;31m' # Red
UGreen='\e[4;32m' # Green
UYellow='\e[4;33m' # Yellow
UBlue='\e[4;34m' # Blue
UPurple='\e[4;35m' # Purple
UCyan='\e[4;36m' # Cyan
UWhite='\e[4;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
# High Intensity
IBlack='\e[0;90m' # Black
IRed='\e[0;91m' # Red
IGreen='\e[0;92m' # Green
IYellow='\e[0;93m' # Yellow
IBlue='\e[0;94m' # Blue
IPurple='\e[0;95m' # Purple
ICyan='\e[0;96m' # Cyan
IWhite='\e[0;97m' # White
# Bold High Intensity
BIBlack='\e[1;90m' # Black
BIRed='\e[1;91m' # Red
BIGreen='\e[1;92m' # Green
BIYellow='\e[1;93m' # Yellow
BIBlue='\e[1;94m' # Blue
BIPurple='\e[1;95m' # Purple
BICyan='\e[1;96m' # Cyan
BIWhite='\e[1;97m' # White
# High Intensity backgrounds
On_IBlack='\e[0;100m' # Black
On_IRed='\e[0;101m' # Red
On_IGreen='\e[0;102m' # Green
On_IYellow='\e[0;103m' # Yellow
On_IBlue='\e[0;104m' # Blue
On_IPurple='\e[0;105m' # Purple
On_ICyan='\e[0;106m' # Cyan
On_IWhite='\e[0;107m' # White