Sha256: b72125503b55e29372800e99efde5d2b6fa532f54d04db9b1ed0b102d22440bd

Contents?: true

Size: 1.8 KB

Versions: 13

Compression:

Stored size: 1.8 KB

Contents

module VagrantPlugins
  module GuestFedora
    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?

          update_etc_hostname
          update_etc_hosts
          refresh_hostname_service
        end

        def should_change?
          new_hostname != current_hostname
        end

        def current_hostname
          @current_hostname ||= get_current_hostname
        end

        def get_current_hostname
          hostname = ""
          sudo "hostname -f" do |type, data|
            hostname = data.chomp if type == :stdout && hostname.empty?
          end

          hostname
        end

        def update_etc_hostname
          sudo("echo '#{short_hostname}' > /etc/hostname")
        end

        # /etc/hosts should resemble:
        # 127.0.0.1   localhost
        # 127.0.1.1   host.fqdn.com host.fqdn host
        def update_etc_hosts
          ip_address = '([0-9]{1,3}\.){3}[0-9]{1,3}'
          search     = "^(#{ip_address})\\s+#{Regexp.escape(current_hostname)}(\\s.*)?$"
          replace    = "\\1 #{fqdn} #{short_hostname}"
          expression = ['s', search, replace, 'g'].join('@')

          sudo("sed -ri '#{expression}' /etc/hosts")
        end

        def refresh_hostname_service
          sudo("hostname -F /etc/hostname")
        end

        def fqdn
          new_hostname
        end

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

        def sudo(cmd, &block)
          machine.communicate.sudo(cmd, &block)
        end
      end
    end
  end
end

Version data entries

13 entries across 10 versions & 3 rubygems

Version Path
vagrant-registration-0.0.15 plugins/guests/fedora/cap/change_host_name.rb
vagrant-cloudstack-1.1.0 vendor/bundle/bundler/gems/vagrant-1cf2a8db4ccb/plugins/guests/fedora/cap/change_host_name.rb
vagrant-cloudstack-1.1.0 vendor/bundle/bundler/gems/vagrant-272fb27e0536/plugins/guests/fedora/cap/change_host_name.rb
vagrant-cloudstack-1.1.0 vendor/bundle/bundler/gems/vagrant-309e896975d1/plugins/guests/fedora/cap/change_host_name.rb
vagrant-cloudstack-1.1.0 vendor/bundle/bundler/gems/vagrant-b421af58e8b3/plugins/guests/fedora/cap/change_host_name.rb
vagrant-registration-0.0.13 plugins/guests/fedora/cap/change_host_name.rb
vagrant-registration-0.0.12 plugins/guests/fedora/cap/change_host_name.rb
vagrant-registration-0.0.11 plugins/guests/fedora/cap/change_host_name.rb
vagrant-registration-0.0.10 plugins/guests/fedora/cap/change_host_name.rb
vagrant-registration-0.0.9 plugins/guests/fedora/cap/change_host_name.rb
vagrant-registration-0.0.8 plugins/guests/fedora/cap/change_host_name.rb
vagrant-registration-0.0.7 plugins/guests/fedora/cap/change_host_name.rb
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/bundler/gems/vagrant-1e28f1ac31e7/plugins/guests/fedora/cap/change_host_name.rb