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
|
2018-11-02 10:20:32 +13:00
|
|
|
|
|
|
|
|
# import logger
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2020-01-08 01:34:44 +00:00
|
|
|
|
|
|
|
|
class IrisSystemError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IrisSystemPermissionError(IrisSystemError):
|
|
|
|
|
reason = "Permission denied"
|
|
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
|
message = "Password-less access to %s was refused. Check your /etc/sudoers file." % path.as_uri()
|
|
|
|
|
super().__init__(message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IrisSystemMissingError(IrisSystemError):
|
|
|
|
|
reason = "Not found"
|
|
|
|
|
|
|
|
|
|
def __init__(self, path):
|
|
|
|
|
message = "Unable to access %s." % path.as_uri()
|
|
|
|
|
super().__init__(message)
|
|
|
|
|
|
|
|
|
|
|
2020-01-01 21:41:38 +13:00
|
|
|
class IrisSystemThread(Thread):
|
2020-01-03 06:01:36 +13:00
|
|
|
def __init__(self, action, callback):
|
2020-01-01 21:41:38 +13:00
|
|
|
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
|
|
|
|
|
##
|
2020-01-01 21:41:38 +13:00
|
|
|
def run(self):
|
2019-12-30 21:13:15 +13:00
|
|
|
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()
|
2020-01-08 01:34:44 +00:00
|
|
|
except IrisSystemError as e:
|
2018-11-02 16:10:38 +13:00
|
|
|
logger.error(e)
|
|
|
|
|
|
2018-11-02 21:44:16 +13:00
|
|
|
error = {
|
2020-01-08 01:34:44 +00:00
|
|
|
'message': e.reason,
|
|
|
|
|
'description': e.message
|
2018-11-02 21:44:16 +13:00
|
|
|
}
|
2020-01-01 12:13:18 +13:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'error': error
|
|
|
|
|
}
|
2018-11-02 21:44:16 +13:00
|
|
|
|
2020-01-08 01:34:44 +00:00
|
|
|
logger.debug("sudo %s %s", self.script_path.as_uri(), self.action)
|
2019-12-30 21:13:15 +13:00
|
|
|
|
2020-01-08 01:34:44 +00:00
|
|
|
proc = subprocess.Popen([b"sudo", bytes(self.script_path), self.action.encode()],
|
2020-01-01 12:13:18 +13:00
|
|
|
stdout=subprocess.PIPE,
|
2020-01-08 01:34:44 +00:00
|
|
|
stderr=subprocess.PIPE)
|
2020-01-01 12:13:18 +13:00
|
|
|
|
|
|
|
|
stdout,stderr = proc.communicate()
|
2019-12-30 21:13:15 +13:00
|
|
|
|
2020-01-01 12:13:18 +13:00
|
|
|
if stderr:
|
2020-01-08 01:34:44 +00:00
|
|
|
error_string = os.fsdecode(stderr)
|
|
|
|
|
logger.error(error_string)
|
|
|
|
|
self.callback(None, {'error': error_string})
|
2020-01-01 12:13:18 +13:00
|
|
|
else:
|
2020-01-08 01:34:44 +00:00
|
|
|
response_string = os.fsdecode(stdout)
|
|
|
|
|
logger.info(response_string)
|
|
|
|
|
self.callback({'output': response_string}, None)
|
2020-01-01 21:41:38 +13:00
|
|
|
|
2018-11-02 10:20:32 +13:00
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
2018-11-02 16:10:38 +13:00
|
|
|
# Check if we have access to the system script (system.sh)
|
|
|
|
|
#
|
|
|
|
|
# @return boolean or exception
|
2018-11-02 10:20:32 +13:00
|
|
|
##
|
2019-04-04 14:37:40 +13:00
|
|
|
def can_run(self, *args, **kwargs):
|
2020-01-08 01:34:44 +00:00
|
|
|
if not self.script_path.is_file():
|
|
|
|
|
raise IrisSystemMissingError(self.script_path)
|
2018-11-02 10:20:32 +13:00
|
|
|
|
2018-11-02 21:44:16 +13:00
|
|
|
# Attempt an empty call to our system file
|
2020-01-08 01:34:44 +00:00
|
|
|
process = subprocess.Popen(b"sudo -n %s check" % bytes(self.script_path), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
|
2019-04-10 16:33:41 +12:00
|
|
|
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-08 01:34:44 +00:00
|
|
|
raise IrisSystemPermissionError(self.script_path)
|
2019-04-10 16:33:41 +12:00
|
|
|
else:
|
|
|
|
|
return True
|