Files
Iris/mopidy_iris/system.py

68 lines
2.0 KiB
Python
Raw Normal View History

2018-11-02 16:10:38 +13:00
from threading import Thread
2020-01-06 00:26:55 +00:00
import logging, pathlib, subprocess, json
# import logger
logger = logging.getLogger(__name__)
class IrisSystemThread(Thread):
2020-01-03 06:01:36 +13:00
def __init__(self, action, callback):
Thread.__init__(self)
2018-11-02 16:10:38 +13:00
self.action = action
2020-01-03 06:01:36 +13:00
self.callback = callback
2020-01-06 00:26:55 +00:00
self.script_path = pathlib.Path(__file__).parent / "system.sh"
2018-11-02 16:10:38 +13:00
##
# Run the defined action
##
def run(self):
logger.info("Running system action '"+self.action+"'")
2018-11-02 16:10:38 +13:00
try:
2019-04-04 14:37:40 +13:00
self.can_run()
except Exception as e:
2018-11-02 16:10:38 +13:00
logger.error(e)
2018-11-02 21:44:16 +13:00
error = {
'message': "Permission denied",
'description': str(e)
}
return {
'error': error
}
2018-11-02 21:44:16 +13:00
2020-01-06 00:26:55 +00:00
logger.debug("sudo %s %s", self.script_path, self.action)
2020-01-06 00:26:55 +00:00
proc = subprocess.Popen(["sudo", str(self.script_path), self.action],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout,stderr = proc.communicate()
if stderr:
logger.error(stderr.decode())
2020-01-03 06:01:36 +13:00
self.callback(None, { 'error': stderr.decode() })
else:
logger.info(stdout.decode())
2020-01-03 06:01:36 +13:00
self.callback({ 'output': stdout.decode() }, None)
##
2018-11-02 16:10:38 +13:00
# Check if we have access to the system script (system.sh)
#
# @return boolean or exception
##
2019-04-04 14:37:40 +13:00
def can_run(self, *args, **kwargs):
2018-11-02 21:44:16 +13:00
# Attempt an empty call to our system file
2020-01-06 00:26:55 +00:00
process = subprocess.Popen("sudo -n %s check" % self.script_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
result, error = process.communicate()
exitCode = process.wait()
# Some kind of failure, so we can't run any commands this way
if exitCode > 0:
2020-01-06 00:26:55 +00:00
raise Exception("Password-less access to %s was refused. Check your /etc/sudoers file." % self.script_path)
else:
return True