def runCommand(self):
        self.parseArgs(
            _("""
    execute-rule --app-name=APPNAME --rule-name=RULENAME [--data-file=DATAFILE]

Description:
    The execute-rule tool forces execution of a given rule in the Tortuga Rule
    Engine.
"""))
        application_name, rule_name = self.getApplicationNameAndRuleName()
        data_file = self.getOptions().dataFile
        application_data = ''
        if data_file:
            if not os.path.exists(data_file):
                raise FileNotFound(
                    _('Invalid application data file: {}').format(data_file))
            else:
                f = open(data_file, 'r')
                application_data = f.read()
                f.close()
                if not len(application_data):
                    raise InvalidCliRequest(_('Empty application data file.'))

        self.get_rule_api().executeRule(application_name, rule_name,
                                        application_data)
        print(_('Executed rule {}/{}').format(application_name, rule_name))
Example #2
0
def restoreFile(filePath):
    backupFilePath = __getBackupFileName(filePath)

    if not os.path.exists(backupFilePath):
        raise FileNotFound('Backup file for %s doe not exist' % filePath)

    shutil.move(backupFilePath, filePath)
Example #3
0
    def runCommand(self):
        self.parseArgs(_("""
    post-application-data --app-name=APPLICATIONNAME --data-file=DATAFILE

Description:
    The  post-application-data tool posts an XML file to the Tortuga Rule
    Engine web service as input for configured rules.
"""))
        application_name = self.getArgs().applicationName

        if not application_name:
            raise InvalidCliRequest(_('Missing application name.'))
        data_file = self.getArgs().dataFile

        if not data_file:
            raise InvalidCliRequest(_('Missing application data file.'))

        if not os.path.exists(data_file):
            raise FileNotFound(_('Invalid application data file: %s.') % data_file)

        f = open(data_file, 'r')
        application_data = f.read()
        f.close()

        if not len(application_data):
            raise InvalidCliRequest(_('Empty application data file.'))

        self.get_rule_api().postApplicationData(application_name,
                                                application_data)
Example #4
0
def backupFile(filePath):
    backupFilePath = __getBackupFileName(filePath)

    if not os.path.exists(filePath):
        raise FileNotFound('Path %s does not exist' % filePath)

    shutil.copy(filePath, backupFilePath)
Example #5
0
    def install_wheel_matching_filespec(self, whl_pathspec):
        # Find an whl matching the filespec
        whl_files = glob.glob(whl_pathspec)

        if not whl_files:
            raise FileNotFound('No files found matching spec %s' %
                               (whl_pathspec))

        # Use the first whl file found
        cmd = '%s/pip install %s' % (self._config.getBinDir(), whl_files[0])

        tortugaSubprocess.executeCommandAndIgnoreFailure(cmd)
Example #6
0
def cpio_copytree(src, dst):
    """
    A cpio-based copytree functionality. Only use this when
    shutil.copytree don't cut it.
    """

    # convert paths to be absolute
    src = os.path.abspath(src)
    dst = os.path.abspath(dst)

    if not os.path.exists(src):
        raise FileNotFound("Source directory does not exist!")

    createDir(dst)

    return os.system('cpio-helper.sh %s %s' % (src, dst))
Example #7
0
def retrieve(kitFileName, srcUrl, destDir):
    """
    :raises FileNotFound:

    """
    if checkSupportedScheme(srcUrl):
        # This is a urllib2 supported url scheme, download kit.
        kitUrl = assembleKitUrl(srcUrl, kitFileName)
        download([kitUrl], destDir)
    elif os.path.isdir(srcUrl):
        kitUrl = assembleKitUrl(srcUrl, kitFileName)
        copy(kitUrl, destDir)
    elif os.path.isfile(srcUrl) and \
            os.path.basename(srcUrl) == kitFileName:
        copy(srcUrl, destDir)
    else:
        raise FileNotFound('File [%s] not found at URL [%s]' %
                           (kitFileName, srcUrl))

    return assembleKitUrl(destDir, kitFileName)
Example #8
0
def copy(srcFile, destDir):
    """
    :raises FileNotFound:

    """

    destFile = '%s/%s' % (destDir, os.path.basename(srcFile))

    if os.path.exists(destFile) and filecmp.cmp(srcFile, destFile):
        logger.debug('Files [%s] and [%s] are the same, skipping copy.' % (
            srcFile, destFile))
    else:
        logger.debug('Copying [%s] to [%s]' % (srcFile, destFile))

        try:
            shutil.copy(srcFile, destDir)
        except Exception as ex:
            logger.debug('Copy failed, exception reported: %s' % ex)

            raise FileNotFound('Invalid kit at [%s]' % (srcFile))
Example #9
0
def download(urlList, dest):
    """
    TODO: this should use a curl/wget download module

    """
    logger = logging.getLogger('tortuga.kit.utils')
    logger.addHandler(logging.NullHandler())

    for url in urlList:
        i = url.rfind('/')

        destFile = dest + '/' + url[i + 1:]

        logger.debug(url + '->' + destFile)

        try:
            filein = urllib.request.urlopen(url)
        except urllib.error.URLError as ex:
            if ex.code == 404:
                raise FileNotFound('File not found at URL [%s]' % (url))

            raise TortugaException(exception=ex)
        except Exception as ex:
            raise TortugaException(exception=ex)

        fileout = open(destFile, "wb")

        while True:
            try:
                buf = filein.read(1024000)

                fileout.write(buf)
            except IOError as ex:
                raise TortugaException(exception=ex)

            if not bytes:
                break

        filein.close()
        fileout.close()
        logger.debug('Successfully dowloaded file [%s]' % (destFile))
Example #10
0
    def parse(self, ruleXmlFile):
        """
        Parse rule XML file and return rule object.

        Raises:
            InvalidXml
            FileNotFound
        """

        if not os.path.exists(ruleXmlFile):
            raise FileNotFound('File %s is not found' % (ruleXmlFile))

        try:
            self._logger.debug('Parsing: %s' % (ruleXmlFile))

            xmlDoc = minidom.parse(ruleXmlFile)

            return self.__buildRule(xmlDoc)
        except TortugaException as ex:
            raise
        except Exception as ex:
            raise InvalidXml(
                'Could not parse XML file %s (%s)' % (ruleXmlFile, ex))
Example #11
0
def __getFileNameFromBackup(filePath):
    if filePath is None:
        raise FileNotFound('Invalid path specified')

    return filePath.rsplit(BACKUP_FILE_SUFFIX, 1)[0]
Example #12
0
def __getBackupFileName(filePath):
    if filePath is None:
        raise FileNotFound('Invalid path specified')

    return '%s%s' % (filePath, BACKUP_FILE_SUFFIX)