Files
Iris/mopidy_iris/system.py

61 lines
1.8 KiB
Python
Raw Normal View History

2018-11-02 16:10:38 +13:00
from threading import Thread
import os, logging, subprocess
# import logger
logger = logging.getLogger(__name__)
2018-11-02 16:10:38 +13:00
class IrisSystemThread(Thread):
def __init__(self, action, callback):
Thread.__init__(self)
self.action = action
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)
try:
2019-04-04 14:37:40 +13:00
self.can_run()
2018-11-02 16:10:38 +13:00
except Exception, e:
logger.error(e)
2018-11-02 21:44:16 +13:00
error = {
'message': "Permission denied",
'description': str(e)
}
2018-11-02 16:10:38 +13:00
if self.callback:
2018-11-02 21:44:16 +13:00
self.callback(False, error)
2019-04-04 14:37:40 +13:00
return
2018-11-02 16:10:38 +13:00
# Run the actual task (this is the process-blocking instruction)
2019-04-04 14:37:40 +13:00
output = subprocess.check_output(["sudo -n "+self.path+"/system.sh "+self.action], shell=True)
2018-11-02 16:10:38 +13:00
# And then, when complete, return to our callback
if self.callback:
response = {
'output': output
}
2018-11-02 16:10:38 +13:00
self.callback(response, False)
##
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
2019-04-04 14:37:40 +13:00
# If this fails, we can't run any commands this way
try:
subprocess.check_output("sudo -n "+self.path+"/system.sh", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
2018-11-02 16:10:38 +13:00
return True
2019-04-04 14:37:40 +13:00
except Exception, e:
raise Exception("Password-less access to "+self.path+"/system.sh was refused. Check your /etc/sudoers file.")
return False