87815216 |
import os.path |
2820c61a |
from Logger import Logger
from CommandUtils import CommandUtils
from constants import constants
class ChrootUtils(object): |
87815216 |
def __init__(self, logName=None, logPath=None): |
2820c61a |
if logName is None:
logName = "ChrootUtils"
if logPath is None:
logPath = constants.logPath |
87815216 |
self.logName = logName
self.logPath = logPath
self.logger = Logger.getLogger(logName, logPath)
def createChroot(self, chrootName):
chrootID = constants.buildRootPath + "/" + chrootName |
65584c99 |
if os.path.isdir(chrootID):
if not self.destroyChroot(chrootID): |
87815216 |
self.logger.error("Given chroot " + chrootID +
" is already exists. unable to destroy it ")
return False, None |
2820c61a |
# need to add timeout for this step
# http://stackoverflow.com/questions/1191374/subprocess-with-timeout |
87815216 |
cmdUtils = CommandUtils()
returnVal = cmdUtils.runCommandInShell("mkdir -p " + chrootID) |
65584c99 |
if not returnVal: |
87815216 |
self.logger.error("Unable to create chroot:" + chrootID + ".Unknown error.")
return False, None
return True, chrootID
def destroyChroot(self, chrootID): |
2820c61a |
# need to add timeout for this step
# http://stackoverflow.com/questions/1191374/subprocess-with-timeout |
87815216 |
cmdUtils = CommandUtils()
returnVal = cmdUtils.runCommandInShell("./clean-up-chroot.py " + chrootID) |
65584c99 |
if not returnVal: |
87815216 |
self.logger.error("Unable to destroy chroot:" + chrootID + ".Unknown error.") |
65584c99 |
return False |
87815216 |
returnVal = cmdUtils.runCommandInShell("rm -rf " + chrootID) |
65584c99 |
if not returnVal: |
87815216 |
self.logger.error("Unable to destroy chroot:" + chrootID + ".Unknown error.") |
2820c61a |
return False |
87815216 |
self.logger.info("Successfully destroyed chroot:" + chrootID) |
2820c61a |
return True |