From 12f96f5043383905ff029e458bccb8b94b3cc881 Mon Sep 17 00:00:00 2001 From: jo Date: Tue, 18 Jan 2022 05:55:16 +0100 Subject: [PATCH] feat(playout): change playout working directory - use working dir for storing files BREAKING CHANGE: the playout working directory changed from '/var/tmp/airtime/pypo/' to '/var/lib/libretime/playout' when running with systemd and the current directory by default. --- install | 4 ++-- .../install/systemd/libretime-playout.service | 1 + playout/libretime_playout/config.py | 4 ++++ playout/libretime_playout/main.py | 16 +++++++--------- playout/libretime_playout/pypofetch.py | 16 +++------------- playout/libretime_playout/pypofile.py | 1 - playout/libretime_playout/recorder.py | 4 +++- 7 files changed, 20 insertions(+), 26 deletions(-) create mode 100644 playout/libretime_playout/config.py diff --git a/install b/install index 96e39d728..ddcd3616f 100755 --- a/install +++ b/install @@ -1052,6 +1052,7 @@ verbose "...Done" verbose "\n * Installing playout and liquidsoap..." loudCmd "$pip_cmd install ${AIRTIMEROOT}/playout" +mkdir_and_chown "${web_user}:${web_user}" "${LIBRETIME_WORKING_DIR}/playout" loudCmd "mkdir -p /var/log/airtime/{pypo,pypo-liquidsoap} /var/tmp/airtime/pypo/{cache,files,tmp} /var/tmp/airtime/show-recorder/" loudCmd "chown -R ${web_user}:${web_user} /var/log/airtime/{pypo,pypo-liquidsoap} /var/tmp/airtime/pypo/{cache,files,tmp} /var/tmp/airtime/show-recorder/" systemInitInstall libretime-liquidsoap "$web_user" @@ -1076,8 +1077,7 @@ verbose "...Done" verbose "\n * Installing libretime-analyzer..." loudCmd "$pip_cmd install ${AIRTIMEROOT}/analyzer" -loudCmd "mkdir -p ${LIBRETIME_WORKING_DIR}/analyzer" -loudCmd "chown -R ${web_user}:${web_user} ${LIBRETIME_WORKING_DIR}/analyzer" +mkdir_and_chown "${web_user}:${web_user}" "${LIBRETIME_WORKING_DIR}/analyzer" systemInitInstall libretime-analyzer "$web_user" verbose "...Done" diff --git a/playout/install/systemd/libretime-playout.service b/playout/install/systemd/libretime-playout.service index a40b64268..d7369c7a9 100644 --- a/playout/install/systemd/libretime-playout.service +++ b/playout/install/systemd/libretime-playout.service @@ -4,6 +4,7 @@ After=network-online.target [Service] Environment=LIBRETIME_LOG_FILEPATH=/var/log/libretime/playout.log +WorkingDirectory=/var/lib/libretime/playout ExecStart=/usr/local/bin/libretime-playout User=libretime-pypo diff --git a/playout/libretime_playout/config.py b/playout/libretime_playout/config.py new file mode 100644 index 000000000..9ff3f2080 --- /dev/null +++ b/playout/libretime_playout/config.py @@ -0,0 +1,4 @@ +from pathlib import Path + +CACHE_DIR = Path.cwd() / "scheduler" +RECORD_DIR = Path.cwd() / "recorder" diff --git a/playout/libretime_playout/main.py b/playout/libretime_playout/main.py index e46b684ee..485acabf4 100644 --- a/playout/libretime_playout/main.py +++ b/playout/libretime_playout/main.py @@ -24,6 +24,7 @@ from libretime_shared.logging import level_from_name, setup_logger from loguru import logger from . import pure +from .config import CACHE_DIR, RECORD_DIR from .listenerstat import ListenerStat from .pypofetch import PypoFetch from .pypofile import PypoFile @@ -35,13 +36,6 @@ from .timeout import ls_timeout LIQUIDSOAP_MIN_VERSION = "1.1.1" -PYPO_HOME = "/var/tmp/airtime/pypo/" - - -def configure_environment(): - os.environ["HOME"] = PYPO_HOME - os.environ["TERM"] = "xterm" - class Global: def __init__(self, api_client): @@ -117,13 +111,17 @@ def cli(log_level: str, log_filepath: Optional[Path]): """ setup_logger(level_from_name(log_level), log_filepath) - configure_environment() - # loading config file try: config = ConfigObj("/etc/airtime/airtime.conf") except Exception as e: logger.error("Error loading config file: %s", e) + + try: + for dir_path in [CACHE_DIR, RECORD_DIR]: + dir_path.mkdir(exist_ok=True) + except OSError as exception: + logger.error(exception) sys.exit(1) logger.info("###########################################") diff --git a/playout/libretime_playout/pypofetch.py b/playout/libretime_playout/pypofetch.py index 8a49c0baf..d92fc75c6 100644 --- a/playout/libretime_playout/pypofetch.py +++ b/playout/libretime_playout/pypofetch.py @@ -2,6 +2,7 @@ import copy import json import mimetypes import os +from pathlib import Path import signal import subprocess import sys @@ -17,6 +18,7 @@ from libretime_api_client import version2 as api_client from loguru import logger from . import pure +from .config import CACHE_DIR from .timeout import ls_timeout @@ -52,21 +54,9 @@ class PypoFetch(Thread): self.pypo_liquidsoap = pypo_liquidsoap - self.cache_dir = os.path.join(config["cache_dir"], "scheduler") + self.cache_dir = CACHE_DIR logger.debug("Cache dir %s", self.cache_dir) - try: - if not os.path.isdir(dir): - """ - We get here if path does not exist, or path does exist but - is a file. We are not handling the second case, but don't - think we actually care about handling it. - """ - logger.debug("Cache dir does not exist. Creating...") - os.makedirs(dir) - except Exception as e: - pass - self.schedule_data = [] logger.info("PypoFetch: init complete") diff --git a/playout/libretime_playout/pypofile.py b/playout/libretime_playout/pypofile.py index caec0d6fc..404bbaf99 100644 --- a/playout/libretime_playout/pypofile.py +++ b/playout/libretime_playout/pypofile.py @@ -24,7 +24,6 @@ class PypoFile(Thread): Thread.__init__(self) self.media_queue = schedule_queue self.media = None - self.cache_dir = os.path.join(config["cache_dir"], "scheduler") self._config = self.read_config_file(CONFIG_PATH) self.api_client = api_client.AirtimeApiClient() diff --git a/playout/libretime_playout/recorder.py b/playout/libretime_playout/recorder.py index f0448910e..23c325467 100644 --- a/playout/libretime_playout/recorder.py +++ b/playout/libretime_playout/recorder.py @@ -16,6 +16,8 @@ from configobj import ConfigObj from libretime_api_client.version1 import AirtimeApiClient as AirtimeApiClientV1 from loguru import logger +from libretime_playout.config import RECORD_DIR + def api_client(): """ @@ -74,7 +76,7 @@ class ShowRecorder(Thread): else: filetype = "ogg" - joined_path = os.path.join(config["pypo"]["base_recorded_files"], filename) + joined_path = os.path.join(RECORD_DIR, filename) filepath = "%s.%s" % (joined_path, filetype) br = config["pypo"]["record_bitrate"]