Skip to content

Commit

Permalink
feat: add installer support for configurable session root dir
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Usiskin <[email protected]>
  • Loading branch information
jusiskin committed Dec 17, 2024
1 parent e1abc09 commit a3b033d
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 18 deletions.
17 changes: 17 additions & 0 deletions src/deadline_worker_agent/config/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# type: ignore

from __future__ import annotations
import argparse
Expand Down Expand Up @@ -41,6 +42,9 @@ class ParsedArguments(argparse.Namespace):
"""A Windows username to override the queue jobRunAs configuration. If False, then no
modification is made. If None, then the setting is unset."""

session_root_dir: str | None = None
"""Path to the parent directory where the worker agent creates per-session subdirectories under"""


def create_argument_parser() -> argparse.ArgumentParser:
"""Creates the argparse ArgumentParser for the deadline_worker_agent.config module"""
Expand All @@ -62,6 +66,12 @@ def create_argument_parser() -> argparse.ArgumentParser:
help="The unique identifier for the Deadline Cloud fleet",
required=False,
)
worker_group.add_argument(
"--session-root-dir",
help="The parent directory that worker agent creates per-session subdirectories under",
required=False,
type=str,
)

aws_group = parser.add_argument_group("AWS", "Settings related to AWS and EC2 hosts")
parser.set_defaults(allow_ec2_instance_profile=None)
Expand Down Expand Up @@ -193,6 +203,13 @@ def args_to_setting_modifications(parsed_args: ParsedArguments) -> list[SettingM
raise NotImplementedError(
f"Unexpected value for windows_job_user: {parsed_args.windows_job_user}"
)
if parsed_args.session_root_dir is not None:
settings_to_modify.append(
SettingModification(
setting=ModifiableSetting.SESSION_ROOT_DIR,
value=parsed_args.session_root_dir,
)
)

return settings_to_modify

Expand Down
14 changes: 14 additions & 0 deletions src/deadline_worker_agent/config/config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ class ModifiableSetting(Enum):
To prevent the worker agent from shutting down the host when being told to stop, uncomment the
line below:
""".lstrip(),
)
SESSION_ROOT_DIR = ModifiableSettingData(
setting_name="session_root_dir",
table_name="worker",
preceding_comment="""
The session root directory is a parent directory where worker agent creates per-session
subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
variable is set or the --session-root-dir command-line argument is specified.
The default session root directory on POSIX systems is "/sessions" and on Windows systems is
"C:\ProgramData\Amazon\OpenJD".
Uncomment the line below and replace the value with your desired session root directory:
""".lstrip(),
)
WINDOWS_JOB_USER = ModifiableSettingData(
Expand Down
21 changes: 20 additions & 1 deletion src/deadline_worker_agent/installer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import sys
import sysconfig

from deadline_worker_agent.config.settings import (
DEFAULT_POSIX_SESSION_ROOT_DIR,
DEFAULT_WINDOWS_SESSION_ROOT_DIR,
)


if sys.platform == "win32":
from deadline_worker_agent.installer.win_installer import (
Expand Down Expand Up @@ -70,7 +75,7 @@ def install() -> None:
sys.exit(1)

arg_parser = get_argument_parser()
args = arg_parser.parse_args(namespace=ParsedCommandLineArguments)
args = arg_parser.parse_args(namespace=ParsedCommandLineArguments())
scripts_path = Path(sysconfig.get_path("scripts"))

if args.region is None:
Expand All @@ -91,6 +96,7 @@ def install() -> None:
parser=arg_parser,
grant_required_access=args.grant_required_access,
allow_ec2_instance_profile=not args.disallow_instance_profile,
session_root_dir=args.session_root_dir,
)
if args.user:
installer_args.update(user_name=args.user)
Expand Down Expand Up @@ -124,6 +130,8 @@ def install() -> None:
str(scripts_path),
"--python-interpreter-path",
sys.executable,
"--session-root-dir",
str(args.session_root_dir),
]
if args.vfs_install_path:
cmd += ["--vfs-install-path", args.vfs_install_path]
Expand Down Expand Up @@ -169,6 +177,7 @@ class ParsedCommandLineArguments(Namespace):
grant_required_access: bool
disallow_instance_profile: bool
windows_job_user: Optional[str] = None
session_root_dir: Path


def get_argument_parser() -> ArgumentParser: # pragma: no cover
Expand Down Expand Up @@ -259,6 +268,16 @@ def get_argument_parser() -> ArgumentParser: # pragma: no cover
action="store_true",
default=False,
)
parser.add_argument(
"--session-root-dir",
help="The root directory under which the worker agent creates session directories",
type=Path,
default=(
str(DEFAULT_WINDOWS_SESSION_ROOT_DIR)
if sys.platform == "win32"
else str(DEFAULT_POSIX_SESSION_ROOT_DIR)
), # pragma: nocover
)

if sys.platform == "win32":
parser.add_argument(
Expand Down
20 changes: 14 additions & 6 deletions src/deadline_worker_agent/installer/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ telemetry_opt_out="no"
warning_lines=()
vfs_install_path="unset"
python_interpreter_path=unset
session_root_dir="/sessions"

usage()
{
Expand Down Expand Up @@ -106,6 +107,8 @@ usage()
echo " agent makes requests to the EC2 instance meta-data service (IMDS) to check for an instance profile. "
echo " If an instance profile is detected, the worker agent will stop and exit. When this is not provided, "
echo " the worker agent no longer performs these checks, allowing it to run with an EC2 instance profile."
echo " --session-root-dir SESSION_ROOT_DIR"
echo " The root directory under which the worker agent will create session directories."

exit 2
}
Expand All @@ -131,7 +134,7 @@ validate_deadline_id() {
}

# Validate arguments
PARSED_ARGUMENTS=$(getopt -n install.sh --longoptions farm-id:,fleet-id:,region:,user:,group:,scripts-path:,python-interpreter-path:,vfs-install-path:,start,allow-shutdown,no-install-service,telemetry-opt-out,disallow-instance-profile -- "y" "$@")
PARSED_ARGUMENTS=$(getopt -n install.sh --longoptions farm-id:,fleet-id:,region:,user:,group:,scripts-path:,python-interpreter-path:,vfs-install-path:,session-root-dir:,start,allow-shutdown,no-install-service,telemetry-opt-out,disallow-instance-profile -- "y" "$@")
VALID_ARGUMENTS=$?
if [ "${VALID_ARGUMENTS}" != "0" ]; then
usage
Expand All @@ -152,6 +155,7 @@ do
--scripts-path) scripts_path="$2" ; shift 2 ;;
--python-interpreter-path) python_interpreter_path="$2" ; shift 2 ;;
--vfs-install-path) vfs_install_path="$2" ; shift 2 ;;
--session-root-dir) session_root_dir="$2" ; shift 2 ;;
--allow-shutdown) allow_shutdown="yes" ; shift ;;
--disallow-instance-profile) disallow_instance_profile="yes" ; shift ;;
--no-install-service) no_install_service="yes" ; shift ;;
Expand Down Expand Up @@ -276,6 +280,7 @@ echo "Worker agent user: ${wa_user}"
echo "Worker agent group: ${wa_group}"
echo "Worker job group: ${job_group}"
echo "Scripts path: ${scripts_path}"
echo "Session root directory: ${session_root_dir}"
echo "Worker agent program path: ${worker_agent_program}"
echo "Deadline client program path: ${client_library_program}"
echo "Allow worker agent shutdown: ${allow_shutdown}"
Expand Down Expand Up @@ -382,10 +387,12 @@ if [ -f /var/lib/deadline/worker.json ]; then
fi
echo "Done provisioning persistence directory (/var/lib/deadline)"

echo "Provisioning root directory for OpenJD Sessions (/sessions)"
mkdir -p /sessions
chown "${wa_user}:${job_group}" /sessions
chmod 755 /sessions
# Provision session directory
echo "Provisioning root directory for OpenJD Sessions (${session_root_dir})"
mkdir -p "${session_root_dir}"
chown "${wa_user}:${job_group}" "${session_root_dir}"
chmod 755 "${session_root_dir}"
echo "Done provisioning root directory for OpenJD Sessions (${session_root_dir})"

echo "Provisioning configuration directory (/etc/amazon/deadline)"
mkdir -p /etc/amazon/deadline
Expand Down Expand Up @@ -420,7 +427,8 @@ fi
--farm-id "${farm_id}" \
--fleet-id "${fleet_id}" \
"${allow_ec2_instance_profile_flag}" \
"${shutdown_on_stop_flag}"
"${shutdown_on_stop_flag}" \
--session-root-dir "${session_root_dir}"

if ! [[ "${no_install_service}" == "yes" ]]; then
# Set up the service
Expand Down
33 changes: 31 additions & 2 deletions src/deadline_worker_agent/installer/win_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def update_config_file(
shutdown_on_stop: Optional[bool] = None,
allow_ec2_instance_profile: Optional[bool] = None,
windows_job_user: Optional[str] = None,
session_root_dir: Optional[Path] = None,
) -> None:
"""
Updates the worker configuration file, creating it from the example if it does not exist.
Expand Down Expand Up @@ -370,6 +371,13 @@ def update_config_file(
value=allow_ec2_instance_profile,
)
)
if session_root_dir is not None:
settings_to_modify.append(
SettingModification(
setting=ModifiableSetting.SESSION_ROOT_DIR,
value=str(session_root_dir),
)
)

updated_keys = [sm.setting.value.setting_name for sm in settings_to_modify]

Expand All @@ -382,7 +390,11 @@ def update_config_file(
logging.info(f"Done configuring {updated_keys} in {config_path}")


def provision_directories(agent_username: str) -> WorkerAgentDirectories:
def provision_directories(
*,
agent_username: str,
session_root_dir: Path,
) -> WorkerAgentDirectories:
"""
Creates all required directories for Deadline Worker Agent.
This function creates the following directories:
Expand All @@ -394,6 +406,8 @@ def provision_directories(agent_username: str) -> WorkerAgentDirectories:
Parameters
agent_username(str): Worker Agent's username used for setting the permission for the directories
session_root_dir(Path): Path to the parent directory where the worker agent will create session directories
under
Returns
WorkerAgentDirectories: all directories created in the function
Expand Down Expand Up @@ -433,6 +447,18 @@ def provision_directories(agent_username: str) -> WorkerAgentDirectories:
os.makedirs(deadline_config_subdir, exist_ok=True)
logging.info(f"Done provisioning config directory ({deadline_config_subdir})")

logging.info(f"Porvisioning session root directory ({session_root_dir})")
os.makedirs(session_root_dir, exist_ok=True)
_set_windows_permissions(
path=session_root_dir,
user=agent_username,
user_permission=FileSystemPermissionEnum.FULL_CONTROL,
group="Administrators",
group_permission=FileSystemPermissionEnum.FULL_CONTROL,
agent_user_permission=None,
)
logging.info(f"Done provisioning session root directory ({session_root_dir})")

return WorkerAgentDirectories(
deadline_dir=Path(deadline_dir),
deadline_log_subdir=Path(deadline_log_subdir),
Expand Down Expand Up @@ -775,6 +801,7 @@ def start_windows_installer(
region: str,
allow_shutdown: bool,
parser: ArgumentParser,
session_root_dir: Path,
user_name: str = DEFAULT_WA_USER,
password: Optional[str] = None,
group_name: str = DEFAULT_JOB_GROUP,
Expand Down Expand Up @@ -854,6 +881,7 @@ def print_helping_info_and_exit():
f"Region: {region}\n"
f"Worker agent user: {user_name}\n"
f"Worker job group: {group_name}\n"
f"Session root directory: {session_root_dir}\n"
f"Allow worker agent shutdown: {allow_shutdown}\n"
f"Install Windows service: {install_service}\n"
f"Start service: {start_service}\n"
Expand Down Expand Up @@ -949,7 +977,7 @@ def print_helping_info_and_exit():
add_user_to_group(group_name, user_name)

# Create directories and configure their permissions
agent_dirs = provision_directories(user_name)
agent_dirs = provision_directories(agent_username=user_name, session_root_dir=session_root_dir)
update_config_file(
str(agent_dirs.deadline_config_subdir),
farm_id,
Expand All @@ -959,6 +987,7 @@ def print_helping_info_and_exit():
shutdown_on_stop=allow_shutdown,
allow_ec2_instance_profile=allow_ec2_instance_profile,
windows_job_user=windows_job_user,
session_root_dir=session_root_dir,
)

if telemetry_opt_out:
Expand Down
12 changes: 12 additions & 0 deletions test/integ/config/data/commented/session_root_dir/input.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[worker]

# The session root directory is a parent directory where worker agent creates per-session
# subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
# variable is set or the --session-root-dir command-line argument is specified.
#
# The default session root directory on POSIX systems is "/sessions" and on Windows systems is
# "C:\ProgramData\Amazon\OpenJD".
#
# Uncomment the line below and replace the value with your desired session root directory:
#
# session_root_dir = "/sessions"
12 changes: 12 additions & 0 deletions test/integ/config/data/commented/session_root_dir/output.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[worker]

# The session root directory is a parent directory where worker agent creates per-session
# subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
# variable is set or the --session-root-dir command-line argument is specified.
#
# The default session root directory on POSIX systems is "/sessions" and on Windows systems is
# "C:\ProgramData\Amazon\OpenJD".
#
# Uncomment the line below and replace the value with your desired session root directory:
#
session_root_dir = "/custom-session-root"
12 changes: 12 additions & 0 deletions test/integ/config/data/existing/session_root_dir/input.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[worker]

# The session root directory is a parent directory where worker agent creates per-session
# subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
# variable is set or the --session-root-dir command-line argument is specified.
#
# The default session root directory on POSIX systems is "/sessions" and on Windows systems is
# "C:\ProgramData\Amazon\OpenJD".
#
# Uncomment the line below and replace the value with your desired session root directory:
#
session_root_dir = "/sessions"
12 changes: 12 additions & 0 deletions test/integ/config/data/existing/session_root_dir/output.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[worker]

# The session root directory is a parent directory where worker agent creates per-session
# subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
# variable is set or the --session-root-dir command-line argument is specified.
#
# The default session root directory on POSIX systems is "/sessions" and on Windows systems is
# "C:\ProgramData\Amazon\OpenJD".
#
# Uncomment the line below and replace the value with your desired session root directory:
#
session_root_dir = "/custom-session-root"
7 changes: 7 additions & 0 deletions test/integ/config/data/missing/session_root_dir/input.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[worker]

# Some prior content that should be preserved
prior_setting_1 = 1

# More content to preserve
prior_setting_2 = "abc" # and here's an inline comment
18 changes: 18 additions & 0 deletions test/integ/config/data/missing/session_root_dir/output.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[worker]

# Some prior content that should be preserved
prior_setting_1 = 1

# More content to preserve
prior_setting_2 = "abc" # and here's an inline comment


# The session root directory is a parent directory where worker agent creates per-session
# subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
# variable is set or the --session-root-dir command-line argument is specified.
#
# The default session root directory on POSIX systems is "/sessions" and on Windows systems is
# "C:\ProgramData\Amazon\OpenJD".
#
# Uncomment the line below and replace the value with your desired session root directory:
session_root_dir = "/custom-session-root"
12 changes: 12 additions & 0 deletions test/integ/config/data/unset/session_root_dir/input.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[worker]

# The session root directory is a parent directory where worker agent creates per-session
# subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
# variable is set or the --session-root-dir command-line argument is specified.
#
# The default session root directory on POSIX systems is "/sessions" and on Windows systems is
# "C:\ProgramData\Amazon\OpenJD".
#
# Uncomment the line below and replace the value with your desired session root directory:
#
session_root_dir = "/custom-session-root"
12 changes: 12 additions & 0 deletions test/integ/config/data/unset/session_root_dir/output.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[worker]

# The session root directory is a parent directory where worker agent creates per-session
# subdirectories under. This value is overridden when the DEADLINE_WORKER_SESSION_ROOT_DIR environment
# variable is set or the --session-root-dir command-line argument is specified.
#
# The default session root directory on POSIX systems is "/sessions" and on Windows systems is
# "C:\ProgramData\Amazon\OpenJD".
#
# Uncomment the line below and replace the value with your desired session root directory:
#
# session_root_dir = "/custom-session-root"
Loading

0 comments on commit a3b033d

Please sign in to comment.