def wait_for_instance(self, instance_id, state): """ wait for EC2 instance to reach a state """ msg( "... waiting EC2 instance " + instance_id + " to be in " + state + " state ..." ) reached = False instance = None while not reached: insts = self.connection.get_all_instances([instance_id]) for res in insts : instance = res.instances[0] debug_msg( "... current state: " + instance.state ) if instance.state == state : reached = True break if not reached: BaseDatetime.sleep(10) return instance
key = self.connection.create_key_pair(ec2_key_pair) key.save(key_dir) else: raise reservation = self.connection.run_instances(ec2_ami, key_name=ec2_key_pair, instance_type=ec2_instancetype, security_groups=[group]) instance = reservation.instances[0] conn.create_tags([instance.id], {"Name":ec2_tag}) while instance.state == u'pending': msg( "Instance state: %s" % instance.state ) BaseDatetime.sleep(10) instance.update() msg( "Instance.ID: %s: " % instance.id ) msg( "Instance state: %s " % instance.state ) msg( "Public DNS: %s" % instance.public_dns_name ) return instance def start_instance(self, instance_id): """ start EC2 instance with given ID """ msg( "... starting EC2 instance " + instance_id + " ..." ) self.connection.start_instances(instance_ids=[instance_id]) return self.wait_for_instance(instance_id, "running")