Class: Roadworker::HealthCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/roadworker/route53-health-check.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route53) ⇒ HealthCheck

of class method



75
76
77
# File 'lib/roadworker/route53-health-check.rb', line 75

def initialize(route53)
  @route53 = route53
end

Class Method Details

.config_to_hash(config) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/roadworker/route53-health-check.rb', line 16

def config_to_hash(config)
  ipaddr = config[:ip_address]
  port   = config[:port]
  type   = config[:type].downcase
  path   = config[:resource_path]
  fqdn   = config[:fully_qualified_domain_name]
  fqdn   = fqdn.downcase if fqdn
  search_string     = config[:search_string]
  request_interval  = config[:request_interval]
  failure_threshold = config[:failure_threshold]

  ulr = nil

  if ipaddr
    url = "#{type}://#{ipaddr}:#{port}"
  else
    url = "#{type}://#{fqdn}:#{port}"
    fqdn = nil
  end

  url << path if path && path != '/'

  {
    :url               => url,
    :host              => fqdn,
    :search_string     => search_string,
    :request_interval  => request_interval,
    :failure_threshold => failure_threshold,
  }
end

.gc(route53, options = {}) ⇒ Object



12
13
14
# File 'lib/roadworker/route53-health-check.rb', line 12

def gc(route53, options = {})
  self.new(route53).gc(options)
end

.health_checks(route53, options = {}) ⇒ Object



8
9
10
# File 'lib/roadworker/route53-health-check.rb', line 8

def health_checks(route53, options = {})
  self.new(route53).health_checks(options)
end

.parse_url(url) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/roadworker/route53-health-check.rb', line 47

def parse_url(url)
  url = URI.parse(url)
  path = url.path

  if path.nil? or path.empty? or path == '/'
    path = nil
  end

  config = {}

  {
    :port          => url.port,
    :type          => url.scheme.upcase,
    :resource_path => path,
  }.each {|key, value|
    config[key] = value if value
  }

  if url.host =~ /\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z/
    config[:ip_address] = url.host
  else
    config[:fully_qualified_domain_name] = url.host
  end

  return config
end

Instance Method Details

#gc(options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/roadworker/route53-health-check.rb', line 121

def gc(options = {})
  AWS.memoize {
    check_list = health_checks
    return if check_list.empty?

    if (logger = options[:logger])
      logger.info('Clean HealthChecks (pass `--no-health-check-gc` if you do not want to clean)')
    end

    @route53.hosted_zones.each do |zone|
      zone.rrsets.each do |record|
        check_list.delete(record.health_check_id)
      end
    end

    check_list.each do |health_check_id, config|
      @route53.client.delete_health_check(:health_check_id  => health_check_id)
    end
  }
end

#health_checks(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/roadworker/route53-health-check.rb', line 79

def health_checks(options = {})
  check_list = {}

  is_truncated = true
  next_marker = nil

  while is_truncated
    opts = next_marker ? {:marker => next_marker} : {}
    response = @route53.client.list_health_checks(opts)

    response[:health_checks].each do |check|
      check_list[check[:id]] = check[:health_check_config]
    end

    is_truncated = response[:is_truncated]
    next_marker = response[:next_marker]
  end

  if options[:extended]
    check_list.instance_variable_set(:@route53, @route53)

    def check_list.find_or_create(attrs)
      health_check_id, config = self.find {|hcid, elems| elems == attrs }

      unless health_check_id
        response = @route53.client.create_health_check({
          :caller_reference    => UUID.new.generate,
          :health_check_config => attrs,
        })

        health_check_id = response[:health_check][:id]
        config = response[:health_check][:health_check_config]
        self[health_check_id] = config
      end

      return health_check_id
    end
  end

  return check_list
end