Module: Rhapr::Environment

Included in:
Interface
Defined in:
lib/rhapr/environment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configString (readonly)

Returns The raw contents of the HAProxy configuration file.

Returns:

  • (String)

    The raw contents of the HAProxy configuration file.

Raises:

  • (RuntimeError)

    If it cannot read the contents of #config_path.



25
26
27
# File 'lib/rhapr/environment.rb', line 25

def config
  @config
end

#config_pathString? (readonly)

Returns The path to the HAProxy configuration file, or nil if not found. Set the ENV variable $HAPROXY_CONFIG to override defaults.

Returns:

  • (String, nil)

    The path to the HAProxy configuration file, or nil if not found. Set the ENV variable $HAPROXY_CONFIG to override defaults.



8
9
10
# File 'lib/rhapr/environment.rb', line 8

def config_path
  @config_path
end

#execString? (readonly)

Returns The path to the HAProxy executable will be returned, if found. Set ENV variable $HAPROXY_BIN to override.

Returns:

  • (String, nil)

    The path to the HAProxy executable will be returned, if found. Set ENV variable $HAPROXY_BIN to override



40
41
42
# File 'lib/rhapr/environment.rb', line 40

def exec
  @exec
end

#haproxy_pidObject (readonly)

Returns the value of attribute haproxy_pid.



5
6
7
# File 'lib/rhapr/environment.rb', line 5

def haproxy_pid
  @haproxy_pid
end

#socket_pathString (readonly)

@todo: Should there be an ENV var for this? Perhaps allow config-less runs of rhapr?

Returns:

  • (String)

    The path to the HAProxy stats socket.

Raises:

  • (RuntimeError)

    Raised if no stats socket has been specified, in the HAProxy configuration.



71
72
73
# File 'lib/rhapr/environment.rb', line 71

def socket_path
  @socket_path
end

Instance Method Details

#check_runningString? Also known as: pidof

@todo: Look for something other than pidof, for searching the process list.

Could read from the pid file, but there's potential that it will go stale.

Returns:

  • (String, nil)

    Returns the PID of HAProxy as a string, if running. Nil otherwise.



91
92
93
94
95
96
# File 'lib/rhapr/environment.rb', line 91

def check_running
  pidof = `pidof haproxy`
  pidof.strip!

  return pidof unless pidof.empty?
end

#has_exec?true, false

Returns Whether or not the HAProxy executable can be found.

Returns:

  • (true, false)

    Whether or not the HAProxy executable can be found.

See Also:



35
36
37
# File 'lib/rhapr/environment.rb', line 35

def has_exec?
  !exec.nil?
end

#pidString

@todo: Should there even be an assumption? Does HAProxy create a pid file, if not told to by the configuration? @todo: Should there be an ENV var for this? Perhaps allow config-less runs of rhapr?

Returns:

  • (String)

    Returns the path to the pidfile, specified in the HAProxy configuration. Returns an assumption, if not found.



81
82
83
84
85
86
# File 'lib/rhapr/environment.rb', line 81

def pid
  @pid  ||= begin
              config.match /pidfile ([^\s]*)/
              $1 || '/var/run/haproxy.pid'
            end
end

#socketUNIXSocket

Returns A connection to the HAProxy Socket.

Returns:

  • (UNIXSocket)

    A connection to the HAProxy Socket

Raises:

  • (RuntimeError)

    Raised if a socket connection could not be established



60
61
62
63
64
65
66
# File 'lib/rhapr/environment.rb', line 60

def socket
  begin
    UNIXSocket.open(socket_path)
  rescue Errno::EACCES => e
    raise RuntimeError.new("Could not open a socket with HAProxy. Error message: #{e.message}")
  end
end