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.



478
479
480
# File 'lib/awscli/ec2.rb', line 478

def initialize(connection)
  @conn = connection
end

Instance Method Details

#attach_volume(options) ⇒ Object



494
495
496
497
498
499
500
501
502
503
# File 'lib/awscli/ec2.rb', line 494

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



544
545
546
547
548
# File 'lib/awscli/ec2.rb', line 544

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



490
491
492
# File 'lib/awscli/ec2.rb', line 490

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

#create_snapshot(options) ⇒ Object



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

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



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

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



550
551
552
553
554
555
# File 'lib/awscli/ec2.rb', line 550

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



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

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



505
506
507
508
509
510
511
512
513
514
515
# File 'lib/awscli/ec2.rb', line 505

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



482
483
484
485
486
487
488
# File 'lib/awscli/ec2.rb', line 482

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