You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2010/11/29 16:25:31 UTC

Added name for EC2 instances

Hi,

For now every single instance in EC2 could have user specified
name and this name is also reported throught API.
For names I use 'tags', which was recently added into EC2.
Tag is automaticly created after new instance is created and assigned
to that instance. When listing instances I'm also list all 'instance'
tags and assign name based on tag name.

Unfortunately, this feature is available only in new 'aws' gem,
so if you want to try this patch, you need to apply previous EC2
patch and then apply this patch on top of that.

  -- Michal


Re: Added name for EC2 instances

Posted by Toby Crawley <tc...@redhat.com>.
On 12/07/2010 09:15 AM, Toby Crawley wrote:
> On 11/29/2010 10:25 AM, mfojtik@redhat.com wrote:
>> Hi,
>>
>> For now every single instance in EC2 could have user specified name and this name is also reported throught API. For names I use 'tags',
>> which was recently added into EC2. Tag is automaticly created after new instance is created and assigned to that instance. When listing
>> instances I'm also list all 'instance' tags and assign name based on tag name.
>>
>> Unfortunately, this feature is available only in new 'aws' gem, so if you want to try this patch, you need to apply previous EC2 patch
>> and then apply this patch on top of that.
>>
>> -- Michal
>>
>
> Michal:
>
> I played with this patch, and will have to NACK it based on the following issues:
>
> * for the name to show in the AWS console name column, the tag must be 'Name' instead of 'name'
>
> * tagging on instance create does not work, with ec2 reporting that the instance does not exist (in the same manner as the attach ip feature):

Actually, tagging on create works occasionally, and is probably dependent on the timing of the requests.

> 08:50:31,129 INFO [STDOUT] ##### Aws::Ec2 returned an error: 400 Bad Request
> 08:50:31,130 INFO [STDOUT] <?xml version="1.0" encoding="UTF-8"?>
> 08:50:31,130 INFO [STDOUT] <Response><Errors><Error><Code>InvalidInstanceID.NotFound</Code><Message>The instance ID 'i-eefbdf83' does not
> exist</Message></Error></Errors><RequestID>40fbb385-6cea-4993-b333-21aaebcc5ec7</RequestID></Response> #####
>
> * the tag methods on ec2_driver (tag_instance, untag_instance) could be better named, since they don't handle generic tags, but just the
> 'name' tag. The tag/untag methods should be generic, and allow for management of any tag on the instance.
>
> Toby


Re: Added name for EC2 instances

Posted by Toby Crawley <tc...@redhat.com>.
On 11/29/2010 10:25 AM, mfojtik@redhat.com wrote:
> Hi,
>
> For now every single instance in EC2 could have user specified name and this name is also reported throught API. For names I use 'tags',
> which was recently added into EC2. Tag is automaticly created after new instance is created and assigned to that instance. When listing
> instances I'm also list all 'instance' tags and assign name based on tag name.
>
> Unfortunately, this feature is available only in new 'aws' gem, so if you want to try this patch, you need to apply previous EC2 patch
> and then apply this patch on top of that.
>
> -- Michal
>

Michal:

I played with this patch, and will have to NACK it based on the following issues:

* for the name to show in the AWS console name column, the tag must be 'Name' instead of 'name'

* tagging on instance create does not work, with ec2 reporting that the instance does not exist (in the same manner as the attach ip feature):
08:50:31,129 INFO  [STDOUT] ##### Aws::Ec2 returned an error: 400 Bad Request
08:50:31,130 INFO  [STDOUT] <?xml version="1.0" encoding="UTF-8"?>
08:50:31,130 INFO  [STDOUT] <Response><Errors><Error><Code>InvalidInstanceID.NotFound</Code><Message>The instance ID 'i-eefbdf83' does not
exist</Message></Error></Errors><RequestID>40fbb385-6cea-4993-b333-21aaebcc5ec7</RequestID></Response> #####

* the tag methods on ec2_driver (tag_instance, untag_instance) could be better named, since they don't handle generic tags, but just the
'name' tag. The tag/untag methods should be generic, and allow for management of any tag on the instance.

Toby

[PATCH core] Added names for EC2 instances

Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>

---
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb |   43 +++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 59206d7..e2a6087 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -160,6 +160,16 @@ module Deltacloud
             inst_arr = ec2.describe_instances.collect do |instance| 
               convert_instance(instance) if instance
             end.flatten
+            tags = ec2.describe_tags(
+              'Filter.1.Name' => 'resource-type', 'Filter.1.Value' => 'instance'
+            )
+            inst_arr.each do |inst|
+              name_tag = tags.select { |t| (t[:aws_resource_id] == inst.id) and t[:aws_key] == 'name' }
+              unless name_tag.empty?
+                inst.name = name_tag.first[:aws_value]
+              end
+            end
+            delete_unused_tags(credentials, inst_arr.collect {|inst| inst.id})
           end
           inst_arr = filter_on( inst_arr, :id, opts )
           filter_on( inst_arr, :state, opts )
@@ -178,6 +188,9 @@ module Deltacloud
             if opts[:public_ip]
               ec2.associate_address(new_instance.id, opts[:public_ip])
             end
+            if opts[:name]
+              tag_instance(credentials, new_instance, opts[:name])
+            end
             new_instance
           end
         end
@@ -194,7 +207,9 @@ module Deltacloud
         def destroy_instance(credentials, instance_id)
           ec2 = new_client(credentials)
           puts "Terminating instance #{instance_id}"
+          instance_id = instance_id
           if ec2.terminate_instances([instance_id])
+            untag_instance(credentials, instance_id)
             instance(credentials, instance_id)
           else
             raise Deltacloud::BackendError.new(500, "Instance", "Instance cannot be terminated", "")
@@ -401,6 +416,34 @@ module Deltacloud
             when :s3 then Aws::S3.new(credentials.user, credentials.password)
           end
         end
+
+        def tag_instance(credentials, instance, name)
+          ec2 = new_client(credentials)
+          safely do
+            ec2.create_tag(instance.id, 'name', name)
+          end
+        end
+
+        def untag_instance(credentials, instance_id)
+          ec2 = new_client(credentials)
+          safely do
+            ec2.delete_tag(instance_id, 'name')
+          end
+        end
+
+        def delete_unused_tags(credentials, inst_ids)
+          ec2 = new_client(credentials)
+          tags = []
+          safely do
+            tags = ec2.describe_tags('Filter.1.Name' => 'resource-type', 'Filter.1.Value' => 'instance')
+            tags.collect! { |t| t[:aws_resource_id] }
+            inst_ids.each do |inst_id|
+              unless tags.include?(inst_id)
+                ec2.delete_tag(inst_id, 'name')
+              end
+            end
+          end
+        end
         
         def convert_bucket(s3_bucket)
           #get blob list:
-- 
1.7.3.2