awsome 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/awsome/ec2.rb CHANGED
@@ -75,6 +75,32 @@ module Awsome
75
75
  end
76
76
 
77
77
  @@describe_volumes_fields = %w(
78
+ volume_identifier
79
+ volume_id
80
+ size_gb
81
+ type
82
+ iops
83
+ snapshot
84
+ availability_zone
85
+ state
86
+ timestamp
87
+ tags
88
+ )
89
+
90
+ def self.describe_volumes(filters={}, *volume_ids)
91
+ cmd = [Awsome::Ec2.command('ec2-describe-volumes')]
92
+ cmd += volume_ids
93
+ cmd += filters.collect { |k,v| "--filter \"#{k}=#{v}\"" }
94
+ Awsome.execute(cmd, columns: @@describe_volumes_fields, filter: /^VOLUME/)
95
+ end
96
+
97
+ def self.volume_available?(volume_id)
98
+ volumes = describe_volumes(volume_id)
99
+ raise "volume #{volume_id} not found" if volumes.empty?
100
+ volumes.first['state'] == 'available'
101
+ end
102
+
103
+ @@describe_attachments_fields = %w(
78
104
  attachment_identifier
79
105
  volume_id
80
106
  instance_id
@@ -83,15 +109,23 @@ module Awsome
83
109
  date
84
110
  )
85
111
 
86
- def self.describe_volumes(filters={})
112
+ def self.describe_attachments(filters={})
87
113
  cmd = [Awsome::Ec2.command('ec2-describe-volumes')]
88
114
  cmd += filters.collect { |k,v| "--filter \"#{k}=#{v}\"" }
89
115
  Awsome.execute(cmd, columns: @@describe_volumes_fields, filter: /^ATTACHMENT/)
90
116
  end
91
117
 
92
- def self.detach_volume(volume_id)
93
- cmd = Awsome::Ec2.command('ec2-detach-volume', volume_id)
94
- Awsome.execute(cmd)
118
+ def self.detach_volume(volume_id, device, preumount)
119
+ attachments = describe_attachments('volume-id' => volume_id)
120
+ if attachments.any?
121
+ instance_id = attachments.first['instance_id']
122
+ instance = describe_instances('instance-id' => instance_id).first
123
+ instance.ssh preumount if preumount
124
+ instance.ssh "sudo umount #{device}"
125
+
126
+ cmd = Awsome::Ec2.command('ec2-detach-volume', volume_id)
127
+ Awsome.execute(cmd)
128
+ end
95
129
  end
96
130
 
97
131
  def self.attach_volume(volume_id, instance_id, device)
@@ -13,7 +13,7 @@ module Awsome
13
13
  end
14
14
 
15
15
  def volumes
16
- Awsome::Ec2.describe_volumes('attachment.instance-id' => @properties['instance_id']).collect do |p|
16
+ Awsome::Ec2.describe_attachments('attachment.instance-id' => @properties['instance_id']).collect do |p|
17
17
  p['volume_id']
18
18
  end.to_set
19
19
  end
@@ -29,9 +29,14 @@ module Awsome
29
29
  Awsome.wait_until(interval: 10) { has_ssh? }
30
30
  end
31
31
 
32
+ def ssh(*args)
33
+ Awsome::Ssh.ssh(@properties['public_dns_name'], *args)
34
+ end
35
+
32
36
  def reattach_volumes(*volumes)
33
37
  volumes.each do |info|
34
- Awsome::Ec2.detach_volume(info['id'])
38
+ Awsome::Ec2.detach_volume(info['id'], info['device'], info['preumount'])
39
+ Awsome.wait_until(interval: 10) { Awsome::Ec2.volume_available?(info['id']) }
35
40
  Awsome::Ec2.attach_volume(info['id'], @properties['instance_id'], info['device'])
36
41
  end
37
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awsome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: