improve hostname/IP default values

Merges branch 'mkropat-populate-hostname-ip'
This commit is contained in:
Joshua Tauberer 2014-06-07 14:57:22 -04:00
commit 3fa8e384d4
4 changed files with 63 additions and 12 deletions

2
Vagrantfile vendored
View File

@ -18,7 +18,7 @@ Vagrant.configure("2") do |config|
# machine figure out its own public IP and it'll take a
# subdomain on our justtesting.email domain so we can get
# started quickly.
export PUBLIC_IP=auto-web
export PUBLIC_IP=auto
export PUBLIC_HOSTNAME=auto-easy
export CSR_COUNTRY=US

View File

@ -19,6 +19,51 @@ function apt_install {
DEBIAN_FRONTEND=noninteractive apt-get -qq -y install $PACKAGES > /dev/null;
}
function get_default_hostname {
# 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.
set -- $(hostname --fqdn 2>/dev/null ||
hostname --all-fqdns 2>/dev/null ||
hostname 2>/dev/null)
printf '%s\n' "$1" # return this value
}
function get_default_publicip {
# 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
}
function get_publicip_from_web_service {
# 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.
curl --fail --silent icanhazip.com 2>/dev/null
}
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.
set -- $(hostname --ip-address 2>/dev/null) \
$(hostname --all-ip-addresses 2>/dev/null)
while (( $# )) && is_loopback_ip "$1"; do
shift
done
printf '%s\n' "$1" # return this value
}
function is_loopback_ip {
# helper for get_publicip_fallback
[[ "$1" == 127.* ]]
}
function ufw_allow {
if [ -z "$DISABLE_FIREWALL" ]; then
# ufw has completely unhelpful output

View File

@ -2,6 +2,8 @@
# This is the entry point for configuring the system.
#####################################################
source setup/functions.sh # load our functions
# Check system setup.
if [ "`lsb_release -d | sed 's/.*:\s*//'`" != "Ubuntu 14.04 LTS" ]; then
@ -31,7 +33,7 @@ if [ -z "$PUBLIC_HOSTNAME" ]; then
if [ -z "$DEFAULT_PUBLIC_HOSTNAME" ]; then
# set a default on first run
DEFAULT_PUBLIC_HOSTNAME=`hostname`
DEFAULT_PUBLIC_HOSTNAME=`get_default_hostname`
fi
read -e -i "$DEFAULT_PUBLIC_HOSTNAME" -p "Hostname: " PUBLIC_HOSTNAME
@ -46,7 +48,7 @@ if [ -z "$PUBLIC_IP" ]; then
if [ -z "$DEFAULT_PUBLIC_IP" ]; then
# set a default on first run
DEFAULT_PUBLIC_IP=`hostname -i`
DEFAULT_PUBLIC_IP=`get_default_publicip`
fi
read -e -i "$DEFAULT_PUBLIC_IP" -p "Public IP: " PUBLIC_IP
@ -69,18 +71,13 @@ fi
# Automatic configuration, e.g. as used in our Vagrant configuration.
if [ "$PUBLIC_IP" == "auto" ]; then
# Assume `hostname -i` gives the correct public IP address for the machine.
PUBLIC_IP=`hostname -i`
echo "IP Address: $PUBLIC_IP"
fi
if [ "$PUBLIC_IP" == "auto-web" ]; then
# Use a public API to get our public IP address.
PUBLIC_IP=`curl -s icanhazip.com`
PUBLIC_IP=`get_default_publicip`
echo "IP Address: $PUBLIC_IP"
fi
if [ "$PUBLIC_HOSTNAME" == "auto-easy" ]; then
# Generate a probably-unique subdomain under our justtesting.email domain.
PUBLIC_HOSTNAME=m`hostname -i | sha1sum | cut -c1-5`.justtesting.email
PUBLIC_HOSTNAME=m`get_default_publicip | sha1sum | cut -c1-5`.justtesting.email
echo "Public Hostname: $PUBLIC_HOSTNAME"
fi

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3
import sys, urllib.request, urllib.error
import sys, getpass, urllib.request, urllib.error
def mgmt(cmd, data=None):
req = urllib.request.Request('http://localhost:10222' + cmd, urllib.parse.urlencode(data).encode("utf8") if data else None)
@ -11,6 +11,15 @@ def mgmt(cmd, data=None):
sys.exit(1)
return response.read().decode('utf8')
def read_password():
first = getpass.getpass('password: ')
second = getpass.getpass(' (again): ')
while first != second:
print('Passwords not the same. Try again.')
first = getpass.getpass('password: ')
second = getpass.getpass(' (again): ')
return first
if len(sys.argv) < 2:
print("Usage: ")
print(" tools/mail.py user (lists users)")
@ -33,7 +42,7 @@ elif sys.argv[1] == "user" and sys.argv[2] in ("add", "password"):
email = input("email: ")
else:
email = sys.argv[3]
pw = input("password: ")
pw = read_password()
else:
email, pw = sys.argv[3:5]