|
9 | 9 |
|
10 | 10 | import requests |
11 | 11 |
|
12 | | -from flask import g, abort, Response, send_file, request, after_this_request |
| 12 | +from flask import g, abort, Response, send_file, request, redirect |
13 | 13 | from flask_restplus import Resource, fields |
14 | 14 |
|
15 | 15 | from pyinfraboxutils import get_logger, get_env |
@@ -394,9 +394,8 @@ def get(self, project_id, job_id): |
394 | 394 | job_cluster = result['cluster_name'] |
395 | 395 | key = '%s/%s' % (job_id, filename) |
396 | 396 |
|
397 | | - if os.environ['INFRABOX_CLUSTER_NAME'] == job_cluster: |
398 | | - f = storage.download_archive(key) |
399 | | - else: |
| 397 | + f = storage.download_archive(key) |
| 398 | + if not f and os.environ['INFRABOX_CLUSTER_NAME'] != job_cluster: |
400 | 399 | c = g.db.execute_one_dict(''' |
401 | 400 | SELECT * |
402 | 401 | FROM cluster |
@@ -450,53 +449,7 @@ def get(self, project_id, job_id): |
450 | 449 | ''' |
451 | 450 | Returns all archives |
452 | 451 | ''' |
453 | | - result = g.db.execute_one_dict(''' |
454 | | - SELECT archive |
455 | | - FROM job |
456 | | - WHERE id = %s |
457 | | - AND project_id = %s |
458 | | - ''', [job_id, project_id]) |
459 | | - |
460 | | - if not result or not result['archive']: |
461 | | - abort(404) |
462 | | - |
463 | | - base_path = os.path.join('/tmp', str(uuid.uuid4())) |
464 | | - archive_dir = os.path.join(base_path, 'archive') |
465 | | - os.mkdir(base_path) |
466 | | - os.mkdir(archive_dir) |
467 | | - |
468 | | - @after_this_request |
469 | | - def _remove_file(response): |
470 | | - if os.path.exists(base_path): |
471 | | - shutil.rmtree(base_path) |
472 | | - return response |
473 | | - |
474 | | - for item in result['archive']: |
475 | | - filename = item['filename'] |
476 | | - url = "%s/api/v1/projects/%s/jobs/%s/archive/download?filename=%s" % \ |
477 | | - (get_env('INFRABOX_ROOT_URL'), project_id, job_id, filename) |
478 | | - try: |
479 | | - token = encode_user_token(g.token['user']['id']) |
480 | | - except Exception: |
481 | | - #public project has no token here. |
482 | | - token = "" |
483 | | - headers = {'Authorization': 'bearer ' + token} |
484 | | - |
485 | | - r = requests.get(url, headers=headers, timeout=120, verify=False) |
486 | | - if r.status_code != 200: |
487 | | - continue |
488 | | - |
489 | | - with open(os.path.join(archive_dir, os.path.basename(filename)), 'w') as f: |
490 | | - f.write(r.content) |
491 | | - |
492 | | - if not os.listdir(archive_dir): |
493 | | - abort(404) |
494 | | - |
495 | | - tar_file = os.path.join(base_path, 'archive_%s' % job_id +'.tar.gz') |
496 | | - with tarfile.open(tar_file, mode='w:gz') as archive: |
497 | | - archive.add(archive_dir, arcname='archive') |
498 | | - |
499 | | - return send_file(tar_file, as_attachment=True, attachment_filename=os.path.basename(tar_file)) |
| 452 | + return redirect("/api/v1/projects/%s/jobs/%s/archive/download?filename=all_archives.tar.gz" %(project_id, job_id)) |
500 | 453 |
|
501 | 454 | @ns.route('/<job_id>/console') |
502 | 455 | @api.response(403, 'Not Authorized') |
|
0 commit comments