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
- .associate_address(instance_id, ip_address) ⇒ Object
- .attach_volume(volume_id, instance_id, device) ⇒ Object
- .command(*cmd) ⇒ Object
- .config ⇒ Object
- .create_tags(resource_id, tags) ⇒ Object
- .describe_attachments(filters = {}) ⇒ Object
- .describe_instances(filters = {}) ⇒ Object
- .describe_volumes(*volume_ids) ⇒ Object
- .detach_volume(volume_id, dir, preumount) ⇒ Object
- .map_table(*args) ⇒ Object
- .run_instance(properties) ⇒ Object
- .terminate_instances(*instance_ids) ⇒ Object
- .volume_available?(volume_id) ⇒ Boolean
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 = cmd << "--region #{config.region}" << "--url #{config.url}" << "--aws-access-key #{config.aws_access_key}" << "--aws-secret-key #{config.aws_secret_key}" << "--connection-timeout #{config.connection_timeout}" if config.connection_timeout << "--request-timeout #{config.request_timeout}" if config.request_timeout << "--verbose" if config.verbose << "--show-empty-fields" if config.show_empty_fields << "--debug" if config.debug args && args.each { |k,v| << "--#{k} #{v}" } .join(' ') 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.(resource_id, ) = .collect { |k, v| v ? "--tag #{k}=#{v}" : "--tag #{k}" } cmd = command('ec2-create-tags', resource_id, *) 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.(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) = ('volume-id' => volume_id) if .any? instance_id = .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
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 |