Files
Iris/mopidy_iris/system.py

69 lines
2.0 KiB
Python
Raw Normal View History

2018-11-02 16:10:38 +13:00
from threading import Thread
import os, logging, 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
2018-11-02 21:44:16 +13:00
self.path = os.path.dirname(__file__)
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
logger.debug("sudo "+ self.path +"/system.sh "+ self.action)
proc = subprocess.Popen(["sudo", self.path+"/system.sh", 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-03 06:01:36 +13:00
process = subprocess.Popen("sudo -n "+self.path+"/system.sh check", 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:
2019-04-04 14:37:40 +13:00
raise Exception("Password-less access to "+self.path+"/system.sh was refused. Check your /etc/sudoers file.")
else:
return True