Class: KingslyCertbot::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/kingsly_certbot/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
12
13
14
# File 'lib/kingsly_certbot/runner.rb', line 7

def initialize(args)
  raise 'Argument passed is not of type Array' if args.class != Array
  raise '--config argument missing' if args[0].nil? || args[0].strip == ''
  raise "Unknown argument '#{args[0]}'" if args[0] != '--config'
  raise "Config file does not exist at '#{args[1]}'" unless File.exist?(args[1])

  @config_path = args[1]
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



5
6
7
# File 'lib/kingsly_certbot/runner.rb', line 5

def configuration
  @configuration
end

Instance Method Details

#configureObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kingsly_certbot/runner.rb', line 16

def configure
  begin
    local_config = YAML.load_file(@config_path)
    $logger.info("Loaded config file from #{@config_path}")
  rescue Psych::SyntaxError => e
    raise StandardError, "Invalid YAML config file '#{@config_path}', original message: '#{e.message}'"
  end

  @configuration = KingslyCertbot::Configuration.new(local_config)
  $logger.info("Loaded configuration: #{@configuration}")
  Raven.configure do |config|
    config.dsn = @configuration.sentry_dsn
    config.encoding = 'json'
    config.environments = %w[production integration]
    config.current_environment = @configuration.environment
    config.logger = $logger
    config.release = KingslyCertbot::VERSION
  end
  self
end

#executeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kingsly_certbot/runner.rb', line 37

def execute
  @configuration.validate!
  $logger.info("Querying Kingsly server for certificate to domain #{@configuration.sub_domain}.#{@configuration.top_level_domain}")
  cert_bundle = KingslyClient.get_cert_bundle(
    kingsly_server_host: @configuration.kingsly_server_host,
    kingsly_server_port: @configuration.kingsly_server_port,
    top_level_domain: @configuration.top_level_domain,
    sub_domain: @configuration.sub_domain,
    kingsly_http_read_timeout: @configuration.kingsly_http_read_timeout,
    kingsly_http_open_timeout: @configuration.kingsly_http_open_timeout
  )
  $logger.info("Updating assets for server type #{@configuration.server_type}")
  adapter = case @configuration.server_type
            when 'ipsec'
              IpSecCertAdapter.new(cert_bundle, @configuration.ipsec_root)
            else
              raise "Unsupported server type #{@configuration.server_type}"
            end
  if adapter.update_assets
    adapter.restart_service
    $logger.info("Updated assets for server type #{@configuration.server_type} :: Restarting service")
  else
    $logger.info("The assets are already in their latest change for server type #{@configuration.server_type}. :: Not restarting the service")
  end
rescue StandardError => e
  $logger.error('FAILED - Kingsly Certbot execution failed for following reason:')
  $logger.error(e.message)
  $logger.error e.backtrace.join("\n")
  Raven.capture_exception(e, 'Failed in KingslyCertbot::Runner.execute operation')
end