Skip to content

Commit

Permalink
Merge pull request #94 from ynput/enhancement/93-do-not-set-environme…
Browse files Browse the repository at this point in the history
…nt-core-variables

Do not set environment env variables from ayon-core
  • Loading branch information
iLLiCiTiT authored Jan 7, 2025
2 parents 1b237ab + a37bb5a commit 607ce74
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 116 deletions.
70 changes: 35 additions & 35 deletions client/ayon_deadline/lib.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import json
from dataclasses import dataclass, field, asdict
from functools import partial
Expand All @@ -8,6 +7,7 @@

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 All @@ -22,6 +22,8 @@

# Constant defining where we store job environment variables on instance or
# context data
# DEPRECATED: Use `FARM_JOB_ENV_DATA_KEY` from `ayon_core.pipeline.publish`
# This variable is NOT USED anywhere in deadline addon.
JOB_ENV_DATA_KEY: str = "farmJobEnv"


Expand All @@ -33,33 +35,10 @@ class DeadlineServerInfo:
machines: List[str]


def get_ayon_render_job_envs() -> "dict[str, str]":
"""Get required env vars for valid render job submission."""
return {
"AYON_LOG_NO_COLORS": "1",
"AYON_RENDER_JOB": "1",
"AYON_BUNDLE_NAME": os.environ["AYON_BUNDLE_NAME"]
}


def get_instance_job_envs(instance) -> "dict[str, str]":
"""Add all job environments as specified on the instance and context.
Any instance `job_env` vars will override the context `job_env` vars.
class DeadlineWebserviceError(Exception):
"""
Exception to throw when connection to Deadline server fails.
"""
env = {}
for job_env in [
instance.context.data.get(JOB_ENV_DATA_KEY, {}),
instance.data.get(JOB_ENV_DATA_KEY, {})
]:
if job_env:
env.update(job_env)

# Return the dict sorted just for readability in future logs
if env:
env = dict(sorted(env.items()))

return env


def get_deadline_pools(
Expand Down Expand Up @@ -184,10 +163,32 @@ def _get_deadline_info(
return sorted(response.json(), key=lambda value: (value != "none", value))


class DeadlineWebserviceError(Exception):
"""
Exception to throw when connection to Deadline server fails.
# ------------------------------------------------------------
# NOTE It is pipeline related logic from here, probably
# should be moved to './pipeline' and used from there.
# - This file is imported in `ayon_deadline/addon.py` which should not
# have any pipeline logic.
def get_instance_job_envs(instance) -> "dict[str, str]":
"""Add all job environments as specified on the instance and context.
Any instance `job_env` vars will override the context `job_env` vars.
"""
# Avoid import from 'ayon_core.pipeline'
from ayon_core.pipeline.publish import FARM_JOB_ENV_DATA_KEY

env = {}
for job_env in [
instance.context.data.get(FARM_JOB_ENV_DATA_KEY, {}),
instance.data.get(FARM_JOB_ENV_DATA_KEY, {})
]:
if job_env:
env.update(job_env)

# Return the dict sorted just for readability in future logs
if env:
env = dict(sorted(env.items()))

return env


class DeadlineKeyValueVar(dict):
Expand Down Expand Up @@ -222,7 +223,7 @@ def serialize(self):
key = key + "{}"

return {
key.format(index): "{}={}".format(var_key, var_value)
key.format(index): f"{var_key}={var_value}"
for index, (var_key, var_value) in enumerate(sorted(self.items()))
}

Expand Down Expand Up @@ -273,10 +274,10 @@ def __iadd__(self, other):

def __setitem__(self, key, value):
if not isinstance(key, int):
raise TypeError("Key must be an integer: {}".format(key))
raise TypeError(f"Key must be an integer: {key}")

if key < 0:
raise ValueError("Negative index can't be set: {}".format(key))
raise ValueError(f"Negative index can't be set: {key}")
dict.__setitem__(self, key, value)


Expand Down Expand Up @@ -535,8 +536,7 @@ def from_dict(cls, data: Dict[str, Any]) -> 'AYONDeadlineJobInfo':

def add_render_job_env_var(self):
"""Add required env vars for valid render job submission."""
for key, value in get_ayon_render_job_envs().items():
self.EnvironmentKeyValue[key] = value
self.EnvironmentKeyValue["AYON_RENDER_JOB"] = "1"

def add_instance_job_env_vars(self, instance):
"""Add all job environments as specified on the instance and context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pyblish.api

from ayon_deadline.lib import JOB_ENV_DATA_KEY
from ayon_core.pipeline.publish import FARM_JOB_ENV_DATA_KEY


class CollectDeadlineJobEnvVars(pyblish.api.ContextPlugin):
Expand All @@ -12,31 +12,23 @@ class CollectDeadlineJobEnvVars(pyblish.api.ContextPlugin):
targets = ["local"]

ENV_KEYS = [
# AYON
"AYON_BUNDLE_NAME",
"AYON_DEFAULT_SETTINGS_VARIANT",
"AYON_PROJECT_NAME",
"AYON_FOLDER_PATH",
"AYON_TASK_NAME",
# applications addon
"AYON_APP_NAME",
"AYON_WORKDIR",
"AYON_APP_NAME",
"AYON_LOG_NO_COLORS",
"AYON_IN_TESTS",
"IS_TEST", # backwards compatibility

# Ftrack
# ftrack addon
"FTRACK_API_KEY",
"FTRACK_API_USER",
"FTRACK_SERVER",
"PYBLISHPLUGINPATH",

# Shotgrid
# Shotgrid / Flow addon
"OPENPYPE_SG_USER",

# Not sure how this is usefull for farm, scared to remove
"PYBLISHPLUGINPATH",
]

def process(self, context):
env = context.data.setdefault(JOB_ENV_DATA_KEY, {})
env = context.data.setdefault(FARM_JOB_ENV_DATA_KEY, {})
for key in self.ENV_KEYS:
# Skip already set keys
if key in env:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pyblish.api

from ayon_deadline.lib import JOB_ENV_DATA_KEY
from ayon_core.pipeline.publish import FARM_JOB_ENV_DATA_KEY

try:
from ayon_usd import get_usd_pinning_envs
Expand Down Expand Up @@ -41,7 +41,7 @@ def process(self, instance):
self.log.debug("Should not be processed on farm, skipping.")
return

job_env = instance.data.setdefault(JOB_ENV_DATA_KEY, {})
job_env = instance.data.setdefault(FARM_JOB_ENV_DATA_KEY, {})
usd_pinning_envs: dict = get_usd_pinning_envs(instance)

# Log the job envs that are set
Expand Down
42 changes: 8 additions & 34 deletions client/ayon_deadline/plugins/publish/global/submit_publish_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
import pyblish.api

from ayon_core.pipeline import publish
from ayon_core.lib import EnumDef, is_in_tests
from ayon_core.lib import EnumDef
from ayon_core.pipeline.version_start import get_versioning_start

from ayon_core.pipeline.farm.pyblish_functions import (
create_skeleton_instance,
create_instances_for_aov,
attach_instances_to_product,
prepare_representations,
create_metadata_path
)

from ayon_deadline.lib import get_instance_job_envs
from ayon_deadline.abstract_submit_deadline import requests_post


Expand Down Expand Up @@ -116,17 +117,6 @@ class ProcessSubmittedJobOnFarm(pyblish.api.InstancePlugin,
},
]

environ_keys = [
"FTRACK_API_USER",
"FTRACK_API_KEY",
"FTRACK_SERVER",
"AYON_APP_NAME",
"AYON_USERNAME",
"AYON_SG_USERNAME",
"KITSU_LOGIN",
"KITSU_PWD"
]

# custom deadline attributes
deadline_department = ""
deadline_pool = ""
Expand Down Expand Up @@ -187,25 +177,12 @@ def _submit_deadline_post_job(self, instance, job, instances):
metadata_path, rootless_metadata_path = \
create_metadata_path(instance, anatomy)

settings_variant = os.environ["AYON_DEFAULT_SETTINGS_VARIANT"]
environment = {
"AYON_PROJECT_NAME": instance.context.data["projectName"],
"AYON_FOLDER_PATH": instance.context.data["folderPath"],
"AYON_TASK_NAME": instance.context.data["task"],
"AYON_USERNAME": instance.context.data["user"],
"AYON_LOG_NO_COLORS": "1",
"AYON_IN_TESTS": str(int(is_in_tests())),
environment = get_instance_job_envs(instance)
environment.update({
"AYON_PUBLISH_JOB": "1",
"AYON_RENDER_JOB": "0",
"AYON_REMOTE_PUBLISH": "0",
"AYON_BUNDLE_NAME": os.environ["AYON_BUNDLE_NAME"],
"AYON_DEFAULT_SETTINGS_VARIANT": settings_variant,
}

# add environments from self.environ_keys
for env_key in self.environ_keys:
if os.getenv(env_key):
environment[env_key] = os.environ[env_key]
})

priority = self.deadline_priority or instance.data.get("priority", 50)

Expand All @@ -221,6 +198,7 @@ def _submit_deadline_post_job(self, instance, job, instances):
]
# TODO remove when AYON launcher respects environment variable
# 'AYON_DEFAULT_SETTINGS_VARIANT'
settings_variant = os.environ["AYON_DEFAULT_SETTINGS_VARIANT"]
if settings_variant == "staging":
args.append("--use-staging")

Expand Down Expand Up @@ -442,11 +420,7 @@ def process(self, instance):
render_job["Props"]["User"] = instance.context.data.get(
"deadlineUser", getpass.getuser())

render_job["Props"]["Env"] = {
"FTRACK_API_USER": os.environ.get("FTRACK_API_USER"),
"FTRACK_API_KEY": os.environ.get("FTRACK_API_KEY"),
"FTRACK_SERVER": os.environ.get("FTRACK_SERVER"),
}
render_job["Props"]["Env"] = get_instance_job_envs(instance)

# get default deadline webservice url from deadline module
self.deadline_url = instance.data["deadline"]["url"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pyblish.api

from ayon_core.pipeline import publish
from ayon_core.lib import EnumDef, is_in_tests
from ayon_core.lib import EnumDef
from ayon_core.pipeline.version_start import get_versioning_start
from ayon_core.pipeline.farm.pyblish_functions import (
create_skeleton_instance_cache,
Expand All @@ -18,6 +18,7 @@
prepare_cache_representations,
create_metadata_path
)
from ayon_deadline.lib import get_instance_job_envs
from ayon_deadline.abstract_submit_deadline import requests_post


Expand Down Expand Up @@ -61,17 +62,6 @@ class ProcessSubmittedCacheJobOnFarm(pyblish.api.InstancePlugin,

families = ["publish.hou"]

environ_keys = [
"FTRACK_API_USER",
"FTRACK_API_KEY",
"FTRACK_SERVER",
"AYON_APP_NAME",
"AYON_USERNAME",
"AYON_SG_USERNAME",
"KITSU_LOGIN",
"KITSU_PWD"
]

# custom deadline attributes
deadline_department = ""
deadline_pool = ""
Expand Down Expand Up @@ -118,26 +108,12 @@ def _submit_deadline_post_job(self, instance, job):
metadata_path, rootless_metadata_path = \
create_metadata_path(instance, anatomy)

environment = {
"AYON_PROJECT_NAME": instance.context.data["projectName"],
"AYON_FOLDER_PATH": instance.context.data["folderPath"],
"AYON_TASK_NAME": instance.context.data["task"],
"AYON_USERNAME": instance.context.data["user"],
"AYON_LOG_NO_COLORS": "1",
"AYON_IN_TESTS": str(int(is_in_tests())),
environment = get_instance_job_envs(instance)
environment.update({
"AYON_PUBLISH_JOB": "1",
"AYON_RENDER_JOB": "0",
"AYON_REMOTE_PUBLISH": "0",
"AYON_BUNDLE_NAME": os.environ["AYON_BUNDLE_NAME"],
"AYON_DEFAULT_SETTINGS_VARIANT": (
os.environ["AYON_DEFAULT_SETTINGS_VARIANT"]
),
}

# add environments from self.environ_keys
for env_key in self.environ_keys:
if os.getenv(env_key):
environment[env_key] = os.environ[env_key]
})

priority = self.deadline_priority or instance.data.get("priority", 50)

Expand Down

0 comments on commit 607ce74

Please sign in to comment.