guests/ubuntu: Change host name in one command

This also removes the dependency on the debian hostname setting.
This commit is contained in:
Seth Vargo 2016-06-06 11:37:22 -04:00
parent dd2e76472b
commit 155aa8cf1d
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
1 changed files with 40 additions and 39 deletions

View File

@ -1,49 +1,50 @@
module VagrantPlugins
module GuestUbuntu
module Cap
class ChangeHostName < VagrantPlugins::GuestDebian::Cap::ChangeHostName
class ChangeHostName
def self.change_host_name(machine, name)
super
end
comm = machine.communicate
def update_etc_hostname
return super unless systemd?
sudo("hostnamectl set-hostname '#{short_hostname}'")
end
if !comm.test("hostname -f | grep -w '#{name}'")
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Set the hostname
echo '#{name}' > /etc/hostname
hostname -F /etc/hostname
def refresh_hostname_service
if hardy?
# hostname.sh returns 1, so use `true` to get a 0 exitcode
sudo("/etc/init.d/hostname.sh start; true")
elsif systemd?
# Service runs via hostnamectl
else
sudo("service hostname start")
end
end
if command -v hostnamectl; then
hostnamectl set-hostname '#{name}'
fi
def hardy?
os_version("hardy")
end
# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
def renew_dhcp
sudo("ifdown -a; ifup -a; ifup -a --allow=hotplug")
end
# Prepend ourselves to /etc/hosts
grep -w '#{name}' /etc/hosts || {
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
}
private
# Update mailname
echo '#{name}' > /etc/mailname
def init_package
machine.communicate.execute('cat /proc/1/comm') do |type, data|
return data.chomp if type == :stdout
end
end
# Restart networking and force new DHCP
if [ test -f /etc/init.d/hostname ]; then
/etc/init.d/hostname start || true
fi
def os_version(name)
machine.communicate.test("[ `lsb_release -c -s` = #{name} ]")
end
if [ test -f /etc/init.d/hostname.sh ]; then
/etc/init.d/hostname.sh start || true
fi
def systemd?
init_package == 'systemd'
if [ test -f /etc/init.d/networking ]; then
/etc/init.d/networking force-reload
fi
if [ test -f /etc/init.d/network-manager ]; then
/etc/init.d/network-manager force-reload
fi
EOH
end
end
end
end