Class: Deadpool::FailoverProtocol::EtcHosts

Inherits:
Base
  • Object
show all
Defined in:
lib/deadpool/failover_protocol/etc_hosts.rb

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#initialize, #initiate_failover_protocol!

Constructor Details

This class inherits a constructor from Deadpool::FailoverProtocol::Base

Instance Method Details

#preflight_checkObject



21
22
23
# File 'lib/deadpool/failover_protocol/etc_hosts.rb', line 21

def preflight_check
  @client_hosts.map { |h| test_client(h) && verify_client(h) }.all?
end

#promote_to_primary(new_primary) ⇒ Object



58
59
60
61
62
63
# File 'lib/deadpool/failover_protocol/etc_hosts.rb', line 58

def promote_to_primary(new_primary)
  @client_hosts.map do |client_host|
    # logger.debug "client_host: #{client_host}, New Primary: #{new_primary}"
    promote_to_primary_on_client(client_host, new_primary)
  end.all?
end

#promote_to_primary_on_client(client_host, new_primary) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/deadpool/failover_protocol/etc_hosts.rb', line 65

def promote_to_primary_on_client(client_host, new_primary)
  # logger.debug "Assigning #{new_primary} as new primary on #{client_host}"
  command_arguments = "--switch --host_name='#{@service_host_name}' --ip_address='#{new_primary}'"
  command_arguments += " --host_file='#{@service_hosts_file}'" if @service_hosts_file
  output            = run_script_command(client_host, command_arguments)
  logger.debug "Output received From Client: #{output}"

  if output.class == String
    okay = (output =~ /^OK/) != nil
    okay ? logger.info("#{client_host}: " + output.strip) : logger.error("#{client_host}: " + output.strip)
  else
    logger.error "Promote to Primary on Client had a critical failure on '#{client_host}'"
  end

  return okay
end

#setupObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/deadpool/failover_protocol/etc_hosts.rb', line 10

def setup
  @script_path        = @failover_config[:script_path]
  @service_host_name  = @failover_config[:service_host_name]
  @service_hosts_file = @failover_config[:service_hosts_file]
  @client_hosts       = @failover_config[:client_hosts]
  @username           = @failover_config[:username]
  @password           = @failover_config[:password]
  @use_sudo           = @failover_config[:use_sudo]
  @sudo_path          = @failover_config[:sudo_path].nil? ? 'sudo' : @failover_config[:sudo_path]
end

#system_checkObject



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
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/deadpool/failover_protocol/etc_hosts.rb', line 82

def system_check
  writable             = []
  not_writable         = []
  pointed_at_primary   = []
  pointed_at_secondary = []
  pointed_at_neither   = []
    
  # Collect check data
  @client_hosts.each do |client_host|
    if test_client(client_host)
      writable << client_host
    else
      not_writable << client_host
    end

    if verify_client(client_host, @primary_host)
      pointed_at_primary << client_host
    else
      if verify_client(client_host, @secondary_host)
        pointed_at_secondary << client_host
      else
        pointed_at_neither << client_host
      end
    end
  end
    
  # Compile write check data.
  if !writable.empty? && not_writable.empty?
    @state.set_state OK, "Write check passed all servers: #{writable.join(', ')}"
  elsif !writable.empty? && !not_writable.empty?
    @state.set_state WARNING, "Write check passed on: #{writable.join(', ')}"
    @state.add_error_message "Write check failed on #{not_writable.join(', ')}"
  elsif writable.empty?
    @state.set_state WARNING, "Write check failed all servers: #{not_writable.join(', ')}"
  end
    

  # Compile verification data
  if !pointed_at_primary.empty? && pointed_at_secondary.empty? && pointed_at_neither.empty?
    @state.add_message "All client hosts are pointed at the primary."
  elsif pointed_at_primary.empty? && !pointed_at_secondary.empty? && pointed_at_neither.empty?
    @state.escalate_status_code WARNING
    @state.add_error_message "All client hosts are pointed at the secondary."
  else
    @state.escalate_status_code CRITICAL
    @state.add_error_message "Client hosts are pointing in different directions."
  end

  return Deadpool::StateSnapshot.new @state
end

#test_client(client_host) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/deadpool/failover_protocol/etc_hosts.rb', line 25

def test_client(client_host)
  logger.debug "Testing Client #{client_host}"
  output = run_script_command(client_host, '--test')
  logger.debug "Output recieved From Client: #{output}"

  if output.class == String
    okay = (output =~ /^OK/) != nil
    okay ? logger.info("#{client_host}: " + output.strip) : logger.error("#{client_host}: " + output.strip)
  else
    logger.error "Test Client had a critical failure on '#{client_host}'"
  end

  return okay
end

#verify_client(client_host, primary_host = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/deadpool/failover_protocol/etc_hosts.rb', line 40

def verify_client(client_host, primary_host=nil)
  logger.debug "Verifying Client #{client_host}"
  primary_host      = primary_host.nil? ? @primary_host : primary_host
  command_arguments = "--verify --host_name='#{@service_host_name}' --ip_address='#{primary_host}'"
  command_arguments += " --host_file='#{@service_hosts_file}'" if @service_hosts_file
  output            = run_script_command(client_host, command_arguments)
  logger.debug "Output recieved From Client: #{output}"

  if output.class == String
    okay = (output =~ /^OK/) != nil
    okay ? logger.info("#{client_host}: " + output.strip) : logger.error("#{client_host}: " + output.strip)
  else
    logger.error "Verify Client had a critical failure on '#{client_host}'"
  end

  return okay
end