Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jobbergate-agent-snap): Fixed problem config hook #695

Closed
wants to merge 1 commit into from
Closed
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
41 changes: 24 additions & 17 deletions jobbergate-agent-snap/hooks/bin/configure
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import subprocess
import sys
from contextlib import contextmanager
from pathlib import Path
from typing import Union

Expand All @@ -28,25 +29,30 @@ AGENT_VARIABLES_MAP: dict[str, Union[str, int]] = {
}


@contextmanager
def handle_error(message: str):
"""Handle any errors encountered in this context manager."""
try:
yield
except Exception as exc:
sys.exit(f"Failed to {message} (from configure hook) -- {exc}")


def run_bash(bash_string: str) -> str:
"""Run bash command and return output as string."""
return subprocess.check_output(bash_string.split()).decode().rstrip()


def daemon_starter():
"""Start the daemon."""
try:
run_bash(f"snapctl start {SNAP_INSTANCE_NAME}.daemon")
except Exception:
sys.exit(1)
with handle_error(f"start {SNAP_INSTANCE_NAME}.daemon"):
run_bash(f"snapctl start --enable {SNAP_INSTANCE_NAME}.daemon")


def daemon_stopper():
"""Stop the daemon."""
try:
run_bash(f"snapctl stop {SNAP_INSTANCE_NAME}.daemon")
except Exception:
sys.exit(1)
with handle_error(f"stop {SNAP_INSTANCE_NAME}.daemon"):
run_bash(f"snapctl stop --disable {SNAP_INSTANCE_NAME}.daemon")


def snapctl_get(snap_config_value: str) -> Union[str, None]:
Expand All @@ -65,15 +71,16 @@ def snapctl_get(snap_config_value: str) -> Union[str, None]:

def configure_dotenv_files():
"""Configure the .env files based on the snap mode."""
env_file_content = ""
for env_var, env_value in AGENT_VARIABLES_MAP.items():
snapctl_value = snapctl_get(env_var.lower().replace("_", "-"))
if snapctl_value is not None:
env_value = snapctl_value
elif bool(env_value) is False:
continue
env_file_content += f"{DOTENV_PREFIX}{env_var}={env_value}\n"
DOTENV_FILE_LOCATION.write_text(env_file_content)
with handle_error(f"configure .env for {SNAP_INSTANCE_NAME}.daemon"):
env_file_content = ""
for env_var, env_value in AGENT_VARIABLES_MAP.items():
snapctl_value = snapctl_get(env_var.lower().replace("_", "-"))
if snapctl_value is not None:
env_value = snapctl_value
elif bool(env_value) is False:
continue
env_file_content += f"{DOTENV_PREFIX}{env_var}={env_value}\n"
DOTENV_FILE_LOCATION.write_text(env_file_content)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions jobbergate-agent-snap/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ parts:
- jobbergate-agent
build-packages:
- python3
- python3-distutils
- libapt-pkg-dev
- gcc
- g++
Expand Down