class SyncWrap::Network

Make updates to system configration for hostname and name resolution. These changes are distro specific.

Host component dependencies: <Distro>

Attributes

dns_domain[RW]

A dns search list (a single domain or space delimited list of domains) for addition to the resolv.conf search option, etc. (default: nil)

dns_domain=[RW]

A dns search list (a single domain or space delimited list of domains) for addition to the resolv.conf search option, etc. (default: nil)

Public Class Methods

new( opts = {} ) click to toggle source
Calls superclass method SyncWrap::Component.new
# File lib/syncwrap/components/network.rb, line 43
def initialize( opts = {} )
  @dns_search = nil
  super
end

Public Instance Methods

install() click to toggle source
# File lib/syncwrap/components/network.rb, line 48
def install
  update_hostname
  update_resolver
end
set_etc_hostname( name ) click to toggle source

Test if change to etc/hostname is needed. If so also immediately set (in kernel) hostname.

# File lib/syncwrap/components/network.rb, line 69
def set_etc_hostname( name )
  sudo <<-SH
    if [ ! -e /etc/hostname -o "$(< /etc/hostname)" != "#{name}" ]; then
      echo #{name} > /etc/hostname
      hostname #{name}
    fi
  SH
end
set_etc_hosts_name( name ) click to toggle source
# File lib/syncwrap/components/network.rb, line 78
def set_etc_hosts_name( name )
  f = '/etc/hosts'
  # If name not already in /etc/hosts append it to the beginning
  # of the file.
  sudo <<-SH
    if ! grep -q -E '\s#{name}(\s|$)' #{f}; then
      cp -f #{f} #{f}~
      echo '127.0.0.1 #{name}' > #{f}
      cat #{f}~ >> #{f}
    fi
  SH
end
set_sysconfig_network( name ) click to toggle source

Test if change to etc/sysconfig/network is needed. If so also immediately set (in kernel) hostname.

# File lib/syncwrap/components/network.rb, line 93
def set_sysconfig_network( name )
  # If not already set correctly, backup, delete old line, append
  # new line.
  sudo <<-SH
    if ! grep -q '^HOSTNAME=#{name}$' /etc/sysconfig/network; then
      cp -f /etc/sysconfig/network /etc/sysconfig/network~
      sed -i '/^HOSTNAME=.*/d' /etc/sysconfig/network
      echo 'HOSTNAME=#{name}' >> /etc/sysconfig/network
      hostname #{name}
    fi
  SH
end
update_hostname() click to toggle source
# File lib/syncwrap/components/network.rb, line 53
def update_hostname
  name = host.name
  if ( distro.is_a?( AmazonLinux ) ||
       ( distro.is_a?( RHEL ) && version_lt?( rhel_version, [7] ) ) )
    set_sysconfig_network( name )
  else
    set_etc_hostname( name )
  end

  if distro.is_a?( Debian ) && !dns_search
    set_etc_hosts_name( name )
  end
end
update_resolver() click to toggle source
# File lib/syncwrap/components/network.rb, line 106
def update_resolver
  if dns_search
    case distro
    when RHEL
      update_resolv_conf

      f='/etc/sysconfig/network-scripts/ifcfg-eth0'
      sudo <<-SH
        if ! grep -q '^SEARCH=.*#{dns_search}' #{f}; then
          cp -f #{f} #{f}~
          sed -i '/^SEARCH=/d' #{f}
          echo 'SEARCH="#{dns_search}"' >> #{f}
        fi
      SH

    when Debian
      if distro.is_a?( Ubuntu )
        # As of 12.04 - Precise; /etc/resolv.conf is a symlink that
        # should not be lost.
        update_resolv_conf( '/run/resolvconf/resolv.conf' )
      else
        update_resolv_conf
      end

      f='/etc/network/interfaces'
      sudo <<-SH
        if ! grep -E -q '^\\s*dns-search #{dns_search}' #{f}; then
          cp -f #{f} #{f}~
          sed -r -i '/^\\s*dns-search/d' #{f}
          echo '     dns-search #{dns_search}' >> #{f}
        fi
      SH

    end
  end
end

Protected Instance Methods

update_resolv_conf( f = '/etc/resolv.conf' ) click to toggle source

Make a temporary adjustment to search domains directly to the resolv.conf file.

# File lib/syncwrap/components/network.rb, line 147
def update_resolv_conf( f = '/etc/resolv.conf' )
  sudo <<-SH
    if ! grep -q '^search.* #{dns_search}' #{f}; then
      cp -f #{f} #{f}~
      sed -i '/^search/d' #{f}
      echo 'search #{dns_search}' >> #{f}
    fi
  SH
end