21
1
Fork 0
mirror of https://github.com/Evolix/chexpire.git synced 2024-04-27 14:30:49 +02:00
chexpire/app/services/ssl.rb
2018-07-02 17:21:08 +02:00

67 lines
1.6 KiB
Ruby

require "null_logger"
require "system_command"
require_relative "ssl/parser"
require_relative "ssl/response"
require_relative "ssl/errors"
module SSL
class << self
def ask(domain, system_klass: SystemCommand, logger: NullLogger.new)
Service.new(domain, system_klass: system_klass, logger: logger).call
end
end
class Service
attr_reader :domain
attr_reader :logger
attr_reader :system_klass
attr_reader :configuration
def initialize(domain, system_klass: SystemCommand, configuration: nil, logger: NullLogger.new)
@domain = domain
@logger = logger
@system_klass = system_klass
@configuration = configuration || default_configuration
end
def call
result = run_command
parse(result)
rescue StandardError => ex
logger.log :service_error, ex
raise
end
def run_command
command = system_klass.new(check_http_path, check_http_args, logger: logger)
result = command.execute
unless result.exit_status.zero?
fail SSLCommandError, "SSL command failed with status #{result.exit_status}"
end
result
end
def parse(result)
parser = Parser.new(domain, logger: logger)
parser.parse(result.stdout)
end
def check_http_path
configuration.check_http_path.presence || "check_http"
end
def check_http_args
[
configuration.check_http_args.presence,
"-H '#{domain}'",
].compact
end
def default_configuration
OpenStruct.new(Rails.configuration.chexpire.fetch("checks_ssl") { {} })
end
end
end