def mix(self, flavor): # build response response = {"response": "success", "result": {"message": ""}} # the first instance which has this flavor assigned but is not running (active == 1) instances = db.session.query(Instances).filter_by(flavor_id=flavor.id, state=1).all() # create a minimum number of instances based on hot amount for flavor if len(instances) < flavor.hot: for x in range(len(instances), flavor.hot): # create a new instance instance = Instances() instance.name = "smi-%s" % generate_token(size=8, caselimit=True) instance.flavor_id = flavor.id # grab the first available image for a holder for the warm instance image = db.session.query(Images).first() instance.image_id = image.id # timestamps epoch_time = int(time.time()) instance.created = epoch_time instance.updated = epoch_time instance.expires = epoch_time # already expired # set state instance.state = 1 # has address, but no payments/not started (warm) # update - provides instance.id to us instance.update() # finally, assign a bitcoin address addresses = Addresses() address = addresses.assign(instance.id) if address: instance.address = address instance.update() else: # we have no address, so delete what we made instance.delete(instance) response['result'][ 'message'] = "Created new instance and assigned address." app.logger.info("Created new instance=(%s)." % instance.name) else: # found enough instances - make sure they have addresses assigned to them for instance in instances: instance.address = Addresses().assign(instance.id) instance.update() response['result'][ 'message'] = "Found existing instances and assigned addresses." return response
def mix(self, flavor): # build response response = {"response": "success", "result": {"message": ""}} # query by flavor q_flavor = db.session.query(Instances).filter_by(flavor_id=flavor.id) flavor_count = q_flavor.count() # limit query by state flavor_available_count = q_flavor.filter_by(state=1).count() # set create_count according to max_instances limit create_count = flavor.max_instances - flavor_count # if the limit defined by hot is lower than limit defined by max_instances if flavor.hot - flavor_available_count < create_count: create_count = flavor.hot - flavor_available_count # create a minimum number of instances based on hot amount for flavor for x in range(create_count): # create a new instance instance = Instances() instance.name = "smi-%s" % generate_token(size=8, caselimit=True) instance.flavor = flavor # timestamps epoch_time = int(time.time()) instance.created = epoch_time instance.updated = epoch_time instance.expires = epoch_time # already expired # set state instance.state = 1 # has address, but no payments/not started (warm) # update - provides instance.id to us instance.update() response['result']['message'] = "Created new instance." app.logger.info("Created new instance=(%s)." % instance.name) if create_count < 0: for x in range(create_count * -1): instance = q_flavor.first() if instance: app.logger.info("Deleting instance=(%s)." % instance.name) instance.delete() for instance in q_flavor.all(): if not instance.address: addresses = Addresses() address = addresses.assign(instance.id) if not address: # we have no address, so delete what we made instance.delete(instance) return response
def mix(self, flavor): # build response response = {"response": "success", "result": {"message": ""}} # the first instance which has this flavor assigned but is not running (active == 1) instances = db.session.query(Instances).filter_by(flavor_id=flavor.id, state=1).all() # create a minimum number of instances based on hot amount for flavor if len(instances) < flavor.hot: for x in range(len(instances), flavor.hot): # create a new instance instance = Instances() instance.name = "smi-%s" % generate_token(size=8, caselimit=True) instance.flavor_id = flavor.id # grab the first available image for a holder for the warm instance image = db.session.query(Images).first() instance.image_id = image.id # timestamps epoch_time = int(time.time()) instance.created = epoch_time instance.updated = epoch_time instance.expires = epoch_time # already expired # set state instance.state = 1 # has address, but no payments/not started (warm) # update - provides instance.id to us instance.update() # finally, assign a bitcoin address addresses = Addresses() address = addresses.assign(instance.id) if address: instance.address = address instance.update() else: # we have no address, so delete what we made instance.delete(instance) response['result']['message'] = "Created new instance and assigned address." app.logger.info("Created new instance=(%s)." % instance.name) else: # found enough instances - make sure they have addresses assigned to them for instance in instances: instance.address = Addresses().assign(instance.id) instance.update() response['result']['message'] = "Found existing instances and assigned addresses." return response