Sha256: 7d63796d6895caabced0db1c09e9adeff1850ab7561d4c5ad6c023dac43d9285

Contents?: true

Size: 1.72 KB

Versions: 24

Compression:

Stored size: 1.72 KB

Contents

module VagrantPlugins
  module GuestALT
    module Cap
      class ChangeHostName
        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}/, '')
              # Save current hostname saved in /etc/hosts
              CURRENT_HOSTNAME_FULL="$(hostname -f)"
              CURRENT_HOSTNAME_SHORT="$(hostname -s)"

              # New hostname to be saved in /etc/hosts
              NEW_HOSTNAME_FULL='#{name}'
              NEW_HOSTNAME_SHORT="${NEW_HOSTNAME_FULL%%.*}"

              # Update sysconfig
              sed -i 's/\\(HOSTNAME=\\).*/\\1#{name}/' /etc/sysconfig/network

              # Set the hostname - use hostnamectl if available
              if command -v hostnamectl; then
                hostnamectl set-hostname --static '#{name}'
                hostnamectl set-hostname --transient '#{name}'
              else
                hostname '#{name}'
              fi

              # Update ourselves in /etc/hosts
              if grep -w "$CURRENT_HOSTNAME_FULL" /etc/hosts; then
                sed -i -e "s/\(\s\)$CURRENT_HOSTNAME_FULL\(\s\)/\1$NEW_HOSTNAME_FULL\2/g" -e "s/\(\s\)$CURRENT_HOSTNAME_FULL$/\1$NEW_HOSTNAME_FULL/g" /etc/hosts
              fi
              if grep -w "$CURRENT_HOSTNAME_SHORT" /etc/hosts; then
                sed -i -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT\(\s\)/\1$NEW_HOSTNAME_SHORT\2/g" -e "s/\(\s\)$CURRENT_HOSTNAME_SHORT$/\1$NEW_HOSTNAME_SHORT/g" /etc/hosts
              fi

              # Restart network
              service network restart
            EOH
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 20 versions & 4 rubygems

Version Path
vagrant-unbundled-2.2.5.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.2.4.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.2.3.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.2.2.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.2.0.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.1.4.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.1.2.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/alt/cap/change_host_name.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/alt/cap/change_host_name.rb
vagrant-packet-0.1.2 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/alt/cap/change_host_name.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/alt/cap/change_host_name.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/alt/cap/change_host_name.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/bundler/gems/vagrant-04f7215b5e3f/plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.1.1.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.0.4.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.0.3.0 plugins/guests/alt/cap/change_host_name.rb
vagrant-aws-detiber-0.7.2.pre.4 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/alt/cap/change_host_name.rb
vagrant-aws-detiber-0.7.2.pre.3 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/alt/cap/change_host_name.rb
vagrant-aws-detiber-0.7.2.pre.2 vendor/bundle/ruby/2.3.0/bundler/gems/vagrant-419afb4dcffe/plugins/guests/alt/cap/change_host_name.rb
vagrant-unbundled-2.0.2.0 plugins/guests/alt/cap/change_host_name.rb