guests/pld: Change host name in one command
This commit is contained in:
parent
5e5a91956c
commit
9702abb5c4
|
@ -3,15 +3,27 @@ module VagrantPlugins
|
||||||
module Cap
|
module Cap
|
||||||
class ChangeHostName
|
class ChangeHostName
|
||||||
def self.change_host_name(machine, name)
|
def self.change_host_name(machine, name)
|
||||||
machine.communicate.tap do |comm|
|
comm = machine.communicate
|
||||||
# Only do this if the hostname is not already set
|
|
||||||
if !comm.test("sudo hostname | grep --line-regexp '#{name}'")
|
if !comm.test("hostname | grep -w '#{name}'", sudo: false)
|
||||||
comm.sudo("sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network")
|
basename = name.split(".", 2)[0]
|
||||||
comm.sudo("hostname #{name}")
|
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
||||||
comm.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} #{name.split('.')[0]} @' /etc/hosts")
|
hostname '#{name}'
|
||||||
comm.sudo("sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{name}\"/' /etc/sysconfig/interfaces/ifcfg-*")
|
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
|
||||||
comm.sudo("service network restart")
|
|
||||||
end
|
sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{name}\"/' /etc/sysconfig/interfaces/ifcfg-*
|
||||||
|
|
||||||
|
# Remove comments and blank lines from /etc/hosts
|
||||||
|
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
|
||||||
|
|
||||||
|
# Prepend ourselves to /etc/hosts
|
||||||
|
grep -w '#{name}' /etc/hosts || {
|
||||||
|
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
||||||
|
}
|
||||||
|
|
||||||
|
# Restart networking
|
||||||
|
service network restart
|
||||||
|
EOH
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
require_relative "../../../../base"
|
||||||
|
|
||||||
|
describe "VagrantPlugins::GuestPld::Cap::ChangeHostName" do
|
||||||
|
let(:caps) do
|
||||||
|
VagrantPlugins::GuestPld::Plugin
|
||||||
|
.components
|
||||||
|
.guest_capabilities[:pld]
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:machine) { double("machine") }
|
||||||
|
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(machine).to receive(:communicate).and_return(comm)
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
comm.verify_expectations!
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".change_host_name" do
|
||||||
|
let(:cap) { caps.get(:change_host_name) }
|
||||||
|
|
||||||
|
let(:name) { "banana-rama.example.com" }
|
||||||
|
|
||||||
|
it "sets the hostname" do
|
||||||
|
comm.stub_command("hostname | grep -w '#{name}'", exit_code: 1)
|
||||||
|
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
expect(comm.received_commands[1]).to match(/hostname '#{name}'/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not change the hostname if already set" do
|
||||||
|
comm.stub_command("hostname | grep -w '#{name}'", exit_code: 0)
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
expect(comm.received_commands.size).to eq(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue