-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathchange_host_name.rb
106 lines (92 loc) · 3.8 KB
/
change_host_name.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require "log4r"
require 'vagrant/util/guest_hosts'
require 'vagrant/util/guest_inspection'
require_relative "../../linux/cap/network_interfaces"
module VagrantPlugins
module GuestDebian
module Cap
class ChangeHostName
extend Vagrant::Util::GuestInspection::Linux
extend Vagrant::Util::GuestHosts::Linux
def self.change_host_name(machine, name)
@logger = Log4r::Logger.new("vagrant::guest::debian::changehostname")
comm = machine.communicate
if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
network_with_hostname = machine.config.vm.networks.map {|_, c| c if c[:hostname] }.compact[0]
if network_with_hostname
replace_host(comm, name, network_with_hostname[:ip])
else
add_hostname_to_loopback_interface(comm, name)
end
basename = name.split(".", 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Set the hostname
echo '#{basename}' > /etc/hostname
# Update mailname
echo '#{name}' > /etc/mailname
EOH
if hostnamectl?(comm)
comm.sudo("hostnamectl set-hostname '#{basename}'")
else
comm.sudo <<-EOH.gsub(/^ {14}/, '')
hostname -F /etc/hostname
# Restart hostname services
if test -f /etc/init.d/hostname; then
/etc/init.d/hostname start || true
fi
if test -f /etc/init.d/hostname.sh; then
/etc/init.d/hostname.sh start || true
fi
EOH
end
restart_command = nil
if systemd?(comm)
if systemd_networkd?(comm)
@logger.debug("Attempting to restart networking with systemd-networkd")
restart_command = "systemctl restart systemd-networkd.service"
elsif systemd_controlled?(comm, "NetworkManager.service")
@logger.debug("Attempting to restart networking with NetworkManager")
restart_command = "systemctl restart NetworkManager.service"
end
end
if restart_command
comm.sudo(restart_command)
else
restart_each_interface(machine, @logger)
end
end
end
protected
# Due to how most Debian systems and older Ubuntu systems handle restarting
# networking, we cannot simply run the networking init script or use the ifup/down
# tools to restart all interfaces to renew the machines DHCP lease when setting
# its hostname. This method is a workaround for those older systems that
# cannoy reliably restart networking. It restarts each individual interface
# on its own instead.
#
# @param [Vagrant::Machine] machine
# @param [Log4r::Logger] logger
def self.restart_each_interface(machine, logger)
comm = machine.communicate
interfaces = VagrantPlugins::GuestLinux::Cap::NetworkInterfaces.network_interfaces(machine)
nettools = true
if systemd?(comm)
logger.debug("Attempting to restart networking with systemctl")
nettools = false
else
logger.debug("Attempting to restart networking with ifup/down nettools")
end
interfaces.each do |iface|
logger.debug("Restarting interface #{iface} on guest #{machine.name}")
if nettools
restart_command = "ifdown #{iface};ifup #{iface}"
else
restart_command = "systemctl stop ifup@#{iface}.service;systemctl start ifup@#{iface}.service"
end
comm.sudo(restart_command)
end
end
end
end
end
end