Merge pull request #11000 from ladar/master
Fix the hostname config logic for Alpine.
This commit is contained in:
commit
75f1fde1e5
|
@ -16,9 +16,6 @@ module VagrantPlugins
|
||||||
NEW_HOSTNAME_FULL='#{name}'
|
NEW_HOSTNAME_FULL='#{name}'
|
||||||
NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}"
|
NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}"
|
||||||
|
|
||||||
# Update sysconfig
|
|
||||||
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
|
|
||||||
|
|
||||||
# Set the hostname - use hostnamectl if available
|
# Set the hostname - use hostnamectl if available
|
||||||
if command -v hostnamectl; then
|
if command -v hostnamectl; then
|
||||||
hostnamectl set-hostname --static '#{name}'
|
hostnamectl set-hostname --static '#{name}'
|
||||||
|
@ -35,8 +32,30 @@ module VagrantPlugins
|
||||||
sed -i -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT\(\s\)/\1$NEW_HOSTNAME_SHORT\2/g" -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT$/\1$NEW_HOSTNAME_SHORT/g" /etc/hosts
|
sed -i -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT\(\s\)/\1$NEW_HOSTNAME_SHORT\2/g" -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT$/\1$NEW_HOSTNAME_SHORT/g" /etc/hosts
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Restart network
|
# Persist hostname change across reboots
|
||||||
service network restart
|
if [ -f /etc/sysconfig/network ]; then
|
||||||
|
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
|
||||||
|
elif [ -f /etc/hostname ]; then
|
||||||
|
sed -i 's/.*/#{name}/' /etc/hostname
|
||||||
|
else
|
||||||
|
echo 'Unrecognized system. Hostname change may not persist across reboots.'
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Restart the network if we find a recognized SYS V init script
|
||||||
|
if command -v service; then
|
||||||
|
if [ -f /etc/init.d/network ]; then
|
||||||
|
service network restart
|
||||||
|
elif [ -f /etc/init.d/networking ]; then
|
||||||
|
service networking restart
|
||||||
|
elif [ -f /etc/init.d/NetworkManager ]; then
|
||||||
|
service NetworkManager restart
|
||||||
|
else
|
||||||
|
echo 'Unrecognized system. Networking was not restarted following hostname change.'
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
EOH
|
EOH
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,17 +20,87 @@ describe "VagrantPlugins::GuestALT::Cap::ChangeHostName" do
|
||||||
|
|
||||||
describe ".change_host_name" do
|
describe ".change_host_name" do
|
||||||
let(:cap) { caps.get(:change_host_name) }
|
let(:cap) { caps.get(:change_host_name) }
|
||||||
|
let(:name) { 'banana-rama.example.com' }
|
||||||
|
let(:systemd) { true }
|
||||||
|
let(:hostnamectl) { true }
|
||||||
|
let(:networkd) { true }
|
||||||
|
let(:service) { true }
|
||||||
|
let(:network_manager) { false }
|
||||||
|
|
||||||
let(:name) { "banana-rama.example.com" }
|
before do
|
||||||
|
allow(cap).to receive(:systemd?).and_return(systemd)
|
||||||
|
allow(cap).to receive(:service?).and_return(service)
|
||||||
|
allow(cap).to receive(:hostnamectl?).and_return(hostnamectl)
|
||||||
|
allow(cap).to receive(:systemd_networkd?).and_return(networkd)
|
||||||
|
allow(cap).to receive(:systemd_controlled?).with(anything, /NetworkManager/).and_return(network_manager)
|
||||||
|
end
|
||||||
|
|
||||||
it "sets the hostname" do
|
it "sets the hostname if not set" do
|
||||||
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
|
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
|
||||||
|
|
||||||
cap.change_host_name(machine, name)
|
cap.change_host_name(machine, name)
|
||||||
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/)
|
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
|
||||||
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --static '#{name}'/)
|
end
|
||||||
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --transient '#{name}'/)
|
|
||||||
expect(comm.received_commands[1]).to match(/service network restart/)
|
context "when hostnamectl is in use" do
|
||||||
|
let(:hostnamectl) { true }
|
||||||
|
|
||||||
|
it "sets hostname with hostnamectl" do
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
comm.received_commands.find { |cmd| cmd =~ /^hostnamectl/ }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when hostnamectl is not in use" do
|
||||||
|
let(:hostnamectl) { false }
|
||||||
|
|
||||||
|
it "sets hostname with hostname command" do
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
comm.received_commands.find { |cmd| cmd =~ /^hostname -F/ }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "restarts the network" do
|
||||||
|
context "when networkd is in use" do
|
||||||
|
let(:networkd) { true }
|
||||||
|
|
||||||
|
it "restarts networkd with systemctl" do
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
comm.received_commands.find { |cmd| cmd =~ /systemctl restart systemd-networkd/ }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when NetworkManager is in use with systemctl" do
|
||||||
|
let(:networkd) { false }
|
||||||
|
let(:network_manager) { true }
|
||||||
|
|
||||||
|
it "restarts NetworkManager with systemctl" do
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
comm.received_commands.find { |cmd| cmd =~ /systemctl restart NetworkManager/ }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when NetworkManager is in use without systemctl" do
|
||||||
|
let(:networkd) { false }
|
||||||
|
let(:network_manager) { true }
|
||||||
|
let(:systemd) { false }
|
||||||
|
|
||||||
|
it "restarts NetworkManager without systemctl" do
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
comm.received_commands.find { |cmd| cmd =~ /service NetworkManager restart/ }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when systemd is not in use" do
|
||||||
|
let(:networkd) { false }
|
||||||
|
let(:network_manager) { false }
|
||||||
|
let(:systemd) { false }
|
||||||
|
|
||||||
|
it "restarts networking with networking init script" do
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
comm.received_commands.find { |cmd| cmd =~ /service networking restart/ }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not change the hostname if already set" do
|
it "does not change the hostname if already set" do
|
||||||
|
|
Loading…
Reference in New Issue