Beispiel #1
0
    def mass_delete(cls, pathname_list, user, delete_pkgs=False):
        '''Deletes pkginfo files from a list and optionally deletes the
        associated installer items (pkgs)'''
        errors = []
        catalog_items = []
        if delete_pkgs:
            # cache the catalog_items once; we don't want to incur the
            # overhead of reading the 'all' catalog multiple times
            catalog_items = Catalog.detail('all')

        for pathname in pathname_list:
            delete_this_pkg = False
            if delete_pkgs:
                ref_count, pkg_path = pkg_ref_count(pathname, catalog_items)
                if ref_count == 1:
                    # OK to delete the pkg if there is only one pkginfo
                    # file that refers to it
                    delete_this_pkg = True
            try:
                cls.delete('pkgsinfo', pathname, user)
            except FileDeleteError, err:
                errors.append('Error %s when removing %s' % (err, pathname))
            else:
                if delete_this_pkg:
                    try:
                        MunkiFile.delete('pkgs', pkg_path, user)
                    except FileDeleteError, err:
                        errors.append('Error %s when removing %s' %
                                      (err, pkg_path))
Beispiel #2
0
    def mass_delete(cls, pathname_list, user, delete_pkgs=False):
        '''Deletes pkginfo files from a list and optionally deletes the
        associated installer items (pkgs)'''
        errors = []
        catalog_items = []
        if delete_pkgs:
            # cache the catalog_items once; we don't want to incur the
            # overhead of reading the 'all' catalog multiple times
            catalog_items = Catalog.detail('all')

        for pathname in pathname_list:
            delete_this_pkg = False
            if delete_pkgs:
                ref_count, pkg_path = pkg_ref_count(pathname, catalog_items)
                if ref_count == 1:
                    # OK to delete the pkg if there is only one pkginfo
                    # file that refers to it
                    delete_this_pkg = True
            try:
                cls.delete('pkgsinfo', pathname, user)
            except FileDeleteError, err:
                errors.append('Error %s when removing %s' % (err, pathname))
            else:
                if delete_this_pkg:
                    try:
                        MunkiFile.delete('pkgs', pkg_path, user)
                    except FileDeleteError, err:
                        errors.append('Error %s when removing %s'
                                      % (err, pkg_path))
Beispiel #3
0
def file_api(request, kind, filepath=None):
    """Basic API calls for working with non-plist Munki files"""
    if kind not in ["icons", "pkgs"]:
        return HttpResponse(status=404)
    if request.method == "GET":
        LOGGER.debug("Got API GET request for %s", kind)
        if filepath:
            fullpath = MunkiFile.get_fullpath(kind, filepath)
            if not os.path.exists(fullpath):
                return HttpResponse(
                    json.dumps(
                        {
                            "result": "failed",
                            "exception_type": "FileDoesNotExist",
                            "detail": "%s does not exist" % filepath,
                        }
                    ),
                    content_type="application/json",
                    status=404,
                )
            try:
                response = FileResponse(open(fullpath, "rb"), content_type=mimetypes.guess_type(fullpath)[0])
                response["Content-Length"] = os.path.getsize(fullpath)
                response["Content-Disposition"] = 'attachment; filename="%s"' % os.path.basename(filepath)
                return response
            except (IOError, OSError), err:
                return HttpResponse(
                    json.dumps({"result": "failed", "exception_type": str(type(err)), "detail": str(err)}),
                    content_type="application/json",
                    status=403,
                )
        else:
            response = MunkiFile.list(kind)
            return HttpResponse(json.dumps(response) + "\n", content_type="application/json")
Beispiel #4
0
def file_api(request, kind, filepath=None):
    '''Basic API calls for working with non-plist Munki files'''
    if kind not in ['icons', 'pkgs']:
        return HttpResponse(status=404)
    if request.method == 'GET':
        LOGGER.debug("Got API GET request for %s", kind)
        if filepath:
            fullpath = MunkiFile.get_fullpath(kind, filepath)
            if not os.path.exists(fullpath):
                return HttpResponse(
                    json.dumps({'result': 'failed',
                                'exception_type': 'FileDoesNotExist',
                                'detail': '%s does not exist' % filepath}),
                    content_type='application/json', status=404)
            try:
                response = FileResponse(
                    open(fullpath, 'rb'),
                    content_type=mimetypes.guess_type(fullpath)[0])
                response['Content-Length'] = os.path.getsize(fullpath)
                response['Content-Disposition'] = (
                    'attachment; filename="%s"' % os.path.basename(filepath))
                return response
            except (IOError, OSError), err:
                return HttpResponse(
                    json.dumps({'result': 'failed',
                                'exception_type': str(type(err)),
                                'detail': str(err)}),
                    content_type='application/json', status=403)
        else:
            response = MunkiFile.list(kind)
            return HttpResponse(json.dumps(response) + '\n',
                                content_type='application/json')
Beispiel #5
0
        LOGGER.debug("Got API POST request for %s", kind)
        if not request.user.has_perm("pkgsinfo.create_pkginfofile"):
            raise PermissionDenied
        filename = request.POST.get("filename") or filepath
        filedata = request.FILES.get("filedata")
        if not (filename and filedata):
            # malformed request
            return HttpResponse(
                json.dumps(
                    {"result": "failed", "exception_type": "BadRequest", "detail": "Missing filename or filedata"}
                ),
                content_type="application/json",
                status=400,
            )
        try:
            MunkiFile.new(kind, filedata, filename, request.user)
        except FileError, err:
            return HttpResponse(
                json.dumps({"result": "failed", "exception_type": str(type(err)), "detail": str(err)}),
                content_type="application/json",
                status=403,
            )
        else:
            return HttpResponse(json.dumps({"filename": filename}), content_type="application/json", status=200)

    if request.method in ("PUT", "PATCH"):
        LOGGER.debug("Got API %s request for %s", request.method, kind)
        response = HttpResponse(
            json.dumps({"result": "failed", "exception_type": "NotAllowed", "detail": "This method is not supported"}),
            content_type="application/json",
            status=405,
Beispiel #6
0
            filename = request.POST.get('filename') or filepath
            filedata = request.FILES.get('filedata')
        else:
            filename = filepath
            filedata = request.body
        LOGGER.debug("Filename is %s" % filename)
        if not (filename and filedata):
            # malformed request
            return HttpResponse(
                json.dumps({'result': 'failed',
                            'exception_type': 'BadRequest',
                            'detail': 'Missing filename or filedata'}),
                content_type='application/json', status=400)
        try:
            if request.method == 'POST':
                MunkiFile.new(kind, filedata, filename, request.user)
            else:
                MunkiFile.writedata(kind, filedata, filename, request.user)
        except FileError, err:
            return HttpResponse(
                json.dumps({'result': 'failed',
                            'exception_type': str(type(err)),
                            'detail': str(err)}),
                content_type='application/json', status=403)
        else:
            return HttpResponse(
                json.dumps({'filename': filename}),
                content_type='application/json', status=200)

    if request.method == 'PATCH':
        LOGGER.debug("Got API PATCH request for %s", kind)