def initialize(self, ip): # generate a new API token self.apitoken = generate_token() # remainder of settings self.ngroktoken = "" self.name = "%s Virtual Appliance" % app.config['POOL_NAME'] self.subdomain = "" self.dynamicimages = 1 # important, do not remove self.secret = generate_token(size=8, caselimit=True) # not used. having fun yet? self.cbapikey = "" self.cbapisecret = "" self.cbaccesstoken = "" self.cbrefreshtoken = "" # get geodata geo_data = get_geodata() self.latitude = geo_data['latitude'] self.longitude = geo_data['longitude'] # set local IP address self.local_ip = ip # create entry self.update(self)
def assign(self, instance_id): # check if the instance id is already assigned to an address (just in case) address = db.session.query(Addresses).filter_by(instance_id=instance_id).first() # appliance object appliance = db.session.query(Appliance).first() # if we found an address, update and return it if address: address.instance_id = instance_id address.update(address) return address else: # check if we have an empty address to assign address = db.session.query(Addresses).filter_by(instance_id=0).first() # we found one, so assign instance_id if address: # assign the instance id to the address address.instance_id = instance_id address.update(address) return address else: # need a new address at this point # check if coinbase connection is live settings = Status().check_settings() if not settings['coinbase']: return None # check if appliance has a subdomain assigned if not appliance.subdomain: return None # now ask coinbase for new address and set callback token = generate_token(size=16, caselimit=True) callback_url = "https://%s.%s/api/address/%s" % ( appliance.subdomain, app.config['POOL_SSL_PROXY_DOMAIN'], token ) label = md5.new(appliance.cbapikey).hexdigest() response = coinbase_generate_address(appliance, callback_url, label) # create new address in db if response['response'] == "success": address = Addresses() address.address = response['result']['address'] address.token = token address.instance_id = instance_id address.subdomain = appliance.subdomain address.update() app.logger.info("Allocated new Coinbase address=(%s) to an instance." % address.address) return address else: # something went wrong with coinbase # return 0 and let calling code handle it return None
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 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 token_refresh(self): self.apitoken = generate_token(size=64)
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()