Class: AMITool
- Inherits:
-
Object
- Object
- AMITool
- Includes:
- AMIToolExceptions
- Defined in:
- lib/ec2/amitools/tool_base.rb
Direct Known Subclasses
BundleDeleter, BundleDownloader, BundleMigrator, BundleTool, BundleUploader, ManifestMigrator, Unbundler
Constant Summary collapse
- PROMPT_TIMEOUT =
30
- MAX_TRIES =
5
- BACKOFF_PERIOD =
5
Instance Method Summary collapse
-
#get_manual ⇒ Object
——————————————————————————# Methods to override in subclasses ——————————————————————————#.
- #get_name ⇒ Object
-
#get_parameters(params_class) ⇒ Object
——————————————————————————#.
-
#handle_early_exit_parameters(params) ⇒ Object
——————————————————————————# Standard behaviour ——————————————————————————#.
-
#interactive? ⇒ Boolean
——————————————————————————#.
-
#interactive_prompt(message, name = nil) ⇒ Object
Display a message (without appending a newline) and ask for a response.
- #main(params) ⇒ Object
-
#retry_s3(retrying = true) ⇒ Object
—————————————————————————-#.
-
#run(params_class) ⇒ Object
——————————————————————————#.
-
#warn_confirm(message) ⇒ Object
Display a message on stderr.
Instance Method Details
#get_manual ⇒ Object
——————————————————————————# Methods to override in subclasses ——————————————————————————#
68 69 70 71 |
# File 'lib/ec2/amitools/tool_base.rb', line 68 def get_manual() # We have to get the manual text into here. raise "NotImplemented: get_manual()" end |
#get_name ⇒ Object
73 74 75 76 |
# File 'lib/ec2/amitools/tool_base.rb', line 73 def get_name() # We have to get the tool name into here. raise "NotImplemented: get_name()" end |
#get_parameters(params_class) ⇒ Object
——————————————————————————#
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/ec2/amitools/tool_base.rb', line 169 def get_parameters(params_class) # Parse the parameters and die on errors. # Assume that if we're parsing parameters, it's safe to exit. begin params = params_class.new(ARGV) rescue StandardError => e $stderr.puts e. $stderr.puts "Try '#{get_name} --help'" exit 1 end # Deal with help, verion, etc. if params.early_exit? handle_early_exit_parameters(params) exit 0 end # Some general flags that we want to set @debug = params.debug @interactive = params.interactive? # Finally, return the leftovers. params end |
#handle_early_exit_parameters(params) ⇒ Object
——————————————————————————# Standard behaviour ——————————————————————————#
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ec2/amitools/tool_base.rb', line 144 def handle_early_exit_parameters(params) if params.version puts get_name() + " " + params.version_copyright_string() return end if params.show_help puts params.help return end if params.manual puts get_manual() return end end |
#interactive? ⇒ Boolean
——————————————————————————#
163 164 165 |
# File 'lib/ec2/amitools/tool_base.rb', line 163 def interactive? @interactive end |
#interactive_prompt(message, name = nil) ⇒ Object
Display a message (without appending a newline) and ask for a response. Returns user response or nil if interactivity is not desired. Raises exception on timeout.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ec2/amitools/tool_base.rb', line 90 def interactive_prompt(, name=nil) return nil unless interactive? begin $stdout.print() $stdout.flush Timeout::timeout(PROMPT_TIMEOUT) do return gets end rescue Timeout::Error raise PromptTimeout.new(name) end end |
#main(params) ⇒ Object
78 79 80 81 |
# File 'lib/ec2/amitools/tool_base.rb', line 78 def main(params) # Main entry point. raise "NotImplemented: main()" end |
#retry_s3(retrying = true) ⇒ Object
—————————————————————————-#
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ec2/amitools/tool_base.rb', line 122 def retry_s3(=true) tries = 0 while true tries += 1 begin result = yield return result rescue TryFailed => e $stderr.puts e. if and tries < MAX_TRIES $stdout.puts "Retrying in #{BACKOFF_PERIOD}s ..." else raise EC2FatalError.new(3, e.) end end end end |
#run(params_class) ⇒ Object
——————————————————————————#
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/ec2/amitools/tool_base.rb', line 196 def run(params_class) # We want to be able to reuse bits without having to parse # parameters, so run() is not called from the constructor. begin params = get_parameters(params_class) main(params) rescue AMIToolExceptions::EC2StopExecution => e # We've been asked to stop. exit e.code rescue AMIToolExceptions::PromptTimeout => e $stderr.puts e. exit e.code rescue AMIToolExceptions::EC2FatalError => e $stderr.puts "ERROR: #{e.}" puts e.backtrace if @debug exit e.code rescue Interrupt => e $stderr.puts "\n#{get_name} interrupted." puts e.backtrace if @debug exit 255 rescue => e $stderr.puts "ERROR: #{e.}" puts e.inspect if @debug puts e.backtrace if @debug exit 254 end end |
#warn_confirm(message) ⇒ Object
Display a message on stderr. If interactive, asks for confirmation (yes/no). Returns true if in batch mode or user agrees, false if user disagrees. Raises exception on timeout.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/ec2/amitools/tool_base.rb', line 109 def warn_confirm() $stderr.puts() $stderr.flush return true unless interactive? response = interactive_prompt("Are you sure you want to continue? [y/N]") if response =~ /^[Yy]/ return true end return false end |