Beispiel #1
0
    def close_downloader(self, downloader_uid, file_uid, secret):
        """Close the downloader associated with the passed
           downloader_uid and file_uid,
           authenticated using the passed secret
        """
        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.Service import get_service_account_bucket \
            as _get_service_account_bucket

        bucket = _get_service_account_bucket()
        key = "%s/%s/%s/%s" % (_downloader_root, self._drive_uid, file_uid,
                               downloader_uid)

        try:
            data = _ObjectStore.get_object_from_json(bucket, key)
        except:
            data = None

        if data is None:
            # the downloader has already been closed
            return

        shared_secret = data["secret"]

        if secret != shared_secret:
            raise PermissionError(
                "Invalid request - you do not have permission to "
                "close this downloader")

        try:
            _ObjectStore.take_object_from_json(bucket, key)
        except:
            pass
    def close(par):
        """Close the passed PAR. This will remove the registration
           for the PAR and will also call the associated
           cleanup_function (if any)
        """
        from Acquire.Service import is_running_service as _is_running_service

        if not _is_running_service():
            return

        from Acquire.Service import get_service_account_bucket \
            as _get_service_account_bucket

        from Acquire.ObjectStore import OSPar as _OSPar

        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.ObjectStore import datetime_to_string \
            as _datetime_to_string
        from Acquire.ObjectStore import Function as _Function

        if par is None:
            return

        if not isinstance(par, _OSPar):
            raise TypeError("You can only close OSPar objects!")

        if par.is_null():
            return

        expire_string = _datetime_to_string(par.expires_when())

        bucket = _get_service_account_bucket()

        key = "%s/expire/%s/%s" % (_registry_key, expire_string, par.uid())
        try:
            _ObjectStore.delete_object(bucket=bucket, key=key)
        except:
            pass

        key = "%s/uid/%s/%s" % (_registry_key, par.uid(), expire_string)

        try:
            data = _ObjectStore.take_object_from_json(bucket=bucket, key=key)
        except:
            data = None

        if data is None:
            # this PAR has already been closed
            return

        if "cleanup_function" in data:
            cleanup_function = _Function.from_data(data["cleanup_function"])
            cleanup_function(par=par)
Beispiel #3
0
    def close_uploader(self, file_uid, secret):
        """Close the uploader associated with the passed file_uid,
           authenticated using the passed secret
        """
        from Acquire.ObjectStore import ObjectStore as _ObjectStore
        from Acquire.Service import get_service_account_bucket \
            as _get_service_account_bucket

        bucket = _get_service_account_bucket()
        key = "%s/%s/%s" % (_uploader_root, self._drive_uid, file_uid)

        try:
            data = _ObjectStore.get_object_from_json(bucket, key)
        except:
            data = None

        if data is None:
            # the uploader has already been closed
            return

        shared_secret = data["secret"]

        if secret != shared_secret:
            raise PermissionError(
                "Invalid request - you do not have permission to "
                "close this uploader")

        try:
            data2 = _ObjectStore.take_object_from_json(bucket, key)
        except:
            data2 = None

        if data2 is None:
            # someone else is already in the process of closing
            # this uploader - let them do it!
            return

        filename = data["filename"]
        version = data["version"]

        # now get the FileInfo for this file
        from Acquire.Storage import FileInfo as _FileInfo
        fileinfo = _FileInfo.load(drive=self,
                                  filename=filename,
                                  version=version)

        file_key = data["filekey"]
        file_bucket = self._get_file_bucket(file_key)
        fileinfo.close_uploader(file_bucket=file_bucket)
        fileinfo.save()
Beispiel #4
0
    def get_job(self, uid, start_state="pending", end_state=None,
                passphrase=None):
        """Return the job with specified 'uid' in the specified
           state (start_state) - this will move the job to
           'end_state' if this is specified. If you are on the
           service you need to supply a valid passphrase
        """
        if end_state is None:
            resource = "get_job %s %s" % (uid, start_state)
        else:
            resource = "get_job %s %s->%s" % (uid, start_state, end_state)

        if Cluster._is_running_service():
            self.verify_passphrase(resource=resource, passphrase=passphrase)

            start_state = JobState(start_state)

            if end_state is not None:
                end_state = JobState(end_state)

            from Acquire.ObjectStore import ObjectStore as _ObjectStore
            from Acquire.Service import get_service_account_bucket \
                as _get_service_account_bucket

            bucket = _get_service_account_bucket()
            key = "compute/%s/%s" % (start_state.value, uid)

            if (end_state is None) or (end_state == start_state):
                try:
                    data = _ObjectStore.get_object_from_json(bucket=bucket,
                                                             key=key)
                except:
                    data = None
            else:
                from Acquire.ObjectStore import get_datetime_now_to_string \
                    as _get_datetime_now_to_string

                try:
                    data = _ObjectStore.take_object_from_json(bucket=bucket,
                                                              key=key)
                    data[end_state.value] = _get_datetime_now_to_string()
                    key = "compute/%s/%s" % (end_state.value, uid)
                    _ObjectStore.set_object_from_json(bucket=bucket, key=key,
                                                      data=data)
                except:
                    data = None

            if data is None:
                raise KeyError(
                    "There is no job with UID %s in state %s" %
                    (uid, start_state.value))

            # the data is a dictionary of the submission time and the
            # job UID
            if uid != data["uid"]:
                raise ValueError("The job info for UID %s is corrupt? %s" %
                                 (uid, data))

            # now load the actual job info
            from Acquire.Compute import ComputeJob as _ComputeJob
            return _ComputeJob.load(uid=uid)
        else:
            passphrase = self.passphrase(resource)
            args = {"uid": str(uid),
                    "passphrase": passphrase,
                    "start_state": str(start_state)}

            if end_state is not None:
                args["end_state"] = str(end_state)

            result = self.compute_service().call_function(function="get_job",
                                                          args=args)

            from Acquire.Compute import ComputeJob as _ComputeJob
            return _ComputeJob.from_data(self.decrypt_data(result["job"]))