Class: OneacctOpts
- Inherits:
-
Object
- Object
- OneacctOpts
- Defined in:
- lib/oneacct_opts.rb
Overview
Class for parsing command line arguments
Constant Summary collapse
- BLOCKING_DEFAULT =
false- TIMEOUT_DEFAULT =
60 * 60
- COMPATIBILITY_DEFAULT =
false
Class Method Summary collapse
-
.check_options_restrictions(options) ⇒ Object
Make sure command line parameters are sane.
- .check_restrictions(options) ⇒ Object
-
.check_settings_restrictions ⇒ Object
Make sure configuration is sane.
- .parse(args) ⇒ Object
-
.set_defaults(options) ⇒ Object
Set default values for not specified options.
Class Method Details
.check_options_restrictions(options) ⇒ Object
Make sure command line parameters are sane
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/oneacct_opts.rb', line 97 def self.() #make sure date range make sense if .records_from && .records_to && .records_from >= .records_to fail ArgumentError, 'Wrong time range for records retrieval.' end #make sure only one group restriction is used if .include_groups && .exclude_groups fail ArgumentError, 'Mixing of group options is not possible.' end #make sure group file option is not used without specifying group restriction type unless .include_groups || .exclude_groups if .groups_file fail ArgumentError, 'Cannot use group file without specifying group restriction type.' end end #make sure that timeout option is not used without blocking option if .timeout && !.blocking fail ArgumentError, 'Cannot set timeout without a blocking mode.' end end |
.check_restrictions(options) ⇒ Object
91 92 93 94 |
# File 'lib/oneacct_opts.rb', line 91 def self.check_restrictions() () check_settings_restrictions end |
.check_settings_restrictions ⇒ Object
Make sure configuration is sane
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/oneacct_opts.rb', line 122 def self.check_settings_restrictions #make sure all mandatory parameters are set unless Settings['site_name'] && Settings['cloud_type'] && Settings['endpoint'] && Settings['output'] && Settings.output['output_dir'] && Settings.output['output_type'] fail ArgumentError, 'Missing some mandatory parameters. Check your configuration file.' end Settings['endpoint'].chop! if Settings['endpoint'].end_with?('/') #make sure log file is specified while loggin to file if Settings['logging'] && Settings.logging['log_type'] == 'file' && !Settings.logging['log_file'] fail ArgumentError, 'Missing file for logging. Check your configuration file.' end #make sure specified template really exists template_filename = OneWriter.template_filename(Settings.output['output_type']) unless File.exist?(template_filename) fail ArgumentError, "Non-existing template #{Settings.output['output_type']}." end end |
.parse(args) ⇒ Object
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 79 80 |
# File 'lib/oneacct_opts.rb', line 13 def self.parse(args) = OpenStruct.new opt_parser = OptionParser.new do |opts| opts. = 'Usage oneacct-export [options]' opts.separator '' opts.on('--records-from TIME', Time, 'Retrieves only records newer than TIME') do |time| .records_from = time end opts.on('--records-to TIME', Time, 'Retrieves only records older than TIME') do |time| .records_to = time end opts.on('--include-groups GROUP1[,GROUP2,...]', Array, 'Retrieves only records of virtual machines which '\ 'belong to the specified groups') do |groups| .include_groups = groups end opts.on('--exclude-groups GROUP1[,GROUP2,...]', Array, 'Retrieves only records of virtual machines which '\ "don't belong to the specified groups") do |groups| .exclude_groups = groups end opts.on('--group-file FILE', 'If --include-groups or --exclude-groups specified, '\ 'loads groups from file FILE') do |file| .groups_file = file end opts.on('-b', '--[no-]blocking', 'Run in a blocking mode - '\ 'wait until all submitted jobs are processed') do |blocking| .blocking = blocking end opts.on('-t', '--timeout N', Integer, 'Timeout for blocking mode in seconds. '\ 'Default is 1 hour.') do |timeout| .timeout = timeout end opts.on('-c', '--[no-]compatibility-mode', 'Run in compatibility mode - '\ 'supports OpenNebula 4.4.x') do |compatibility| .compatibility = compatibility end opts.on_tail('-h', '--help', 'Shows this message') do puts opts exit end opts.on_tail('-v', '--version', 'Shows version') do puts OneacctExporter::VERSION exit end end opt_parser.parse!(args) set_defaults() check_restrictions() end |
.set_defaults(options) ⇒ Object
Set default values for not specified options
83 84 85 86 87 88 89 |
# File 'lib/oneacct_opts.rb', line 83 def self.set_defaults() .blocking = BLOCKING_DEFAULT unless .blocking unless .timeout .timeout = TIMEOUT_DEFAULT if .blocking end .compatibility = COMPATIBILITY_DEFAULT unless .compatibility end |