guests/redhat: Change host name in one command
This commit is contained in:
parent
665534e620
commit
b91c167b19
|
@ -3,104 +3,38 @@ module VagrantPlugins
|
||||||
module Cap
|
module Cap
|
||||||
class ChangeHostName
|
class ChangeHostName
|
||||||
def self.change_host_name(machine, name)
|
def self.change_host_name(machine, name)
|
||||||
new(machine, name).change!
|
comm = machine.communicate
|
||||||
end
|
|
||||||
|
|
||||||
attr_reader :machine, :new_hostname
|
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
|
||||||
|
|
||||||
def initialize(machine, new_hostname)
|
# Update DNS
|
||||||
@machine = machine
|
sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/' /etc/sysconfig/network-scripts/ifcfg-*
|
||||||
@new_hostname = new_hostname
|
|
||||||
end
|
|
||||||
|
|
||||||
def change!
|
# Set the hostname - use hostnamectl if available
|
||||||
return unless should_change?
|
echo '#{name}' > /etc/hostname
|
||||||
|
if command -v hostnamectl; then
|
||||||
|
hostnamectl set-hostname '#{name}'
|
||||||
|
else
|
||||||
|
hostname '#{name}'
|
||||||
|
fi
|
||||||
|
|
||||||
case machine.guest.capability("flavor")
|
# Remove comments and blank lines from /etc/hosts
|
||||||
when :rhel_7
|
sed -i'' -e 's/#.*$//' -e '/^$/d' /etc/hosts
|
||||||
update_hostname_rhel7
|
|
||||||
update_etc_hosts
|
# Prepend ourselves to /etc/hosts
|
||||||
else
|
grep -w '#{name}' /etc/hosts || {
|
||||||
update_sysconfig
|
sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
|
||||||
update_hostname
|
}
|
||||||
update_etc_hosts
|
|
||||||
update_dhcp_hostnames
|
# Restart network
|
||||||
restart_networking
|
service network restart
|
||||||
|
EOH
|
||||||
end
|
end
|
||||||
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
|
|
||||||
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,70 +1,42 @@
|
||||||
require File.expand_path("../../../../../base", __FILE__)
|
require_relative "../../../../base"
|
||||||
require File.expand_path("../../../support/shared/redhat_like_host_name_examples", __FILE__)
|
|
||||||
|
|
||||||
describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do
|
describe "VagrantPlugins::GuestRedHat::Cap::ChangeHostName" do
|
||||||
let(:described_class) do
|
let(:caps) do
|
||||||
VagrantPlugins::GuestRedHat::Plugin.components.guest_capabilities[:redhat].get(:change_host_name)
|
VagrantPlugins::GuestRedHat::Plugin
|
||||||
|
.components
|
||||||
|
.guest_capabilities[:redhat]
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:machine) { double("machine") }
|
let(:machine) { double("machine") }
|
||||||
let(:communicator) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
|
||||||
let(:guest) { double("guest") }
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(guest).to receive(:capability).and_return(nil)
|
allow(machine).to receive(:communicate).and_return(comm)
|
||||||
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')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
after do
|
||||||
communicator.verify_expectations!
|
comm.verify_expectations!
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when oldhostname is qualified' do
|
describe ".change_host_name" do
|
||||||
let(:old_hostname) { 'oldhostname.olddomain.tld' }
|
let(:cap) { caps.get(:change_host_name) }
|
||||||
let(:similar_hostname) {'oldhostname'}
|
|
||||||
|
|
||||||
it_behaves_like 'a full redhat-like host name change'
|
let(:name) { "banana-rama.example.com" }
|
||||||
|
|
||||||
include_examples 'inserting hostname in /etc/hosts'
|
it "sets the hostname" do
|
||||||
include_examples 'swapping simple hostname in /etc/hosts'
|
comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 1)
|
||||||
include_examples 'swapping qualified hostname in /etc/hosts'
|
|
||||||
end
|
|
||||||
|
|
||||||
context 'when oldhostname is simple' do
|
cap.change_host_name(machine, name)
|
||||||
let(:old_hostname) { 'oldhostname' }
|
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network/)
|
||||||
let(:similar_hostname) {'oldhostname.olddomain.tld'}
|
expect(comm.received_commands[1]).to match(/\/etc\/sysconfig\/network-scripts\/ifcfg/)
|
||||||
|
expect(comm.received_commands[1]).to match(/hostnamectl set-hostname '#{name}'/)
|
||||||
it_behaves_like 'a full redhat-like host name change'
|
expect(comm.received_commands[1]).to match(/service network restart/)
|
||||||
|
|
||||||
include_examples 'inserting hostname in /etc/hosts'
|
|
||||||
include_examples 'swapping simple hostname in /etc/hosts'
|
|
||||||
|
|
||||||
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_behaves_like 'a full redhat-like host name change'
|
|
||||||
|
|
||||||
include_examples 'inserting hostname in /etc/hosts'
|
|
||||||
include_examples 'swapping simple hostname in /etc/hosts'
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
context 'when the short version of hostname is localhost' do
|
it "does not change the hostname if already set" do
|
||||||
let(:old_hostname) { 'localhost.olddomain.tld' }
|
comm.stub_command("hostname -f | grep -w '#{name}'", exit_code: 0)
|
||||||
|
cap.change_host_name(machine, name)
|
||||||
it_behaves_like 'a partial redhat-like host name change'
|
expect(comm.received_commands.size).to eq(1)
|
||||||
|
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue