class AMITool

Constants

BACKOFF_PERIOD
MAX_TRIES
PROMPT_TIMEOUT

Public Instance Methods

get_manual() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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.message
    $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) click to toggle source
# 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?() click to toggle source
# File lib/ec2/amitools/tool_base.rb, line 163
def interactive?
  @interactive
end
interactive_prompt(message, name=nil) click to toggle source

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.

# File lib/ec2/amitools/tool_base.rb, line 90
def interactive_prompt(message, name=nil)
  return nil unless interactive?
  begin
    $stdout.print(message)
    $stdout.flush
    Timeout::timeout(PROMPT_TIMEOUT) do
      return gets
    end
  rescue Timeout::Error
    raise PromptTimeout.new(name)
  end
end
main(params) click to toggle source
# File lib/ec2/amitools/tool_base.rb, line 78
def main(params)
  # Main entry point.
  raise "NotImplemented: main()"
end
retry_s3(retrying=true) { || ... } click to toggle source
# File lib/ec2/amitools/tool_base.rb, line 122
def retry_s3(retrying=true)
  tries = 0
  while true
    tries += 1
    begin
      result = yield
      return result
    rescue TryFailed => e
      $stderr.puts e.message
      if retrying and tries < MAX_TRIES
        $stdout.puts "Retrying in #{BACKOFF_PERIOD}s ..."
      else
        raise EC2FatalError.new(3, e.message)
      end
    end
  end
end
run(params_class) click to toggle source
# 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.message
    exit e.code
  rescue AMIToolExceptions::EC2FatalError => e
    $stderr.puts "ERROR: #{e.message}"
    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.message}"
    puts e.inspect if @debug
    puts e.backtrace if @debug
    exit 254
  end
end
warn_confirm(message) click to toggle source

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.

# File lib/ec2/amitools/tool_base.rb, line 109
def warn_confirm(message)
  $stderr.puts(message)
  $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