Pass common environment variable to scripts

This commit is contained in:
JonnyWong16 2018-03-16 18:37:50 -07:00
parent 2479533d07
commit ee041db63d
3 changed files with 47 additions and 34 deletions

View file

@ -933,3 +933,36 @@ def eval_logic_groups_to_bool(logic_groups, eval_conds):
result = result or eval_cond result = result or eval_cond
return result return result
def get_plexpy_url(hostname=None):
if plexpy.CONFIG.ENABLE_HTTPS:
scheme = 'https'
else:
scheme = 'http'
if hostname is None and plexpy.CONFIG.HTTP_HOST == '0.0.0.0':
import socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.connect(('<broadcast>', 0))
hostname = s.getsockname()[0]
except socket.error:
hostname = socket.gethostbyname(socket.gethostname())
if not hostname:
hostname = 'localhost'
else:
hostname = hostname or plexpy.CONFIG.HTTP_HOST
if plexpy.CONFIG.HTTP_PORT not in (80, 443):
port = ':' + str(plexpy.CONFIG.HTTP_PORT)
else:
port = ''
if plexpy.CONFIG.HTTP_ROOT.strip('/'):
root = '/' + plexpy.CONFIG.HTTP_ROOT.strip('/')
else:
root = ''
return scheme + '://' + hostname + port + root

View file

@ -2954,6 +2954,13 @@ class SCRIPTS(Notifier):
process.kill() process.kill()
self.script_killed = True self.script_killed = True
# Common environment variables
env = {'PLEX_URL': plexpy.CONFIG.PMS_URL,
'PLEX_TOKEN': plexpy.CONFIG.PMS_TOKEN,
'TAUTULLI_URL': helpers.get_plexpy_url(hostname='localhost'),
'TAUTULLI_APIKEY': plexpy.CONFIG.API_KEY
}
self.script_killed = False self.script_killed = False
output = error = '' output = error = ''
try: try:
@ -2961,7 +2968,8 @@ class SCRIPTS(Notifier):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=self.config['script_folder']) cwd=self.config['script_folder'],
env=env)
if self.config['timeout'] > 0: if self.config['timeout'] > 0:
timer = threading.Timer(self.config['timeout'], kill_script, (process,)) timer = threading.Timer(self.config['timeout'], kill_script, (process,))
@ -2969,11 +2977,13 @@ class SCRIPTS(Notifier):
timer = None timer = None
try: try:
if timer: timer.start() if timer:
timer.start()
output, error = process.communicate() output, error = process.communicate()
status = process.returncode status = process.returncode
finally: finally:
if timer: timer.cancel() if timer:
timer.cancel()
except OSError as e: except OSError as e:
logger.error(u"Tautulli Notifiers :: Failed to run script: %s" % e) logger.error(u"Tautulli Notifiers :: Failed to run script: %s" % e)

View file

@ -5284,34 +5284,4 @@ class WebInterface(object):
@cherrypy.tools.json_out() @cherrypy.tools.json_out()
@requireAuth() @requireAuth()
def get_plexpy_url(self, **kwargs): def get_plexpy_url(self, **kwargs):
if plexpy.CONFIG.ENABLE_HTTPS: return helpers.get_plexpy_url()
scheme = 'https'
else:
scheme = 'http'
# Have to return some hostname if socket fails even if 127.0.0.1 won't work
hostname = '127.0.0.1'
if plexpy.CONFIG.HTTP_HOST == '0.0.0.0':
import socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.connect(('<broadcast>', 0))
hostname = s.getsockname()[0]
except socket.error:
hostname = socket.gethostbyname(socket.gethostname())
else:
hostname = plexpy.CONFIG.HTTP_HOST
if plexpy.CONFIG.HTTP_PORT not in (80, 443):
port = ':' + str(plexpy.CONFIG.HTTP_PORT)
else:
port = ''
if plexpy.CONFIG.HTTP_ROOT.strip('/'):
root = '/' + plexpy.CONFIG.HTTP_ROOT.strip('/')
else:
root = ''
return scheme + '://' + hostname + port + root