Refactor linux change host cap

This commit is contained in:
sophia 2020-07-29 10:36:33 -05:00
parent 9eb431b1bf
commit e4367d1539
6 changed files with 37 additions and 44 deletions

View File

@ -13,10 +13,6 @@ module VagrantPlugins
CURRENT_HOSTNAME_FULL="$(hostname -f)"
CURRENT_HOSTNAME_SHORT="$(hostname -s)"
# New hostname to be saved in /etc/hosts
NEW_HOSTNAME_FULL='#{name}'
NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}"
# Set the hostname - use hostnamectl if available
if command -v hostnamectl; then
hostnamectl set-hostname --static '#{name}'

View File

@ -7,11 +7,7 @@ module VagrantPlugins
extend VagrantPlugins::GuestLinux::Cap::ChangeHostName
def self.change_name_command(name)
basename = name.split(".", 2)[0]
return <<-EOH.gsub(/^ {14}/, "")
# Set hostname
hostnamectl set-hostname '#{basename}'
EOH
"hostnamectl set-hostname '#{name.split(".", 2).first}'"
end
end
end

View File

@ -7,11 +7,7 @@ module VagrantPlugins
extend VagrantPlugins::GuestLinux::Cap::ChangeHostName
def self.change_name_command(name)
basename = name.split(".", 2)[0]
return <<-EOH.gsub(/^ {14}/, "")
# Set hostname
hostnamectl set-hostname '#{basename}'
EOH
"hostnamectl set-hostname '#{name.split(".", 2).first}'"
end
end
end

View File

@ -4,34 +4,42 @@ module VagrantPlugins
module GuestLinux
module Cap
module ChangeHostName
include Vagrant::Util::GuestHosts::Linux
def change_name_command(name)
return <<-EOH.gsub(/^ {14}/, '')
# Set the hostname
echo '#{name}' > /etc/hostname
hostname '#{name}'
EOH
end
def change_host_name?(comm, name)
!comm.test("hostname -f | grep '^#{name}$'", sudo: false)
end
def change_host_name(machine, name)
comm = machine.communicate
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)
module Methods
def change_name_command(name)
return <<-EOH.gsub(/^ {14}/, '')
# Set the hostname
echo '#{name}' > /etc/hostname
hostname '#{name}'
EOH
end
if change_host_name?(comm, name)
comm.sudo(change_name_command(name))
def change_host_name?(comm, name)
!comm.test("hostname -f | grep '^#{name}$'", sudo: false)
end
def change_host_name(machine, name)
comm = machine.communicate
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
if change_host_name?(comm, name)
comm.sudo(change_name_command(name))
end
end
end
def self.extended(klass)
klass.extend(Vagrant::Util::GuestHosts::Linux)
klass.extend(Methods)
end
extend Vagrant::Util::GuestHosts::Linux
extend Methods
end
end
end

View File

@ -35,14 +35,14 @@ describe "VagrantPlugins::GuestALT::Cap::ChangeHostName" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
described_class.change_host_name(machine, name)
expect(comm.received_commands[2]).to match(/NEW_HOSTNAME_FULL='#{name}'/)
expect(comm.received_commands[2]).to match(/hostnamectl set-hostname --static '#{name}'/)
end
it "does not change the hostname if already set" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 0)
described_class.change_host_name(machine, name)
expect(comm).to_not receive(:sudo).with(/NEW_HOSTNAME_FULL='#{name}'/)
expect(comm).to_not receive(:sudo).with(/hostnamectl set-hostname --static '#{name}'/)
end
end
end

View File

@ -2,12 +2,10 @@ require_relative "../../../../base"
describe "VagrantPlugins::GuestLinux::Cap::ChangeHostName" do
let(:described_class) do
Class.new do
extend VagrantPlugins::GuestLinux::Plugin
VagrantPlugins::GuestLinux::Plugin
.components
.guest_capabilities[:linux]
.get(:change_host_name)
end
end
let(:machine) { double("machine") }
@ -35,7 +33,6 @@ describe "VagrantPlugins::GuestLinux::Cap::ChangeHostName" do
it "sets the hostname" do
comm.stub_command("hostname -f | grep '^#{name}$'", exit_code: 1)
described_class.change_host_name(machine, name)
expect(comm.received_commands[2]).to match(/hostname '#{name}'/)
end