Sha256: bf2c653dd4c84ad90b47ef3fa37a9b7f4f18b77ee426715ad90218ffa3dc7bbe

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module HAProxyCTL
  module Environment
    attr_accessor :pidof, :config_path, :config, :exec

    def version
      puts "HAProxyCTL #{HAProxyCTL::VERSION}"
    end

    def config_path
      @config_path ||= ENV['HAPROXY_CONFIG'] || '/etc/haproxy/haproxy.cfg'
    end

    def config
      @config ||= File.read(config_path)
    end

    def has_exec?
      !exec.nil?
    end

    def exec
      return(@exec) if @exec

      @exec   = ENV['HAPROXY_BIN']
      @exec ||= `which haproxy`.chomp

      if @exec.empty?
        begin
          `haproxy -v 2>/dev/null`
          @exec = 'haproxy'
        rescue Errno::ENOENT => e
          @exec = nil
        end
      end

      return(@exec)
    end

    def socket
      @socket ||= begin
        config.match /stats socket \s*([^\s]*)/
        $1 || raise("Expecting 'stats socket <UNIX_socket_path>' in #{config_path}")
      end
    end

    def pidfile
      if config.match(/pidfile \s*([^\s]*)/)
        @pidfile = $1
      else
        std_pid = "/var/run/haproxy.pid"
        if File.exists?(std_pid)
          @pidfile = std_pid
        else
          raise("Expecting 'pidfile <pid_file_path>' in #{config_path} or a pid file in #{std_pid}")
        end
      end
    end

    # @return [String, nil] Returns the PID of HAProxy as a string, if running. Nil otherwise.
    def check_running
      if File.exists?(pidfile)
        pid = File.read(pidfile)
        pid.strip!
      end

      # verify this pid exists and is haproxy
      if pid =~ /^\d+$/ and `ps -p #{pid} -o cmd=` =~ /#{exec}/
        return pid
      end
    end
    alias :pidof :check_running
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
haproxyctl-1.0.0 lib/haproxyctl/environment.rb