Skip to content

Commit

Permalink
guests/redhat: Change host name in one command
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Jun 6, 2016
1 parent 665534e commit b91c167
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 147 deletions.
126 changes: 30 additions & 96 deletions plugins/guests/redhat/cap/change_host_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,37 @@ module GuestRedHat
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
new(machine, name).change!
end

attr_reader :machine, :new_hostname

def initialize(machine, new_hostname)
@machine = machine
@new_hostname = new_hostname
end

def change!
return unless should_change?

case machine.guest.capability("flavor")
when :rhel_7
update_hostname_rhel7
update_etc_hosts
else
update_sysconfig
update_hostname
update_etc_hosts
update_dhcp_hostnames
restart_networking
end
end

def should_change?
new_hostname != current_hostname
end

def current_hostname
@current_hostname ||= get_current_hostname
end

def get_current_hostname
hostname = ''
block = lambda do |type, data|
if type == :stdout
hostname += data.chomp
end
comm = machine.communicate

if !comm.test("hostname -f | grep -w '#{name}'", sudo: false)
basename = name.split('.', 2)[0]
comm.sudo <<-EOH.gsub(/^ {14}/, '')
# Update sysconfig
sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
# Update DNS
sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/' /etc/sysconfig/network-scripts/ifcfg-*
# Set the hostname - use hostnamectl if available
echo '#{name}' > /etc/hostname
if command -v hostnamectl; then
hostnamectl set-hostname '#{name}'
else
hostname '#{name}'
fi
# 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 network
service network restart
EOH
end

execute 'hostname -f', error_check: false, &block
execute 'hostname',&block if hostname.empty?
/localhost(\..*)?/.match(hostname) ? '' : hostname
end

def update_sysconfig
sudo "sed -i 's/\\(HOSTNAME=\\).*/\\1#{fqdn}/' /etc/sysconfig/network"
end

def update_hostname
sudo "hostname #{fqdn}"
end

def update_hostname_rhel7
sudo "hostnamectl set-hostname #{fqdn}"
end

# /etc/hosts should resemble:
# 127.0.0.1 host.fqdn.com host localhost ...
def update_etc_hosts
s = '[[:space:]]'
current_fqdn = Regexp.escape(current_hostname)
current_short = Regexp.escape(current_hostname.split('.').first.to_s)
currents = "\\(#{current_fqdn}#{s}\\+\\|#{current_short}#{s}\\+\\)*" unless current_hostname.empty?
local_ip = '127[.]0[.]0[.]1'
search = "^\\(#{local_ip}#{s}\\+\\)#{currents}"
replace = "\\1#{fqdn} "
replace = "#{replace}#{short_hostname} " unless fqdn == short_hostname
expression = ['s', search, replace,''].join('@')

sudo "sed -i '#{expression}' /etc/hosts"
end

def update_dhcp_hostnames
sudo "sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{short_hostname}\"/' /etc/sysconfig/network-scripts/ifcfg-*"
end

def restart_networking
sudo 'service network restart'
end

def fqdn
new_hostname
end

def short_hostname
new_hostname.split('.').first
end

def execute(cmd, opts=nil, &block)
machine.communicate.execute(cmd, opts, &block)
end

def sudo(cmd, opts=nil, &block)
machine.communicate.sudo(cmd, opts, &block)
end
end
end
Expand Down
74 changes: 23 additions & 51 deletions test/unit/plugins/guests/redhat/cap/change_host_name_test.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,42 @@
require File.expand_path("../../../../../base", __FILE__)
require File.expand_path("../../../support/shared/redhat_like_host_name_examples", __FILE__)
require_relative "../../../../base"

describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do
let(:described_class) do
VagrantPlugins::GuestRedHat::Plugin.components.guest_capabilities[:redhat].get(:change_host_name)
let(:caps) do
VagrantPlugins::GuestRedHat::Plugin
.components
.guest_capabilities[:redhat]
end

let(:machine) { double("machine") }
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
let(:guest) { double("guest") }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }

before do
allow(guest).to receive(:capability).and_return(nil)
allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:guest).and_return(guest)
communicator.stub_command('hostname -f', stdout: old_hostname)
communicator.expect_command('hostname -f')
allow(machine).to receive(:communicate).and_return(comm)
end

after do
communicator.verify_expectations!
end

context 'when oldhostname is qualified' do
let(:old_hostname) { 'oldhostname.olddomain.tld' }
let(:similar_hostname) {'oldhostname'}

it_behaves_like 'a full redhat-like host name change'

include_examples 'inserting hostname in /etc/hosts'
include_examples 'swapping simple hostname in /etc/hosts'
include_examples 'swapping qualified hostname in /etc/hosts'
comm.verify_expectations!
end

context 'when oldhostname is simple' do
let(:old_hostname) { 'oldhostname' }
let(:similar_hostname) {'oldhostname.olddomain.tld'}

it_behaves_like 'a full redhat-like host name change'
describe ".change_host_name" do
let(:cap) { caps.get(:change_host_name) }

include_examples 'inserting hostname in /etc/hosts'
include_examples 'swapping simple hostname in /etc/hosts'
let(:name) { "banana-rama.example.com" }

context 'and is only able to be determined by hostname (without -f)' do
before do
communicator.stub_command('hostname -f',nil)
communicator.stub_command('hostname', stdout: old_hostname)
communicator.expect_command('hostname')
end
it "sets the hostname" do
comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1)

it_behaves_like 'a full redhat-like host name change'

include_examples 'inserting hostname in /etc/hosts'
include_examples 'swapping simple hostname in /etc/hosts'
cap.change_host_name(machine, name)
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/)
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/)
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{name}'/)
expect(comm.received_commands[1]).to match(/service network restart/)
end
end

context 'when the short version of hostname is localhost' do
let(:old_hostname) { 'localhost.olddomain.tld' }

it_behaves_like 'a partial redhat-like host name change'

include_examples 'inserting hostname in /etc/hosts'

it "does more even when the provided hostname is not different" do
described_class.change_host_name(machine, old_hostname)
expect(communicator.received_commands.to_set).not_to eq(communicator.expected_commands.keys.to_set)
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

0 comments on commit b91c167

Please sign in to comment.