Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/src/uds/services/Proxmox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2012-2023 Virtual Cable S.L.U.
# Copyright (c) 2012-2023 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down
11 changes: 8 additions & 3 deletions server/src/uds/services/Proxmox/deployment_fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down Expand Up @@ -78,13 +78,13 @@ def reset(self) -> types.states.TaskState:
self.service().provider().api.reset_vm(int(self._vmid))
except Exception: # nosec: if cannot reset, ignore it
pass # If could not reset, ignore it...

return types.states.TaskState.FINISHED

def op_start(self) -> None:
vminfo = self.service().get_vm_info(int(self._vmid)).validate()

if not vminfo.status.is_running():
if not vminfo.status.is_running():
self._store_task(self.service().provider().api.start_vm(int(self._vmid)))

# Check methods
Expand Down Expand Up @@ -113,3 +113,8 @@ def op_start_checker(self) -> types.states.TaskState:
Checks if machine has started
"""
return self._check_task_finished()

def get_console_connection(
self,
) -> types.services.ConsoleConnectionInfo | None:
return self.service().get_console_connection(self._vmid)
28 changes: 4 additions & 24 deletions server/src/uds/services/Proxmox/deployment_linked.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand All @@ -37,7 +37,6 @@

from uds.core import types
from uds.core.services.generics.dynamic.userservice import DynamicUserService
from uds.core.managers.userservice import UserServiceManager
from uds.core.util import autoserializable
import uds.services.Proxmox.proxmox.exceptions

Expand All @@ -46,7 +45,7 @@

# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from .service_linked import ProxmoxServiceLinked
from .service import ProxmoxService
from .publication import ProxmoxPublication

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -144,8 +143,8 @@ def _check_task_finished(self) -> types.states.TaskState:

return types.states.TaskState.RUNNING

def service(self) -> 'ProxmoxServiceLinked':
return typing.cast('ProxmoxServiceLinked', super().service())
def service(self) -> 'ProxmoxService':
return typing.cast('ProxmoxService', super().service())

def publication(self) -> 'ProxmoxPublication':
pub = super().publication()
Expand Down Expand Up @@ -204,22 +203,3 @@ def get_console_connection(
self,
) -> typing.Optional[types.services.ConsoleConnectionInfo]:
return self.service().get_console_connection(self._vmid)

def desktop_login(
self,
username: str,
password: str,
domain: str = '',
) -> None:
script = (
'import sys\n'
'if sys.platform == "win32":\n'
'from uds import operations\n'
f'''operations.writeToPipe("\\\\.\\pipe\\VDSMDPipe", struct.pack('!IsIs', 1, '{username}'.encode('utf8'), 2, '{password}'.encode('utf8')), True)'''
)
# Post script to service
# operations.writeToPipe("\\\\.\\pipe\\VDSMDPipe", packet, True)
try:
UserServiceManager.manager().send_script(self.db_obj(), script)
except Exception as e:
logger.info('Exception sending loggin to %s: %s', self.db_obj(), e)
8 changes: 4 additions & 4 deletions server/src/uds/services/Proxmox/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2012-2023 Virtual Cable S.L.U.
# Copyright (c) 2012-2023 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down Expand Up @@ -41,11 +41,13 @@

logger = logging.getLogger(__name__)


def get_provider(parameters: typing.Any) -> 'ProxmoxProvider':
return typing.cast(
'ProxmoxProvider', models.Provider.objects.get(uuid=parameters['prov_uuid']).get_instance()
)


def get_storage(parameters: typing.Any) -> types.ui.CallbackResultType:
logger.debug('Parameters received by getResources Helper: %s', parameters)
provider = get_provider(parameters)
Expand All @@ -59,8 +61,6 @@ def get_storage(parameters: typing.Any) -> types.ui.CallbackResultType:
res: list[types.ui.ChoiceItem] = []
# Get storages for that datacenter
for storage in sorted(provider.api.list_storages(node=vm_info.node), key=lambda x: int(not x.shared)):
if storage.type in ('lvm', 'iscsi', 'iscsidirect'): # does not allow differential storage (snapshots, etc.)
continue
space, free = (
storage.avail / 1024 / 1024 / 1024,
(storage.avail - storage.used) / 1024 / 1024 / 1024,
Expand Down
17 changes: 8 additions & 9 deletions server/src/uds/services/Proxmox/jobs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2012-2023 Virtual Cable S.L.U.
# Copyright (c) 2012-2023 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down Expand Up @@ -56,7 +56,7 @@ class ProxmoxDeferredRemoval(jobs.Job):
frecuency = 60 * 3 # Once every NN minutes
friendly_name = 'Proxmox removal'
counter = 0

def get_vmid_stored_data_from(self, data: bytes) -> typing.Tuple[int, bool]:
vmdata = data.decode()
if ':' in vmdata:
Expand All @@ -66,7 +66,6 @@ def get_vmid_stored_data_from(self, data: bytes) -> typing.Tuple[int, bool]:
vmid = vmdata
try_graceful_shutdown = False
return int(vmid), try_graceful_shutdown


# @staticmethod
# def remove(provider_instance: 'provider.ProxmoxProvider', vmid: int, try_graceful_shutdown: bool) -> None:
Expand All @@ -79,19 +78,19 @@ def get_vmid_stored_data_from(self, data: bytes) -> typing.Tuple[int, bool]:
# vminfo = provider_instance.get_machine_info(vmid)
# if vminfo.status == 'running':
# if try_graceful_shutdown:
# # If running vm, simply try to shutdown
# # If running vm, simply try to shutdown
# provider_instance.shutdown_machine(vmid)
# # Store for later removal
# else:
# else:
# # If running vm, simply stops it and wait for next
# provider_instance.stop_machine(vmid)

# store_for_deferred_removal()
# return

# provider_instance.remove_machine(vmid) # Try to remove, launch removal, but check later
# store_for_deferred_removal()

# except client.ProxmoxNotFound:
# return # Machine does not exists
# except Exception as e:
Expand Down Expand Up @@ -132,7 +131,7 @@ def run(self) -> None:
vmid, _try_graceful_shutdown = self.get_vmid_stored_data_from(data[1])
# In fact, here, _try_graceful_shutdown is not used, but we keep it for mayby future use
# The soft shutdown has already being initiated by the remove method

try:
vm_info = instance.api.get_vm_info(vmid)
logger.debug('Found %s for removal %s', vmid, data)
Expand Down
7 changes: 4 additions & 3 deletions server/src/uds/services/Proxmox/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down Expand Up @@ -39,7 +39,7 @@
from uds.core.util.unique_id_generator import UniqueIDGenerator

from .proxmox import client, types as prox_types, exceptions as prox_exceptions
from .service_linked import ProxmoxServiceLinked
from .service import ProxmoxService
from .service_fixed import ProxmoxServiceFixed

# Not imported at runtime, just for type checking
Expand All @@ -64,7 +64,7 @@ class ProxmoxProvider(services.ServiceProvider):
type_description = _('Proxmox platform service provider')
icon_file = 'provider.png'

offers = [ProxmoxServiceLinked, ProxmoxServiceFixed]
offers = [ProxmoxService, ProxmoxServiceFixed]

host = gui.TextField(
length=64,
Expand Down Expand Up @@ -161,6 +161,7 @@ def initialize(self, values: 'types.core.ValuesType') -> None:

if values is not None:
self.timeout.value = validators.validate_timeout(self.timeout.value)
self.macs_range.value = validators.validate_mac_range(self.macs_range.value)
logger.debug(self.host.value)

# All proxmox use same UniqueId generator, even if they are different servers
Expand Down
5 changes: 2 additions & 3 deletions server/src/uds/services/Proxmox/proxmox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2019-2021 Virtual Cable S.L.U.
# Copyright (c) 2019-2021 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand All @@ -27,4 +27,3 @@
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""

28 changes: 19 additions & 9 deletions server/src/uds/services/Proxmox/proxmox/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2019-2021 Virtual Cable S.L.U.
# Copyright (c) 2019-2021 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down Expand Up @@ -383,9 +383,18 @@ def clone_vm(
# Ensure exists target pool, (id is in fact the name of the pool)
if target_pool and not any(p.id == target_pool for p in self.list_pools()):
raise exceptions.ProxmoxDoesNotExists(f'Pool "{target_pool}" does not exist')

logger.debug('Cloning VM %s to %s', vmid, new_vmid)
logger.debug('Parameters: %s %s %s %s %s %s %s', name, description, as_linked_clone, target_node, target_storage, target_pool, must_have_vgpus)
logger.debug(
'Parameters: %s %s %s %s %s %s %s',
name,
description,
as_linked_clone,
target_node,
target_storage,
target_pool,
must_have_vgpus,
)

src_node = vminfo.node

Expand All @@ -403,13 +412,13 @@ def clone_vm(
target_node = node.name
else:
target_node = src_node
logger.debug('Target node: %s', target_node)

logger.debug('Target node: %s', target_node)

# Ensure exists target node
if not any(n.name == target_node for n in self.get_cluster_info().nodes):
raise exceptions.ProxmoxDoesNotExists(f'Node "{target_node}" does not exist')

logger.debug('VM info: %s', vminfo)
logger.debug('Type of vminfo.vgpu_type: %s', type(vminfo.vgpu_type))
logger.debug('Value of vminfo.vgpu_type: %s', vminfo.vgpu_type)
Expand Down Expand Up @@ -537,7 +546,7 @@ def get_guest_ip_address(
if found_ips:
sorted_ips = sorted(found_ips, key=lambda x: ':' in x)
return sorted_ips[0]

except Exception as e:
logger.warning('Error getting guest ip address for machine %s: %s', vmid, e)
raise exceptions.ProxmoxError(f'No ip address for vm {vmid}: {e}')
Expand Down Expand Up @@ -822,9 +831,10 @@ def list_storages(
case collections.abc.Iterable():
node_list = set(node)

version = self.get_version()
return sorted(
[
types.StorageInfo.from_dict(st_info)
types.StorageInfo.from_dict(st_info, version=version)
for st_info in self.get_cluster_resources('storage')
if st_info['node'] in node_list and (content is None or content in st_info['content'])
],
Expand Down
6 changes: 3 additions & 3 deletions server/src/uds/services/Proxmox/proxmox/consts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2012-2023 Virtual Cable S.L.U.
# Copyright (c) 2012-2023 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down Expand Up @@ -40,4 +40,4 @@
CACHE_INFO_DURATION: typing.Final[int] = consts.cache.SHORT_CACHE_TIMEOUT
CACHE_VM_INFO_DURATION: typing.Final[int] = consts.cache.SHORTEST_CACHE_TIMEOUT
# Cache duration is 3 minutes, so this is 60 mins * 24 = 1 day (default)
CACHE_DURATION_LONG: typing.Final[int] = consts.cache.EXTREME_CACHE_TIMEOUT
CACHE_DURATION_LONG: typing.Final[int] = consts.cache.EXTREME_CACHE_TIMEOUT
6 changes: 4 additions & 2 deletions server/src/uds/services/Proxmox/proxmox/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2012-2023 Virtual Cable S.L.U.
# Copyright (c) 2012-2023 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand All @@ -10,7 +10,7 @@
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# * Neither the name of Virtual Cable S.L. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
Expand Down Expand Up @@ -43,9 +43,11 @@ class ProxmoxConnectionError(ProxmoxError, exceptions.services.generics.Retryabl
class ProxmoxAuthError(ProxmoxError, exceptions.services.generics.FatalError):
pass


class ProxmoxDoesNotExists(ProxmoxError):
pass


class ProxmoxNotFound(ProxmoxDoesNotExists, exceptions.services.generics.NotFoundError):
pass

Expand Down
Loading
Loading