Esempio n. 1
0
File: config.py Progetto: idiap/KiSC
    def resolveFile(self, _sFile_from, _sFile_to, _mHost = None, _mResource = None, _bBootstrap = False, _tPermissions = None):
        """
        Copy the given source file to the given destination, resolving cluster variables in their content

        This method will substitute cluster variables found in the file; see self.resolveString().

        @param  str  _sFile_from    Source file (standard input if None)
        @param  str  _sFile_to      Destination file (standard output if None)
        @param  str  _sHost_id      Target host ID (substition for '$HOST' magic ID)
        @param  str  _sResource_id  Target resource ID (substition for '$SELF' magic ID)
        @param  bool _bBootstrap    Whether to consider bootstrap (host startup) resources
        @param  tup  _tPermissions  Permissions tuple (<user>, <group>, <mode>)

        @exception OSError       On file I/O error
        @exception RuntimeError  On cluster variables subsitution errors
        """
        if self._iVerbose: self._INFO('Caching file: %s > %s' % (_sFile_from, _sFile_to))

        # Load source file
        if self._iVerbose: self._DEBUG('Reading file (%s)' % _sFile_from)
        if _sFile_from is None:
            sFile = sys.stdin.read()
        else:
            with open(_sFile_from, 'r') as oFile:
                sFile = oFile.read()

        # Resolve variables
        sFile = self.resolveString(sFile, _mHost, _mResource, _bBootstrap)

        # Save destination file
        if self._iVerbose: self._DEBUG('Writing file (%s)' % _sFile_to)
        if _sFile_to is None:
            sys.stdout.write(sFile)
        else:
            os.makedirs(os.path.dirname(_sFile_to), exist_ok=True)
            iUmask = os.umask(0o077)
            oFile = None
            try:
                oFile = open(_sFile_to, 'w')
                oFile.write(sFile)
                if _tPermissions is not None:
                    (mUser, mGroup, mMode) = _tPermissions
                    KiscRuntime.perms(oFile.fileno(), mUser, mGroup, mMode, _bTrace = self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
            finally:
                if oFile: oFile.close()
                os.umask(iUmask)
Esempio n. 2
0
    def start(self):
        if self._iVerbose: self._INFO('Starting')
        lsErrors = list()

        # Be idempotent
        if self.status(
                True,
                KiscRuntime.STATUS_STARTED) == KiscRuntime.STATUS_STARTED:
            if self._iVerbose: self._INFO('Already started')
            return lsErrors

        # Start the resource
        try:

            # ... mkdir ?
            if KiscRuntime.parseBool(self._dsConfig.get('mkdir', True)):
                os.makedirs(os.path.dirname(self._dsConfig['destination']),
                            exist_ok=True)

            # ... pre-cache command ?
            if 'command_pre' in self._dsConfig:
                KiscRuntime.shell(
                    self._dsConfig['command_pre'].split(' '),
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... permissions
            mUser = self._dsConfig.get('user', None)
            mGroup = self._dsConfig.get('group', None)
            try:
                mMode = int(self._dsConfig['mode'], 8)
            except KeyError:
                mMode = None

            # ... cache file
            if 'config_file' in self._dsConfig:
                oClusterConfig = KiscCluster_config(
                    self._dsConfig['config_file'])
                sHost_id = oClusterConfig.getHostByHostname().id()
                oClusterConfig.resolveFile(self._dsConfig['source'],
                                           self._dsConfig['destination'],
                                           _sHost_id=sHost_id,
                                           _tPermissions=(mUser, mGroup,
                                                          mMode))
            else:
                if self._iVerbose:
                    self._DEBUG('Reading file (%s)' % self._dsConfig['source'])
                with open(self._dsConfig['source'], 'r') as oFile:
                    sFile = oFile.read()
                if self._iVerbose:
                    self._DEBUG('Writing file (%s)' %
                                self._dsConfig['destination'])
                iUmask = os.umask(0o077)
                oFile = None
                try:
                    oFile = open(self._dsConfig['destination'], 'w')
                    KiscRuntime.perms(
                        oFile.fileno(),
                        mUser,
                        mGroup,
                        mMode,
                        _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)
                    oFile.write(sFile)
                finally:
                    if oFile: oFile.close()
                    os.umask(iUmask)

            # ... post-cache command ?
            if 'command_post' in self._dsConfig:
                KiscRuntime.shell(
                    self._dsConfig['command_post'].split(' '),
                    _bTrace=self._iVerbose >= KiscRuntime.VERBOSE_TRACE)

            # ... done
            self._iStatus = KiscRuntime.STATUS_STARTED
            if self._iVerbose: self._INFO('Started')

        except (OSError, RuntimeError) as e:
            if self._iVerbose: self._ERROR(str(e))
            self._iStatus = KiscRuntime.STATUS_ERROR
            lsErrors.append(str(e))

        # Done
        return lsErrors