Class: IPVSLitmus::CLI::Admin

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

Instance Method Summary collapse

Instance Method Details

#_default_optionsObject



50
51
52
# File 'lib/ipvs_litmus/cli/admin.rb', line 50

def _default_options
  options = { :port => 9292, :host => 'localhost' }
end

#_extend_default_parser(options, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ipvs_litmus/cli/admin.rb', line 54

def _extend_default_parser(options, &block)
  OptionParser.new do |opts|
    block.call(opts)

    opts.on("-p", "--port=port", Integer, "Port litmus is running on", "Default: 9292") do |port|
      options[:port] = port
    end
    opts.on("-h", "--host=ip", String, ":Host litmus is running on", "Default: localhost") do |host|
      options[:host] = host
    end
    opts.on("--help", "Show this help message.") { puts opts; exit }
  end
end

#_litmus_request(host, port, request) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ipvs_litmus/cli/admin.rb', line 68

def _litmus_request(host, port, request)
  begin
    http = Net::HTTP.start(host, port)
    response = http.request(request)

    puts response.body
    case response
    when Net::HTTPSuccess then exit 0
    when Net::HTTPClientError then exit 2
    else exit 1
    end
  rescue Errno::ECONNREFUSED => e
    puts "Unable to connect to litmus on #{host}:#{port}: #{e.message}"
    exit 1
  end
end

#force(args) ⇒ Object



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
# File 'lib/ipvs_litmus/cli/admin.rb', line 9

def force(args)
  options = _default_options
  opt_parser = _extend_default_parser(options) do |opts|
    opts.banner = "Usage: litmusctl force <up|down> [service] [options]"
    opts.on("-d", "--delete", "Remove status file") do
      options[:delete] = true
    end
    opts.on("-r", "--reason=reason", String, "Reason for status file") do |reason|
      options[:reason] = reason
    end
  end

  opt_parser.parse! args
  direction, service = args

  if options[:delete]
    request = Net::HTTP::Delete.new("/force/#{direction}/#{service}")
  else
    if !options.has_key?(:reason)
      print "Reason? "
      options[:reason] = gets.chomp
    end
    request = Net::HTTP::Post.new("/force/#{direction}/#{service}")
    request.set_form_data('reason' => options[:reason])
  end

  _litmus_request(options[:host], options[:port], request)
end

#run(argv = ARGV) ⇒ Object



4
5
6
7
# File 'lib/ipvs_litmus/cli/admin.rb', line 4

def run(argv = ARGV)
  command = argv.shift
  send(command, argv)
end

#status(args) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ipvs_litmus/cli/admin.rb', line 38

def status(args)
  options = _default_options
  opt_parser = _extend_default_parser(options) do |opts|
    opts.banner = "Usage: litmusctl status <service> [options]"
  end

  opt_parser.parse! args
  service = args.shift

  _litmus_request(options[:host], options[:port], Net::HTTP::Get.new("/#{service}/status"))
end