Class AutomateIt::ServiceManager::SYSV
In: lib/automateit/service_manager/sysv.rb
Parent: AutomateIt::ServiceManager::BaseDriver

ServiceManager::SYSV

The SYSV driver implements the ServiceManager methods for running?, start and stop on Unix-like platforms that use the System V init process using a /etc/init.d directory.

It also implements a basic enabled? method that‘s very fast but may not work correctly on all SysV platforms. This method should be overridden by more specific drivers when reasonable.

It does not implement the enable and disable methods because these are not standardized and must be implemented using platform-specific drivers, e.g., Chkconfig on RedHat-like platforms.

Methods

Constants

ETC_INITD = "/etc/init.d"

Public Instance methods

See ServiceManager#enabled?

[Source]

# File lib/automateit/service_manager/sysv.rb, line 135
  def enabled?(service)
    return ! Dir["/etc/rc*.d/*"].grep(/\/S\d{2}#{service}$/).empty?
  end

See ServiceManager#restart

[Source]

# File lib/automateit/service_manager/sysv.rb, line 120
  def restart(service, opts={})
    if started?(service, :wait => opts[:pause])
      # We're certain that service is started
      stop_opts = opts.clone
      stop_opts[:force] = true # Don't check again
      stop(service, stop_opts)
    end

    # We're certain that service is stopped
    start_opts = opts.clone
    start_opts[:force] = true # Don't check again
    return start(service, start_opts)
  end

See ServiceManager#running?

[Source]

# File lib/automateit/service_manager/sysv.rb, line 50
  def running?(service, opts={})
    return started?(service, opts)
  end

See ServiceManager#start

[Source]

# File lib/automateit/service_manager/sysv.rb, line 96
  def start(service, opts={})
    if not opts[:force] and started?(service, :wait => opts[:wait])
      # Already started
      return false
    else
      # Needs starting or forced
      tell(service, :start, opts)
      return true
    end
  end

See ServiceManager#started?

[Source]

# File lib/automateit/service_manager/sysv.rb, line 86
  def started?(service, opts={})
    return _started_and_stopped_helper(:started?, service, opts)
  end

See ServiceManager#stop

[Source]

# File lib/automateit/service_manager/sysv.rb, line 108
  def stop(service, opts={})
    if not opts[:force] and stopped?(service, :wait => opts[:wait])
      # Already stopped
      return false
    else
      # Needs stopping or forced
      tell(service, :stop, opts)
      return true
    end
  end

See ServiceManager#stopped?

[Source]

# File lib/automateit/service_manager/sysv.rb, line 91
  def stopped?(service, opts={})
    return ! _started_and_stopped_helper(:stopped?, service, opts)
  end

See ServiceManager#tell

[Source]

# File lib/automateit/service_manager/sysv.rb, line 45
  def tell(service, action, opts={})
    return _run_command(["#{ETC_INITD}/#{service}", action.to_s], opts)
  end

Protected Instance methods

[Source]

# File lib/automateit/service_manager/sysv.rb, line 54
  def _started_and_stopped_helper(kind, service, opts={})
    expected = \
      case kind
      when :started?
        true
      when :stopped?
        false
      else 
        raise ArgumentError.new("unknown kind argument: #{kind}")
      end

    result = tell(service, :status, :check => true)
    nitpick("_sash top: k=%s r=%s e=%s" % [kind, result, expected])
    return result if expected == result
    if opts[:wait]
      timeout = Time.now + opts[:wait]
      while timeout > Time.now
        log.info(PNOTE+" ServiceManager#%s waiting on '%s' for %0.1f more seconds" %
          [kind, service, timeout - Time.now])
        sleep 0.5
        result = tell(service, :status, :check => true)
        nitpick("_sash rep: k=%s r=%s e=%s" % [kind, result, expected])
        break if expected == result
      end
      log.info(PNOTE+" ServiceManager#%s finished waiting for '%s', got: %s" %
        [kind, service, result])
    end
    return result
  end

[Validate]