From 50d61c735dfc0b390997c3f9384866b7636b3716 Mon Sep 17 00:00:00 2001 From: Jonh Wendell Date: Mon, 6 Apr 2015 14:18:14 -0300 Subject: [PATCH] ec2_launch: Store the instance object directly in the info dictionary Instead of storing just its ID. This gives quick access to the recently created instance, which allows us to simplify the code that needs to fetch the instance object every time it was necessary. --- bootstrapvz/plugins/ec2_launch/tasks.py | 29 ++++++++++--------------- 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/bootstrapvz/plugins/ec2_launch/tasks.py b/bootstrapvz/plugins/ec2_launch/tasks.py index df159db..c597254 100644 --- a/bootstrapvz/plugins/ec2_launch/tasks.py +++ b/bootstrapvz/plugins/ec2_launch/tasks.py @@ -26,14 +26,14 @@ class LaunchEC2Instance(Task): r = conn.run_instances(info._ec2['image'], security_group_ids=info.manifest.plugins['ec2_launch'].get('security_group_ids'), instance_type=info.manifest.plugins['ec2_launch'].get('instance_type', 't2.micro')) - info._ec2['instance_id'] = r.instances[0].id + info._ec2['instance'] = r.instances[0] if 'tags' in info.manifest.plugins['ec2_launch']: def apply_format(v): return v.format(**info.manifest_vars) tags = info.manifest.plugins['ec2_launch']['tags'] r = {k: apply_format(v) for k, v in tags.items()} - conn.create_tags([info._ec2['instance_id']], r) + conn.create_tags([info._ec2['instance'].id], r) class PrintPublicIPAddress(Task): @@ -50,22 +50,15 @@ class PrintPublicIPAddress(Task): filename = '/dev/null' f = open(filename, 'w') - r = ec2['connection'].get_only_instances([ec2['instance_id']]) - if not r and not r[0]: - logger.error('Could not get instance metadata') - f.write('') + def instance_has_ip(): + ec2['instance'].update() + return ec2['instance'].ip_address + + if waituntil(instance_has_ip, timeout=120, interval=5): + logger.info('******* EC2 IP ADDRESS: %s *******' % ec2['instance'].ip_address) + f.write(ec2['instance'].ip_address) else: - instance = r[0] - - def instance_has_ip(): - instance.update() - return instance.ip_address - - if waituntil(instance_has_ip, timeout=120, interval=5): - logger.info('******* EC2 IP ADDRESS: %s *******' % instance.ip_address) - f.write(instance.ip_address) - else: - logger.error('Could not get IP address for the instance') - f.write('') + logger.error('Could not get IP address for the instance') + f.write('') f.close()