Class: LitmusPaper::CLI::AgentCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/litmus_paper/cli/agent_check.rb

Instance Method Summary collapse

Instance Method Details

#parse_args(args) ⇒ Object



8
9
10
11
12
13
14
15
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
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
# File 'lib/litmus_paper/cli/agent_check.rb', line 8

def parse_args(args)
  options = {}
  options[:pid_file] = '/tmp/litmus-agent-check.pid'
  optparser = OptionParser.new do |opts|
    opts.on("-s", "--service SERVICE:PORT,...", Array, "agent-check service to port mappings") do |s|
      options[:services] = s
    end
    opts.on("-c", "--config CONFIG", "Path to litmus paper config file") do |c|
      options[:litmus_paper_config] = c
    end
    opts.on("-p", "--pid-file PID_FILE", String, "Where to write the pid") do |p|
      options[:pid_file] = p
    end
    opts.on("-w", "--workers WORKERS", Integer, "Number of worker processes") do |w|
      options[:workers] = w
    end
    opts.on("-D", "--daemonize", "Daemonize the process") do |d|
      options[:daemonize] = d
    end
    opts.on("-h", "--help", "Help text") do |h|
      options[:help] = h
    end
  end

  begin
    optparser.parse! args
  rescue OptionParser::InvalidOption => e
    puts e
    puts optparser
    exit 1
  end

  if options[:help]
    puts optparser
    exit 0
  end

  if !options.has_key?(:services)
    puts "Error: `-s SERVICE:PORT,...` required"
    puts optparser
    exit 1
  end

  if !options.has_key?(:workers) || options[:workers] <= 0
    puts "Error: `-w WORKERS` required, and must be greater than 0"
    puts "  Use a value equal to the number of expected concurrent"
    puts "  agent checks from HAProxy"
    puts optparser
    exit 1
  end

  options[:services] = options[:services].reduce({}) do |memo, service|
    if service.split(':').length == 2
      service, port = service.split(':')
      memo[port.to_i] = service
      memo
    else
      puts "Error: Incorrect service port arg `-s SERVICE:PORT,...`"
      puts optparser
      exit 1
    end
  end

  options
end

#run(args) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/litmus_paper/cli/agent_check.rb', line 74

def run(args)
  options = parse_args(args)
  agent_check_server = LitmusPaper::AgentCheckServer.new(
    options[:litmus_paper_config],
    options[:services],
    options[:workers],
    options[:pid_file],
    options[:daemonize],
  )
  agent_check_server.run
end