Module: Awsome::Ec2

Defined in:
lib/awsome/ec2.rb,
lib/awsome/ec2/instance.rb

Defined Under Namespace

Classes: Instance

Constant Summary collapse

@@run_instance_fields =
%w(
  instance_identifier
  instance_id
  ami_id
  state
  key
  ami_launch_index
  product_code
  instance_type
  instance_launch_time
  availability_zone
)
@@describe_instance_fields =
%w( 
  reservation_identifier
  reservation_id
  aws_account_id
  security_group_ids
  instance_identifier
  instance_id
  ami_id
  public_dns_name
  private_dns_name
  state
  key
  ami_launch_index
  product_codes
  instance_type
  instance_launch_time
  availability_zone
)
@@describe_volumes_fields =
%w(
  volume_identifier
  volume_id
  size_gb
  iops
  availability_zone
  state
  timestamp
  type
)
@@describe_attachments_fields =
%w(
  attachment_identifier
  volume_id
  instance_id
  device
  state
  date
)
@@associate_address_columns =
%w(
  identifier 
  elastic_ip
  instance_id
)

Class Method Summary collapse

Class Method Details

.associate_address(instance_id, ip_address) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/awsome/ec2.rb', line 169

def self.associate_address(instance_id, ip_address)
  cmd = Awsome::Ec2.command('ec2-associate-address', ip_address, instance: instance_id)
  Awsome.execute(
    cmd, 
    columns: @@associate_address_columns, 
    filter: /^ADDRESS/, 
    task: 'associating ip'
  )
end

.attach_volume(volume_id, instance_id, device) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/awsome/ec2.rb', line 179

def self.attach_volume(volume_id, instance_id, device)
  cmd = Awsome::Ec2.command('ec2-attach-volume', volume_id, instance: instance_id, device: device)
  Awsome.execute(
    cmd, 
    task: 'attaching volume'
  )
end

.command(*cmd) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/awsome/ec2.rb', line 7

def self.command(*cmd)
  args = cmd.last.is_a?(Hash) && cmd.pop
  options = cmd
  options << "--region #{config.region}"
  options << "--url #{config.url}"
  options << "--aws-access-key #{config.aws_access_key}"
  options << "--aws-secret-key #{config.aws_secret_key}"
  options << "--connection-timeout #{config.connection_timeout}" if config.connection_timeout
  options << "--request-timeout #{config.request_timeout}" if config.request_timeout
  options << "--verbose" if config.verbose
  options << "--show-empty-fields" if config.show_empty_fields
  options << "--debug" if config.debug
  args && args.each { |k,v| options << "--#{k} #{v}" }
  options.join(' ')
end

.configObject



3
4
5
# File 'lib/awsome/ec2.rb', line 3

def self.config
  Awsome.config
end

.create_tags(resource_id, tags) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/awsome/ec2.rb', line 56

def self.create_tags(resource_id, tags)
  tags = tags.collect { |k, v| v ? "--tag #{k}=#{v}" : "--tag #{k}" }
  cmd = command('ec2-create-tags', resource_id, *tags)
  Awsome.execute(
    cmd, 
    task: "creating tags",
    output: false
  )
end

.describe_attachments(filters = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/awsome/ec2.rb', line 136

def self.describe_attachments(filters={})
  cmd = [Awsome::Ec2.command('ec2-describe-volumes')]
  cmd += filters.collect { |k,v| "--filter \"#{k}=#{v}\"" }
  Awsome.execute(
    cmd, 
    columns: @@describe_attachments_fields, 
    filter: /^ATTACHMENT/,
    verbose: false
  )
end

.describe_instances(filters = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/awsome/ec2.rb', line 85

def self.describe_instances(filters={})
  cmd = [Awsome::Ec2.command('ec2-describe-instances')]
  cmd += filters.collect { |k,v| "--filter \"#{k}=#{v}\"" }
  preprocess = Proc.new { |text| text.gsub("\nINSTANCE", " INSTANCE") }
  properties = Awsome.execute(
    cmd, 
    columns: @@describe_instance_fields, 
    filter: /^RESERVATION/, 
    preprocess: preprocess,
    verbose: false
  )
  properties.collect { |p| Awsome::Ec2::Instance.new(p) }
end

.describe_volumes(*volume_ids) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/awsome/ec2.rb', line 110

def self.describe_volumes(*volume_ids)
  return [] if volume_ids.empty?
  cmd = [Awsome::Ec2.command('ec2-describe-volumes')] + volume_ids
  Awsome.execute(
    cmd, 
    columns: @@describe_volumes_fields, 
    filter: /^VOLUME/,
    verbose: false
  )
end

.detach_volume(volume_id, dir, preumount) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/awsome/ec2.rb', line 147

def self.detach_volume(volume_id, dir, preumount)
  attachments = describe_attachments('volume-id' => volume_id)
  if attachments.any?
    instance_id = attachments.first['instance_id']
    instance = describe_instances('instance-id' => instance_id).first
    instance.ssh preumount if preumount
    instance.ssh "sudo umount #{dir}"

    cmd = Awsome::Ec2.command('ec2-detach-volume', volume_id)
    Awsome.execute(
      cmd, 
      task: 'detaching volume'
    )
  end
end

.map_table(*args) ⇒ Object



23
24
25
# File 'lib/awsome/ec2.rb', line 23

def self.map_table(*args)
  Awsome.map_table(*args)
end

.run_instance(properties) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/awsome/ec2.rb', line 40

def self.run_instance(properties)
  cmd = command('ec2-run-instances', properties['ami_id'], 
    :group => properties['security_group_ids'], 
    :key => properties['key'], 
    'instance-type'.to_sym => properties['instance_type'], 
    'availability-zone'.to_sym => properties['availability_zone']
  )
  result = Awsome.execute(
    cmd, 
    columns: @@run_instance_fields, 
    filter: /^INSTANCE/, 
    task: 'creating instance'
  )
  Awsome::Ec2::Instance.new(result.first)
end

.terminate_instances(*instance_ids) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/awsome/ec2.rb', line 187

def self.terminate_instances(*instance_ids)
  return if instance_ids.empty?
  cmd = Awsome::Ec2.command("ec2-terminate-instances #{instance_ids.join(' ')}") 
  Awsome.execute(
    cmd, 
    task: 'terminating instances'
  )
end

.volume_available?(volume_id) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
# File 'lib/awsome/ec2.rb', line 121

def self.volume_available?(volume_id)
  volumes = describe_volumes(volume_id)
  raise "volume #{volume_id} not found" if volumes.empty?
  volumes.first['state'] == 'available'
end