1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 04:36:48 +00:00

perf: a VM's owners' SSH keys in one query

The VM's keys page (instances:sshkeys) and the cloud-init user data
collected the owners' keys with one query an owner. Both now read them
in one query joined over the VM's owners, ordered owner by owner as
before.
This commit is contained in:
catborise 2026-11-04 21:24:25 +03:00
parent 2484d4efb3
commit 4dd72766cf
3 changed files with 23 additions and 10 deletions

View file

@ -1,7 +1,7 @@
import json
import socket
from accounts.models import UserInstance, UserSSHKey
from accounts.models import UserSSHKey
from computes.models import Compute
from django.contrib.auth.decorators import login_not_required
from django.core.exceptions import PermissionDenied
@ -65,8 +65,9 @@ def os_userdata(request, version):
# into the other VM by naming theirs the same. No keys then.
instances = list(Instance.objects.filter(name=hostname.split(".")[0])[:2]) if hostname else []
if len(instances) == 1:
for ui in UserInstance.objects.filter(instance=instances[0]):
instance_keys += UserSSHKey.objects.filter(user=ui.user).values_list("keypublic", flat=True)
# the keys of the VM's owners, owner by owner
instance_keys = list(UserSSHKey.objects.filter(user__userinstance__instance=instances[0])
.order_by("user__userinstance__id", "id").values_list("keypublic", flat=True))
# quoted, so a comment such as "it's: mine" stays one YAML string
instance_keys = [json.dumps(key) for key in instance_keys]