mardi 28 avril 2015

EC2 instance creation and polling until state is 'running' using boto library

I want to create a new spot instance using boto library and run some commands on it using fabric. I'm using the below code

instance = reservations[0].instances[0]
status = instance.update()
tries = 40
i=0
while status == 'pending':
  log.info("Status of instance: [ " + job_instance_id + " ] is pending")
  time.sleep(10)
  status = instance.update()
  i=i+1
  if i > tries:
    break
log.info("Status of instance [ " + job_instance_id + " ] is " + status)
if status == 'running':
  log.info("Adding tag")
  instance.add_tag("Name", "test_tag")
  public_dns_name = instance.public_dns_name
  log.info("Host Name : " + public_dns_name)
  init_instance(public_dns_name)

In init_instance I'm running some commands using fabric. These commands fail sometimes with "Unable to connect to host" error. Sometimes it works fine without any issues. Can you please let me know why does it fail sometimes? How can I handle this? I already keep polling untill the state of the instance changes to 'running' and run the commands via ssh only after the instance state moves to 'running'




Aucun commentaire:

Enregistrer un commentaire