def get(self, project_id, job_id): filename = request.args.get('filename', None) if not filename: abort(404) result = g.db.execute_one_dict( ''' SELECT cluster_name FROM job WHERE id = %s AND project_id = %s ''', [job_id, project_id]) if not result or not result['cluster_name']: abort(404) job_cluster = result['cluster_name'] key = '%s/%s' % (job_id, filename) if os.environ['INFRABOX_CLUSTER_NAME'] == job_cluster: f = storage.download_archive(key) else: c = g.db.execute_one_dict( ''' SELECT * FROM cluster WHERE name=%s ''', [job_cluster]) url = '%s/api/v1/projects/%s/jobs/%s/archive/download?filename=%s' % ( c['root_url'], project_id, job_id, filename) try: token = encode_user_token(g.token['user']['id']) except AttributeError: #public project has no token here. token = "" headers = {'Authorization': 'bearer ' + token} logger.info('get archive %s from %s', [filename, url]) # TODO(ib-steffen): allow custom ca bundles r = requests.get(url, headers=headers, timeout=120, verify=False, stream=True) f = r.raw if not f: logger.error(key) abort(404) return send_file(f, as_attachment=True, attachment_filename=os.path.basename(filename))
def get(self, project_id, job_id): token = request.headers.get('Authorization') if token: token = token.replace("bearer ", "").replace("Bearer ","") filename = request.args.get('filename', None) force_download = request.args.get('view', "false") == "false" if not filename: abort(404) result = g.db.execute_one_dict(''' SELECT cluster_name FROM job WHERE id = %s AND project_id = %s ''', [job_id, project_id]) if not result or not result['cluster_name']: abort(404) job_cluster = result['cluster_name'] key = '%s/%s' % (job_id, filename) f = storage.download_archive(key) if not f and os.environ['INFRABOX_CLUSTER_NAME'] != job_cluster: c = g.db.execute_one_dict(''' SELECT * FROM cluster WHERE name=%s ''', [job_cluster]) url = '%s/api/v1/projects/%s/jobs/%s/archive/download?filename=%s' % (c['root_url'], project_id, job_id, filename) if not token: try: token = encode_user_token(g.token['user']['id']) except Exception: #public project has no token here. token = "" headers = {'Authorization': 'bearer ' + token} # TODO(ib-steffen): allow custom ca bundles r = requests.get(url, headers=headers, timeout=120, verify=False) f = BytesIO(r.content) f.seek(0) if not f: abort(404) filename = os.path.basename(filename) return send_file(f, as_attachment=force_download, attachment_filename=filename,\ mimetype=mimetypes.guess_type(filename)[0])
def get(self, project_id, job_id): f = request.args.get('filename', None) if not f: abort(404) key = '%s/%s' % (job_id, f) f = storage.download_archive(key) if not f: logger.error(key) abort(404) return send_file(f, attachment_filename=os.path.basename(f))
def get(self, project_id): job_name = request.args.get('job_name', None) filename = request.args.get('filename', None) branch = request.args.get('branch', None) if not filename or not job_name: abort(404) p = g.db.execute_one_dict( ''' SELECT type, name FROM project WHERE id = %s ''', [project_id]) project_type = p['type'] project_name = p['name'] result = None if branch and project_type in ('github', 'gerrit'): result = g.db.execute_one_dict( ''' SELECT j.id, name, c.id commit_id, b.build_number, b.restart_counter, c.branch FROM ( SELECT id, name, unnest(archive) AS archive, definition->>'name' AS realname, start_date, build_id FROM job WHERE project_id = %s ) j JOIN build b ON b.id = j.build_id JOIN "commit" c ON b.commit_id = c.id WHERE archive->>'filename' = %s AND realname = %s AND c.branch = %s ORDER BY start_date DESC; ''', [project_id, filename, job_name, branch]) else: result = g.db.execute_one_dict( ''' SELECT j.id, name, c.id commit_id, b.build_number, b.restart_counter, c.branch FROM ( SELECT id, name, unnest(archive) AS archive, definition->>'name' AS realname, start_date, build_id FROM job WHERE project_id = %s ) j JOIN build b ON b.id = j.build_id LEFT JOIN commit c ON b.commit_id = c.id WHERE archive->>'filename' = %s AND realname = %s ORDER BY start_date DESC; ''', [project_id, filename, job_name]) if not result: abort(404) job_id = result['id'] build_number = '{build_number}.{restart_counter}'.format( build_number=result['build_number'], restart_counter=result['restart_counter']) root_url = get_root_url('global') build_url = '%s/dashboard/#/project/%s/build/%s/%s' % ( root_url, project_name, result['build_number'], result['restart_counter']) job_url = '%s/job/%s' % (build_url, result['name']) headers = { 'Infrabox-Build-Number': build_number, 'Infrabox-Build-Url': build_url, 'Infrabox-Job-Url': job_url, } if result['branch']: headers['Infrabox-Branch'] = result['branch'] if result['commit_id']: headers['Infrabox-Commit'] = result['commit_id'] f = storage.download_archive('%s/%s' % (job_id, filename)) if not f: abort(404) filename = os.path.basename(filename) resp = make_response( send_file(f, attachment_filename=filename, mimetype=mimetypes.guess_type(filename)[0])) resp.headers = headers return resp