2015-07-20 00:09:47 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
set -eu -o pipefail
|
|
|
|
|
|
|
|
|
|
# This file can be used in Dockerfile
|
|
|
|
|
|
2016-12-20 20:49:57 -08:00
|
|
|
readonly USER=yellowtent
|
|
|
|
|
|
2016-12-21 09:50:32 -08:00
|
|
|
readonly USER_DATA_FILE="/root/user_data.img"
|
|
|
|
|
readonly USER_DATA_DIR="/home/yellowtent/data"
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
readonly container_files="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/container"
|
|
|
|
|
|
|
|
|
|
readonly CONFIG_DIR="/home/yellowtent/configs"
|
|
|
|
|
readonly DATA_DIR="/home/yellowtent/data"
|
2016-12-21 09:41:42 -08:00
|
|
|
readonly provider="${1:-generic}"
|
|
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
sed -e 's/^#NTP=/NTP=0.ubuntu.pool.ntp.org 1.ubuntu.pool.ntp.org 2.ubuntu.pool.ntp.org 3.ubuntu.pool.ntp.org/' -i /etc/systemd/timesyncd.conf
|
|
|
|
|
timedatectl set-ntp 1
|
|
|
|
|
timedatectl set-timezone UTC
|
2016-12-23 18:28:18 -08:00
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
echo "==> Setting up firewall"
|
2016-12-21 11:31:58 -08:00
|
|
|
iptables -t filter -N CLOUDRON || true
|
|
|
|
|
iptables -t filter -F CLOUDRON # empty any existing rules
|
|
|
|
|
|
|
|
|
|
# NOTE: keep these in sync with src/apps.js validatePortBindings
|
|
|
|
|
# allow ssh, http, https, ping, dns
|
|
|
|
|
iptables -t filter -I CLOUDRON -m state --state RELATED,ESTABLISHED -j ACCEPT
|
|
|
|
|
# caas has ssh on port 202
|
|
|
|
|
if [[ "${provider}" == "caas" ]]; then
|
|
|
|
|
iptables -A CLOUDRON -p tcp -m tcp -m multiport --dports 25,80,202,443,587,993,4190 -j ACCEPT
|
|
|
|
|
else
|
|
|
|
|
iptables -A CLOUDRON -p tcp -m tcp -m multiport --dports 25,80,22,443,587,993,4190 -j ACCEPT
|
|
|
|
|
fi
|
|
|
|
|
iptables -t filter -A CLOUDRON -p icmp --icmp-type echo-request -j ACCEPT
|
|
|
|
|
iptables -t filter -A CLOUDRON -p icmp --icmp-type echo-reply -j ACCEPT
|
|
|
|
|
iptables -t filter -A CLOUDRON -p udp --sport 53 -j ACCEPT
|
|
|
|
|
iptables -t filter -A CLOUDRON -s 172.18.0.0/16 -j ACCEPT # required to accept any connections from apps to our IP:<public port>
|
|
|
|
|
iptables -t filter -A CLOUDRON -i lo -j ACCEPT # required for localhost connections (mysql)
|
|
|
|
|
|
|
|
|
|
# log dropped incoming. keep this at the end of all the rules
|
|
|
|
|
iptables -t filter -A CLOUDRON -m limit --limit 2/min -j LOG --log-prefix "IPTables Packet Dropped: " --log-level 7
|
|
|
|
|
iptables -t filter -A CLOUDRON -j DROP
|
|
|
|
|
|
|
|
|
|
if ! iptables -t filter -C INPUT -j CLOUDRON 2>/dev/null; then
|
2016-12-23 09:53:41 -08:00
|
|
|
iptables -t filter -I INPUT -j CLOUDRON
|
2016-12-21 11:31:58 -08:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# so it gets restored across reboot
|
|
|
|
|
mkdir -p /etc/iptables && iptables-save > /etc/iptables/rules.v4
|
|
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
echo "==> Configuring docker"
|
|
|
|
|
cp "${container_files}/docker-cloudron-app.apparmor" /etc/apparmor.d/docker-cloudron-app
|
|
|
|
|
systemctl restart apparmor
|
|
|
|
|
|
|
|
|
|
usermod yellowtent -a -G docker
|
2016-12-23 10:07:06 -08:00
|
|
|
sed -e 's,^ExecStart=.*$,ExecStart=/usr/bin/docker daemon -H fd:// --log-driver=journald --exec-opt native.cgroupdriver=cgroupfs,' -i /lib/systemd/system/docker.service
|
2016-12-27 22:06:11 -08:00
|
|
|
systemctl enable docker
|
|
|
|
|
systemctl restart docker
|
2016-12-21 11:31:58 -08:00
|
|
|
|
2016-12-21 09:41:42 -08:00
|
|
|
# caas has ssh on port 202 and we disable password login
|
|
|
|
|
if [[ "${provider}" == "caas" ]]; then
|
|
|
|
|
# https://stackoverflow.com/questions/4348166/using-with-sed on why ? must be escaped
|
|
|
|
|
sed -e 's/^#\?PermitRootLogin .*/PermitRootLogin without-password/g' \
|
|
|
|
|
-e 's/^#\?PermitEmptyPasswords .*/PermitEmptyPasswords no/g' \
|
|
|
|
|
-e 's/^#\?PasswordAuthentication .*/PasswordAuthentication no/g' \
|
|
|
|
|
-e 's/^#\?Port .*/Port 202/g' \
|
|
|
|
|
-i /etc/ssh/sshd_config
|
|
|
|
|
|
|
|
|
|
# required so we can connect to this machine since port 22 is blocked by iptables by now
|
|
|
|
|
systemctl reload sshd
|
|
|
|
|
fi
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
echo "==> Setup btrfs data"
|
2016-12-21 09:50:32 -08:00
|
|
|
if ! grep -q loop.ko /lib/modules/`uname -r`/modules.builtin; then
|
|
|
|
|
# on scaleway loop is not built-in
|
|
|
|
|
echo "loop" >> /etc/modules
|
|
|
|
|
modprobe loop
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [[ ! -d "${USER_DATA_DIR}" ]]; then
|
|
|
|
|
truncate -s "8192m" "${USER_DATA_FILE}" # 8gb start (this will get resized dynamically by cloudron-system-setup.service)
|
|
|
|
|
mkfs.btrfs -L UserDataHome "${USER_DATA_FILE}"
|
|
|
|
|
mkdir -p "${USER_DATA_DIR}"
|
|
|
|
|
mount -t btrfs -o loop,nosuid "${USER_DATA_FILE}" ${USER_DATA_DIR}
|
|
|
|
|
fi
|
|
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
echo "==> Configuring journald"
|
2016-12-20 20:53:42 -08:00
|
|
|
sed -e "s/^#SystemMaxUse=.*$/SystemMaxUse=100M/" \
|
|
|
|
|
-e "s/^#ForwardToSyslog=.*$/ForwardToSyslog=no/" \
|
|
|
|
|
-i /etc/systemd/journald.conf
|
|
|
|
|
|
|
|
|
|
# When rotating logs, systemd kills journald too soon sometimes
|
|
|
|
|
# See https://github.com/systemd/systemd/issues/1353 (this is upstream default)
|
|
|
|
|
sed -e "s/^WatchdogSec=.*$/WatchdogSec=3min/" \
|
|
|
|
|
-i /lib/systemd/system/systemd-journald.service
|
|
|
|
|
|
2016-12-20 20:49:57 -08:00
|
|
|
# Give user access to system logs
|
2016-12-27 22:06:11 -08:00
|
|
|
usermod -a -G systemd-journal yellowtent
|
2016-12-20 20:49:57 -08:00
|
|
|
mkdir -p /var/log/journal # in some images, this directory is not created making system log to /run/systemd instead
|
|
|
|
|
chown root:systemd-journal /var/log/journal
|
|
|
|
|
systemctl restart systemd-journald
|
2016-12-27 22:06:11 -08:00
|
|
|
setfacl -n -m u:yellowtent:r /var/log/journal/*/system.journal
|
2016-12-20 20:49:57 -08:00
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
echo "==> Creating config directory"
|
|
|
|
|
rm -rf "${CONFIG_DIR}" && mkdir "${CONFIG_DIR}"
|
|
|
|
|
chown yellowtent:yellowtent "${CONFIG_DIR}"
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
echo "==> Adding systemd services"
|
2015-09-07 11:18:44 -07:00
|
|
|
cp -r "${container_files}/systemd/." /etc/systemd/system/
|
|
|
|
|
systemctl daemon-reload
|
|
|
|
|
systemctl enable cloudron.target
|
2016-12-20 20:55:54 -08:00
|
|
|
systemctl enable iptables-restore
|
|
|
|
|
systemctl enable cloudron-system-setup
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
rm -rf /etc/collectd
|
|
|
|
|
ln -sfF "${DATA_DIR}/collectd" /etc/collectd
|
2016-12-27 14:12:31 -08:00
|
|
|
systemctl restart collectd
|
|
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
echo "==> Configuring system"
|
|
|
|
|
rm -f /etc/sudoers.d/yellowtent
|
|
|
|
|
cp "${container_files}/sudoers" /etc/sudoers.d/yellowtent
|
|
|
|
|
|
2016-12-27 14:12:31 -08:00
|
|
|
# For logrotate
|
|
|
|
|
systemctl enable cron
|
|
|
|
|
|
|
|
|
|
# DO uses Google nameservers by default. This causes RBL queries to fail (host 2.0.0.127.zen.spamhaus.org)
|
|
|
|
|
# We do not use dnsmasq because it is not a recursive resolver and defaults to the value in the interfaces file (which is Google DNS!)
|
|
|
|
|
systemctl enable unbound
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-12-27 22:06:11 -08:00
|
|
|
# link nginx config to system config
|
|
|
|
|
unlink /etc/nginx 2>/dev/null || rm -rf /etc/nginx
|
|
|
|
|
ln -s "${DATA_DIR}/nginx" /etc/nginx
|
|
|
|
|
|
|
|
|
|
cp "${container_files}/mysql.cnf" /etc/mysql/mysql.cnf
|
|
|
|
|
|