1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-10 12:16:49 +00:00

feat(operations): a job's error and warning show in the reader's language

A job stored its error and its result's warning as English text, much
of it built with the values in it (volume paths, a disk, libvirt's
error), so no catalog could translate it, and the job pages and panel
showed English whatever the language.

A job now stores them as messages: our English text, marked for
translation, and its values; libvirt's own text is a message without
values and is shown as it is. The jobs API and pages put them in the
reader's language. OperationError keeps its text and values apart for
the same reason, so a refusal raised while a job is submitted is shown
translated too. Messages one after another still read as sentences.

Errors stored before are kept as one message each, shown as they were.
This commit is contained in:
catborise 2026-11-02 23:49:45 +03:00
parent 0eab09e19d
commit 1e66367bb4
29 changed files with 927 additions and 333 deletions

View file

@ -55,12 +55,12 @@ class CloneVolume(Kind):
compute, stg = _storage(params)
volumes = stg.get_volumes()
if params["image"] not in volumes:
raise util.OperationError(f"The pool has no volume named {params['image']}")
raise util.OperationError(gettext_noop("The pool has no volume named %(name)s"), name=params["image"])
if is_iso(params["image"]):
raise util.OperationError("An ISO image is not cloned")
raise util.OperationError(gettext_noop("An ISO image is not cloned"))
target = stg.clone_name(params["name"], params["format"] or stg.get_volume_format_type(params["image"]))
if target in volumes:
raise util.OperationError("Name of volume already in use")
raise util.OperationError(gettext_noop("Name of volume already in use"))
pool = claims.pool_key(compute.pk, params["pool"])
return Plan(claims={claims.compute_key(compute.pk): Claim.SHARED, pool: Claim.SHARED,
claims.volume_key(compute.pk, params["pool"], target): Claim.EXCLUSIVE},
@ -80,9 +80,9 @@ class CloneVolume(Kind):
return Outcome(Operation.SUCCEEDED)
_compute, stg = _storage(op.params) # unreachable: recovery waits and retries
if op.intent["target"] in stg.get_volumes():
return Outcome(Operation.UNCERTAIN, error="The copy exists, but the job did not see it finish; "
"check it, then resolve this job.")
return Outcome(Operation.FAILED, error="The copy was not made")
return Outcome(Operation.UNCERTAIN, error=utils.say(gettext_noop(
"The copy exists, but the job did not see it finish; check it, then resolve this job.")))
return Outcome(Operation.FAILED, error=utils.say(gettext_noop("The copy was not made")))
def log_entry(self, op, state):
return _log(op, state, f"clone volume {op.params['image']} as {op.intent['target']}")
@ -134,7 +134,7 @@ class UploadVolume(Kind):
logger.error("cancelling upload %s failed: %s", op.pk, utils.safe_text(err))
def expire(self, op):
upload.end(op, Operation.FAILED, upload.IDLE)
upload.end(op, Operation.FAILED, utils.say(upload.IDLE))
def log_entry(self, op, state):
return _log(op, state, f"upload volume {op.params['name']}")

View file

@ -18,6 +18,7 @@ from django.utils import timezone
from libvirt import VIR_ERR_NO_STORAGE_VOL, VIR_ERR_SYSTEM_ERROR, libvirtError
from computes.models import Compute
from operations.utils import words
from operations import claims, jobs
from operations.models import Claim, Operation
from operations.worker import housekeeping
@ -137,7 +138,7 @@ class LocalUploadTests(LocalPool, UploadTestCase):
with self.assertRaisesRegex(ValueError, "The uploaded image is missing"):
self.send(1, b"bbb", total=2, size=6)
op.refresh_from_db()
self.assertEqual((op.state, op.error), (Operation.FAILED, "The uploaded image is missing"))
self.assertEqual((op.state, words(op.error)), (Operation.FAILED, "The uploaded image is missing"))
self.assertFalse(Claim.objects.exists())
def test_the_first_write_syncs_the_pool_directory_before_the_commit(self):
@ -315,7 +316,7 @@ class LocalUploadTests(LocalPool, UploadTestCase):
self.compute.delete()
Compute.objects.filter(pk=self.compute.pk).delete() # the row gone without Compute.delete()
op = jobs.cancel(op.pk, self.admin)
self.assertEqual((op.state, op.error), (Operation.FAILED, "The compute was deleted"))
self.assertEqual((op.state, words(op.error)), (Operation.FAILED, "The compute was deleted"))
def test_ending_before_anything_was_created_needs_no_connection(self):
op = jobs.submit("volume.upload", self.admin, {"compute": self.compute.pk, "pool": "default",
@ -338,7 +339,7 @@ class LocalUploadTests(LocalPool, UploadTestCase):
Operation.objects.filter(pk=op.pk).update(heartbeat_at=timezone.now() - timedelta(seconds=3601))
housekeeping()
op.refresh_from_db()
self.assertEqual((op.state, op.error), (Operation.FAILED, IDLE))
self.assertEqual((op.state, words(op.error)), (Operation.FAILED, IDLE))
self.assertFalse(os.path.exists(self.part(op)))
self.assertFalse(Claim.objects.exists())
@ -348,7 +349,7 @@ class LocalUploadTests(LocalPool, UploadTestCase):
heartbeat_at=timezone.now() - timedelta(seconds=3601))
housekeeping()
op.refresh_from_db()
self.assertEqual((op.state, op.error), (Operation.CANCELLED, ""))
self.assertEqual((op.state, words(op.error)), (Operation.CANCELLED, ""))
self.assertFalse(os.path.exists(self.part(op)))
def test_a_chunk_after_the_idle_timeout_ends_the_job(self):
@ -357,7 +358,7 @@ class LocalUploadTests(LocalPool, UploadTestCase):
with self.assertRaisesRegex(ValueError, "idle"):
self.send(1, b"bbb")
op.refresh_from_db()
self.assertEqual((op.state, op.error), (Operation.FAILED, IDLE))
self.assertEqual((op.state, words(op.error)), (Operation.FAILED, IDLE))
self.assertFalse(os.path.exists(self.part(op)))
def test_a_symlinked_part_file_is_refused(self):
@ -583,7 +584,7 @@ class LibvirtStreamUploadTests(UploadTestCase):
del self.volumes["disk.iso"]
Operation.objects.filter(pk=self.job().pk).update(heartbeat_at=timezone.now() - timedelta(seconds=3601))
housekeeping()
self.assertEqual((self.job().state, self.job().error), (Operation.FAILED, "The uploaded image is missing"))
self.assertEqual((self.job().state, words(self.job().error)), (Operation.FAILED, "The uploaded image is missing"))
def test_a_failed_volume_lookup_is_raised(self):
error = libvirtError("connection lost")

View file

@ -13,16 +13,18 @@ from xml.sax.saxutils import escape
import paramiko
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.utils.translation import gettext_noop
from libvirt import VIR_ERR_NO_STORAGE_VOL, libvirtError
from computes.models import Compute
from logs.audit import audit
from operations import claims, jobs
from operations.models import Claim, Operation
from operations.utils import say, words
from vrtManager.connection import CONN_SSH, CONN_SOCKET, CONN_TCP, CONN_TLS
from vrtManager.storage import wvmStorage
IDLE = "The upload was idle for over an hour; its partial file was deleted"
IDLE = gettext_noop("The upload was idle for over an hour; its partial file was deleted")
IDLE_SECONDS = 3600
SSH_CONNECT_TIMEOUT_SECONDS = 10
SSH_IO_TIMEOUT_SECONDS = 30
@ -440,29 +442,29 @@ def _complete(target, op):
if target.present():
target.finalize()
elif not target.done():
jobs.finish(op, jobs.Outcome(Operation.FAILED, error="The uploaded image is missing"))
jobs.finish(op, jobs.Outcome(Operation.FAILED, error=say(gettext_noop("The uploaded image is missing"))))
raise ValueError(_("The uploaded image is missing"))
except FileExistsError:
target.remove()
jobs.finish(op, jobs.Outcome(Operation.FAILED, error="An image of this name appeared before the upload "
"finished; the upload was deleted"))
jobs.finish(op, jobs.Outcome(Operation.FAILED, error=say(gettext_noop(
"An image of this name appeared before the upload finished; the upload was deleted"))))
raise
jobs.finish(op, jobs.Outcome(Operation.SUCCEEDED))
def _end(target, op, state, error=""):
def _end(target, op, state, error=()):
if op.intent.get("created"):
target.remove()
jobs.finish(op, jobs.Outcome(state, error=error))
jobs.finish(op, jobs.Outcome(state, error=list(error)))
def _refuse(target, op, state, message, error=""):
def _refuse(target, op, state, message, error=()):
"""End the upload and refuse the chunk."""
_end(target, op, state, error)
raise ValueError(message)
def end(op, state, error=""):
def end(op, state, error=()):
"""Delete what the upload made and end it, unless a chunk request holds
it: that request sees the cancel, or the next expiry pass retries. A
requested cancel ends it cancelled. An upload whose every chunk is in is
@ -470,16 +472,16 @@ def end(op, state, error=""):
try:
with claims.hold({_session_key(op): Claim.EXCLUSIVE}):
op.refresh_from_db()
if op.state not in Operation.ACTIVE or (error == IDLE and not _idle(op)):
if op.state not in Operation.ACTIVE or (error == say(IDLE) and not _idle(op)):
return # ended already, or expiry of an upload that is not idle
if op.cancel_requested:
state, error = Operation.CANCELLED, ""
state, error = Operation.CANCELLED, ()
if not op.intent.get("created"):
jobs.finish(op, jobs.Outcome(state, error=error))
jobs.finish(op, jobs.Outcome(state, error=list(error)))
return
compute = Compute.objects.filter(pk=op.params["compute"]).first()
if compute is None:
jobs.finish(op, jobs.Outcome(Operation.FAILED, error="The compute was deleted"))
jobs.finish(op, jobs.Outcome(Operation.FAILED, error=say(gettext_noop("The compute was deleted"))))
return
conn = wvmStorage(compute.hostname, compute.login, compute.password, compute.type, op.params["pool"])
try:
@ -523,7 +525,7 @@ def write_chunk(request, conn, compute, pool, upload_id, raw, chunk_index, chunk
if op.state == Operation.SUCCEEDED and last:
return op # the answer to the last chunk was lost
if op.state not in Operation.ACTIVE:
raise ValueError(op.error or _("The upload has ended"))
raise ValueError(words(op.error) or _("The upload has ended"))
with _target(conn, op) as target:
expected = op.intent["chunk"]
# once every chunk is in, a cancel comes too late: a retry completes it below
@ -537,7 +539,7 @@ def write_chunk(request, conn, compute, pool, upload_id, raw, chunk_index, chunk
_complete(target, op)
return op
if _idle(op):
_refuse(target, op, Operation.FAILED, IDLE, IDLE)
_refuse(target, op, Operation.FAILED, _(IDLE), say(IDLE))
offset = op.progress_done or 0
if chunk.size <= 0:
raise ValueError(_("Empty upload chunk"))
@ -549,7 +551,7 @@ def write_chunk(request, conn, compute, pool, upload_id, raw, chunk_index, chunk
# a first write that failed before creating it is retried as new
new = not (op.intent["created"] and present)
if new and offset:
jobs.finish(op, jobs.Outcome(Operation.FAILED, error="The partial upload is missing"))
jobs.finish(op, jobs.Outcome(Operation.FAILED, error=say(gettext_noop("The partial upload is missing"))))
raise ValueError(_("The partial upload is missing"))
if not op.intent["created"]:
_commit(op, intent={"chunk": expected, "created": True})