def trashman(self): from webapp.libs.openstack import instance_info from webapp.libs.openstack import instance_decommission # build the response response = {"response": "success", "result": {"message": "", "server": {}}} # get instance (server) info cluster_response = instance_info(self) if cluster_response['response'] == "success": # we should NOT have this, so try to decomission out of desperation cluster_response = instance_decommission(self) response['result']['message'] = "Terminating instance %s" % self.name else: # delete this instance into forever address = Addresses().get_by_id(self.address_id) address.release() self.delete(self) response['result']['message'] = "Instance %s has been deleted." % self.name # make a call to the callback url to report instance details appliance = Appliance().get() callback_url = self.callback_url pool_response = pool_instance(url=callback_url, instance=self, appliance=appliance) 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
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
def trashman(self): from webapp.libs.openstack import instance_info from webapp.libs.openstack import instance_decommission # build the response response = { "response": "success", "result": { "message": "", "server": {} } } # get instance (server) info cluster_response = instance_info(self) if cluster_response['response'] == "success": # we should NOT have this, so try to decomission out of desperation cluster_response = instance_decommission(self) response['result'][ 'message'] = "Terminating instance %s" % self.name else: # delete this instance into forever address = Addresses().get_by_id(self.address_id) address.release() self.delete(self) response['result'][ 'message'] = "Instance %s has been deleted." % self.name # make a call to the callback url to report instance details appliance = Appliance().get() callback_url = self.callback_url pool_response = pool_instance(url=callback_url, instance=self, appliance=appliance) return response
def address_handler(address_token): # look up address address = Addresses().get_by_token(address_token) # build the response response = {"response": "success", "result": {}} # check if we found an address that matches if address: # pull the instance out by id instance = Instances().get_by_id(address.instance_id) try: # find out how much we were paid amount = float(request.json['amount']) except: # bad stuff happens amount = 0 response['response'] = "error" response['result'] = "Amount not received or zero." app.logger.error( "A payment provider callback contains an invalid amount.") return jsonify(response), 401 # coin-op the instance instance_response = instance.coinop(amount) # indicate we were paid and reload the page message( "Instance %s received a payment of %s micro BTC." % (instance.name, int(amount * 1000000)), "success", True) app.logger.info( "Payment of amount=(%s) micro BTC received for instance=(%s)." % (int(amount * 1000000), instance.name)) # load response response['result'] = "acknowledged" return jsonify(response) else: app.logger.error( "A payment was recieved on an unused bitcoin address.") response['response'] = "error" response['result'] = "bitcoin address token not found" return jsonify(response), 401
def configure(): # get the form for the page form = ApplianceForm(request.form) # get existing database entries appliance = db.session.query(Appliance).first() # page is POST'ing data if request.method == "POST": # clear settings cache Status().flush() # load the form into the appliance object (excluding API token) apitoken = appliance.apitoken form.populate_obj(appliance) appliance.apitoken = apitoken if form.validate_on_submit(): # our form validates, so update the database appliance.update(appliance) # check the settings settings = Status().check_settings() if settings["coinbase"]: # sync up addresses with coinbase addresses = Addresses() addresses.sync(appliance) # grab the first address we got from coinbase address = db.session.query(Addresses).first() if address and address.subdomain: # overload the appliance's existing subdomain with one from coinbase address appliance.subdomain = address.subdomain appliance.update() else: # there exists no address with a subdomain, so we generate a new one appliance.subdomain = generate_token(size=16, caselimit=True) appliance.update() # build the tunnel config file - ngrok will start after it's built appliance.build_tunnel_conf() # not ideal, but whatever os.system("monit restart ngrok") # form was valid, so say thanks flash("Setting have been saved.", "success") else: # form was not valid, so show errors flash("There were form errors. Please check your entries and try again.", "error") # populate map try: lat = float(appliance.latitude) lon = float(appliance.longitude) except ValueError, TypeError: geodata = get_geodata() appliance.latitude = geodata["latitude"] appliance.longitude = geodata["longitude"] appliance.update()
def configure(): # get the form for the page form = ApplianceForm(request.form) # get existing database entries appliance = db.session.query(Appliance).first() # page is POST'ing data if request.method == 'POST': # clear settings cache Status().flush() # load the form into the appliance object (excluding API token) apitoken = appliance.apitoken form.populate_obj(appliance) appliance.apitoken = apitoken if form.validate_on_submit(): # our form validates, so update the database appliance.update(appliance) # check the settings settings = Status().check_settings() if settings['coinbase']: # sync up addresses with coinbase addresses = Addresses() addresses.sync(appliance) # grab the first address we got from coinbase address = db.session.query(Addresses).first() if address and address.subdomain: # overload the appliance's existing subdomain with one from coinbase address appliance.subdomain = address.subdomain appliance.update() else: # there exists no address with a subdomain, so we generate a new one appliance.subdomain = generate_token(size=16, caselimit=True) appliance.update() # build the tunnel config file - ngrok will start after it's built appliance.build_tunnel_conf() # not ideal, but whatever os.system('monit restart ngrok') # form was valid, so say thanks flash("Setting have been saved.", "success") else: # form was not valid, so show errors flash( "There were form errors. Please check your entries and try again.", "error") # populate map try: lat = float(appliance.latitude) lon = float(appliance.longitude) except ValueError, TypeError: geodata = get_geodata() appliance.latitude = geodata['latitude'] appliance.longitude = geodata['longitude'] appliance.update()