Class: Deadpool::Admin

Inherits:
Object
  • Object
show all
Defined in:
lib/deadpool/admin.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Admin

Returns a new instance of Admin.



13
14
15
# File 'lib/deadpool/admin.rb', line 13

def initialize(argv)
  @argv     = argv
end

Instance Method Details

#execute_command(options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/deadpool/admin.rb', line 88

def execute_command(options)
  case options[:command]
  when :help
    help
  when :full_report
    full_report options
  when :nagios_report
    nagios_report options
  when :promote_server
    promote_server options
  when :stop
    stop options
  when :start
    start options
  when :foreground
    foreground options
  else
    help
  end
end

#foreground(options) ⇒ Object



168
169
170
# File 'lib/deadpool/admin.rb', line 168

def foreground(options)
  puts Deadpool::Server.new(options).run(false)
end

#full_report(options) ⇒ Object



117
118
119
# File 'lib/deadpool/admin.rb', line 117

def full_report(options)
  puts send_command_to_deadpool_server :command => 'full_report'
end

#help(message = nil) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/deadpool/admin.rb', line 109

def help(message=nil)
  unless message.nil?
    puts message
  end
  puts @option_parser.help
  exit 4
end

#nagios_report(options) ⇒ Object



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

def nagios_report(options)
  response = send_command_to_deadpool_server :command => 'nagios_report'

  if (response.to_s =~ /^OK/) != nil
    puts response.to_s
    exit OK
  elsif (response.to_s =~ /^WARNING/) != nil
    puts response.to_s
    exit WARNING
  elsif (response.to_s =~ /^CRITICAL/) != nil
    puts response.to_s
    exit CRITICAL
  elsif (response.to_s =~ /^UNKNOWN/) != nil
    puts response.to_s
    exit UNKNOWN
  else
    puts "UNKNOWN - #{response.to_s}"
    exit UNKNOWN
  end
end

#parse_command_lineObject



24
25
26
27
28
29
30
31
32
33
34
35
36
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/deadpool/admin.rb', line 24

def parse_command_line
  options               = Hash.new
  options[:command_count] = 0
  options[:config_path]     = '/etc/deadpool'

  @option_parser = OptionParser.new do |opts|
    opts.banner = "Usage: deadpool_hosts --command [options]"

    opts.separator "Commands:"
    opts.on("-h", "--help", "Print this help message.") do |help|
      options[:command_count] += 1
      options[:command]        = :help
    end
    opts.on("--full_report", "Give the full system report.") do |full_report|
      options[:command_count] += 1
      options[:command]        = :full_report
    end
    opts.on("--nagios_report", "Report system state in Nagios plugin format.") do |nagios_report|
      options[:command_count] += 1
      options[:command]        = :nagios_report
    end
    opts.on("--promote_server", "Promote specified server to the master.") do |nagios_report|
      options[:command_count] += 1
      options[:command]        = :promote_server
    end
    opts.on("--stop", "Stop the server.") do |stop|
      options[:command_count] += 1
      options[:command]        = :stop
    end
    opts.on("--start", "Start the server in the background.") do |stop|
      options[:command_count] += 1
      options[:command]        = :start
    end
    opts.on("--foreground", "Start the server in the foreground.") do |stop|
      options[:command_count] += 1
      options[:command]        = :foreground
    end

    opts.separator "Options:"
    opts.on("--server=SERVER_LABEL", String, "primary_host or secondary_host.") do |server|
      options[:server] = server
    end
    opts.on("--pool=POOL_NAME", String, "Deadpool name to operate on.") do |pool|
      options[:pool] = pool
    end
    opts.on("--config_path=PATH", String, 
        "Path to configs and custom plugins. #{options[:config_path]} by default.") do |config_path|
      options[:config_path] = config_path
    end
  end

  remaining_arguments = @option_parser.parse! @argv

  unless remaining_arguments.empty?
    help "[#{remaining_arguments.join(' ')}] is not understood."
  end

  if options[:command_count] == 0
    help "You must specify a command."
  end

  return options
end

#promote_server(options) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/deadpool/admin.rb', line 142

def promote_server(options)
  error_messages = []

  if options[:pool].nil?
    error_messages << "Promoting server requires --pool argument."
  end

  if options[:server].nil?
    error_messages << "Promoting server requires --server argument."
  end
  
  unless error_messages.empty?
    help error_messages.join "\n"
  end

  puts send_command_to_deadpool_server :command => 'promote_server', :pool => options[:pool], :server => options[:server]
end

#runObject



17
18
19
20
21
22
# File 'lib/deadpool/admin.rb', line 17

def run
  @options = self.parse_command_line
  @config  = Deadpool::Helper.configure @options

  self.execute_command(@options)
end

#send_command_to_deadpool_server(options) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/deadpool/admin.rb', line 172

def send_command_to_deadpool_server(options)
  output = ''

  begin
    socket = TCPSocket.open(@config[:admin_hostname], @config[:admin_port])
  rescue
    return "Couldn't connect to deadpool server.  Is it running?"
  end

  if socket
    socket.puts JSON.dump(options)
    while line = socket.gets
      output += line
    end
    socket.close
  else
    return "Couldn't connect to deadpool server."
  end

  return output
end

#start(options) ⇒ Object



164
165
166
# File 'lib/deadpool/admin.rb', line 164

def start(options)
  puts Deadpool::Server.new(options).run(true)
end

#stop(options) ⇒ Object



160
161
162
# File 'lib/deadpool/admin.rb', line 160

def stop(options)
  puts send_command_to_deadpool_server :command => 'stop'
end