guests/photon: Change host name in one command
This commit is contained in:
parent
a9fb66b3e3
commit
2e943428a9
|
@ -3,10 +3,23 @@ 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
|
||||||
unless comm.test("sudo hostname --fqdn | grep '#{name}'")
|
|
||||||
comm.sudo("hostname #{name.split('.')[0]}")
|
if !comm.test("hostname -f | grep -w '#{name}'", sudo: false)
|
||||||
end
|
basename = name.split(".", 2)[0]
|
||||||
|
comm.sudo <<-EOH.gsub(/^ {14}/, '')
|
||||||
|
# Set the hostname
|
||||||
|
echo '#{name}' > /etc/hostname
|
||||||
|
hostname '#{name}'
|
||||||
|
|
||||||
|
# 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
|
||||||
|
}
|
||||||
|
EOH
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,34 +1,42 @@
|
||||||
# encoding: UTF-8
|
|
||||||
# Copyright (c) 2015 VMware, Inc. All Rights Reserved.
|
# Copyright (c) 2015 VMware, Inc. All Rights Reserved.
|
||||||
|
|
||||||
require File.expand_path("../../../../../base", __FILE__)
|
require_relative "../../../../base"
|
||||||
|
|
||||||
describe "VagrantPlugins::GuestPhoton::Cap::ChangeHostName" do
|
describe "VagrantPlugins::GuestPhoton::Cap::ChangeHostName" do
|
||||||
let(:described_class) do
|
let(:caps) do
|
||||||
VagrantPlugins::GuestPhoton::Plugin.components.guest_capabilities[:photon].get(:change_host_name)
|
VagrantPlugins::GuestPhoton::Plugin
|
||||||
|
.components
|
||||||
|
.guest_capabilities[:photon]
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:machine) { double("machine") }
|
let(:machine) { double("machine") }
|
||||||
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(machine).to receive(:communicate).and_return(communicator)
|
allow(machine).to receive(:communicate).and_return(comm)
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
communicator.verify_expectations!
|
comm.verify_expectations!
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should change hostname when hostname is differ from current' do
|
describe ".change_host_name" do
|
||||||
hostname = 'vagrant-photon'
|
let(:cap) { caps.get(:change_host_name) }
|
||||||
expect(communicator).to receive(:test).with("sudo hostname --fqdn | grep 'vagrant-photon'")
|
|
||||||
communicator.should_receive(:sudo).with("hostname #{hostname.split('.')[0]}")
|
|
||||||
described_class.change_host_name(machine, hostname)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should not change hostname when hostname equals current' do
|
let(:name) { "banana-rama.example.com" }
|
||||||
hostname = 'vagrant-photon'
|
|
||||||
communicator.stub(:test).and_return(true)
|
it "sets the hostname" do
|
||||||
communicator.should_not_receive(:sudo)
|
comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1)
|
||||||
described_class.change_host_name(machine, hostname)
|
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
expect(comm.received_commands[1]).to match(/echo '#{name}' > \/etc\/hostname/)
|
||||||
|
expect(comm.received_commands[1]).to match(/hostname '#{name}'/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not change the hostname if already set" do
|
||||||
|
comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0)
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
|
expect(comm.received_commands.size).to eq(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue