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