def main(): """The main interface for the glacier database.""" region = get_set_region() #Update from server _ = get_vaults() #Get all the vaults vaults = Vault.query.filter_by(region=region) #Render them all nicely return render_template("main.html",vaults=vaults,rnom=region,regions=WG.handlers.keys(),clients=get_valid_clients())
def vault_view(vault_name): """ Display the specified vault with all its contents... """ region = get_set_region() vault = Vault.query.filter_by(region=region, name=vault_name).first() if vault is None: return redirect(url_for('main')) #Update the jobs null = check_job_status(vault_name) if vault.lock: #Check the jobs, execute the one we want if it's done live = vault.get_inventory_jobs() #Unlock if we have no live inventory jobs left, or the only live jobs left have completed if live is None or live[0].completed: #The successful option if live is not None: return redirect( url_for('run_jobs', vault_name=vault_name, job_id=live[0].job_id)) vault.lock = False WG.db.session.add(vault) WG.db.session.commit() altvaults = Vault.query.filter_by(region=vault.region).all() altclients = WG.queues.keys() altclients.sort() #Get any completed jobs live = vault.get_inventory_jobs() #Check the lock status and set the complete job if available inv_job = None if live is not None: for j in live: if not j.completed: #If there's an incomplete inventory job, make sure we're locked and finish if not vault.lock: vault.lock = True WG.db.session.add(vault) WG.db.session.commit() inv_job = None break elif j.status_code == "Succeeded": #There's at least one complete job #They're sorted by date, so the first one is what we want if there are more than one if inv_job is None: inv_job = url_for('run_jobs', vault_name=vault_name, job_id=j.job_id) return render_template("vault.html", vault=vault, altvaults=altvaults, inv_job=inv_job, clients=get_valid_clients())
def settings(): """ The settings page. Where you can edit the settings. """ form=SettingsForm(**WG.app.config) if form.validate_on_submit(): cfile=os.path.join(WG.__path__[0],"../settings.cfg") save_settings(form.data,cfile) WG.app.config.from_pyfile("../settings.cfg") WG.app.config.from_envvar("GLACIER_CONFIG",silent=True) return redirect(url_for('main')) rnom=get_set_region() return render_template("settings.html",config=WG.app.config,regions=WG.handlers.keys(),rnom=rnom,form=form,clients=get_valid_clients())
def main(): """The main interface for the glacier database.""" region = get_set_region() #Update from server _ = get_vaults() #Get all the vaults vaults = Vault.query.filter_by(region=region) #Render them all nicely return render_template("main.html", vaults=vaults, rnom=region, regions=WG.handlers.keys(), clients=get_valid_clients())
def validate_connection(): """ The whole app is useless unless we have a database to store things and a valid connection to Amazon Glacier. So check that we do and redirect to settings page if we don't. """ #Make sure we have set a region if 'region' not in session: session['region'] = get_set_region() #Are we at a url where we don't need to check things? if WG.app.config.get("VERBOSE", False): print "Connecting to endpoint %s" % request.endpoint if request.endpoint != 'help' and request.endpoint != 'settings' and request.endpoint != 'static': #If everything has passed before, assume it will again #Is the db connection OK? if not WG.validated_db: try: key = build_db_key(WG.app.config["SQL_DIALECT"], WG.app.config["SQL_DATABASE_NAME"], WG.app.config["SQL_HOSTNAME"], WG.app.config["SQL_USERNAME"], WG.app.config["SQL_PASSWORD"], WG.app.config["SQL_DRIVER"], WG.app.config["SQL_PORT"]) validate_db(key) WG.app.config["SQLALCHEMY_DATABASE_URI"] = key WG.db.create_all() WG.validated_db = True except: print "Can't connect to database." WG.validated_db = False return redirect(url_for('settings')) #Is the Amazon Glacier config okay? if not WG.validated_glacier: try: validate_glacier(WG.app.config["AWS_ACCESS_KEY"], WG.app.config["AWS_SECRET_ACCESS_KEY"], WG.app.config.get("DEFAULT_REGION")) init_handlers_from_config() WG.validated_glacier = True except: print "Can't connect to Glacier." WG.validated_glacier = False return redirect(url_for('settings'))
def settings(): """ The settings page. Where you can edit the settings. """ form = SettingsForm(**WG.app.config) if form.validate_on_submit(): cfile = os.path.join(WG.__path__[0], "../settings.cfg") save_settings(form.data, cfile) WG.app.config.from_pyfile("../settings.cfg") WG.app.config.from_envvar("GLACIER_CONFIG", silent=True) return redirect(url_for('main')) rnom = get_set_region() return render_template("settings.html", config=WG.app.config, regions=WG.handlers.keys(), rnom=rnom, form=form, clients=get_valid_clients())
def vault_view(vault_name): """ Display the specified vault with all its contents... """ region = get_set_region() vault = Vault.query.filter_by(region=region,name=vault_name).first() if vault is None: return redirect(url_for('main')) #Update the jobs null=check_job_status(vault_name) if vault.lock: #Check the jobs, execute the one we want if it's done live=vault.get_inventory_jobs() #Unlock if we have no live inventory jobs left, or the only live jobs left have completed if live is None or live[0].completed: #The successful option if live is not None: return redirect(url_for('run_jobs',vault_name=vault_name,job_id=live[0].job_id)) vault.lock=False WG.db.session.add(vault) WG.db.session.commit() altvaults=Vault.query.filter_by(region=vault.region).all() altclients=WG.queues.keys() altclients.sort() #Get any completed jobs live=vault.get_inventory_jobs() #Check the lock status and set the complete job if available inv_job=None if live is not None: for j in live: if not j.completed: #If there's an incomplete inventory job, make sure we're locked and finish if not vault.lock: vault.lock=True WG.db.session.add(vault) WG.db.session.commit() inv_job=None break elif j.status_code=="Succeeded": #There's at least one complete job #They're sorted by date, so the first one is what we want if there are more than one if inv_job is None: inv_job=url_for('run_jobs',vault_name=vault_name,job_id=j.job_id) return render_template("vault.html",vault=vault,altvaults=altvaults,inv_job=inv_job,clients=get_valid_clients())
def upload_archive_queue(vault_name,path,client,description=''): """ Create an upload job after some minor validation. """ region = get_set_region() vault = Vault.query.filter_by(name=vault_name,region=region).first() if vault is None: abort(401) if vault.lock: abort(401) command = {} command['action']='UPLOAD' command['access_key']=WG.app.config['AWS_ACCESS_KEY'] command['secret_access_key']=WG.app.config["AWS_SECRET_ACCESS_KEY"] command['region_name']=region command['vault_name']=vault.name command['file_pattern']=path command['target']=request.remote_addr command['description']=description k='u'+str(time.time())+"_"+str(os.urandom(16).encode('hex'))[:4] if client not in WG.queues: WG.queues[client]={} WG.queues[client][k]=command
def validate_connection(): """ The whole app is useless unless we have a database to store things and a valid connection to Amazon Glacier. So check that we do and redirect to settings page if we don't. """ #Make sure we have set a region if 'region' not in session: session['region'] = get_set_region() #Are we at a url where we don't need to check things? if WG.app.config.get("VERBOSE",False): print "Connecting to endpoint %s"%request.endpoint if request.endpoint!='help' and request.endpoint!='settings' and request.endpoint!='static': #If everything has passed before, assume it will again #Is the db connection OK? if not WG.validated_db: try: key=build_db_key(WG.app.config["SQL_DIALECT"],WG.app.config["SQL_DATABASE_NAME"],WG.app.config["SQL_HOSTNAME"],WG.app.config["SQL_USERNAME"],WG.app.config["SQL_PASSWORD"],WG.app.config["SQL_DRIVER"],WG.app.config["SQL_PORT"]) validate_db(key) WG.app.config["SQLALCHEMY_DATABASE_URI"]=key WG.db.create_all() WG.validated_db=True except: print "Can't connect to database." WG.validated_db=False return redirect(url_for('settings')) #Is the Amazon Glacier config okay? if not WG.validated_glacier: try: validate_glacier(WG.app.config["AWS_ACCESS_KEY"],WG.app.config["AWS_SECRET_ACCESS_KEY"],WG.app.config.get("DEFAULT_REGION")) init_handlers_from_config() WG.validated_glacier=True except: print "Can't connect to Glacier." WG.validated_glacier=False return redirect(url_for('settings'))
def download_archive(vault_name,archive_id,client): """ Puts a download job in the queue, after some minor validation. """ if WG.app.config.get("VERBOSE",False): print "Comm queues are %s"%str(WG.queues) region = get_set_region() vault = Vault.query.filter_by(name=vault_name,region=region).first() if vault is None: abort(401) #Need to get the archive too... archive = Archive.query.filter_by(archive_id=archive_id).first() if archive is None: abort(401) if archive.filename!="NOT_GIVEN": fname=archive.filename else: fname=WG.app.config["UNKNOWN_FILENAME"] #Is there a finished job knocking about? job=archive.jobs.filter_by(action='download',completed=True,live=True,status_message="Succeeded").first() if job is None: abort(401) #Very well, stick a job in the queue command = {} command['action']='DOWNLOAD' command['access_key']=WG.app.config['AWS_ACCESS_KEY'] command['secret_access_key']=WG.app.config["AWS_SECRET_ACCESS_KEY"] command['region_name']=region command['file_name']=fname command['file_size']=job.archive.filesize command['vault_name']=vault.name command['job_id']=job.job_id command['target']=request.remote_addr k='d'+str(time.time())+"_"+str(os.urandom(16).encode('hex'))[:4] if client not in WG.queues: WG.queues[client]={} WG.queues[client][k] = command