Class: Rubicante::CLI

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

Overview

A command-line interface for Rubicante

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ CLI

Returns a new instance of CLI.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rubicante/cli.rb', line 8

def initialize(debug = false)
  # Set up the logger for this CLI session
  @log = Logging::Logger['rubicante']
  @log.add_appenders(Logging::Appender.stdout)
  @log.level = debug ? :debug : :info

  # Set up logger for Environment
  Logging::Appender['rubicante'] = Logging::Appender.stdout
  Logging::Logger['Rubicante::Environment'].add_appenders(Logging::Appender['rubicante'])
  Logging::Logger['Rubicante::Environment'].level = @log.level

  # Prepare Environment
  @env = Environment.new
end

Instance Method Details

#handle(cmd) ⇒ Object

Handles sending the command to the Environment and dealing with any return values that require output to the user



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

def handle(cmd)
  exit if cmd == "exit"

  begin
    result = @env.eval_command(cmd)
  rescue NotImplementedError
    @log.error "Unrecognized command '#{cmd.split[0]}'."
  end

  # 'what' commands return Arrays
  if result.kind_of? Array
    result.each do |response|
      process(response)
    end
  end
end

#load(file) ⇒ Object

Load in a file, executing each line



62
63
64
65
# File 'lib/rubicante/cli.rb', line 62

def load(file)
  cmds = File.new(file)
  cmds.each_line { |cmd| handle(cmd) }
end

#process(resp) ⇒ Object



23
24
25
26
27
# File 'lib/rubicante/cli.rb', line 23

def process(resp)
  if resp.kind_of? HostError
    process_host_error(resp)
  end
end

#process_host_error(resp) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/rubicante/cli.rb', line 29

def process_host_error(resp)
  if not resp.ping
    puts_error(resp.hostname, "is unreachable.")
  else
    process_port_errors(resp)
    process_service_errors(resp)
    process_website_errors(resp)
  end
end

#process_port_errors(resp) ⇒ Object



39
40
41
42
43
# File 'lib/rubicante/cli.rb', line 39

def process_port_errors(resp)
  resp.bad_ports.each do |bad_port|
    puts_error(resp.hostname, "port #{bad_port} is closed.")
  end
end

#process_service_errors(resp) ⇒ Object



45
46
47
48
49
# File 'lib/rubicante/cli.rb', line 45

def process_service_errors(resp)
  resp.bad_services.each do |bad_service|
    puts_error(resp.hostname, "service #{bad_service} is stopped.")
  end
end

#process_website_errors(resp) ⇒ Object



51
52
53
54
55
# File 'lib/rubicante/cli.rb', line 51

def process_website_errors(resp)
  resp.website_errors.each do |website_error|
    puts_error(resp.hostname, "is returning code #{website_error.code} for website #{website_error.url}.")
  end
end

#puts_error(hostname, error) ⇒ Object



57
58
59
# File 'lib/rubicante/cli.rb', line 57

def puts_error(hostname, error)
  @log.info "[#{hostname}] #{error}"
end

#runObject



86
87
88
89
90
91
92
93
94
# File 'lib/rubicante/cli.rb', line 86

def run
  while true do
    print 'rubicante> '

    cmd = gets.downcase.strip

    handle(cmd)
  end
end