Skip to content

Commit

Permalink
Detect host name by the "hostname" command.
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Sep 16, 2014
1 parent aa1c974 commit 83e0b0b
Showing 1 changed file with 26 additions and 41 deletions.
67 changes: 26 additions & 41 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ setup_configuration_directory() {

[ "$HOST" = "Auto Detect" ] &&
determine_hostname \
"If this node has a global host name or a global IP address, then choose \"Manual Input\" and type it. Otherwise, choose a preferred IP address which can be accessed from the droonga-engine node." \
"Enter a global host name or a global IP address for this node" &&
"Enter a host name or an IP address which is accessible from the droonga-engine node" &&
HOST=$DETERMINED_HOSTNAME

curl -o $config_file.template $SCRIPT_URL/$PLATFORM/$NAME.yaml
Expand All @@ -111,55 +110,41 @@ setup_configuration_directory() {
}


get_addresses_with_interface() {
if exist_command ip; then
ip addr | grep "inet " | \
$sed -e "s/^ *inet ([0-9\.]+).+ ([^ ]+)\$/\1 \2/"
return 0
fi

if exist_command ifconfig; then
interfaces=$(ifconfig -s | cut -d " " -f 1 | tail -n +2)
for interface in $interfaces; do
address=$(LANG=C ifconfig $interface | grep "inet addr" | \
$sed -e "s/^ *inet addr:([0-9\.]+).+\$/\1/")
if [ "$address" != "" ]; then
echo $address $interface
fi
done
return 0
guess_global_hostname() {
if hostname -d > /dev/null 2>&1; then
domain=$(hostname -d)
hostname=$(hostname)
if [ "$domain" != "" ]; then
echo "$hostname.$domain"
return 0
fi
fi

echo "127.0.0.1 lo"
return 0
echo ""
return 1
}

determine_hostname() {
prompt_for_suggestions="$1"
prompt_for_manual_input="$2"
prompt_for_manual_input="$1"

if [ $(get_addresses_with_interface | wc -l) -eq 1 ]; then
DETERMINED_HOSTNAME=$(get_addresses_with_interface | cut -d " " -f 1)
global_hostname=$(guess_global_hostname)
if [ "$global_hostname" != "" ]; then
DETERMINED_HOSTNAME="$global_hostname"
return 0
fi

PS3="$prompt_for_suggestions: "
select chosen in $(get_addresses_with_interface | \
$sed -e "s/ (.+)\$/(\1)/") "Manual Input"
do
if [ -z "$chosen" ]; then
continue
else
DETERMINED_HOSTNAME=$(echo $chosen | cut -d "(" -f 1)
break
fi
done

if [ "$DETERMINED_HOSTNAME" = "Manual Input" ]; then
input_hostname "$prompt_for_manual_input" &&
DETERMINED_HOSTNAME="$TYPED_HOSTNAME"
address=$(hostname -i | \
$sed -e "s/127\.[0-9]+\.[0-9]+\.[0-9]+//g" \
-e "s/ +/ /g" \
-e "s/^ +| +\$//g" |\
cut -d " " -f 1)
if [ "$address" != "" ]; then
DETERMINED_HOSTNAME="$address"
return 0
fi

input_hostname "$prompt_for_manual_input" &&
DETERMINED_HOSTNAME="$TYPED_HOSTNAME"

return 0
}

Expand Down

0 comments on commit 83e0b0b

Please sign in to comment.