2010-11-05 15:54:15 +01:00
|
|
|
"""
|
|
|
|
Python part of radio playout (pypo)
|
|
|
|
"""
|
2020-01-16 15:32:51 +01:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
|
2021-06-03 15:20:39 +02:00
|
|
|
import importlib
|
2012-04-27 23:25:12 +02:00
|
|
|
import locale
|
2018-12-23 11:01:25 +01:00
|
|
|
import logging
|
2012-04-27 23:25:12 +02:00
|
|
|
import os
|
2013-05-13 21:45:56 +02:00
|
|
|
import re
|
2018-12-23 11:01:25 +01:00
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import telnetlib
|
|
|
|
import time
|
2021-06-03 15:20:39 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from optparse import OptionParser
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2021-10-18 09:44:39 +02:00
|
|
|
from api_clients.version1 import AirtimeApiClient as ApiClient
|
2018-12-23 11:01:25 +01:00
|
|
|
from configobj import ConfigObj
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2019-08-18 18:04:06 +02:00
|
|
|
try:
|
|
|
|
from queue import Queue
|
|
|
|
except ImportError: # Python 2.7.5 (CentOS 7)
|
2020-01-16 15:32:51 +01:00
|
|
|
from queue import Queue
|
2021-06-03 15:20:39 +02:00
|
|
|
|
2012-03-16 04:14:19 +01:00
|
|
|
from threading import Lock
|
|
|
|
|
2021-06-03 15:20:39 +02:00
|
|
|
from . import pure
|
2018-12-23 11:01:25 +01:00
|
|
|
from .listenerstat import ListenerStat
|
|
|
|
from .pypofetch import PypoFetch
|
|
|
|
from .pypofile import PypoFile
|
|
|
|
from .pypoliquidsoap import PypoLiquidsoap
|
|
|
|
from .pypomessagehandler import PypoMessageHandler
|
|
|
|
from .pypopush import PypoPush
|
|
|
|
from .recorder import Recorder
|
|
|
|
from .timeout import ls_timeout
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
LOG_PATH = "/var/log/airtime/pypo/pypo.log"
|
2015-05-23 00:20:45 +02:00
|
|
|
LOG_LEVEL = logging.INFO
|
2017-03-13 16:28:07 +01:00
|
|
|
logging.captureWarnings(True)
|
2015-05-23 00:20:45 +02:00
|
|
|
|
2010-11-08 22:54:54 +01:00
|
|
|
# Set up command-line options
|
2010-11-05 15:54:15 +01:00
|
|
|
parser = OptionParser()
|
|
|
|
|
2011-03-02 22:43:46 +01:00
|
|
|
# help screen / info
|
2010-11-05 15:54:15 +01:00
|
|
|
usage = "%prog [options]" + " - python playout system"
|
|
|
|
parser = OptionParser(usage=usage)
|
|
|
|
|
2010-11-08 22:54:54 +01:00
|
|
|
# Options
|
2018-12-23 11:01:25 +01:00
|
|
|
parser.add_option(
|
|
|
|
"-v",
|
|
|
|
"--compat",
|
|
|
|
help="Check compatibility with server API version",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
dest="check_compat",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_option(
|
|
|
|
"-t",
|
|
|
|
"--test",
|
|
|
|
help="Do a test to make sure everything is working properly.",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
dest="test",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_option(
|
|
|
|
"-b",
|
|
|
|
"--cleanup",
|
|
|
|
help="Cleanup",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
dest="cleanup",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_option(
|
|
|
|
"-c",
|
|
|
|
"--check",
|
|
|
|
help="Check the cached schedule and exit",
|
|
|
|
default=False,
|
|
|
|
action="store_true",
|
|
|
|
dest="check",
|
|
|
|
)
|
2010-11-05 15:54:15 +01:00
|
|
|
|
|
|
|
# parse options
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
2013-05-13 21:45:56 +02:00
|
|
|
LIQUIDSOAP_MIN_VERSION = "1.1.1"
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
PYPO_HOME = "/var/tmp/airtime/pypo/"
|
|
|
|
|
2012-04-21 00:32:10 +02:00
|
|
|
|
2015-01-28 19:19:50 +01:00
|
|
|
def configure_environment():
|
|
|
|
os.environ["HOME"] = PYPO_HOME
|
2018-12-23 11:01:25 +01:00
|
|
|
os.environ["TERM"] = "xterm"
|
|
|
|
|
2015-01-28 19:19:50 +01:00
|
|
|
|
|
|
|
configure_environment()
|
|
|
|
|
|
|
|
# need to wait for Python 2.7 for this..
|
|
|
|
logging.captureWarnings(True)
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2012-06-28 22:15:39 +02:00
|
|
|
# configure logging
|
|
|
|
try:
|
2015-05-23 00:20:45 +02:00
|
|
|
# Set up logging
|
2018-12-23 11:01:25 +01:00
|
|
|
logFormatter = logging.Formatter(
|
|
|
|
"%(asctime)s [%(module)s] [%(levelname)-5.5s] %(message)s"
|
|
|
|
)
|
2015-05-23 00:20:45 +02:00
|
|
|
rootLogger = logging.getLogger()
|
|
|
|
rootLogger.setLevel(LOG_LEVEL)
|
|
|
|
logger = rootLogger
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
fileHandler = logging.handlers.RotatingFileHandler(
|
|
|
|
filename=LOG_PATH, maxBytes=1024 * 1024 * 30, backupCount=8
|
|
|
|
)
|
2017-08-05 23:56:23 +02:00
|
|
|
fileHandler.setFormatter(logFormatter)
|
|
|
|
rootLogger.addHandler(fileHandler)
|
|
|
|
|
2015-05-23 00:20:45 +02:00
|
|
|
consoleHandler = logging.StreamHandler()
|
|
|
|
consoleHandler.setFormatter(logFormatter)
|
|
|
|
rootLogger.addHandler(consoleHandler)
|
2018-12-23 11:01:25 +01:00
|
|
|
except Exception as e:
|
2020-01-20 13:44:17 +01:00
|
|
|
print("Couldn't configure logging: {}".format(e))
|
2013-03-14 21:50:55 +01:00
|
|
|
sys.exit(1)
|
2012-06-28 22:15:39 +02:00
|
|
|
|
2010-11-05 15:54:15 +01:00
|
|
|
# loading config file
|
|
|
|
try:
|
2018-12-23 11:01:25 +01:00
|
|
|
config = ConfigObj("/etc/airtime/airtime.conf")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Error loading config file: %s", e)
|
2013-04-26 04:11:26 +02:00
|
|
|
sys.exit(1)
|
2011-03-02 22:43:46 +01:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2010-11-05 15:54:15 +01:00
|
|
|
class Global:
|
2013-05-06 17:42:39 +02:00
|
|
|
def __init__(self, api_client):
|
|
|
|
self.api_client = api_client
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-03-02 22:43:46 +01:00
|
|
|
def selfcheck(self):
|
2011-05-13 22:08:04 +02:00
|
|
|
return self.api_client.is_server_compatible()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2011-03-02 23:26:39 +01:00
|
|
|
def test_api(self):
|
|
|
|
self.api_client.test()
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2011-03-22 16:42:09 +01:00
|
|
|
def keyboardInterruptHandler(signum, frame):
|
2011-06-01 18:32:42 +02:00
|
|
|
logger = logging.getLogger()
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.info("\nKeyboard Interrupt\n")
|
2011-06-01 18:32:42 +02:00
|
|
|
sys.exit(0)
|
2011-03-02 23:26:39 +01:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2013-06-11 21:55:17 +02:00
|
|
|
@ls_timeout
|
2013-05-13 21:45:56 +02:00
|
|
|
def liquidsoap_get_info(telnet_lock, host, port, logger):
|
2012-06-28 22:15:39 +02:00
|
|
|
logger.debug("Checking to see if Liquidsoap is running")
|
|
|
|
try:
|
|
|
|
telnet_lock.acquire()
|
|
|
|
tn = telnetlib.Telnet(host, port)
|
|
|
|
msg = "version\n"
|
2020-01-23 11:37:49 +01:00
|
|
|
tn.write(msg.encode("utf-8"))
|
|
|
|
tn.write("exit\n".encode("utf-8"))
|
|
|
|
response = tn.read_all().decode("utf-8")
|
2018-12-23 11:01:25 +01:00
|
|
|
except Exception as e:
|
2020-01-23 11:37:49 +01:00
|
|
|
logger.error(e)
|
2013-05-13 21:45:56 +02:00
|
|
|
return None
|
2012-06-28 22:15:39 +02:00
|
|
|
finally:
|
|
|
|
telnet_lock.release()
|
|
|
|
|
2013-05-13 21:45:56 +02:00
|
|
|
return get_liquidsoap_version(response)
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2013-05-13 21:45:56 +02:00
|
|
|
def get_liquidsoap_version(version_string):
|
2013-05-17 18:14:33 +02:00
|
|
|
m = re.match(r"Liquidsoap (\d+.\d+.\d+)", version_string)
|
2013-05-13 21:45:56 +02:00
|
|
|
|
|
|
|
if m:
|
|
|
|
return m.group(1)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if m:
|
|
|
|
current_version = m.group(1)
|
|
|
|
return pure.version_cmp(current_version, LIQUIDSOAP_MIN_VERSION) >= 0
|
|
|
|
return False
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
|
2021-10-18 09:44:39 +02:00
|
|
|
def liquidsoap_startup_test(telnet_lock, ls_host, ls_port):
|
2013-05-13 21:45:56 +02:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
liquidsoap_version_string = liquidsoap_get_info(
|
|
|
|
telnet_lock, ls_host, ls_port, logger
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
while not liquidsoap_version_string:
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.warning(
|
|
|
|
"Liquidsoap doesn't appear to be running!, " + "Sleeping and trying again"
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
time.sleep(1)
|
2018-12-23 11:01:25 +01:00
|
|
|
liquidsoap_version_string = liquidsoap_get_info(
|
|
|
|
telnet_lock, ls_host, ls_port, logger
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
|
|
|
|
while pure.version_cmp(liquidsoap_version_string, LIQUIDSOAP_MIN_VERSION) < 0:
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.warning(
|
|
|
|
"Liquidsoap is running but in incorrect version! "
|
|
|
|
+ "Make sure you have at least Liquidsoap %s installed"
|
|
|
|
% LIQUIDSOAP_MIN_VERSION
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
time.sleep(1)
|
2018-12-23 11:01:25 +01:00
|
|
|
liquidsoap_version_string = liquidsoap_get_info(
|
|
|
|
telnet_lock, ls_host, ls_port, logger
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
|
|
|
|
logger.info("Liquidsoap version string found %s" % liquidsoap_version_string)
|
2012-06-28 22:15:39 +02:00
|
|
|
|
2011-03-02 22:43:46 +01:00
|
|
|
|
2021-10-18 09:44:39 +02:00
|
|
|
def run():
|
2018-12-23 11:01:25 +01:00
|
|
|
logger.info("###########################################")
|
|
|
|
logger.info("# *** pypo *** #")
|
|
|
|
logger.info("# Liquidsoap Scheduled Playout System #")
|
|
|
|
logger.info("###########################################")
|
2011-03-02 22:43:46 +01:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
# Although all of our calculations are in UTC, it is useful to know what timezone
|
|
|
|
# the local machine is, so that we have a reference for what time the actual
|
|
|
|
# log entries were made
|
2012-06-28 22:16:02 +02:00
|
|
|
logger.info("Timezone: %s" % str(time.tzname))
|
|
|
|
logger.info("UTC time: %s" % str(datetime.utcnow()))
|
2012-06-28 18:12:22 +02:00
|
|
|
|
2011-03-22 16:42:09 +01:00
|
|
|
signal.signal(signal.SIGINT, keyboardInterruptHandler)
|
2011-05-31 18:45:27 +02:00
|
|
|
|
2021-10-18 09:44:39 +02:00
|
|
|
api_client = ApiClient()
|
2013-05-06 17:42:39 +02:00
|
|
|
g = Global(api_client)
|
2011-05-13 22:08:04 +02:00
|
|
|
|
2012-06-28 22:15:39 +02:00
|
|
|
while not g.selfcheck():
|
|
|
|
time.sleep(5)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-05-06 17:42:39 +02:00
|
|
|
success = False
|
|
|
|
while not success:
|
|
|
|
try:
|
2018-12-23 11:01:25 +01:00
|
|
|
api_client.register_component("pypo")
|
2013-05-06 17:42:39 +02:00
|
|
|
success = True
|
2018-12-23 11:01:25 +01:00
|
|
|
except Exception as e:
|
2013-05-06 17:42:39 +02:00
|
|
|
logger.error(str(e))
|
|
|
|
time.sleep(10)
|
|
|
|
|
2012-06-28 22:15:39 +02:00
|
|
|
telnet_lock = Lock()
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
ls_host = config["pypo"]["ls_host"]
|
|
|
|
ls_port = config["pypo"]["ls_port"]
|
2013-05-13 21:45:56 +02:00
|
|
|
|
2021-10-18 09:44:39 +02:00
|
|
|
liquidsoap_startup_test(telnet_lock, ls_host, ls_port)
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2011-03-03 06:22:28 +01:00
|
|
|
if options.test:
|
|
|
|
g.test_api()
|
2013-04-26 04:11:26 +02:00
|
|
|
sys.exit(0)
|
2011-03-02 22:43:46 +01:00
|
|
|
|
2012-02-28 20:44:39 +01:00
|
|
|
pypoFetch_q = Queue()
|
2012-02-24 19:12:50 +01:00
|
|
|
recorder_q = Queue()
|
2012-02-28 20:44:39 +01:00
|
|
|
pypoPush_q = Queue()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
pypo_liquidsoap = PypoLiquidsoap(logger, telnet_lock, ls_host, ls_port)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-03-01 23:58:44 +01:00
|
|
|
"""
|
|
|
|
This queue is shared between pypo-fetch and pypo-file, where pypo-file
|
2013-04-26 04:11:26 +02:00
|
|
|
is the consumer. Pypo-fetch will send every schedule it gets to pypo-file
|
2012-03-01 23:58:44 +01:00
|
|
|
and pypo will parse this schedule to determine which file has the highest
|
2013-04-26 04:11:26 +02:00
|
|
|
priority, and retrieve it.
|
2012-03-01 23:58:44 +01:00
|
|
|
"""
|
|
|
|
media_q = Queue()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2014-12-17 01:42:07 +01:00
|
|
|
# Pass only the configuration sections needed; PypoMessageHandler only needs rabbitmq settings
|
2018-12-23 11:01:25 +01:00
|
|
|
pmh = PypoMessageHandler(pypoFetch_q, recorder_q, config["rabbitmq"])
|
2012-02-28 20:44:39 +01:00
|
|
|
pmh.daemon = True
|
|
|
|
pmh.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
pfile = PypoFile(media_q, config["pypo"])
|
2012-03-01 23:58:44 +01:00
|
|
|
pfile.daemon = True
|
|
|
|
pfile.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
pf = PypoFetch(
|
|
|
|
pypoFetch_q, pypoPush_q, media_q, telnet_lock, pypo_liquidsoap, config["pypo"]
|
|
|
|
)
|
2012-02-28 20:44:39 +01:00
|
|
|
pf.daemon = True
|
|
|
|
pf.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
pp = PypoPush(pypoPush_q, telnet_lock, pypo_liquidsoap, config["pypo"])
|
2011-08-23 17:13:06 +02:00
|
|
|
pp.daemon = True
|
2011-03-21 00:34:43 +01:00
|
|
|
pp.start()
|
|
|
|
|
2012-02-24 19:12:50 +01:00
|
|
|
recorder = Recorder(recorder_q)
|
|
|
|
recorder.daemon = True
|
|
|
|
recorder.start()
|
|
|
|
|
2012-11-07 23:19:47 +01:00
|
|
|
stat = ListenerStat(config)
|
2012-11-02 22:50:43 +01:00
|
|
|
stat.daemon = True
|
|
|
|
stat.start()
|
|
|
|
|
2015-05-23 00:20:45 +02:00
|
|
|
# Just sleep the main thread, instead of blocking on pf.join().
|
|
|
|
# This allows CTRL-C to work!
|
|
|
|
while True:
|
|
|
|
time.sleep(1)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-04-26 04:11:26 +02:00
|
|
|
logger.info("System exit")
|