Example #1
0
def _helper_main(args):
    global WEBUI

    awsregion = os.environ['AWS_REGION']
    sqsurl = os.environ['AWS_SQS_URL']

    keyname = os.environ['KEYNAME']
    keypath = generate_skey(awsregion, keyname)

    sqsconn = boto.sqs.connect_to_region(awsregion)
    queue = boto.sqs.queue.Queue(connection=sqsconn, url=sqsurl)
    if queue is None:
        raise Exception("Queue %s not found" % sqsurl)

    mypath = os.path.dirname(os.path.realpath(__file__))
    tpl = os.path.join(mypath, 'www', 'index.j2')
    target = os.path.join(mypath, 'www', 'index.html')
    WEBUI = WebUI(tpl, target)

    WEBUI.waiting()

    while True:
        msg = queue.read(30)
        if msg == None:
            LOG.debug('no message')
            time.sleep(10)
            continue

        msgbody = json.loads(msg.get_body())
        LOG.debug("message: %s", msgbody['Message'])
        crmsg = json.loads(msgbody['Message'])

        rt = crmsg.get('RequestType', None)
        if rt == 'Create':
            WEBUI.add_request(crmsg.get('RequestId'))
            try:
                success, reason, data = execute_playbook(
                    keypath, crmsg.get('ResourceProperties', {}))
                LOG.debug("playbook result: %s %s %s", success, reason, data)
            except:
                LOG.exception("exception in execute_playbook")
                reply_to_msg(crmsg,
                             success=False,
                             reason="Exception executing playbook")
                WEBUI.set_request_result(False, "Exception executing playbook")
            else:
                reply_to_msg(crmsg, success=success, reason=reason, data=data)
                WEBUI.set_request_result(success, reason, data)
        elif rt == 'Delete':
            reply_to_msg(crmsg, success=True, reason="OK")
        else:
            LOG.warning("Unhandled RequestType %s", rt)
            reply_to_msg(crmsg, succces=True, reason="OK")

        WEBUI.waiting()

        queue.delete_message(msg)
Example #2
0
def consume_message(name):
    """
    Read and erase message
    curl -s -X DELETE -H 'Accept: application/json' http://localhost:5000/queues/<name>/msgs | python -mjson.tool
    """
    queue = conn.get_queue(name)
    m = queue.read()
    message = m.get_body()
    queue.delete_message(m)
    return Response(response='{"Message deleted": "%s"}' % message, mimetype="application/json")
Example #3
0
def consume_message(name):
	conn = get_conn()
	queue = conn.get_queue(name)
	if queue is None:
                resp = json.dumps("Queue not found\n")
	else:
		messages = queue.get_messages()
		if len(messages) > 0:
			resp = json.dumps("Message: " + messages[0].get_body())
			queue.delete_message(messages[0])
		else:
			resp = json.dumps("No messages")
	return Response(response=resp,mimetype="application/json")
Example #4
0
def messages_consume(name):
	"""
	Consume message from queue
	curl -X DELETE -H 'Accept: application/json' http://localhost:8080/queues/<queue>/msgs
	"""

	conn = get_conn()
	queue = conn.get_queue(name)
	messages = queue.get_messages()
	if len(messages) > 0:
		message = messages[0]
		resp = "Queue: "+ name +" \nDeleted message: "+ message.get_body()+" \n"
		queue.delete_message(message)
	else:
		resp = "No messages for queue "+ name +"\n"
	return Response(response=resp, mimetype="application/json")
Example #5
0
def read_message(name):
	res = requests.get('http://ec2-52-30-7-5.eu-west-1.compute.amazonaws.com:81/key')
	str = res.text.split(':')

	# Get the keys from a specific url and then use them to connect to AWS Service 
	access_key_id = str[0]
	secret_access_key = str[1]

	# Set up a connection to the AWS service. 
	conn = boto.sqs.connect_to_region("eu-west-1", aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key)

	# Get the queue object to count the messages
	queue = conn.get_queue(name)

	# Count the messages
	if queue is not None:
		message = queue.read()
		print("Messgae: " + message.get_body())

		if queue.delete_message(message) is True:
			print("Messgae deleted")
		else:
			print("Message could not be deleted")
			
	else:
		print("Queue could not be found")
Example #6
0
def messages_consume(name):
    """
	Consume message from queue
	curl -X DELETE -H 'Accept: application/json' http://localhost:8080/queues/<queue>/msgs
	"""

    conn = get_conn()
    queue = conn.get_queue(name)
    messages = queue.get_messages()
    if len(messages) > 0:
        message = messages[0]
        resp = "Queue: " + name + " \nDeleted message: " + message.get_body(
        ) + " \n"
        queue.delete_message(message)
    else:
        resp = "No messages for queue " + name + "\n"
    return Response(response=resp, mimetype="application/json")
Example #7
0
def consume_message(name):
    """
    Read and erase message
    
    curl -s -X DELETE -H 'Accept: application/json' http://localhost:5000/queues/<name>/msgs | python -mjson.tool

    """
    queue = conn.get_queue(name)
    if(queue is not None):
	if(queue.read() is not None):
	    msgObj = queue.read()
            msg = msgObj.get_body()
            queue.delete_message(msgObj)
            resp = 'Message %s has been deleted\n' % msg
	else:
	    resp = 'No message to delete\n'
    else :
	resp = 'Queue % was not found\n' % name   
    return Response(response=resp, mimetype="application/json")
Example #8
0
import boto.sqs
import boto.sqs.queue
from boto.sqs.message import Message
from boto.sqs.connection import SQSConnection
from boto.exception import SQSError
import sys
import requests

url = "http://ec2-52-30-7-5.eu-west-1.compute.amazonaws.com:81/key"
data = requests.get(url).text
keys = data.split(":")
access_key_id = keys[0]
secret_access_key = keys[1]

conn = boto.sqs.connect_to_region("eu-west-1", aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key)

queue = conn.get_queue(sys.argv[1])
messages = queue.get_messages()
if len(messages) > 0:
    print "Message from queue " + sys.argv[1] + ": " + messages[0].get_body()
    queue.delete_message(messages[0])
    print "Message deleted!"
else:
    print "No messages"
Example #9
0
import os
import boto.sqs
import boto.sqs.queue
from boto.sqs.message import Message
from boto.sqs.connection import SQSConnection
from boto.exception import SQSError
import sys
import urllib2

response = urllib2.urlopen('http://ec2-52-30-7-5.eu-west-1.compute.amazonaws.com:81/key')
content = response.read();
content = content.split (':');
response.close();

# Get the keys from a specific url and then use them to connect to AWS Service 
access_key_id = str(content[0])
secret_access_key = str(content[1])

# Set up a connection to the AWS service. 
conn = boto.sqs.connect_to_region("eu-west-1", aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key)

#Read message
queue = conn.get_queue(sys.argv[1])
msg = queue.read()
print msg.get_body()

#Delete message
queue.delete_message(msg)
print "Deleted message"
Example #10
0


# Get the keys from a specific url and then use them to connect to AWS Service 
access_key_id = str[0]
secret_access_key = str[1]

# Set up a connection to the AWS service. 
conn = boto.sqs.connect_to_region("eu-west-1", aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key)

# read messages from queue

queue = conn.get_queue(queue_name)


m = queue.read()



str = m.get_body()

queue.delete_message(m)
print "Message: " + str
print "Message deleted from the queue"






Example #11
0
i = 0

start_time = time.time()
while True:
    dbconnection = boto.connect_dynamodb(AWS_KEY, AWS_ACCESS_KEY)
    rs = queue.get_messages(message_attributes='Values')
    if len(rs) != 0:
        msg = rs[0]
        id = rs[0].message_attributes['Values']['string_value']
        f = msg.get_body()
        # try:
        print "Connecting to Table...."
        table = dbconnection.get_table('task_table')

        if table.has_item(hash_key=id):
            queue.delete_message(msg)
        else:
            task_data = {'task': f}
            job = table.new_item(hash_key=id, attrs=task_data)
            job.put()
            print "Data inserted successfully into DynamoDB...."
            duration = f[6:]
            time.sleep(int(duration) * 1.0 / 1000)
            queue.delete_message(msg)

        # except:
        # print "Connection UnSuccessfull!!!"
    else:
        break

end_time = time.time() - start_time