Merge pull request #11000 from ladar/master

Fix the hostname config logic for Alpine.
This commit is contained in:
Brian Cain 2019-08-27 14:03:31 -07:00 committed by GitHub
commit 75f1fde1e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 12 deletions

View File

@ -16,9 +16,6 @@ module VagrantPlugins
NEW_HOSTNAME_FULL='#{name}'
NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}"
# Update sysconfig
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
# Set the hostname - use hostnamectl if available
if command -v hostnamectl; then
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
fi
# Restart network
service network restart
# Persist hostname change across reboots
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
end
end

View File

@ -20,17 +20,87 @@ describe "VagrantPlugins::GuestALT::Cap::ChangeHostName" do
describe ".change_host_name" do
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)
cap.change_host_name(machine, name)
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/)
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --static '#{name}'/)
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname --transient '#{name}'/)
expect(comm.received_commands[1]).to match(/service network restart/)
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
end
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
it "does not change the hostname if already set" do