lib/swa/cli/ec2_command.rb in swa-0.2.1 vs lib/swa/cli/ec2_command.rb in swa-0.3.0

- old
+ new

@@ -5,17 +5,18 @@ require "swa/cli/tag_filter_options" require "swa/ec2/key_pair" require "swa/ec2/image" require "swa/ec2/instance" require "swa/ec2/security_group" +require "swa/ec2/volume" module Swa module CLI class Ec2Command < BaseCommand - subcommand ["key-pair", "kp"], "show key-pair" do + subcommand ["key-pair", "kp"], "Show key-pair" do parameter "NAME", "key-pair name" include ItemBehaviour @@ -27,11 +28,11 @@ alias_method :item, :key_pair end - subcommand ["key-pairs", "kps"], "list key-pairs" do + subcommand ["key-pairs", "kps"], "List key-pairs" do include CollectionBehaviour private @@ -41,11 +42,11 @@ alias_method :collection, :key_pairs end - subcommand ["image", "ami"], "show image" do + subcommand ["image", "ami"], "Show image" do parameter "IMAGE-ID", "image id" include ItemBehaviour @@ -57,14 +58,14 @@ alias_method :item, :image end - subcommand ["images", "amis"], "list images" do + subcommand ["images", "amis"], "List images" do - option "--owned-by", "OWNER", "limit to those with selected owner", :default => "self" - option "--named", "PATTERN", "limit to those with matching name" + option "--owned-by", "OWNER", "with specified owner", :default => "self" + option "--named", "PATTERN", "with matching name" include TagFilterOptions include CollectionBehaviour private @@ -83,27 +84,45 @@ alias_method :collection, :images end - subcommand ["instance", "i"], "show instance" do + subcommand ["instance", "i"], "Show instance" do parameter "INSTANCE-ID", "instance id" include ItemBehaviour + subcommand ["console-output", "console"], "Display console output" do + + def execute + puts instance.console_output + end + + end + + %w(stop start reboot terminate).each do |action| + class_eval <<-RUBY + subcommand "#{action}", "#{action.capitalize} the instance" do + def execute + instance.#{action} + end + end + RUBY + end + private def instance Swa::EC2::Instance.new(ec2.instance(instance_id)) end alias_method :item, :instance end - subcommand ["instances", "is"], "list instances" do + subcommand ["instances", "is"], "List instances" do option ["--state"], "STATE", "with specified status", :default => "running" option "--named", "NAME", "with matching name" do |name| @@ -123,10 +142,22 @@ end include TagFilterOptions include CollectionBehaviour + %w(stop start reboot terminate).each do |action| + class_eval <<-RUBY + subcommand "#{action}", "#{action.capitalize} all instances" do + def execute + instances.each do |i| + i.#{action} + end + end + end + RUBY + end + private def instances add_filter("instance-state-name", state) options = {:filters => filters} @@ -135,11 +166,11 @@ alias_method :collection, :instances end - subcommand ["security-group", "sg"], "show security-group" do + subcommand ["security-group", "sg"], "Show security-group" do parameter "GROUP-ID", "security-group id" include ItemBehaviour @@ -151,11 +182,11 @@ alias_method :item, :security_group end - subcommand ["security-groups", "sgs"], "list security-groups" do + subcommand ["security-groups", "sgs"], "List security-groups" do include TagFilterOptions include CollectionBehaviour private @@ -165,9 +196,42 @@ options[:filters] = filters unless filters.empty? Swa::EC2::SecurityGroup.list(ec2.security_groups(options)) end alias_method :collection, :security_groups + + end + + subcommand ["volume", "vol", "v"], "Show volume" do + + parameter "VOLUME-ID", "volume id" + + include ItemBehaviour + + private + + def volume + Swa::EC2::Volume.new(ec2.volume(volume_id)) + end + + alias_method :item, :volume + + end + + subcommand ["volumes", "vs"], "List volumes" do + + include TagFilterOptions + include CollectionBehaviour + + private + + def volumes + options = {} + options[:filters] = filters unless filters.empty? + Swa::EC2::Volume.list(ec2.volumes(options)) + end + + alias_method :collection, :volumes end protected