1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-03-11 00:58:39 +03:00

Homogeneizing names of publications

This commit is contained in:
Adolfo Gómez García 2024-06-24 22:57:27 +02:00
parent 17027e50f4
commit d279a44c24
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
6 changed files with 33 additions and 26 deletions

View File

@ -195,6 +195,19 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
def service(self) -> 'DynamicService':
return typing.cast('DynamicService', super().service())
def generate_name(self) -> str:
# Returns a name suitable for the publication
if self.service().has_field('basename'):
name = time.strftime(f'{self.service().basename.value}-%Y%m%d%H%M%S')
else:
# Get the service pool name, and remove all {} macros
name = self.servicepool_name()
return self.service().sanitized_name(f'UDS-Pub-{name}-v{self.revision()}')
def generate_annotation(self) -> str:
return (f'UDS publication for {self.servicepool_name()} created on {time.strftime("%Y-%m-%d %H:%M:%S")}')
def check_space(self) -> bool:
"""
If the service needs to check space before publication, it should override this method
@ -308,16 +321,14 @@ class DynamicPublication(services.Publication, autoserializable.AutoSerializable
def op_initialize(self) -> None:
"""
This method is called when the service is initialized
Default initialization method sets the name and flags the service as not destroyed
Default initialization method sets the name and flags the service as not for destroy after finish (for cancel, i.e.)
"""
if self.check_space() is False:
raise Exception('Not enough space to publish')
# First we should create a full clone, so base machine do not get fullfilled with "garbage" delta disks...
# Add a number based on current time to avoid collisions
self._name = self.service().sanitized_name(
f'UDS-Pub-{self.servicepool_name()}-{int(time.time())%256:2X}-{self.revision()}'
)
self._name = self.generate_name()
self._is_flagged_for_destroy = False
@abc.abstractmethod

View File

@ -31,17 +31,17 @@
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import abc
import re
import typing
from uds.core import types
from uds.core import types
from uds.core.environment import Environmentable
from uds.core.serializable import Serializable
if typing.TYPE_CHECKING:
from uds.core import services
from uds.core import osmanagers
from uds.core.environment import Environment
from uds import models
from uds.core import osmanagers, services
from uds.core.environment import Environment
class Publication(Environmentable, Serializable):
@ -106,7 +106,7 @@ class Publication(Environmentable, Serializable):
just after all internal initialization is completed.
We want to use the env, cache and storage methods outside class. If not called, you must implement your own methods
cache and storage are "convenient" methods to access _env.cache and _env.storage
Args:
environment (Environment): Environment of the service
service (services.Service): Service that owns this publication
@ -179,8 +179,9 @@ class Publication(Environmentable, Serializable):
This name is set by core, using the administrator provided data
at administration interface.
Removes the macros before returning the name
"""
return self._servicepool_name
return re.sub(r'\{.*?\}', '', self._servicepool_name).strip()
def get_uuid(self) -> str:
return self._uuid

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017-2021 Virtual Cable S.L.U.
# Copyright (c) 2017-2024 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,

View File

@ -103,10 +103,7 @@ class OpenStackLivePublication(DynamicPublication, autoserializable.AutoSerializ
"""
Realizes the publication of the service
"""
self._name = self.service().sanitized_name(
'UDS-P-' + self.servicepool_name() + "-" + str(self.revision())
)
# Name is generated on op_initialize by DynamicPublication
volume_snapshot_info = self.service().make_template(self._name)
logger.debug('Publication result: %s', volume_snapshot_info)
self._vmid = volume_snapshot_info.id # In fact is not an vmid, but the volume snapshot id, but this way we can use the same method for all publications

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 2012-2019 Virtual Cable S.L.
# Copyright (c) 2012-2024 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
@ -28,7 +28,6 @@
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import datetime
import time
import logging
import typing
@ -95,13 +94,11 @@ class ProxmoxPublication(DynamicPublication, autoserializable.AutoSerializable):
self._is_flagged_for_destroy = destroy_after != ''
self.mark_for_upgrade() # Flag so manager can save it again with new format
def op_create(self) -> None:
# First we should create a full clone, so base machine do not get fullfilled with "garbage" delta disks...
comments = _('UDS Publication for {0} created at {1}').format(
self.servicepool_name(), str(datetime.datetime.now()).split('.')[0]
)
task = self.service().clone_machine(self._name, comments)
# Name is generated on op_initialize by DynamicPublication
task = self.service().clone_machine(self._name, self.generate_annotation())
self._vmid = str(task.vmid)
self._task = ','.join((task.upid.node, task.upid.upid))
@ -126,9 +123,9 @@ class ProxmoxPublication(DynamicPublication, autoserializable.AutoSerializable):
time.sleep(0.5)
# Mark vm as template
self.service().provider().create_template(int(self._vmid))
def op_delete(self) -> None:
self.service().delete(self, self._vmid)
def machine(self) -> int:
return int(self._vmid)

View File

@ -30,6 +30,7 @@
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import logging
import re
import collections.abc
import typing
@ -221,9 +222,9 @@ class XenLinkedService(DynamicService): # pylint: disable=too-many-public-metho
def sanitized_name(self, name: str) -> str:
"""
Xen Seems to allow all kind of names
Xen Seems to allow all kind of names, but let's sanitize a bit
"""
return name
return re.sub(r'([^a-zA-Z0-9_ .-]+)', r'_', name)
def find_duplicates(self, name: str, mac: str) -> collections.abc.Iterable[str]:
"""