guests/arch: Change hostname in one command

This commit updates the procedure for changing the hostname on arch
guests to occur in a single command. Previously, setting the hostname
and adding the value of the hostname to the /etc/hosts file was done in
two different uploads. This reduces the cycle to a single upload, making
provisioning a bit faster.

Additionally, this changes the behavior of the /etc/hosts file to:

1. Not remove localhost as an alias of 127.0.0.1
2. Prepend our custom hostname before localhost

The resulting /etc/hosts file will look something like:

127.0.0.1 my-host.example.com my-host
127.0.0.1 localhost.mydomain localhost

Tested against `terrywang/archlinux` using the following Vagrantfile:

```ruby
Vagrant.configure(2) do |config|
  config.vm.box = "terrywang/archlinux"
  config.vm.hostname = "banana-ramama.example.com"

  config.vm.network "private_network", type: "dhcp"

  config.vm.network "private_network", ip: "33.33.33.10"

  config.vm.provision "file", source: "Vagrantfile", destination: "/tmp/vf"
  config.vm.provision "shell", inline: "echo hi"
end
```
This commit is contained in:
Seth Vargo 2016-05-30 21:57:24 -04:00
parent cbc90d6391
commit 41d61120a5
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
2 changed files with 22 additions and 14 deletions

View File

@ -3,12 +3,19 @@ module VagrantPlugins
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
machine.communicate.tap do |comm|
# Only do this if the hostname is not already set
if !comm.test("sudo hostname | grep '#{name}'")
comm.sudo("hostnamectl set-hostname #{name}")
comm.sudo("sed -i 's@^\\(127[.]0[.]0[.]1[[:space:]]\\+\\)@\\1#{name} @' /etc/hosts")
end
comm = machine.communicate
if !comm.test("hostname | grep -w '#{name}'", sudo: true)
basename = name.split(".", 2)[0]
comm.sudo <<-EOH
hostnamectl set-hostname '#{name}'
# Remove comments and blank lines from /etc/hosts
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
# Prepend ourselves to /etc/hosts
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
EOH
end
end
end

View File

@ -9,29 +9,30 @@ describe "VagrantPlugins::GuestArch::Cap::ChangeHostName" do
end
let(:machine) { double("machine") }
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do
allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:communicate).and_return(comm)
end
after do
communicator.verify_expectations!
comm.verify_expectations!
end
describe ".change_host_name" do
let(:hostname) { "example.com" }
let(:hostname) { "banana-rama.example.com" }
it "sets the hostname" do
communicator.stub_command("sudo hostname | grep '#{hostname}'", exit_code: 1)
communicator.expect_command("hostnamectl set-hostname #{hostname}")
comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 1)
described_class.change_host_name(machine, hostname)
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{hostname}'/)
end
it "does not change the hostname if already set" do
communicator.stub_command("sudo hostname | grep '#{hostname}'", exit_code: 0)
comm.stub_command("hostname | grep -w '#{hostname}'", exit_code: 0)
described_class.change_host_name(machine, hostname)
expect(communicator.received_commands.size).to eq(1)
expect(comm.received_commands.size).to eq(1)
end
end
end