Class: RightWrapper

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

Constant Summary collapse

SNAP_WAIT_TIME =
5
AVAILABLE_DEVICES =
("j".."p").map {|x| "/dev/sd#{x}"}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aws_access_key_id, aws_secret_access_key) ⇒ RightWrapper

Returns a new instance of RightWrapper.



14
15
16
# File 'lib/right_wrapper.rb', line 14

def initialize(aws_access_key_id, aws_secret_access_key)
  @ec2 = RightAws::Ec2.new(aws_access_key_id, aws_secret_access_key)
end

Instance Attribute Details

#ec2Object

Returns the value of attribute ec2.



12
13
14
# File 'lib/right_wrapper.rb', line 12

def ec2
  @ec2
end

Class Method Details

.freeze_xfs(device, &block) ⇒ Object



155
156
157
158
159
# File 'lib/right_wrapper.rb', line 155

def freeze_xfs(device, &block)
  system("xfs_freeze -f #{device}")
  yield
  system("xfs_freeze -u #{device}")      
end

Instance Method Details

#attach_volume(volume_id, target_instance, device) ⇒ Object



125
126
127
128
129
# File 'lib/right_wrapper.rb', line 125

def attach_volume(volume_id, target_instance, device)
  puts "Attaching volume #{volume_id} to instance #{target_instance[:aws_instance_id]} on #{device}"
  @ec2.attach_volume(volume_id, target_instance[:aws_instance_id], device)
  wait_for_volume(volume_id, "attached")
end

#create_snapshot(target_volume, tags = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/right_wrapper.rb', line 77

def create_snapshot(target_volume, tags={})
  tag_string = ''
  target_volume_id = target_volume[:aws_id]

  puts "Creating snapshot of #{target_volume_id}"
  unless tags.empty?
    tag_string = tags.map {|k,v| [k.to_s, v.to_s].join(":")}.join(" ")
    puts "-- Tagging snapshot with #{tag_string}"
  end

  snap_id = @ec2.create_snapshot(target_volume_id, tag_string)[:aws_id]

  while true
    snap_data = @ec2.describe_snapshots.detect {|snap| snap[:aws_id] == snap_id}    

    puts "-- Progress of snapshot #{snap_id} is #{snap_data[:aws_progress]}."
    break if snap_data[:aws_status] == "completed"

    sleep SNAP_WAIT_TIME
  end

  snap_id
end

#create_volume(snap_id, volume_size, zone) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/right_wrapper.rb', line 115

def create_volume(snap_id, volume_size, zone)
  puts "Creating volume for snapshot #{snap_id}"
  puts "-- Source volume is #{volume_size}GB"
  puts "-- Target instance is in the '#{zone}' zone"
  
  vol_id = @ec2.create_volume(snap_id, volume_size, zone)[:aws_id]
  wait_for_volume(vol_id, "available")
  vol_id
end

#delete_snapshot(snap_id) ⇒ Object



110
111
112
113
# File 'lib/right_wrapper.rb', line 110

def delete_snapshot(snap_id)
  puts "Deleting snapshot #{snap_id}"
  @ec2.delete_snapshot(snap_id)
end

#delete_volume(volume) ⇒ Object



137
138
139
140
# File 'lib/right_wrapper.rb', line 137

def delete_volume(volume)
  puts "Deleting the volume #{volume[:aws_id]}"
  @ec2.delete_volume(volume[:aws_id])
end

#detach_volume(volume) ⇒ Object



131
132
133
134
135
# File 'lib/right_wrapper.rb', line 131

def detach_volume(volume)
  puts "Detatching volume #{volume[:aws_id]} from #{volume[:device]} on instance #{volume[:aws_instance_id]}"
  @ec2.detach_volume(volume[:aws_id], volume[:aws_instance_id], volume[:device])
  wait_for_volume(volume[:aws_id], "available")
end

#find_attached_volumes(target_instance) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/right_wrapper.rb', line 60

def find_attached_volumes(target_instance)
  target_instance_id = target_instance[:aws_instance_id] or raise "Can't find instance id of nil"

  all_volumes = @ec2.describe_volumes
  current_volumes = all_volumes.select {|vol| vol[:aws_instance_id] == target_instance_id && vol[:aws_attachment_status] == "attached"}
  if current_volumes.size > 0
    puts "Found the following volumes attached to #{target_instance_id}:"
    current_volumes.each do |vol|
      puts "-- #{vol[:aws_id]} attached to #{vol[:aws_device]} on #{vol[:aws_attached_at]}"
    end
  else
    puts "No volumes currently attached to #{target_instance_id}."
  end

  current_volumes
end

#find_instance(target_instance_id) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/right_wrapper.rb', line 18

def find_instance(target_instance_id)
  instances = @ec2.describe_instances([target_instance_id])
  unless target_instance = instances.first
    $stderr.puts "Target instance #{target_instance_id} is not up. Cannot complete operation."
    exit 1
  end
  target_instance
end

#find_instance_by_eip(eip) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/right_wrapper.rb', line 27

def find_instance_by_eip(eip)
  unless address = @ec2.describe_addresses.detect {|x| x[:public_ip] == eip}
    puts "No address information found for #{eip}"
  end
  
  if instance_id = address[:instance_id]
    find_instance(instance_id)
  else
    puts "No instance currently associated with #{eip}"
  end
end

#find_snapshots_by(options) ⇒ Object



54
55
56
57
58
# File 'lib/right_wrapper.rb', line 54

def find_snapshots_by(options)
  @ec2.describe_snapshots.select do |snap|
    options.to_a.all? { |k, v| snap[k] == v}
  end
end

#find_volume(target_volume_id) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/right_wrapper.rb', line 39

def find_volume(target_volume_id)
  volumes = @ec2.describe_volumes([target_volume_id])
  unless target_volume = volumes.first
    $stderr.puts "Target volume #{target_volume_id} is not up. Cannot complete operation."
    exit 1
  end
  target_volume
end

#find_volumes_by(options) ⇒ Object



48
49
50
51
52
# File 'lib/right_wrapper.rb', line 48

def find_volumes_by(options)
  @ec2.describe_volumes.select do |volume|
    options.to_a.all? { |k, v| volume[k] == v}
  end
end

#get_tags_for_snapshot(snap) ⇒ Object



106
107
108
# File 'lib/right_wrapper.rb', line 106

def get_tags_for_snapshot(snap)
  snap[:aws_description].squeeze(" ").split(" ").inject({}) { |c, t| key, value = t.split(":"); c[key.to_sym] = value; c}
end

#share_snapshot(snap_id, *users) ⇒ Object



101
102
103
104
# File 'lib/right_wrapper.rb', line 101

def share_snapshot(snap_id, *users)
  puts "Sharing snapshot #{snap_id} with #{users.join(', ')}"
  @ec2.modify_snapshot_attribute_create_volume_permission_add_users(snap_id, *users)
end

#wait_for_volume(vol_id, status) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/right_wrapper.rb', line 142

def wait_for_volume(vol_id, status)
  while true
    vol_data = @ec2.describe_volumes.detect {|vol| vol[:aws_id] == vol_id}
    puts "-- Attachment status of volume #{vol_id} is #{vol_data[:aws_attachment_status]}." unless vol_data[:aws_attachment_status].nil?
    puts "-- AWS Status of volume #{vol_id} is #{vol_data[:aws_status]}"
    puts "-- Progress of volume #{vol_id} is #{vol_data[:aws_progress]}." unless vol_data[:aws_progress].nil?
    break if vol_data[:aws_status] == status || vol_data[:aws_attachment_status] == status

    sleep SNAP_WAIT_TIME
  end
end