class SyncWrap::RHEL

Customizations for RedHat Enterprise Linux and base class for derivatives like CentOS and AmazonLinux.

Attributes

distro_version[RW]

RHEL version, i.e. '6'. No default value.

rhel_version[RW]

RHEL version, i.e. '6'. No default value.

systemd[W]

Set true/false to override the default, distro version based determination of whether systemd is PID 1 on the system.

Public Class Methods

new( opts = {} ) click to toggle source
Calls superclass method SyncWrap::Distro.new
# File lib/syncwrap/components/rhel.rb, line 44
def initialize( opts = {} )
  super
end

Public Instance Methods

dist_enable_init_service( name ) click to toggle source

Enable a service by (short) name either via RHEL/System V `chkconfig on` or systemd `systemctl enable`.

# File lib/syncwrap/components/rhel.rb, line 141
def dist_enable_init_service( name )
  if systemd?
    systemctl( 'enable', dot_service( name ) )
  else
    sudo "/sbin/chkconfig #{name} on"
  end
end
dist_if_not_installed?( pkgs, chk = true, opts = {}, &block ) click to toggle source

If chk is true, then wrap block in a sudo bash conditional that tests if any specified pkgs are not installed. Otherwise just yield to block.

# File lib/syncwrap/components/rhel.rb, line 124
def dist_if_not_installed?( pkgs, chk = true, opts = {}, &block )
  if chk
    pkgs = Array( pkgs )
    cnt = "rpm -q #{pkgs.join ' '} | grep -cv 'not installed'"
    sudo_if( %Q{[ "$(#{cnt})" != "#{pkgs.count}" ]}, opts, &block )
  else
    block.call
  end
end
dist_install( *pkgs ) click to toggle source

Install the specified packages. When rpm HTTP URLs or local file paths are given instead of package names, these are installed first and individually via dist_install_url. Calling that explicitly may be preferable. A trailing hash is interpreted as options, see below.

Options

:check_install

Short-circuit if packages are already installed, and thus don't perform updates unless versions are specified. (Default: true)

:yum_flags

Additional array of flags to pass to `yum install`.

Options are also passed to the sudo calls.

# File lib/syncwrap/components/rhel.rb, line 70
def dist_install( *pkgs )
  opts = pkgs.last.is_a?( Hash ) && pkgs.pop || {}
  chk = opts[ :check_install ]
  chk = check_install? if chk.nil?
  flags = Array( opts[ :yum_flags ] )
  pkgs.flatten!
  rpms,names = pkgs.partition { |p| p =~ /\.rpm$/ || p =~ /^http(s)?:/i }
  rpms.each do |url|
    dist_install_url( url, nil, opts )
  end
  !names.empty? && dist_if_not_installed?( names, chk != false, opts ) do
    sudo( "yum install -q -y #{(flags + names).join ' '}", opts )
  end
end
dist_install_init_service( name ) click to toggle source

Install a System V style init.d service script

# File lib/syncwrap/components/rhel.rb, line 135
def dist_install_init_service( name )
  sudo "/sbin/chkconfig --add #{name}"
end
dist_install_url( url, name = nil, opts = {} ) click to toggle source

Install packages by HTTP URL or local file path to rpm. Uses name to check_install. If not specified, name is deduced via `File.basename( url, '.rpm' )`. It is not recommended to set option `check_install: false`, because `yum` will fail with “Error: Nothing to do” if given a file/URL and the package is already installed.

Options

:check_install

Short-circuit if package is already installed. (Default: true)

:yum_flags

Additional array of flags to pass to `yum install`.

Options are also passed to the sudo calls.

# File lib/syncwrap/components/rhel.rb, line 100
def dist_install_url( url, name = nil, opts = {} )
  name ||= File.basename( url, '.rpm' )
  chk = opts[ :check_install ]
  flags = Array( opts[ :yum_flags ] )
  dist_if_not_installed?( name, chk != false, opts ) do
    sudo( "yum install -q -y #{(flags + [url]).join ' '}", opts )
  end
end
dist_service( *args ) click to toggle source

Run the service command typically supporting 'start', 'stop', 'restart', 'status', etc. actions. Arguments are in order: shortname, action

# File lib/syncwrap/components/rhel.rb, line 152
def dist_service( *args )
  if systemd?
    dist_service_via_systemctl( *args )
  else
    sudo( [ '/sbin/service', *args ].join( ' ' ) )
  end
end
dist_uninstall( *pkgs ) click to toggle source

Uninstall the specified package names. A trailing hash is interpreted as options. These are passed to the sudo.

# File lib/syncwrap/components/rhel.rb, line 111
    def dist_uninstall( *pkgs )
      opts = pkgs.last.is_a?( Hash ) && pkgs.pop || {}
      pkgs.flatten!
      sudo( "        if yum list -C -q installed #{pkgs.join ' '} >/dev/null 2>&1; then
          yum remove -q -y #{pkgs.join ' '}
        fi
", opts )
    end
systemd?() click to toggle source
# File lib/syncwrap/components/rhel.rb, line 48
def systemd?
  if @systemd.nil?
    @systemd = version_gte?( rhel_version, [7] )
  end
  @systemd
end