2014-05-01 19:13:00 +00:00
|
|
|
function apt_install {
|
|
|
|
# Report any packages already installed.
|
|
|
|
PACKAGES=$@
|
|
|
|
TO_INSTALL=""
|
|
|
|
for pkg in $PACKAGES; do
|
|
|
|
if dpkg -s $pkg 2>/dev/null | grep "^Status: install ok installed" > /dev/null; then
|
|
|
|
echo $pkg is already installed \(`dpkg -s $pkg | grep ^Version: | sed -e "s/.*: //"`\)
|
|
|
|
else
|
|
|
|
TO_INSTALL="$TO_INSTALL""$pkg "
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# List the packages about to be installed.
|
|
|
|
if [[ ! -z "$TO_INSTALL" ]]; then
|
|
|
|
echo installing $TO_INSTALL...
|
|
|
|
fi
|
|
|
|
|
|
|
|
# 'DEBIAN_FRONTEND=noninteractive' is to prevent dbconfig-common from asking you questions.
|
|
|
|
DEBIAN_FRONTEND=noninteractive apt-get -qq -y install $PACKAGES > /dev/null;
|
|
|
|
}
|
|
|
|
|
2014-06-07 17:54:58 +00:00
|
|
|
function get_default_hostname {
|
2014-06-07 18:55:57 +00:00
|
|
|
# Guess the machine's hostname. It should be a fully qualified
|
|
|
|
# domain name suitable for DNS. None of these calls may provide
|
|
|
|
# the right value, but it's the best guess we can make.
|
2014-06-07 17:54:58 +00:00
|
|
|
set -- $(hostname --fqdn 2>/dev/null ||
|
|
|
|
hostname --all-fqdns 2>/dev/null ||
|
|
|
|
hostname 2>/dev/null)
|
2014-06-07 18:55:57 +00:00
|
|
|
printf '%s\n' "$1" # return this value
|
2014-06-07 17:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_default_publicip {
|
2014-06-07 18:55:57 +00:00
|
|
|
# Get the machine's public IP address. The machine might have
|
|
|
|
# an IP on a private network, but the IP address that we put
|
|
|
|
# into DNS must be one on the public Internet. Try a public
|
|
|
|
# API, but if that fails (maybe we don't have Internet access
|
|
|
|
# right now) then use the IP address that this machine knows
|
|
|
|
# itself as.
|
|
|
|
get_publicip_from_web_service || get_publicip_fallback
|
2014-06-07 17:54:58 +00:00
|
|
|
}
|
|
|
|
|
2014-06-08 22:32:52 +00:00
|
|
|
function get_default_publicipv6 {
|
|
|
|
get_publicipv6_from_web_service || get_publicipv6_fallback
|
|
|
|
}
|
|
|
|
|
2014-06-07 17:54:58 +00:00
|
|
|
function get_publicip_from_web_service {
|
2014-06-07 18:55:57 +00:00
|
|
|
# This seems to be the most reliable way to determine the
|
|
|
|
# machine's public IP address: asking a very nice web API
|
|
|
|
# for how they see us. Thanks go out to icanhazip.com.
|
2014-06-08 22:32:52 +00:00
|
|
|
curl -4 --fail --silent icanhazip.com 2>/dev/null
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_publicipv6_from_web_service {
|
|
|
|
curl -6 --fail --silent icanhazip.com 2>/dev/null
|
2014-06-07 17:54:58 +00:00
|
|
|
}
|
|
|
|
|
2014-06-07 18:55:57 +00:00
|
|
|
function get_publicip_fallback {
|
|
|
|
# Return the IP address that this machine knows itself as.
|
|
|
|
# It certainly may not be the IP address that this machine
|
|
|
|
# operates as on the public Internet. The machine might
|
|
|
|
# have multiple addresses if it has multiple network adapters.
|
2014-06-07 17:54:58 +00:00
|
|
|
set -- $(hostname --ip-address 2>/dev/null) \
|
|
|
|
$(hostname --all-ip-addresses 2>/dev/null)
|
2014-06-08 22:32:52 +00:00
|
|
|
while (( $# )) && { ! is_ipv4 "$1" || is_loopback_ip "$1"; }; do
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
printf '%s\n' "$1" # return this value
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_publicipv6_fallback {
|
|
|
|
set -- $(hostname --ip-address 2>/dev/null) \
|
|
|
|
$(hostname --all-ip-addresses 2>/dev/null)
|
|
|
|
while (( $# )) && { ! is_ipv6 "$1" || is_loopback_ipv6 "$1"; }; do
|
2014-06-07 17:54:58 +00:00
|
|
|
shift
|
|
|
|
done
|
2014-06-07 18:55:57 +00:00
|
|
|
printf '%s\n' "$1" # return this value
|
2014-06-07 17:54:58 +00:00
|
|
|
}
|
|
|
|
|
2014-06-08 22:32:52 +00:00
|
|
|
function is_ipv4 {
|
|
|
|
# helper for get_publicip_fallback
|
|
|
|
[[ "$1" == *.*.*.* ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_ipv6 {
|
|
|
|
[[ "$1" == *:*:* ]]
|
|
|
|
}
|
|
|
|
|
2014-06-07 17:54:58 +00:00
|
|
|
function is_loopback_ip {
|
2014-06-07 18:55:57 +00:00
|
|
|
# helper for get_publicip_fallback
|
2014-06-07 17:54:58 +00:00
|
|
|
[[ "$1" == 127.* ]]
|
|
|
|
}
|
|
|
|
|
2014-06-08 22:32:52 +00:00
|
|
|
function is_loopback_ipv6 {
|
|
|
|
[[ "$1" == ::1 ]]
|
|
|
|
}
|
|
|
|
|
2014-05-01 19:35:18 +00:00
|
|
|
function ufw_allow {
|
2014-05-02 02:39:45 +00:00
|
|
|
if [ -z "$DISABLE_FIREWALL" ]; then
|
|
|
|
# ufw has completely unhelpful output
|
|
|
|
ufw allow $1 > /dev/null;
|
|
|
|
fi
|
2014-05-01 19:35:18 +00:00
|
|
|
}
|
|
|
|
|