Class: MyOptionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/audit/lib/my_option_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ MyOptionParser

Returns a new instance of MyOptionParser.



7
8
9
# File 'lib/audit/lib/my_option_parser.rb', line 7

def initialize(args)
  @args = args
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/audit/lib/my_option_parser.rb', line 5

def options
  @options
end

Instance Method Details

#benchmarkObject



102
103
104
105
# File 'lib/audit/lib/my_option_parser.rb', line 102

def benchmark
  raise "No benchmark file specified" if @options[:benchmark].nil?
  return @options[:benchmark]
end

#connection_typeObject



80
81
82
83
84
# File 'lib/audit/lib/my_option_parser.rb', line 80

def connection_type
  return :ami unless @options[:ami].nil?
  return :ssh unless @options[:ssh].nil?
  return :none
end

#parseObject



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
73
74
75
76
77
78
# File 'lib/audit/lib/my_option_parser.rb', line 11

def parse()
  @options = {}
  
  opts = OptionParser.new do|opts|
    #Banner string displayed at top of help
    opts.banner = "Usage: ./main.rb\t--ssh HOST[:PORT] [--user USERNAME] --key KEYFILE --benchmark BENCHMARK"
    opts.separator "\t\t\t--ami AMI-ID --credentials CREDENTIALS --benchmark BENCHMARK"


    @options[:verbose] = false
    opts.on('-v', '--[no-]verbose', 'Output more information') do|v|
      @options[:verbose] = v
    end
                
    opts.on('-A', '--ami AMI-ID', String, 'Select this AMI for audit') do|ami|
      @options[:ami] = ami
    end
    
    opts.on('-S', '--ssh HOST', String, 'Select this host for audit') do|host|
      @options[:ssh] = host
    end
    
    opts.on('-B', '--benchmark BENCHMARK', String, 'Use this benchmark file') do|bm|
      @options[:benchmark] = bm
    end
    
    opts.on('-K', '--key KEY', String, 'Use this private key file for authentication') do|key|
      @options[:key] = key
    end
    
    opts.on('-P', '--password PASSWORD', String, 'Use this password for authentication') do|pwd|
      @options[:password] = pwd
    end
    
    opts.on('-C', '--credentials CREDENTIALS', String, 'Use these Amazon Account credentials to start the AMI') do|cd|
      @options[:credentials] = cd
    end
                
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end

  opts.parse(@args)
  
  if @options[:ssh].nil? and @options[:ami].nil? then
    print "You must specify one of the --ssh or --ami options. Exiting.\n"
    exit 1
  end
  
  if not @options[:ssh].nil? and not @options[:ami].nil? then
    print "Options --ssh and --ami are mutually exclusive, but both chosen. Please specify only one of them. Exiting.\n"
    exit 1
  end
  
  if @options[:benchmark].nil? then
    print "Option --benchmark is required. Please specify a benchmark file for this audit. Exiting.\n"
    exit 1
  end
  
  if not @options[:ssh].nil? and @options[:key].nil? and @options[:password].nil? then
    print "At least one authentication method for SSH is required. Please specify either --key or --password. Exiting.\n"
    exit 1
  end
  
  return self
end

#ssh_credentialsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/audit/lib/my_option_parser.rb', line 86

def ssh_credentials
  raise "SSH credentials requested although SSH connection method is not chosen" if @options[:ssh].nil?
  raise "No SSH authentication method chosen" if @options[:password].nil? and @options[:key].nil?
  
  cred = {:host => @options[:ssh]}
  if @options[:user].nil? then
    cred[:user] = 'root' 
  else 
    cred[:user] = @options[:user]
  end
  cred[:key_data] = @options[:key] unless @options[:key].nil?
  cred[:password] = @options[:password] unless @options[:password].nil?
  
  return cred
end