Sha256: 074c1d7237d1842c833d15e191b058e847445e700b41b83891d9b3fec1ab4c9d

Contents?: true

Size: 1.61 KB

Versions: 11

Compression:

Stored size: 1.61 KB

Contents

module VagrantPlugins
  module GuestRedHat
    module Cap
      class ChangeHostName

        extend Vagrant::Util::GuestInspection::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
              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
              grep -w '#{name}' /etc/hosts || {
                sed -i'' '1i 127.0.0.1\\t#{name}\\t#{basename}' /etc/hosts
              }
            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
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
vagrant-unbundled-2.2.9.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.8.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.7.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.6.2 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.6.1 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.6.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.5.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.4.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.3.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.2.0 plugins/guests/redhat/cap/change_host_name.rb
vagrant-unbundled-2.2.0.0 plugins/guests/redhat/cap/change_host_name.rb