Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame^] | 1 | # |
| 2 | # BitBake Toaster Implementation |
| 3 | # |
| 4 | # Copyright (C) 2016 Intel Corporation |
| 5 | # |
| 6 | # This program is free software; you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License version 2 as |
| 8 | # published by the Free Software Foundation. |
| 9 | # |
| 10 | # This program is distributed in the hope that it will be useful, |
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | # GNU General Public License for more details. |
| 14 | # |
| 15 | # You should have received a copy of the GNU General Public License along |
| 16 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | |
| 19 | |
| 20 | # Temporary home for the UI's misc API |
| 21 | |
| 22 | from orm.models import Project, ProjectTarget, Build |
| 23 | from bldcontrol.models import BuildRequest |
| 24 | from bldcontrol import bbcontroller |
| 25 | from django.http import HttpResponse, JsonResponse |
| 26 | from django.views.generic import View |
| 27 | |
| 28 | |
| 29 | class XhrBuildRequest(View): |
| 30 | |
| 31 | def get(self, request, *args, **kwargs): |
| 32 | return HttpResponse() |
| 33 | |
| 34 | def post(self, request, *args, **kwargs): |
| 35 | """ |
| 36 | Build control |
| 37 | |
| 38 | Entry point: /xhr_buildrequest/<project_id> |
| 39 | Method: POST |
| 40 | |
| 41 | Args: |
| 42 | id: id of build to change |
| 43 | buildCancel = build_request_id ... |
| 44 | buildDelete = id ... |
| 45 | targets = recipe_name ... |
| 46 | |
| 47 | Returns: |
| 48 | {"error": "ok"} |
| 49 | or |
| 50 | {"error": <error message>} |
| 51 | """ |
| 52 | |
| 53 | project = Project.objects.get(pk=kwargs['pid']) |
| 54 | |
| 55 | if 'buildCancel' in request.POST: |
| 56 | for i in request.POST['buildCancel'].strip().split(" "): |
| 57 | try: |
| 58 | br = BuildRequest.objects.get(project=project, pk=i) |
| 59 | |
| 60 | try: |
| 61 | bbctrl = bbcontroller.BitbakeController(br.environment) |
| 62 | bbctrl.forceShutDown() |
| 63 | except: |
| 64 | # We catch a bunch of exceptions here because |
| 65 | # this is where the server has not had time to start up |
| 66 | # and the build request or build is in transit between |
| 67 | # processes. |
| 68 | # We can safely just set the build as cancelled |
| 69 | # already as it never got started |
| 70 | build = br.build |
| 71 | build.outcome = Build.CANCELLED |
| 72 | build.save() |
| 73 | |
| 74 | # We now hand over to the buildinfohelper to update the |
| 75 | # build state once we've finished cancelling |
| 76 | br.state = BuildRequest.REQ_CANCELLING |
| 77 | br.save() |
| 78 | |
| 79 | except BuildRequest.DoesNotExist: |
| 80 | return JsonResponse({'error':'No such build id %s' % i}) |
| 81 | |
| 82 | return JsonResponse({'error': 'ok'}) |
| 83 | |
| 84 | if 'buildDelete' in request.POST: |
| 85 | for i in request.POST['buildDelete'].strip().split(" "): |
| 86 | try: |
| 87 | BuildRequest.objects.select_for_update().get(project = project, pk = i, state__lte = BuildRequest.REQ_DELETED).delete() |
| 88 | except BuildRequest.DoesNotExist: |
| 89 | pass |
| 90 | return JsonResponse({'error': 'ok' }) |
| 91 | |
| 92 | if 'targets' in request.POST: |
| 93 | ProjectTarget.objects.filter(project = project).delete() |
| 94 | s = str(request.POST['targets']) |
| 95 | for t in s.translate(None, ";%|\"").split(" "): |
| 96 | if ":" in t: |
| 97 | target, task = t.split(":") |
| 98 | else: |
| 99 | target = t |
| 100 | task = "" |
| 101 | ProjectTarget.objects.create(project = project, |
| 102 | target = target, |
| 103 | task = task) |
| 104 | project.schedule_build() |
| 105 | |
| 106 | return JsonResponse({'error': 'ok' }) |
| 107 | |
| 108 | response = HttpResponse() |
| 109 | response.status_code = 500 |
| 110 | return response |