class Aeolus::CLI::ConfigParser

Constants

COMMANDS

Attributes

args[RW]
command[RW]
options[RW]

Public Class Methods

new(argv) click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 25
def initialize(argv)
  @args = argv
  # Default options
  @options = {}
  @value = %r^(?!-).*$/
end

Public Instance Methods

build() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 305
def build
  b = BuildCommand.new(@options)
  b.run
end
buildOptions() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 141
def buildOptions
  OptionParser.new do |opts|
    opts.banner = "Usage: aeolus-image build [command options]"
    opts.separator ""
    opts.separator "Options:"
    opts.on('-e', '--template FILE', @value, 'path to file that contains template xml') do |file|
      @options[:template] = file
    end
    opts.on('-T', '--target TARGET1,TARGET2', Array, 'provider type (ec2, rackspace, rhevm, etc)') do |name|
      @options[:target] = name
    end
    opts.on('-E', '--environment ENVIRONMENT', @value, 'environment to build for') do |environment|
      @options[:environment] =  environment
    end
    opts.on( '-h', '--help', 'Get usage information for this command')

    opts.separator ""
    opts.separator "Examples:"
    opts.separator "aeolus-image build --target ec2 --template my.tmpl --environment default        # build a new image for ec2 from based on the given template for the default environment"
    opts.separator "aeolus-image build --target ec2,rhevm --template my.tmpl --environment default  # build a new image for ec2 and for rhevm based on the given template for the default environment"
    #opts.separator "aeolus-image build --image $image_id                # (NOT IMPLEMENTED) rebuild the image template and targets from latest build"
    #opts.separator %q{aeolus-image build --target ec2,rackspace \         # rebuild the image with a new template and set of targets
    #         --image $image_i \
    #         --template my.tmpl}
  end
end
delete() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 320
def delete
  if @options[:subcommand].nil?
    # TODO: Pull out Print Usage into seporate method, and print
    puts "Could not find subcommand for delete, run `./aeolus-image --help` for usage instructions"
    exit(1)
  else
    delete_command = DeleteCommand.new(@options)
    delete_command.send(@options[:subcommand])
  end
end
deleteOptions() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 253
def deleteOptions
  OptionParser.new do |opts|
    opts.banner = "Usage: aeolus-image delete [command options]"
    opts.separator ""

    opts.separator "Delete options:"
    opts.on('-I', '--image ID', @value, 'delete build image and associated objects') do |id|
      @options[:subcommand] = :image
      @options[:image] = id
    end
    opts.on('-B', '--build ID', @value, 'delete build and associated objects') do |id|
      @options[:subcommand] = :build
      @options[:build] = id
    end
    opts.on('-m', '--targetimage ID', @value, 'delete target image and its provider images') do |id|
      @options[:subcommand] = :target_image
      @options[:targetimage] = id
    end
    opts.on('-D', '--providerimage ID', @value, 'delete provider image') do |id|
      @options[:subcommand] = :provider_image
      @options[:providerimage] = id
    end
    opts.on( '-h', '--help', 'Get usage information for this command')

    opts.separator ""
    opts.separator "Delete examples:"
    opts.separator "aeolus-image delete --image $image_id               # deletes a image and all associated builds"
    opts.separator "aeolus-image delete --build $build_id               # deletes a build and all associated targetimages"
    opts.separator "aeolus-image delete --targetimage $target_image     # deletes a target image and all associated provider images"
    opts.separator "aeolus-image delete --providerimage $provider_image # deletes a provider image"
    opts.separator ""
    opts.separator "N.B. Aeolus Credentials should be defined in the configuration file ~/.aeolus-cli"
  end
end
generalOptions() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 70
def generalOptions
  subtext = "Aeolus Image Commands:"
  subtext += "\n    list   : Lists Aeolus Image Resources"
  subtext += "\n    build  : Builds a new Image"
  subtext += "\n    push   : Pushes an Image to a particular Provider Account"
  subtext += "\n    import : Imports an existing image"
  subtext += "\n    delete : Delete an Aeolus Image Resource"
  subtext += "\n    status : Check the status of a push or build"
  subtext += "\nSee `aeolus-image <command> -h` for more information on each command"

  general = OptionParser.new do |opts|
    opts.banner = "Usage: aeolus-image [#{COMMANDS.join('|')}] [command options]"
    opts.on( '-h', '--help', 'Get usage information for this tool') do
      puts opts
      exit(0)
    end
    opts.separator(subtext)
  end
  general
end
import() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 315
def import
  import_command = ImportCommand.new(@options)
  import_command.import_image
end
importOptions() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 220
def importOptions
  OptionParser.new do |opts|
    opts.banner = "Usage: aeolus-image import [command options]"
    opts.separator ""
    opts.separator "Options:"
    opts.on('-d', '--id ID', 'id for a given object') do |id|
      @options[:id] = id
    end
    opts.on('-r', '--description NAME', @value, 'description (e.g. "<image><name>MyImage</name></image>" or "/home/user/myImage.xml")') do |description|
      @options[:description] = description
    end
    opts.on('-A', '--account NAME,NAME', Array, 'name of specific account to import to') do |name|
      @options[:provider_account] = name
    end
    opts.on('-E', '--environment ENVIRONMENT', @value, 'environment to import into') do |environment|
      @options[:environment] =  environment
    end
    opts.on( '-h', '--help', 'Get usage information for this command')

    opts.separator ""
    opts.separator "Examples:"
    opts.separator "aeolus-image import --account my-ec2 --id $ami_id --environment default # import an AMI from the specified provider"
    opts.separator "aeolus-image import --account my-ec2 --id $ami_id --environment default --description '<image><name>My Image</name></image>' # import an AMI from the specified provider"
    opts.separator "aeolus-image import --account my-ec2 --id $ami_id --environment default --description <path_to_xml_file> # import an AMI from the specified provider"
    opts.separator ""
    opts.separator "RHEV:"
    opts.separator "Enter the template id for the provider image id. The template id can be found through the RHEV REST API."
    opts.separator "For example: curl https://rhevm.example.org:8443/api/templates --user user@rhevm_domain:password"
    opts.separator ""
    opts.separator "N.B. Aeolus Credentials should be defined in the configuration file ~/.aeolus-cli"
  end
end
list() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 288
def list
  # TODO: Instantiate and call object matching command type, for example:
  # l = ListCommand.new(@options)
  # Each Command will call it's own internal method depending on the contents of the hash.
  # For the list example above, that object would call a method 'images' based on the item
  # @options[:subcommand] being :images, so internally that class may do something like:
  # self.send(@options[:subcommand])
  if @options[:subcommand].nil?
    # TODO: Pull out Print Usage into seporate method, and print
    puts "Could not find options for list, run `./aeolus-image list --help` for usage instructions"
    exit(1)
  else
    list_command = ListCommand.new(@options)
    list_command.send(@options[:subcommand])
  end
end
listOptions() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 91
def listOptions
  OptionParser.new do |opts|
    opts.banner = "Usage: aeolus-image list [command options]"
    opts.on('-i', '--images', @value, 'Retrieve a list of images') do
      @options[:subcommand] = :images
    end
    opts.on('-E', '--environment ENVIRONMENT', @value, 'Limit image list to environment') do |environment|
      @options[:environment] =  environment
    end
    opts.on('-b', '--builds ID', @value, 'Retrieve the builds of an image') do |id|
      @options[:subcommand] = :builds
      @options[:id] = id
    end
    opts.on('-t', '--targetimages ID', @value, 'Retrieve the target images from a build') do |id|
      @options[:subcommand] = :targetimages
      @options[:id] = id
    end
    opts.on('-P', '--providerimages ID', @value, 'Retrieve the provider images from a target image') do |id|
      @options[:subcommand] = :providerimages
      @options[:id] = id
    end
    opts.on('-g', '--targets', 'Retrieve the values available for the --target parameter') do
      @options[:subcommand] = :targets
    end
    opts.on('-p', '--providers', 'Retrieve the values available for the --provider parameter') do
      @options[:subcommand] = :providers
    end
    opts.on('-a', '--accounts', 'Retrieve the values available for the --account parameter') do
      @options[:subcommand] = :accounts
    end
    opts.on('-c', '--environments', 'Retrieve the values available for the --environment parameter') do
      @options[:subcommand] = :environments
    end
    opts.on( '-h', '--help', 'Get usage information for this command')

    opts.separator ""
    opts.separator "Examples:"
    opts.separator "aeolus-image list --images                     # list available images"
    opts.separator "aeolus-image list --images --environment $env   # list available images for an environment"
    opts.separator "aeolus-image list --builds $image_id           # list the builds of an image"
    opts.separator "aeolus-image list --targetimages $build_id     # list the target images from a build"
    opts.separator "aeolus-image list --providerimages $target_id  # list the provider images from a target image"
    opts.separator "aeolus-image list --targets                    # list the values available for the --target parameter"
    opts.separator "aeolus-image list --providers                  # list the values available for the --provider parameter"
    opts.separator "aeolus-image list --accounts                   # list the values available for the --account parameter"
    opts.separator ""
    opts.separator "N.B. Aeolus Credentials should be defined in the configuration file ~/.aeolus-cli"
  end
end
parse(opts) click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 58
def parse(opts)
  begin
    opts.parse(@args)
  rescue OptionParser::ParseError => e
    puts "Warning, " + e.message + "\n\tSee `aeolus-image " + @command + " -h` for usage information."
    exit(1)
  rescue
    puts "An error occurred: See `aeolus-image " + @command + " -h` for usage information."
    exit(1)
  end
end
process() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 32
def process
  # Check for command, then call appropriate Optionparser and initiate
  # call to that class.
  @command = @args.shift
  # Eventually get the config file from user dir if it exists.
  # File.expand_path("~")
  if COMMANDS.include?(@command)
    opts = self.send((@command + "Options").to_sym)
    if @args.include?('-h') || @args.include?('--help')
      puts opts
    else
      parse(opts)
      begin
        self.send(@command.to_sym)
      rescue ArgumentError => e
        puts "Warning, #{e.message}"
        puts opts
        exit(1)
      end
    end
  else
    puts generalOptions
    exit(1)
  end
end
push() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 310
def push
  b = PushCommand.new(@options)
  b.run
end
pushOptions() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 168
def pushOptions
  OptionParser.new do |opts|
    opts.banner = "Usage: aeolus-image push [command options]"
    opts.separator ""
    opts.separator "Options:"
    opts.on('-I', '--image ID', @value, 'ID of the base image, can be used in build and push commands, see examples') do |id|
      @options[:image] = id
    end
    opts.on('-B', '--build ID', @value, 'push all target images for a build, to same providers as previously') do |id|
      @options[:build] = id
    end
    opts.on('-t', '--targetimages ID', @value, 'Retrieve the target images from a build') do |id|
      @options[:targetimage] = id
    end
    opts.on('-A', '--account NAME,NAME', Array, 'name of specific provider account to use for push') do |name|
      @options[:account] = name
    end
    opts.on( '-h', '--help', 'Get usage information for this command')

    opts.separator ""
    opts.separator "Examples:"
    opts.separator "aeolus-image push --account ec2-account,ec2-account2 --targetimage $target_image_id   # Push target images to each of the specified account"
    opts.separator "aeolus-image push --account ec2-account,rhevm-account --build $build_id               # Push target images attached to a particular build to each of the specified accounts"
    opts.separator "aeolus-image push --account ec2-account,rhevm-account --image $image_id               # Push target images attached to a particular image to each of the specified accounts"
    opts.separator ""
    opts.separator "N.B. Aeolus Credentials should be defined in the configuration file ~/.aeolus-cli"
  end
end
status() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 331
def status
  status_command = StatusCommand.new(@options)
  status_command.run
end
statusOptions() click to toggle source
# File lib/aeolus_cli/command/config_parser.rb, line 197
def statusOptions
  OptionParser.new do |opts|
    opts.banner = "Usage: aeolus-image status [command options]"
    opts.separator ""
    opts.separator "Options:"
    opts.on('-t', '--targetimage ID', @value, 'target image status') do |id|
      @options[:subcommand] = :target_image
      @options[:targetimage] = id
    end
    opts.on('-P', '--providerimage ID', @value, 'provider image status') do |id|
      @options[:subcommand] = :provider_image
      @options[:providerimage] = id
    end
    opts.on( '-h', '--help', 'Get usage information for this command')

    opts.separator "Examples:"
    opts.separator "aeolus-image status --targetimage $target_image     # status of target image build"
    opts.separator "aeolus-image status --providerimage $provider_image # status of provider image push"
    opts.separator ""
    opts.separator "N.B. Aeolus Credentials should be defined in the configuration file ~/.aeolus-cli"
  end
end