Return-Path: Received: by yocto-www.yoctoproject.org (Postfix, from userid 118) id 337A2E00B3A; Fri, 15 Jan 2016 08:30:32 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on yocto-www.yoctoproject.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 X-Spam-HAM-Report: * -5.0 RCVD_IN_DNSWL_HI RBL: Sender listed at http://www.dnswl.org/, high * trust * [192.55.52.88 listed in list.dnswl.org] * -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% * [score: 0.0000] Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by yocto-www.yoctoproject.org (Postfix) with ESMTP id A4353E00BE8 for ; Fri, 15 Jan 2016 08:30:29 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP; 15 Jan 2016 08:30:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.22,300,1449561600"; d="scan'208,217";a="861256655" Received: from ffolivei-mobl2.ger.corp.intel.com (HELO lp.ger.corp.intel.com) ([10.252.20.40]) by orsmga001.jf.intel.com with ESMTP; 15 Jan 2016 08:30:28 -0800 From: Elliot Smith To: toaster@yoctoproject.org Date: Fri, 15 Jan 2016 16:30:17 +0000 Message-Id: <1452875417-24767-4-git-send-email-elliot.smith@intel.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1452875417-24767-1-git-send-email-elliot.smith@intel.com> References: <1452875417-24767-1-git-send-email-elliot.smith@intel.com> Subject: [review-request][PATCH 3/3] toaster: toastergui: make artifact download more robust X-BeenThere: toaster@yoctoproject.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Web based interface for BitBake List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jan 2016 16:30:32 -0000 When an artifact download is requested, Toaster goes through a convoluted series of conditions to decide which file to push to the response. In the case of build artifact downloads for command line builds, this caused an ugly exception, as command line builds don't have a build request. To simplify and catch more corner cases, remove the code which fetches files via the build environment (we only support the local build environment anyway). Then push all requests along a single path, catching any missing file errors, missing object errors or poorly-formed URLs in a single except clause which always returns a valid response. Also modify the text on the "unavailable artifact" page so it says that the artifact doesn't exist, rather than it "no longer" exists (exceptions may occur because an invalid artifact was requested, rather than an artifact which was removed). [YOCTO #7603] Signed-off-by: Elliot Smith --- .../toastergui/templates/unavailable_artifact.html | 13 ++- bitbake/lib/toaster/toastergui/views.py | 97 +++++++++------------- 2 files changed, 47 insertions(+), 63 deletions(-) diff --git a/bitbake/lib/toaster/toastergui/templates/unavailable_artifact.html b/bitbake/lib/toaster/toastergui/templates/unavailable_artifact.html index 0301a6c..2d3d02c 100644 --- a/bitbake/lib/toaster/toastergui/templates/unavailable_artifact.html +++ b/bitbake/lib/toaster/toastergui/templates/unavailable_artifact.html @@ -3,15 +3,14 @@ {% load humanize %} {% load static %} -{% block title %} Build artifact no longer exists - Toaster {% endblock %} +{% block title %} Build artifact does not exist - Toaster {% endblock %} {% block pagecontent %} - -
-
- The build artifact you are trying to download no longer exists.

-

Back to previous page

+
+
+

The build artifact you are trying to download does not exist.

+

Back to previous page

+
-
{% endblock %} diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index 59e16b2..995937a 100755 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/lib/toaster/toastergui/views.py @@ -35,7 +35,7 @@ from orm.models import BitbakeVersion, CustomImageRecipe from bldcontrol import bbcontroller from django.views.decorators.cache import cache_control from django.core.urlresolvers import reverse, resolve -from django.core.exceptions import MultipleObjectsReturned +from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.http import HttpResponseBadRequest, HttpResponseNotFound from django.utils import timezone @@ -2575,78 +2575,63 @@ if True: return context - def _file_name_for_artifact(b, artifact_type, artifact_id): + def _file_names_for_artifact(build, artifact_type, artifact_id): + """ + Return a tuple (file path, file name for the download response) for an + artifact of type artifact_type with ID artifact_id for build; if + artifact type is not supported, returns (None, None) + """ file_name = None - # Target_Image_File file_name - if artifact_type == "imagefile": - file_name = Target_Image_File.objects.get(target__build = b, pk = artifact_id).file_name + response_file_name = None + + if artifact_type == "cookerlog": + file_name = build.cooker_log_path + response_file_name = "cooker.log" + + elif artifact_type == "imagefile": + file_name = Target_Image_File.objects.get(target__build = build, pk = artifact_id).file_name elif artifact_type == "buildartifact": - file_name = BuildArtifact.objects.get(build = b, pk = artifact_id).file_name + file_name = BuildArtifact.objects.get(build = build, pk = artifact_id).file_name - elif artifact_type == "licensemanifest": - file_name = Target.objects.get(build = b, pk = artifact_id).license_manifest_path + elif artifact_type == "licensemanifest": + file_name = Target.objects.get(build = build, pk = artifact_id).license_manifest_path elif artifact_type == "tasklogfile": - file_name = Task.objects.get(build = b, pk = artifact_id).logfile + file_name = Task.objects.get(build = build, pk = artifact_id).logfile elif artifact_type == "logmessagefile": - file_name = LogMessage.objects.get(build = b, pk = artifact_id).pathname - else: - raise Exception("FIXME: artifact type %s not implemented" % (artifact_type)) + file_name = LogMessage.objects.get(build = build, pk = artifact_id).pathname - return file_name + if file_name and not response_file_name: + response_file_name = os.path.basename(file_name) + return (file_name, response_file_name) def build_artifact(request, build_id, artifact_type, artifact_id): - if artifact_type in ["cookerlog"]: - try: - build = Build.objects.get(pk = build_id) - file_name = build.cooker_log_path + """ + View which returns a build artifact file as a response + """ + file_name = None + response_file_name = None + + try: + build = Build.objects.get(pk = build_id) + file_name, response_file_name = _file_names_for_artifact( + build, artifact_type, artifact_id + ) + + if file_name and response_file_name: fsock = open(file_name, "r") content_type = MimeTypeFinder.get_mimetype(file_name) response = HttpResponse(fsock, content_type = content_type) - disposition = 'attachment; filename=cooker.log' - response['Content-Disposition'] = disposition + disposition = "attachment; filename=" + response_file_name + response["Content-Disposition"] = disposition return response - except IOError: - context = { - 'build' : Build.objects.get(pk = build_id), - } - return render(request, "unavailable_artifact.html", context) - - else: - # retrieve the artifact directly from the build environment - return _get_be_artifact(request, build_id, artifact_type, artifact_id) - - - def _get_be_artifact(request, build_id, artifact_type, artifact_id): - try: - b = Build.objects.get(pk=build_id) - if b.buildrequest is None or b.buildrequest.environment is None: - raise Exception("Artifact not available for download (missing build request or build environment)") - - file_name = _file_name_for_artifact(b, artifact_type, artifact_id) - fsock = None - content_type='application/force-download' - - if file_name is None: - raise Exception("Could not handle artifact %s id %s" % (artifact_type, artifact_id)) else: - content_type = MimeTypeFinder.get_mimetype(file_name) - fsock = b.buildrequest.environment.get_artifact(file_name) - file_name = os.path.basename(file_name) # we assume that the build environment system has the same path conventions as host - - response = HttpResponse(fsock, content_type = content_type) - - # returns a file from the environment - response['Content-Disposition'] = 'attachment; filename=' + file_name - return response - except IOError: - context = { - 'build' : Build.objects.get(pk = build_id), - } - return render(request, "unavailable_artifact.html", context) + return render(request, "unavailable_artifact.html") + except ObjectDoesNotExist, IOError: + return render(request, "unavailable_artifact.html") -- Elliot Smith Software Engineer Intel OTC --------------------------------------------------------------------- Intel Corporation (UK) Limited Registered No. 1134945 (England) Registered Office: Pipers Way, Swindon SN3 1RJ VAT No: 860 2173 47 This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.