[elbe-devel] [PATCH] elbepack: use uuid7 to generate identifiers

Thomas Weißschuh thomas.weissschuh at linutronix.de
Fri Mar 15 14:35:30 CET 2024


uuid7 sort lexicographically by creation date, making it easier to keep
the overview.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 debian/python3-elbe-common.install |  1 +
 elbepack/init/libvirt.xml.mako     |  4 ++--
 elbepack/projectmanager.py         |  6 +++---
 elbepack/uuid7.py                  | 25 +++++++++++++++++++++++++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/debian/python3-elbe-common.install b/debian/python3-elbe-common.install
index e29ff3c1364b..1ea515548424 100644
--- a/debian/python3-elbe-common.install
+++ b/debian/python3-elbe-common.install
@@ -38,6 +38,7 @@
 ./usr/lib/python3.*/*-packages/elbepack/templates.py
 ./usr/lib/python3.*/*-packages/elbepack/toolchain.py
 ./usr/lib/python3.*/*-packages/elbepack/treeutils.py
+./usr/lib/python3.*/*-packages/elbepack/uuid7.py
 ./usr/lib/python3.*/*-packages/elbepack/validate.py
 ./usr/lib/python3.*/*-packages/elbepack/version.py
 ./usr/lib/python3.*/*-packages/elbepack/virtapt.py
diff --git a/elbepack/init/libvirt.xml.mako b/elbepack/init/libvirt.xml.mako
index 341d531d7cfe..8ca767691f2c 100644
--- a/elbepack/init/libvirt.xml.mako
+++ b/elbepack/init/libvirt.xml.mako
@@ -7,14 +7,14 @@
 # TODO: Relativ file path for diskimage
 
 
-import uuid
 import multiprocessing
 import os
 import shutil
 from elbepack.filesystem import size_to_int
+from elbepack.uuid7 import uuid7
 
 # Generate UUID
-uid = uuid.uuid4()
+uid = uuid7()
 
 name = cfg['initvm_domain']
 cpus = int(prj.text('max-cpus', default=defs, key='max-cpus'))
diff --git a/elbepack/projectmanager.py b/elbepack/projectmanager.py
index dcd7e84de60f..f6fd0266e49e 100644
--- a/elbepack/projectmanager.py
+++ b/elbepack/projectmanager.py
@@ -7,7 +7,6 @@ import os
 from os import path
 from shutil import rmtree
 from threading import Lock
-from uuid import uuid4
 
 from elbepack.asyncworker import (
     APTCommitJob,
@@ -29,6 +28,7 @@ from elbepack.asyncworker import (
 from elbepack.db import ElbeDB, get_versioned_filename
 from elbepack.elbexml import ValidationMode
 from elbepack.log import read_loggingQ
+from elbepack.uuid7 import uuid7
 
 
 class ProjectManagerError(Exception):
@@ -80,7 +80,7 @@ class ProjectManager:
         self.worker.stop()
 
     def new_project(self, userid):
-        subdir = str(uuid4())
+        subdir = str(uuid7())
         builddir = path.join(self.basepath, subdir)
         self.db.create_project(builddir, owner_id=userid)
         return builddir
@@ -90,7 +90,7 @@ class ProjectManager:
             userid,
             xml_file,
             url_validation=ValidationMode.CHECK_ALL):
-        subdir = str(uuid4())
+        subdir = str(uuid7())
         builddir = path.join(self.basepath, subdir)
 
         with self.lock:
diff --git a/elbepack/uuid7.py b/elbepack/uuid7.py
new file mode 100644
index 000000000000..c3d1873d0a44
--- /dev/null
+++ b/elbepack/uuid7.py
@@ -0,0 +1,25 @@
+import datetime
+import secrets
+import struct
+import uuid
+
+
+def uuid7(now=None):
+    if now is None:
+        now = datetime.datetime.now()
+
+    res = bytearray()
+
+    res.extend(
+        struct.pack('>Q', int(now.timestamp() * 1000))[2:]
+    )
+
+    res.extend(secrets.token_bytes(10))
+    res[6] = 0b01110000 | (res[6] & 0b00001111)
+    res[8] = 0b10000000 | (res[7] & 0b00111111)
+
+    return uuid.UUID(bytes=bytes(res))
+
+
+if __name__ == '__main__':
+    print(uuid7())

---
base-commit: ef9eeca734b372faba47b88df94950d5b465751d
change-id: 20240315-uuid7-9d2080a63aaf

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh at linutronix.de>



More information about the elbe-devel mailing list