Class: Awscli::EC2::Ebs

Inherits:
Object
  • Object
show all
Defined in:
lib/awscli/ec2.rb

Overview

> AMI

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Ebs

Returns a new instance of Ebs.



500
501
502
# File 'lib/awscli/ec2.rb', line 500

def initialize(connection)
  @conn = connection
end

Instance Method Details

#attach_volume(options) ⇒ Object



516
517
518
519
520
521
522
523
524
525
# File 'lib/awscli/ec2.rb', line 516

def attach_volume(options)
  #The volume and instance must be in the same Availability Zone.
  volume = @conn.volumes.get(options[:volume_id])
  volume.merge_attributes(:device => options[:device])
  server = @conn.servers.get(options[:instance_id])
  abort "Cannot find volume: #{options[:volume_id]}" unless volume
  abort "Cannot find instance: #{options[:instance_id]}" unless server
  volume.server = server
  puts "Attached volume: #{options[:volume_id]} to instance: #{options[:instance_id]}"
end

#copy_snapshot(options) ⇒ Object



566
567
568
569
570
# File 'lib/awscli/ec2.rb', line 566

def copy_snapshot(options)
  # abort "Cannot find snapshot: #{options[:snapshot_id]}" unless @conn.snapshots.get(options[:snapshot_id])
  @conn.copy_snapshot(options[:snapshot_id], options[:source_region])
  puts "Copied snapshot"
end

#create(options) ⇒ Object



512
513
514
# File 'lib/awscli/ec2.rb', line 512

def create(options)
  @conn.volumes.create(options)
end

#create_snapshot(options) ⇒ Object



560
561
562
563
564
# File 'lib/awscli/ec2.rb', line 560

def create_snapshot(options)
  abort "Cannot find volume: #{options[:volume_id]}" unless @conn.volumes.get(options[:volume_id])
  @conn.snapshots.create(options)
  puts "Created snapshot"
end

#delete_detachedObject



546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/awscli/ec2.rb', line 546

def delete_detached
  vols  = @conn.volumes.all('status' => 'available')
  unless vols.empty?
    if agree('Are you sure want to delete all the all volumes that are not in use ?  ')
      puts 'Deleting all volumes which are not in use ...'
      vols.each do |vol|
        vol.destroy
      end
    end
  else
    puts "No volumes found, that are 'not in use'"
  end
end

#delete_snapshot(options) ⇒ Object



572
573
574
575
576
577
# File 'lib/awscli/ec2.rb', line 572

def delete_snapshot(options)
  snap = @conn.snapshots.get(options[:snapshot_id])
  abort "Cannot find snapshot: #{options[:snapshot_id]}" unless snap
  snap.destroy
  puts "Deleted snapshot"
end

#delete_volume(options) ⇒ Object



539
540
541
542
543
544
# File 'lib/awscli/ec2.rb', line 539

def delete_volume(options)
  vol = @conn.volumes.get(options[:volume_id])
  abort "Cannot find volume #{options[:volume_id]}" unless vol
  vol.destroy
  puts "Deleted volume: #{options[:volume_id]}"
end

#detach_volume(options) ⇒ Object



527
528
529
530
531
532
533
534
535
536
537
# File 'lib/awscli/ec2.rb', line 527

def detach_volume(options)
  #Check if the volume is mounted and show warning regarding data loss
  volume = @conn.volumes.get(options[:volume_id])
  abort "Cannot find volume: #{options[:volume_id]}" unless volume
  if options[:force]
    volume.force_detach
  else
    @conn.detach_volume(options[:volume_id])
  end
  puts "Detached volume: #{options[:volume_id]}"
end

#list(options) ⇒ Object



504
505
506
507
508
509
510
# File 'lib/awscli/ec2.rb', line 504

def list(options)
  unless options[:snapshots]
    @conn.volumes.table([:availability_zone, :delete_on_termination, :device, :id, :server_id, :size, :snapshot_id, :state, :tags, :type])
  else
    @conn.snapshots.table([:id, :owner_id, :volume_id, :state, :progress, :tags, :description])
  end
end