def __init__(self, username, api_key, region, group_name, server_name='consumer', cooldown=None, min_entities=None, max_entities=None, flavor='performance1-1', image=None, load_balancers=(), networks=[{'uuid': '11111111-1111-1111-1111-111111111111'}, {'uuid': '00000000-0000-0000-0000-000000000000'}], metadata={}, debug=False): # set the scaling group attributes pyrax.set_setting('identity_type', 'rackspace') if debug: print "username: %s" % username print "api_key: %s" % api_key print "min_entites beg: %s" % min_entities print "max_entites beg: %s" % max_entities pyrax.set_credentials(username, api_key) self.sg = None self.au = pyrax.connect_to_autoscale(region=region) self.cq = pyrax.connect_to_queues(region=region) self.cs = pyrax.connect_to_cloudservers(region=region) self.server_name = server_name self.load_balancers = load_balancers self.networks = networks self.image = image self.flavor = flavor self.debug = debug if not group_name in [i.name for i in self.au.list()]: # set sane default values here scaling group creation self.cooldown = int(cooldown) or 60 # set default 60 self.min_entities = int(min_entities) or 0 # set default to 0 self.max_entities = int(max_entities) or 1 # set default to 1 self._create_scaling_group(group_name, metadata) else: # go ahead and take default here however if they are empty they will # be overriden by an existing scaling group self.cooldown = int(cooldown) self.min_entities = int(min_entities) self.max_entities = int(max_entities) self._get_scaling_group(group_name, metadata)
def run(self): i = 0 while True: time.sleep(int(self.time_interval)) try: print "message: %s" % \ self.cq.post_message(self.queue_name, "client-id: %s\nsequence: %d" % (self.cq.client_id, i), ttl=300) except pyrax.exceptions.ClientException, e: print "Couldn't post message: %s" % e except pyrax.exceptions.AuthenticationFailed, e: print "Authentication failed, will attempt to re-authenticate" # save the id to re-use client_id = self.cq.client_id region = self.cq.region_name self.cq = pyrax.connect_to_queues(region=region) self.cq.client_id = client_id
def __init__(self, username, api_key, queue_name='demo0000', time_interval=2, region="IAD", debug=False): pyrax.set_setting('identity_type', 'rackspace') if debug: print "username: %s" % username print "api_key: %s" % api_key pyrax.set_credentials(username, api_key) self.cq = pyrax.connect_to_queues(region=region) # Demo to run out of IAD self.cq.client_id = str(uuid.uuid4()) # Randomly create a uuid client id if not self.cq.queue_exists(queue_name): self.cq.create(queue_name) self.queue_name = queue_name self.region = region self.time_interval = time_interval
def connect_to_queues(first_client, source_name, dest_name): """ Connects to all available regions and finds the queues with the passed-in names. The first match wins. We pass in the client created during authentication, so we don't have to create a duplicate. Returns clients that can access the respective queues """ REGIONS = ['IAD', 'ORD', 'DFW', 'LON', 'SYD', 'HKG'] clients = {} print "Locating queues in Regions" for region_id in REGIONS: if first_client.region_name == region_id: clients[region_id] = first_client else: newclient = pyrax.connect_to_queues(region=region_id) clients[region_id] = newclient print "Connections established to {0} Regions".format(len(clients)) source_queue = None dest_queue = None for client in clients.values(): queues = client.list() for queue in queues: if queue.name == source_name: print "Source queue found in Region {0}".format(client.region_name) source_queue = queue break for queue in queues: if queue.name == dest_name: print "Destination queue found in Region {0}".format(client.region_name) dest_queue = queue break return source_queue, dest_queue
def connect_to_queue(first_client, region_id, name): """ Returns the requested queue in the requested region. We pass in the client created during authentication, so we don't have to create a duplicate. """ client = None if first_client.region_name == region_id: client = first_client else: client = pyrax.connect_to_queues(region=region_id) print "Connection established to the {0} Region".format(client.region_name) found_queue = None queues = client.list() for queue in queues: if queue.name == name: print "Queue found in Region {0}".format(client.region_name) found_queue = queue break return found_queue