Sha256: 6b8eb4e00139d60939d9496ff61350651535964c4828bfdc040b0643ae0406d3

Contents?: true

Size: 1.96 KB

Versions: 9

Compression:

Stored size: 1.96 KB

Contents

require 'vagrant/util/guest_hosts'

module VagrantPlugins
  module GuestRedHat
    module Cap
      class ChangeHostName

        extend Vagrant::Util::GuestInspection::Linux
        extend Vagrant::Util::GuestHosts::Linux

        def self.change_host_name(machine, name)
          comm = machine.communicate

          if !comm.test("hostname -f | grep '^#{name}$'", sudo: false)
            basename = name.split('.', 2)[0]
            comm.sudo <<-EOH.gsub(/^ {14}/, '')
              # Update sysconfig
              if [ -f /etc/sysconfig/network ]; then
                sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network
              fi
              # Update DNS
              find /etc/sysconfig/network-scripts -maxdepth 1 -type f -name 'ifcfg-*' | xargs -r sed -i 's/\\(DHCP_HOSTNAME=\\).*/\\1\"#{basename}\"/'
              # Set the hostname - use hostnamectl if available
              echo '#{name}' > /etc/hostname
            EOH

            if hostnamectl?(comm)
              comm.sudo("hostnamectl set-hostname --static '#{name}' ; " \
                "hostnamectl set-hostname --transient '#{name}'")
            else
              comm.sudo("hostname -F /etc/hostname")
            end

            restart_command = "service network restart"

            if systemd?(comm)
              if systemd_networkd?(comm)
                restart_command = "systemctl restart systemd-networkd.service"
              elsif systemd_controlled?(comm, "NetworkManager.service")
                restart_command = "systemctl restart NetworkManager.service"
              end
            end
            comm.sudo(restart_command)
          end
          
          network_with_hostname = machine.config.vm.networks.map {|_, c| c if c[:hostname] }.compact[0]
          if network_with_hostname
            replace_host(comm, name, network_with_hostname[:ip])
          else
            add_hostname_to_loopback_interface(comm, name)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
vagrant-unbundled-2.3.6.0 plugins/guests/redhat/cap/change_host_name.rb
tamtam-vagrant-reload-1.2.1 vendor/cache/vagrant-2092df529ae7/plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.3.3.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.3.2.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.19.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.18.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.16.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.14.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-aws-mkubenka-0.7.2.pre.24 vendor/bundle/ruby/2.7.0/bundler/gems/vagrant-22795b161bf6/plugins/guests/redhat/cap/change_host_name.rb