Skip to content

Commit

Permalink
Refactored utility methods for DL api to lib file
Browse files Browse the repository at this point in the history
  • Loading branch information
kalisp committed Nov 7, 2024
1 parent 403d577 commit ba7293d
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 154 deletions.
161 changes: 11 additions & 150 deletions client/ayon_deadline/addon.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import os
import sys

import requests
import six
from typing import Optional, List, Tuple
from typing import Optional, List

from ayon_core.lib import Logger
from ayon_core.addon import AYONAddon, IPluginPaths

from .lib import (
get_deadline_workers,
get_deadline_groups,
get_deadline_limit_groups,
get_deadline_pools
)
from .version import __version__


DEADLINE_ADDON_ROOT = os.path.dirname(os.path.abspath(__file__))


class DeadlineWebserviceError(Exception):
"""
Exception to throw when connection to Deadline server fails.
"""


class DeadlineAddon(AYONAddon, IPluginPaths):
name = "deadline"
version = __version__
Expand Down Expand Up @@ -62,141 +58,6 @@ def get_publish_plugin_paths(
paths.append(os.path.join(publish_dir, host_name))
return paths

@classmethod
def get_deadline_pools(
cls,
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get pools from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice is unreachable.
"""
endpoint = "{}/api/pools?NamesOnly=true".format(webservice_url)
return cls._get_deadline_info(
endpoint, auth, log, item_type="pools")

@classmethod
def get_deadline_groups(
cls,
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get Groups from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice_url is unreachable.
"""
endpoint = "{}/api/groups".format(webservice_url)
return cls._get_deadline_info(
endpoint, auth, log, item_type="groups")

@classmethod
def get_deadline_limit_groups(
cls,
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get Limit Groups from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice_url is unreachable.
"""
endpoint = "{}/api/limitgroups?NamesOnly=true".format(webservice_url)
return cls._get_deadline_info(
endpoint, auth, log, item_type="limitgroups")

@classmethod
def get_deadline_workers(
cls,
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get Workers (eg.machine names) from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice_url is unreachable.
"""
endpoint = "{}/api/slaves?NamesOnly=true".format(webservice_url)
return cls._get_deadline_info(
endpoint, auth, log, item_type="workers")

@classmethod
def _get_deadline_info(
cls,
endpoint,
auth=None,
log=None,
item_type=None
):
from .abstract_submit_deadline import requests_get

if not log:
log = Logger.get_logger(__name__)

try:
kwargs = {}
if auth:
kwargs["auth"] = auth
response = requests_get(endpoint, **kwargs)
except requests.exceptions.ConnectionError as exc:
msg = 'Cannot connect to DL web service {}'.format(endpoint)
log.error(msg)
six.reraise(
DeadlineWebserviceError,
DeadlineWebserviceError('{} - {}'.format(msg, exc)),
sys.exc_info()[2])
if not response.ok:
log.warning(f"No {item_type} retrieved")
return []

return response.json()

def get_pools_by_server_name(self, server_name: str) -> List[str]:
"""Returns dictionary of pools per DL server
Expand All @@ -213,7 +74,7 @@ def get_pools_by_server_name(self, server_name: str) -> List[str]:

auth = (dl_server_info["default_username"],
dl_server_info["default_password"])
pools = self.get_deadline_pools(
pools = get_deadline_pools(
dl_server_info["value"],
auth
)
Expand All @@ -237,7 +98,7 @@ def get_groups_by_server_name(self, server_name: str) -> List[str]:

auth = (dl_server_info["default_username"],
dl_server_info["default_password"])
groups = self.get_deadline_groups(
groups = get_deadline_groups(
dl_server_info["value"],
auth
)
Expand All @@ -261,7 +122,7 @@ def get_limit_groups_by_server_name(self, server_name: str) -> List[str]:

auth = (dl_server_info["default_username"],
dl_server_info["default_password"])
limit_groups = self.get_deadline_limit_groups(
limit_groups = get_deadline_limit_groups(
dl_server_info["value"],
auth
)
Expand All @@ -285,7 +146,7 @@ def get_machines_by_server_nameserver(self, server_name: str) -> List[str]:

auth = (dl_server_info["default_username"],
dl_server_info["default_password"])
machines = self.get_deadline_workers(
machines = get_deadline_workers(
dl_server_info["value"],
auth
)
Expand Down
144 changes: 142 additions & 2 deletions client/ayon_deadline/lib.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import os
import sys
import requests
from dataclasses import dataclass, field, asdict
from functools import partial
from typing import Optional, Dict, Any
from typing import Optional, List, Tuple, Any, Dict
import json

from ayon_core.lib import Logger

# describes list of product typed used for plugin filtering for farm publishing
FARM_FAMILIES = [
"render", "render.farm", "render.frames_farm",
Expand Down Expand Up @@ -49,6 +53,142 @@ def get_instance_job_envs(instance) -> "dict[str, str]":
return env


def get_deadline_pools(
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get pools from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice is unreachable.
"""
endpoint = "{}/api/pools?NamesOnly=true".format(webservice_url)
return _get_deadline_info(
endpoint, auth, log, item_type="pools")


def get_deadline_groups(
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get Groups from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice_url is unreachable.
"""
endpoint = "{}/api/groups".format(webservice_url)
return _get_deadline_info(
endpoint, auth, log, item_type="groups")


def get_deadline_limit_groups(
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get Limit Groups from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice_url is unreachable.
"""
endpoint = "{}/api/limitgroups?NamesOnly=true".format(webservice_url)
return _get_deadline_info(
endpoint, auth, log, item_type="limitgroups")

def get_deadline_workers(
webservice_url: str,
auth: Optional[Tuple[str, str]] = None,
log: Optional[Logger] = None
) -> List[str]:
"""Get Workers (eg.machine names) from Deadline API.
Args:
webservice_url (str): Server url.
auth (Optional[Tuple[str, str]]): Tuple containing username,
password
log (Optional[Logger]): Logger to log errors to, if provided.
Returns:
List[str]: Limit Groups.
Raises:
RuntimeError: If deadline webservice_url is unreachable.
"""
endpoint = "{}/api/slaves?NamesOnly=true".format(webservice_url)
return _get_deadline_info(
endpoint, auth, log, item_type="workers")


def _get_deadline_info(
endpoint,
auth=None,
log=None,
item_type=None
):
from .abstract_submit_deadline import requests_get

if not log:
log = Logger.get_logger(__name__)

try:
kwargs = {}
if auth:
kwargs["auth"] = auth
response = requests_get(endpoint, **kwargs)
except requests.exceptions.ConnectionError as exc:
msg = 'Cannot connect to DL web service {}'.format(endpoint)
log.error(msg)
raise(
DeadlineWebserviceError,
DeadlineWebserviceError('{} - {}'.format(msg, exc)),
sys.exc_info()[2]
)
if not response.ok:
log.warning(f"No {item_type} retrieved")
return []

return response.json()


class DeadlineWebserviceError(Exception):
"""
Exception to throw when connection to Deadline server fails.
"""


class DeadlineKeyValueVar(dict):
"""
Expand Down Expand Up @@ -365,7 +505,7 @@ def filter_data(a, v):
return serialized

@classmethod
def from_dict(cls, data: Dict) -> 'AYONDeadlineJobInfo':
def from_dict(cls, data: Dict[str, Any]) -> 'AYONDeadlineJobInfo':

implemented_field_values = {
"ChunkSize": data["chunk_size"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
PublishXmlValidationError,
OptionalPyblishPluginMixin
)
from ayon_deadline.lib import FARM_FAMILIES
from ayon_deadline.lib import FARM_FAMILIES, get_deadline_pools


class ValidateDeadlinePools(OptionalPyblishPluginMixin,
Expand Down Expand Up @@ -69,7 +69,7 @@ def get_pools(self, deadline_addon, deadline_url, auth):
"Querying available pools for Deadline url: {}".format(
deadline_url)
)
pools = deadline_addon.get_deadline_pools(
pools = get_deadline_pools(
deadline_url, auth=auth, log=self.log
)
# some DL return "none" as a pool name
Expand Down

0 comments on commit ba7293d

Please sign in to comment.