if args.noop: print "Would have created sqs_queue with id: {}\nec2_args:".format( run_id) pprint(ec2_args) ami = "ami-00000" else: run_summary, ami = launch_and_configure(ec2_args) print print "Summary:\n" for run in run_summary: print "{:<30} {:0>2.0f}:{:0>5.2f}".format( run[0], run[1] / 60, run[1] % 60) print "AMI: {}".format(ami) if args.mongo_uri: mongo_con.update_ami(ami) mongo_con.update_deployment(ami) finally: print if not args.no_cleanup and not args.noop: if sqs_queue: print "Cleaning up - Removing SQS queue - {}".format(run_id) sqs.delete_queue(sqs_queue) if instance_id: print "Cleaning up - Terminating instance ID - {}".format( instance_id) # Check to make sure we have an instance id. if instance_id: ec2.terminate_instances(instance_ids=[instance_id])
__author__ = 'mhoyer' import boto.sqs from boto.sqs.message import Message sqs = boto.sqs.connect_to_region("eu-west-1") testqueue = sqs.create_queue('testqueue') # write message message = Message() message.set_body("My first test message") testqueue.write(message) # read message messages = testqueue.get_messages() print messages[0].get_body() sqs.delete_queue(testqueue)
message = 'Finished baking AMI {image_id} for {environment} {deployment} {play}.'.format( image_id=ami, environment=args.environment, deployment=args.deployment, play=args.play) send_hipchat_message(message) except Exception as e: message = 'An error occurred building AMI for {environment} ' \ '{deployment} {play}. The Exception was {exception}'.format( environment=args.environment, deployment=args.deployment, play=args.play, exception=repr(e)) send_hipchat_message(message) error_in_abbey_run = True finally: print if not args.no_cleanup and not args.noop: if sqs_queue: print "Cleaning up - Removing SQS queue - {}".format(run_id) sqs.delete_queue(sqs_queue) if instance_id: print "Cleaning up - Terminating instance ID - {}".format( instance_id) # Check to make sure we have an instance id. if instance_id: ec2.terminate_instances(instance_ids=[instance_id]) if error_in_abbey_run: exit(1)
def wait_tasks(): queue_id = environ.get("SLURM_EC2_QUEUE_ID") if queue_id is None: print("SLURM_EC2_QUEUE_ID environment variable not set", file=stderr) return 1 request_queue_name = "slurm-%s-request" % queue_id response_queue_name = "slurm-%s-response" % queue_id sqs = get_sqs() request_queue = sqs.get_queue(request_queue_name) response_queue = sqs.get_queue(response_queue_name) times_empty = 0 while True: msg = response_queue.read() if msg is None: # Are there pending requests? attrs = request_queue.get_attributes() in_flight = (int(attrs['ApproximateNumberOfMessages']) + int(attrs['ApproximateNumberOfMessagesNotVisible'])) if in_flight == 0: times_empty += 1 else: times_empty = 0 # If we've not seen any responses and haven't found any unserved # requests for MAX_TIMES_EMPTY polls, stop. if times_empty >= MAX_TIMES_EMPTY: break if in_flight == 0: attrs = response_queue.get_attributes() long_poll_time = int( attrs.get('ReceiveMessageWaitTimeSeconds', 0)) print("No tasks in flight... will wait %d more second(s)" % ((MAX_TIMES_EMPTY - times_empty) * (SLEEP_TIME + long_poll_time))) else: print("%s task(s) in flight, but none are ready" % (in_flight, )) sleep(SLEEP_TIME) continue times_empty = 0 try: # Decode the message as JSON response = json_loads(msg.get_body()) id = response.get("id") exit_code = response.get("exit_code") log_dirs = [".", environ["HOME"], "/tmp", "/var/tmp"] for log_dir in log_dirs: filename = "%s/%s.json" % (log_dir, id) try: fd = open(filename, "w") break except IOError: pass else: # Couldn't open a log file. filename = "/dev/null" fd = open(filename, "w") fd.write(msg.get_body()) fd.close() print("Task %s finished with exit code %s; logged to %s" % (id, exit_code, filename)) except: pass msg.delete() sqs.delete_queue(request_queue) sqs.delete_queue(response_queue) return 0