Esempio n. 1
0
	def __init__(self, key, access, cluster):
		try:
			url = "http://169.254.169.254/latest/meta-data/"

			public_hostname = urlopen(url + "public-hostname").read()
			zone = urlopen(url + "placement/availability-zone").read()
			region = zone[:-1]
		except:
			sys.exit("We should be getting user-data here...")

		#us-east-1 breaks the convention. See http://docs.amazonwebservices.com/general/latest/gr/rande.html#sdb_region
		endpoint = "sdb.{0}.amazonaws.com".format(region) if region != "us-east-1" \
			else "sdb.amazonaws.com"
		region_info = RegionInfo(name=region, endpoint=endpoint)

		sdb = SDBConnection(key, access, region=region_info)

		self.domain = sdb.create_domain(cluster)

		self.metadata = self.domain.get_item('metadata', consistent_read=True)
		if None == self.metadata:
			self.metadata = self.domain.new_item('metadata')

			self.metadata.add_value('master', '')
			self.metadata.add_value('slave', '')
			self.metadata.save()
Esempio n. 2
0
 def __init__(self, api=MongoStampedAPI(), logsQuery=logsQuery()):
     self.stamp_collection = api._stampDB._collection
     self.acct_collection = api._userDB._collection
     self.query = logsQuery
     self.writer = statWriter("dashboard")
     conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
     self.domain = conn.get_domain("dashboard")
Esempio n. 3
0
def get_latest_snapshot(key, access, name):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.lookup(name, True)
	if domain == None:
		domain = sdb.create_domain(name)

	return domain.get_item('snapshot', True)['snapshot']
Esempio n. 4
0
def delete_snapshot(key, access, name, snapshot_id):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.lookup(name, True)
	if domain == None:
		domain = sdb.create_domain(name)

	return domain.delete_item(domain.get_item(snapshot_id))
Esempio n. 5
0
def get_latest_snapshot(key, access, name):
    sdb = SDBConnection(key, access, region=region_info)

    domain = sdb.lookup(name, True)
    if domain == None:
        domain = sdb.create_domain(name)

    return domain.get_item('snapshot', True)['snapshot']
Esempio n. 6
0
def delete_snapshot(key, access, name, snapshot_id):
    sdb = SDBConnection(key, access, region=region_info)

    domain = sdb.lookup(name, True)
    if domain == None:
        domain = sdb.create_domain(name)

    return domain.delete_item(domain.get_item(snapshot_id))
Esempio n. 7
0
def get_all_snapshots(key, access, cluster):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.create_domain(cluster)

	now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
	select = "select * from `{0}` where itemName() > 'snap-' and itemName() != 'snapshot'".format(cluster)
	snapshots = domain.select(select, consistent_read=True)
	return snapshots
Esempio n. 8
0
def get_latest_snapshot(key, access, cluster):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.create_domain(cluster)

	try:
		return domain.get_item('snapshot', True)['snapshot']
	except:
		return None
Esempio n. 9
0
def set_RDB(key, access, cluster, location):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.create_domain(cluster)

	# add the latest rdb (for automatic restores)
	latest = domain.new_item('rdb')
	latest.add_value('rdb', location)
	# get the expiration date from the object name (for comparison)
	latest.add_value('created', strftime("%Y-%m-%d %H:%M:%S", gmtime()))
	latest.save()
Esempio n. 10
0
def SDB(domain_name):
    """
    Create connection to SimpleDB and return the specifed domain.
    
    domain_name - the SimpleDB domain you wish to return
    """
    conn = SDBConnection(ACCESS_KEY_ID,SECRET_ACCESS_KEY)
    domain = conn.lookup(domain_name)
    if not domain:
        domain = conn.create_domain(domain_name)
    return domain
Esempio n. 11
0
def get_expired_snapshots(key, access, name):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.lookup(name, True)
	if domain == None:
		domain = sdb.create_domain(name)

	now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
	select = "select * from `{0}` where itemName() > 'snap-' and itemName() != 'snapshot' and expires < '{1}'".format(name, now)
	print select
	snapshots = domain.select(select, consistent_read=True)
	return snapshots
Esempio n. 12
0
def get_all_snapshots(key, access, name):
    sdb = SDBConnection(key, access, region=region_info)

    domain = sdb.lookup(name, True)
    if domain == None:
        domain = sdb.create_domain(name)

    now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    select = "select * from `{0}` where itemName() > 'snap-' and itemName() != 'snapshot'".format(
        name)
    snapshots = domain.select(select, consistent_read=True)
    return snapshots
Esempio n. 13
0
def get_expired_snapshots(key, access, cluster):
    sdb = SDBConnection(key, access, region=region_info)

    domain = sdb.lookup(cluster, True)
    if domain == None:
        domain = sdb.create_domain(cluster)

    now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    select = "select * from `{0}` where itemName() > 'snap-' and itemName() != 'snapshot' and expires < '{1}'".format(
        cluster, now)
    snapshots = domain.select(select, consistent_read=True)
    return snapshots
Esempio n. 14
0
def get_identity(key, access, cluster):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.create_domain(cluster)

	slave_id = hashlib.md5(public_hostname).hexdigest()[:8]
	slave_fqdn = "{0}.{1}".format(slave_id, cluster)

	slave = domain.new_item(slave_fqdn)
	slave.add_value('id', slave_id)
	slave.add_value('endpoint', public_hostname)
	slave.save()

	return slave_fqdn
def submit():
    if len(sys.argv) < 3:
        print "Usage:"
        print "  %s -seed <seed> [<spawn coords>]" % sys.argv[0]
        return

    # TODO use less crappy command line parsing
    seed = sys.argv[2]

    if len(sys.argv) == 4:
        spawn = [int(x) for x in sys.argv[3].split(",")]
        assert(len(spawn) == 3)
        print "Generate world with this seed (\"%s\") with spawn %r [y/N]?" % (seed, spawn)
    else:
        spawn = None
        print "Generate world with this seed (\"%s\") [y/N]?" % seed
    if raw_input().lower() == 'y':
        uid = uuid.uuid4()

        print "Submitting job %s to queue..." % uid
        sqs = SQSConnection()
        sdb = SDBConnection()
        queue = sqs.get_queue("overviewer-genfromseed")
        db = sdb.get_domain("overviewerdb")
        print queue
        print db

        data = dict()
        data['uuid'] = str(uid)
        data['seed'] = seed
        data['generated'] = False
        if spawn:
            data['target_spawn'] = spawn
        if not db.put_attributes(uid, data):
            print "***Error: Failed to update the db"
            return 1
        
        msg = Message()
        msg.set_body(str(uid))
        if not queue.write(msg):
            print "***Error: Failed to enqueue"
            return 1

        print "Ok, job enqueued"


    else:
        print "Ok, not submitting.  Bye"
        return
Esempio n. 16
0
class SimpleDBBackend:
    
    def __init__(self):
        self.connection = SDBConnection(aws_credentials.accessKey,
                                        aws_credentials.secretKey)
        self.domain = self.connection.get_domain(TABLE_NAME)      


    def put(self, key, value):
        sampler.begin()
        try:
            self.domain.put_attributes(key, {VALUE:value})        
        finally:
            sampler.end()


    def get(self, key):
        sampler.begin()
        try:
            # First try an eventually consistent read.
            result = self.domain.get_attributes(key, consistent_read=False)
            return result[VALUE]
        except KeyError:
            # The eventually consistent read failed. Try a strongly consistent
            # read.
            result = self.domain.get_attributes(key, consistent_read=True)
            return result[VALUE]
        finally:
            sampler.end()
        

    def incRefCount(self, key):
        # Not implemented.
        pass
        
        
    def decRefCount(self, key):
        # Not implemented.
        pass
    
    
    def nuke(self):
        # Delete and re-create the table.
        self.connection.delete_domain(TABLE_NAME)
        self.domain = self.connection.create_domain(TABLE_NAME)
        
                    
    def flush(self):
        pass # No-op.
Esempio n. 17
0
	def __init__(self, key, access, cluster):
		try:
			url = "http://169.254.169.254/latest/meta-data/"

			zone = urlopen(url + "placement/availability-zone").read()
			region = zone[:-1]
		except:
			sys.exit("We should be getting user-data here...")

		endpoint = "sdb.{0}.amazonaws.com".format(region)
		region_info = RegionInfo(name=region, endpoint=endpoint)

		sdb = SDBConnection(key, access, region=region_info)

		self.domain = sdb.create_domain(cluster)
		self.metadata = self.domain.get_item('metadata', consistent_read=True)
Esempio n. 18
0
def add_snapshot(key, access, cluster, name, snapshot):
    sdb = SDBConnection(key, access, region=region_info)

    domain = sdb.lookup(cluster, True)
    if domain == None:
        domain = sdb.create_domain(cluster)

    now = strftime("%Y-%m-%d %H:%M:%S", gmtime())

    # add the snapshot for expiration
    backup = domain.new_item(snapshot[0])
    backup.add_value('name', name)
    backup.add_value('snapshot', snapshot[0])
    backup.add_value('created', now)
    backup.add_value('expires', snapshot[1])
    backup.save()
Esempio n. 19
0
def add_snapshot(key, access, cluster, name, snapshot):
    sdb = SDBConnection(key, access, region=region_info)

    domain = sdb.lookup(cluster, True)
    if domain == None:
        domain = sdb.create_domain(cluster)

    now = strftime("%Y-%m-%d %H:%M:%S", gmtime())

    # add the snapshot for expiration
    backup = domain.new_item(snapshot[0])
    backup.add_value("name", name)
    backup.add_value("snapshot", snapshot[0])
    backup.add_value("created", now)
    backup.add_value("expires", snapshot[1])
    backup.save()
Esempio n. 20
0
def get_latest_snapshot(key, access, cluster, name):
    sdb = SDBConnection(key, access, region=region_info)
    now = strftime("%Y-%m-%d %H:%M:%S", gmtime())

    domain = sdb.lookup(cluster, True)
    if domain == None:
        domain = sdb.create_domain(cluster)

    select = "select * from `{0}` where name = '{1}' and created < '{2}' order by created desc limit 1".format(
        cluster, name, now)
    snapshots = domain.select(select, consistent_read=True)

    try:
        snapshot = snapshots.next()
        return snapshot['snapshot']
    except:
        return None
Esempio n. 21
0
def submit():
    if len(sys.argv) < 3:
        print "Usage:"
        print "  %s -submit <world uuid>" % sys.argv[0]
        return

    sdb = SDBConnection()
    db = sdb.get_domain("overviewerdb")

    # TODO use less crappy command line parsing
    world_uuid = uuid.UUID(sys.argv[2])

    world_item = db.get_item(world_uuid)
    if not world_item:
        print "Can't find that world!"
        return 1

    print "Submit this world for rendering? [y/N]"
    if raw_input().lower() != 'y':
        return "Ok, nevermind."
        return 0

    from boto.sqs.connection import SQSConnection 
    sqs = SQSConnection()
    
    queue = sqs.get_queue("overviewer-render")
    
    render_uuid = uuid.uuid4()
    print "Render UUID:", render_uuid
    data = dict()
    data['uuid'] = str(render_uuid)
    data['rendered'] = False
    data['world_uuid'] = str(world_uuid)

    if not db.put_attributes(str(render_uuid), data):
        print "***Error: Failed to update the db"
        return 1

    msg = Message()
    msg.set_body(str(render_uuid))

    if not queue.write(msg):
        print "***Error: Failed to enqueue"
        return 1
    print "Ok, job enqueued"
    return 0
Esempio n. 22
0
    def __init__(self, stack):
        try:
            self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
        except SDBResponseError:
            print "SimpleDB Connection Refused"
            raise

        self._stack = stack
Esempio n. 23
0
def set_cluster_metadata(key, access, cluster):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.create_domain(cluster)

	# set the basic values in the 'master' record
	metadata = domain.new_item('metadata')
	#master = "{0}.{1}".format(
	#						os.environ['REDIS_NAME'].strip(),
	#						os.environ['HOSTED_ZONE_NAME'].rstrip('.'))
	#metadata.add_value('master', master)

	try:
		if "" != os.environ['REDIS_SIZE'].strip():
			metadata.add_value('size', os.environ['REDIS_SIZE'])
	except:
		pass

	try:
		if "" != os.environ['REDIS_PERSISTENCE'].strip():
			metadata.add_value('persistence', os.environ['REDIS_PERSISTENCE'])
		else:
			metadata.add_value('persistence', 'no')
	except:
		metadata.add_value('persistence', 'no')

	try:
		if "" != os.environ['REDIS_SNAPSHOT'].strip():
			metadata.add_value('snapshot', os.environ['REDIS_SNAPSHOT'])
	except:
		pass

	try:
		if "" != os.environ['REDIS_RDB'].strip():
			metadata.add_value('rdb', os.environ['REDIS_RDB'])
	except:
		pass

	try:
		if "" != os.environ['REDIS_AOF'].strip():
			metadata.add_value('aof', os.environ['REDIS_AOF'])
	except:
		pass

	metadata.save()
Esempio n. 24
0
def get_latest_snapshot(key, access, cluster, name):
    sdb = SDBConnection(key, access, region=region_info)
    now = strftime("%Y-%m-%d %H:%M:%S", gmtime())

    domain = sdb.lookup(cluster, True)
    if domain == None:
        domain = sdb.create_domain(cluster)

    select = "select * from `{0}` where name = '{1}' and created < '{2}' order by created desc limit 1".format(
        cluster, name, now
    )
    snapshots = domain.select(select, consistent_read=True)

    try:
        snapshot = snapshots.next()
        return snapshot["snapshot"]
    except:
        return None
Esempio n. 25
0
    def __init__(self,api):
        self.api = api
        self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
        self.domains = {}
        self.statDict = {}

        for i in range (0,16):
            suffix = '0'+hex(i)[2]
            self.domains[suffix] = self.conn.get_domain('stats_dev_%s' % (suffix))
Esempio n. 26
0
    def __init__(self, domain=None):
        self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
        self.domain_name = None
        self.domains = {}

        if domain is None and is_ec2():
            stack = get_stack()
            stack_name = str(stack['instance']['stack'])
            self.domain_name = stack_name
        elif domain is not None:
            self.domain_name = domain
Esempio n. 27
0
def add_snapshot(key, access, name, snapshot):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.lookup(name, True)
	if domain == None:
		domain = sdb.create_domain(name)

	now = strftime("%Y-%m-%d %H:%M:%S", gmtime())

	# add the snapshot for expiration
	backup = domain.new_item(snapshot[0])
	backup.add_value('snapshot', snapshot[0])
	backup.add_value('created', now)
	backup.add_value('expires', snapshot[1])
	backup.save()

	# add the latest (for automatic restores)
	latest = domain.new_item('snapshot')
	latest.add_value('snapshot', snapshot[0])
	latest.save()
Esempio n. 28
0
    def test_1_basic(self):
        print '--- running SDBConnection tests ---'
        c = SDBConnection()
        rs = c.get_all_domains()
        num_domains = len(rs)

        # try illegal name
        try:
            domain = c.create_domain('bad:domain:name')
        except SDBResponseError:
            pass

        # now create one that should work and should be unique (i.e. a new one)
        domain_name = 'test%d' % int(time.time())
        domain = c.create_domain(domain_name)
        rs = c.get_all_domains()
        assert len(rs) == num_domains + 1

        # now let's a couple of items and attributes
        item_1 = 'item1'
        same_value = 'same_value'
        attrs_1 = {'name1': same_value, 'name2': 'diff_value_1'}
        domain.put_attributes(item_1, attrs_1)
        item_2 = 'item2'
        attrs_2 = {'name1': same_value, 'name2': 'diff_value_2'}
        domain.put_attributes(item_2, attrs_2)
        time.sleep(10)

        # try to get the attributes and see if they match
        item = domain.get_attributes(item_1)
        assert len(item.keys()) == len(attrs_1.keys())
        assert item['name1'] == attrs_1['name1']
        assert item['name2'] == attrs_1['name2']

        # try a search or two
        rs = domain.query("['name1'='%s']" % same_value)
        n = 0
        for item in rs:
            n += 1
        assert n == 2
        rs = domain.query("['name2'='diff_value_2']")
        n = 0
        for item in rs:
            n += 1
        assert n == 1

        # delete all attributes associated with item_1
        stat = domain.delete_attributes(item_1)
        assert stat

        # now delete the domain
        stat = c.delete_domain(domain)
        assert stat

        print '--- tests completed ---'
Esempio n. 29
0
def connect_sdb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
    @type aws_access_key_id: string
    @param aws_access_key_id: Your AWS Access Key ID
    
    @type aws_secret_access_key: string
    @param aws_secret_access_key: Your AWS Secret Access Key
    
    @rtype: L{SDBConnection<boto.sdb.connection.SDBConnection>}
    @return: A connection to Amazon's SDB
    """
    from boto.sdb.connection import SDBConnection
    return SDBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
Esempio n. 30
0
def connect_sdb(aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
    """
    :type aws_access_key_id: string
    :param aws_access_key_id: Your AWS Access Key ID

    :type aws_secret_access_key: string
    :param aws_secret_access_key: Your AWS Secret Access Key

    :rtype: :class:`boto.sdb.connection.SDBConnection`
    :return: A connection to Amazon's SDB
    """
    from boto.sdb.connection import SDBConnection
    return SDBConnection(aws_access_key_id, aws_secret_access_key, **kwargs)
Esempio n. 31
0
 def connect(self, **kw_params):
     """
     Connect to this Region's endpoint. Returns an SDBConnection
     object pointing to the endpoint associated with this region.
     You may pass any of the arguments accepted by the SDBConnection
     object's constructor as keyword arguments and they will be
     passed along to the SDBConnection object.
     
     :rtype: :class:`boto.sdb.connection.SDBConnection`
     :return: The connection to this regions endpoint
     """
     from boto.sdb.connection import SDBConnection
     return SDBConnection(region=self, **kw_params)
Esempio n. 32
0
class statWriter(object):
    
    def __init__(self,domain=None):
        self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
        self.domain_name = domain
        
    def write(self,stat,key=None,domain=None):
        if domain is None:
            if self.domain_name is None:
                return False
            else:
                domain = self.domain_name
        
        if key is None:
            key = uuid.uuid1()
            
        if len(stat) > 0:
            try:
                domain = self.conn.get_domain(domain)
            except SDBResponseError:
                domain = self.conn.create_domain(domain)
            domain.put_attributes(key, stat, replace=False)
        
        return True
Esempio n. 33
0
def add_from_url():
    if len(sys.argv) < 3:
        print "Usage:"
        print "  %s -url <url>" % sys.argv[0]
        return

    url = sys.argv[2]
    print "Generate world with this url (\"%s\") [y/N]?" % url
    if raw_input().lower() != 'y':
        print "Ok, nevermind"
        return
    
    uid = uuid.uuid4()
    data = dict()
    data['uuid'] = uid
    data['world_url'] = url

    sdb = SDBConnection()
    db = sdb.get_domain("overviewerdb")
    if not db.put_attributes(uid, data):
        print "***Error: Failed to update the db"
        return 1
    print "Ok. DB updated"
    print uid
Esempio n. 34
0
def add_snapshot(key, access, cluster, snapshot):
	sdb = SDBConnection(key, access, region=region_info)

	domain = sdb.create_domain(cluster)

	now = strftime("%Y-%m-%d %H:%M:%S", gmtime())
	# add the latest rdb (for automatic restores)
	latest = domain.new_item('snapshot')
	latest.add_value('snapshot', snapshot[0])
	# get the expiration date from the object name (for comparison)
	latest.add_value('created', now)
	latest.save()

	# add the snapshot for expiration
	backup = domain.new_item(snapshot[0])
	backup.add_value('snapshot', snapshot[0])
	backup.add_value('created', now)
	backup.add_value('expires', snapshot[1])
	backup.save()

	# add the latest (for automatic restores)
	latest = domain.new_item('snapshot')
	latest.add_value('snapshot', snapshot[0])
	latest.save()
Esempio n. 35
0
    def __init__(self,domain_name=None):
        self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
        self.domains = {}
        self.statSet  = set()
        self.statDict = {}
        self.errDict = {}
        self.statCount = 0
        self.statCountByNode = {}
        self.statTimeByNode = {}

        if domain_name is None:
            domain_name = 'stress'
            
        for i in range (0,16):
            suffix = '0'+hex(i)[2]
            self.domains[suffix] = self.conn.get_domain('%s_%s' % (domain_name,suffix))
    def test_1_basic(self):
        print '--- running SDBConnection tests ---'
        c = SDBConnection()
        rs = c.get_all_domains()
        num_domains = len(rs)
    
        # try illegal name
        try:
            domain = c.create_domain('bad:domain:name')
        except SDBResponseError:
            pass
        
        # now create one that should work and should be unique (i.e. a new one)
        domain_name = 'test%d' % int(time.time())
        domain = c.create_domain(domain_name)
        rs  = c.get_all_domains()
        assert len(rs) == num_domains+1

        # now let's a couple of items and attributes
        item_1 = 'item1'
        same_value = 'same_value'
        attrs_1 = {'name1' : same_value, 'name2' : 'diff_value_1'}
        domain.put_attributes(item_1, attrs_1)
        item_2 = 'item2'
        attrs_2 = {'name1' : same_value, 'name2' : 'diff_value_2'}
        domain.put_attributes(item_2, attrs_2)
        time.sleep(10)

        # try to get the attributes and see if they match
        item = domain.get_attributes(item_1)
        assert len(item.keys()) == len(attrs_1.keys())
        assert item['name1'] == attrs_1['name1']
        assert item['name2'] == attrs_1['name2']

        # try a search or two
        rs = domain.query("['name1'='%s']" % same_value)
        n = 0
        for item in rs:
            n += 1
        assert n == 2
        rs = domain.query("['name2'='diff_value_2']")
        n = 0
        for item in rs:
            n += 1
        assert n == 1

        # delete all attributes associated with item_1
        stat = domain.delete_attributes(item_1)
        assert stat

        # now delete the domain
        stat = c.delete_domain(domain)
        assert stat

        print '--- tests completed ---'
Esempio n. 37
0
 def __init__(self):
     try:
         self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
     except SDBResponseError:
         print "SimpleDB Connection Refused"
     
     self.statList = []
     self.statCount = 0
     
     self.v1_prod_domains = {}
     self.v2_prod_domains = {}
     self.v2_dev_domains = {}
     self.v2_stress_domains = {}
     
     for i in range (0,16):
         suffix = '0'+hex(i)[2]
         
         self.v1_prod_domains[suffix] = self.conn.get_domain('stats_prod_%s' % suffix)
         self.v2_prod_domains[suffix] = self.conn.get_domain('bowser_%s' % suffix)
         self.v2_dev_domains[suffix] = self.conn.get_domain('stats_dev_%s' % suffix)
         self.v2_stress_domains[suffix] = self.conn.get_domain('stress_%s' % suffix)
Esempio n. 38
0
 def __init__(self):
     self.connection = SDBConnection(aws_credentials.accessKey,
                                     aws_credentials.secretKey)
     self.domain = self.connection.get_domain(TABLE_NAME)      
Esempio n. 39
0
    def test_1_basic(self):
        print '--- running SDBConnection tests ---'
        c = SDBConnection()
        rs = c.get_all_domains()
        num_domains = len(rs)

        # try illegal name
        try:
            domain = c.create_domain('bad:domain:name')
        except SDBResponseError:
            pass

        # now create one that should work and should be unique (i.e. a new one)
        domain_name = 'test%d' % int(time.time())
        domain = c.create_domain(domain_name)
        rs = c.get_all_domains()
        assert len(rs) == num_domains + 1

        # now let's a couple of items and attributes
        item_1 = 'item1'
        same_value = 'same_value'
        attrs_1 = {'name1' : same_value, 'name2' : 'diff_value_1'}
        domain.put_attributes(item_1, attrs_1)
        item_2 = 'item2'
        attrs_2 = {'name1' : same_value, 'name2' : 'diff_value_2'}
        domain.put_attributes(item_2, attrs_2)

        # try to get the attributes and see if they match
        item = domain.get_attributes(item_1, consistent_read=True)
        assert len(item.keys()) == len(attrs_1.keys())
        assert item['name1'] == attrs_1['name1']
        assert item['name2'] == attrs_1['name2']

        # try a search or two
        query = 'select * from %s where name1="%s"' % (domain_name, same_value)
        rs = domain.select(query, consistent_read=True)
        n = 0
        for item in rs:
            n += 1
        assert n == 2
        query = 'select * from %s where name2="diff_value_2"' % domain_name
        rs = domain.select(query, consistent_read=True)
        n = 0
        for item in rs:
            n += 1
        assert n == 1

        # delete all attributes associated with item_1
        stat = domain.delete_attributes(item_1)
        assert stat

        # now try a batch put operation on the domain
        item3 = {'name3_1' : 'value3_1',
                 'name3_2' : 'value3_2',
                 'name3_3' : ['value3_3_1', 'value3_3_2']}

        item4 = {'name4_1' : 'value4_1',
                 'name4_2' : ['value4_2_1', 'value4_2_2'],
                 'name4_3' : 'value4_3'}
        items = {'item3' : item3, 'item4' : item4}
        domain.batch_put_attributes(items)

        item = domain.get_attributes('item3', consistent_read=True)
        assert item['name3_2'] == 'value3_2'

        # now try a batch delete operation (variation #1)
        items = {'item3' : item3}
        stat = domain.batch_delete_attributes(items)

        item = domain.get_attributes('item3', consistent_read=True)
        assert not item

        # now try a batch delete operation (variation #2)
        stat = domain.batch_delete_attributes({'item4' : None})

        item = domain.get_attributes('item4', consistent_read=True)
        assert not item

        # now delete the domain
        stat = c.delete_domain(domain)
        assert stat

        print '--- tests completed ---'
Esempio n. 40
0
class SimpleDB(object):
    
    def __init__(self, domain=None):
        self.conn = SDBConnection(keys.aws.AWS_ACCESS_KEY_ID, keys.aws.AWS_SECRET_KEY)
        self.domain_name = None
        self.domains = {}

        if domain is None and is_ec2():
            stack = get_stack()
            stack_name = str(stack['instance']['stack'])
            self.domain_name = stack_name
        elif domain is not None:
            self.domain_name = domain

    def addStat(self, stat):
        if self.domain_name is None:
            return

        try:
            # Only add specific parameters
            data = {}

            if 'user_id' in stat:
                data['uid'] = stat['user_id']

            if 'path' in stat:
                data['uri'] = stat['path']

            if 'method' in stat:
                data['mtd'] = stat['method']

            if 'form' in stat:
                try:
                    for k, v in stat['form'].items():
                        try:
                            if not isinstance(v, basestring):
                                v = str(v)
                            if len(v.encode('utf-8')) > 1024:
                                v = '<INPUT TOO LONG>'
                            data['frm_%s' % k] = v
                        except Exception as e:
                            print e
                except Exception as e:
                    print e

            if 'result' in stat:
                data['cde'] = str(stat['result'])

            if 'begin' in stat:
                data['bgn'] = stat['begin'].isoformat()

            if 'finish' in stat:
                data['end'] = stat['finish'].isoformat()

            if 'node' in stat:
                data['nde'] = stat['node']
                
            if 'client_id' in stat:
                data['cid'] = stat['client_id']

            if 'duration' in stat:
                data['dur'] = "{0:10d}".format(stat['duration'])

            if len(data) > 0:
                statId = str(ObjectId())
                if data['uri'] != '/v1/ping.json' and data['uri'] != '/v1/temp/ping.json':
                    suffix = '0%s' % (sha1(statId).hexdigest()[0])
                    if suffix in self.domains:
                        domain = self.domains[suffix]
                    else:
                        try:
                            domain = self.conn.get_domain('%s_%s' % (self.domain_name, suffix))
                        except SDBResponseError:
                            domain = self.conn.create_domain('%s_%s' % (self.domain_name, suffix))
                        self.domains[suffix] = domain
                    domain.put_attributes(statId, data, replace=False)


        except Exception as e:
            print e
            raise
#!/usr/bin/python2

import sys
import os
from pprint import pprint

try:
    import boto
except ImportError:
    sys.path.append(os.path.expanduser("~/devel/boto"))
    import boto


from boto.sdb.connection import SDBConnection 

uid = sys.argv[1]
print "Looking up", uid

sdb = SDBConnection()
db = sdb.get_domain("overviewerdb")
data = db.get_item(uid)
pprint(data)