def undo(self):
        if self._dryrun:
            return True
        if not self._executed:
            return False
        Log.cout(Log.INFO, 'Undo deactivate pkg %s ...' % self._pkg)

        if self._success:
            if not self._noExecute and not self._processScriptByName('pre-activate'):
                return False

        # roll back unlink from root
        for dest in self._rmdirList:
            if not file_util.makeDir(dest):
                return False
        for src, dest in self._unlinkList:
            if not file_util.link(src, dest):
                return False
        for src, dest in self._confDict.values():
            if not file_util.move(src, dest):
                return False

        if self._modifyDb and not self._addFileToDb():
                return False

        if self._crontabSrcDest:
            cronFilePath, tmpPath = self._crontabSrcDest
            if not file_util.move(tmpPath, cronFilePath):
                return False
        
        # roll back unlink from active
        if self._lastActivePkg:
            if not file_util.symLink(self._lastActivePkg, self._ainstActivePkg):
                return False

        if self._success:
            if not self._noExecute and not self._processScriptByName('post-activate'):
                return False

        # roll back stop
        if not self._noExecute and not self._noStop and self._aicfInfo and\
                self._aicfInfo.autoStart and self._aicfInfo.scripts.has_key('start'):
            scriptContent = self._aicfInfo.scripts['start']
            if scriptContent:
                self._exportToEnv(self._ainstPathEnv)
                self._exportToEnv(self._settingsEnv)
                self._processScript(scriptContent)
                self._removeFromEnv(self._settingsEnv)
                self._removeFromEnv(self._ainstPathEnv)

        self._init()
        return True
 def _generateConfigToRoot(self, ainstPkgDir, aicfInfo, settingMap, confDict):
     if aicfInfo:
         for path, configInfo in aicfInfo.configs.iteritems():
             srcConfigPath = ainstPkgDir + '/' + path
             destConfigPath = self._ainstRoot.getRoot() + '/' + path
             if not file_util.isFile(srcConfigPath):
                 Log.cout(Log.ERROR, 'Config file %s is not exists' % srcConfigPath)
                 return False
             if not file_util.exists(destConfigPath):
                 Log.cout(Log.ERROR, 'Dest config file %s is not exists' % destConfigPath)
                 return False
             tmpDirName = self._ainstRoot.getRootVarAinstDir('tmp')
             tmpPath = tmpDirName + '/' + os.path.basename(destConfigPath) + '.tmp.set'
             if not file_util.move(destConfigPath, tmpPath):
                 Log.cout(Log.ERROR, 'Backup config file %s failed' % destConfigPath)
                 return False
             confDict[path] = (tmpPath, destConfigPath)
             configGenerator = ConfigGenerator()
             if not configGenerator.generateConfig(srcConfigPath, 
                                                   destConfigPath, 
                                                   configInfo.mode, 
                                                   configInfo.noReplace,
                                                   settingMap):
                 Log.cout(Log.ERROR, 'Generate Config file %s failed' % path)
                 return False
     else:
         Log.cout(Log.DEBUG, 'No aicf file, so no config will be changed')
     return True
 def _recoverPkgDir(self):
     if not self._tmpPkgPath:
         return True
     if not file_util.isDir(self._tmpPkgPath) or\
             not file_util.move(self._tmpPkgPath, self._ainstPkgDir):
         return False
     return True
 def _recoverSettings(self):
     if not self._tmpSettingPath:
         return True
     if not file_util.isFile(self._tmpSettingPath) or\
             not file_util.move(self._tmpSettingPath, self._settingPath):
         return False
     return True
 def _removeCrontabFile(self):
     cronFilePath = self._ainstRoot.getRootVarDir('cron') + self._pkg.name
     if file_util.exists(cronFilePath):
         tmpDirName = self._ainstRoot.getRootVarAinstDir('tmp')
         tmpPath = tmpDirName + '/' + self._pkg.name + '.crontab.tmp'
         if not file_util.move(cronFilePath, tmpPath):
             return False
         self._crontabSrcDest = (cronFilePath, tmpPath)
     return True
Exemple #6
0
    def execute(self):
        if self._executed:
            return False
        self._executed = True

        Log.cout(Log.INFO, 'Save root state...')

        if not self._ainstRoot.checkRoot():
            Log.cout(Log.ERROR, 'Check ainst root failed')
            return False

        saveDir = self._ainstRoot.getRootVarAinstDir('save')
        settingsDir = self._ainstRoot.getRootVarAinstDir('settings')
        tmpDir = self._ainstRoot.getRootVarAinstDir('tmp')
        if not saveDir or not settingsDir or not tmpDir:
            Log.cout(Log.ERROR,
                     'There is not save, settings or tmp dir in installroot')
            return False

        if not self._ainstRoot.isValidAinstRoot():
            Log.cout(Log.ERROR,
                     '%s is invalid ainst root' % self._ainstRoot.getRoot())
            return False
        reader = AinstRootReader(self._ainstRoot)
        activePkgs = reader.getActivePackages()
        activePkgNames = [pkgName for pkgName, pkgVer, mtime in activePkgs]
        activePkgInfos = [(pkgVer, mtime)
                          for pkgName, pkgVer, mtime in activePkgs]
        activePkgInfos = sorted(activePkgInfos, key=lambda pkg: pkg[1])
        pkgSettings = reader.getPkgSettings(activePkgNames)

        state = RootState(time.time(), self._command,
                          self._ainstRoot.getRoot(), common.AINST_VERSION,
                          activePkgInfos, pkgSettings)
        content = RootStateStreamer().toString(state)
        if not content:
            Log.cout(Log.ERROR, 'Generate root state content failed')
            return False

        stateFileName = reader.getNextStateFileName()
        self._tmpFile = tmpDir + stateFileName
        if not file_util.writeToFile(self._tmpFile, content):
            Log.cout(Log.ERROR, 'Write state to %s failed' % self._tmpFile)
            return False

        saveFile = saveDir + stateFileName
        if self._saveFile:
            saveFile = self._saveFile

        if not file_util.move(self._tmpFile, saveFile):
            Log.cout(Log.ERROR, 'Move root state file %s failed' % saveFile)
            self.undo()
            return False

        self._stateFileName = saveFile
        return True
 def _removeSettings(self):
     if not file_util.isFile(self._settingPath):
         return True
     tmpPath = self._ainstRoot.getRootVarAinstDir('tmp') +\
         self._pkg.name + self._settingTmpSuffix
     if not file_util.remove(tmpPath) or\
             not file_util.move(self._settingPath, tmpPath):
         return False
     self._tmpSettingPath = tmpPath
     return True
    def _removePkgDir(self, pkgDirName):
        if not file_util.isDir(self._ainstPkgDir):
            return True

        tmpPkgPath = self._ainstRoot.getRootVarAinstDir('tmp') +\
            pkgDirName + self._pkgTmpSuffix
        if not file_util.remove(tmpPkgPath) or\
                not file_util.move(self._ainstPkgDir, tmpPkgPath):
            return False
        self._tmpPkgPath = tmpPkgPath
        return True
    def execute(self):
        if self._executed:
            return False
        self._executed = True

        Log.cout(Log.INFO, 'Save root state...')

        if not self._ainstRoot.checkRoot():
            Log.cout(Log.ERROR, 'Check ainst root failed')
            return False

        saveDir = self._ainstRoot.getRootVarAinstDir('save')
        settingsDir = self._ainstRoot.getRootVarAinstDir('settings')
        tmpDir = self._ainstRoot.getRootVarAinstDir('tmp')
        if not saveDir or not settingsDir or not tmpDir:
            Log.cout(Log.ERROR, 'There is not save, settings or tmp dir in installroot')
            return False

        if not self._ainstRoot.isValidAinstRoot():
            Log.cout(Log.ERROR, '%s is invalid ainst root' % self._ainstRoot.getRoot())
            return False
        reader = AinstRootReader(self._ainstRoot)
        activePkgs = reader.getActivePackages()
        activePkgNames = [pkgName for pkgName, pkgVer, mtime in activePkgs]
        activePkgInfos = [(pkgVer, mtime) for pkgName, pkgVer, mtime in activePkgs]
        activePkgInfos = sorted(activePkgInfos, key = lambda pkg : pkg[1])
        pkgSettings = reader.getPkgSettings(activePkgNames)

        state = RootState(time.time(), self._command, self._ainstRoot.getRoot(),
                          common.AINST_VERSION, activePkgInfos, pkgSettings)
        content = RootStateStreamer().toString(state)
        if not content:
            Log.cout(Log.ERROR, 'Generate root state content failed')
            return False
            
        stateFileName = reader.getNextStateFileName()
        self._tmpFile = tmpDir + stateFileName
        if not file_util.writeToFile(self._tmpFile, content):
            Log.cout(Log.ERROR, 'Write state to %s failed' % self._tmpFile)
            return False

        saveFile = saveDir + stateFileName
        if self._saveFile:
            saveFile = self._saveFile

        if not file_util.move(self._tmpFile, saveFile):
            Log.cout(Log.ERROR, 'Move root state file %s failed' % saveFile)
            self.undo()
            return False

        self._stateFileName = saveFile
        return True
 def _unlinkPkgFromRoot(self, rpmFileInfoList, unlinkList, rmdirList, confDict):
     if self._aicfInfo:
         for path in self._aicfInfo.configs.keys():
             destConfigPath = self._ainstRoot.getRoot() + '/' + path
             if file_util.isFile(destConfigPath):
                 tmpDirName = self._ainstRoot.getRootVarAinstDir('tmp')
                 tmpPath = tmpDirName + '/' + os.path.basename(destConfigPath) + '.tmp'
                 if not file_util.move(destConfigPath, tmpPath):
                     return False
                 confDict[path] = (tmpPath, destConfigPath)
                 dirName = os.path.dirname(destConfigPath) + '/'
                 if not self._removeDir(dirName, rmdirList):
                     return False
     for rpmFileInfo in rpmFileInfoList:
         srcPath = self._ainstPkgDir + '/' + rpmFileInfo.relativePath
         destPath = self._ainstRoot.getRoot() + '/' + rpmFileInfo.relativePath
         if rpmFileInfo.isDir:
             if not self._removeDir(destPath, rmdirList, False):
                 return False
         elif not rpmFileInfo.isConfigFile():
             if not file_util.remove(destPath):
                 return False
             unlinkList.append((srcPath, destPath))
             if not self._removeDir(os.path.dirname(destPath) + '/', rmdirList):
                 return False
         else:
             if confDict.has_key(rpmFileInfo.relativePath):
                 continue
             if file_util.isFile(destPath):
                 dirName = self._ainstRoot.getRootVarAinstDir('tmp')
                 tmpPath = dirName + os.path.basename(destPath) + '.tmp'
                 if not file_util.move(destPath, tmpPath):
                     return False
                 confDict[rpmFileInfo.relativePath] = (tmpPath, destPath)
                 if not self._removeDir(os.path.dirname(destPath) + '/', rmdirList):
                     return False
     return True
Exemple #11
0
    def execute(self):
        Log.cout(Log.INFO, 'Install pkg %s ...' % self._pkg)
        if self._dryrun:
            return True

        if self._executed:
            Log.cout(Log.DEBUG, 'Install %s has always executed' % self._pkg)
            return False
        self._executed = True

        ret, self._rootInitTrace = self._ainstRoot.init()
        if not ret:
            Log.cout(Log.ERROR, 'Init ainst root failed')
            return False

        if not self._ainstRoot.checkRoot():
            Log.cout(Log.ERROR, 'Check ainst root failed')
            return False

        pkgDirName = PackageUtil.getPkgNameVersion(self._pkg)
        if not pkgDirName:
            Log.cout(Log.ERROR, 'Get pkg %s dir name failed' % self._pkg)
            return False

        self._ainstPkgDir = self._ainstRoot.getRootVarAinstDir(
            'packages') + pkgDirName
        if file_util.isDir(self._ainstPkgDir):
            Log.cout(Log.INFO, 'Package %s is always installed' % self._pkg)
            return True

        self._ainstTmpPkgDir = self._ainstRoot.getRootVarAinstDir(
            'tmp') + pkgDirName
        if not self._installInTmpDir(self._pkg, self._ainstTmpPkgDir):
            Log.cout(Log.ERROR,
                     'Install pkg %s to tmp dir failed' % pkgDirName)
            self.undo()
            return False

        if not file_util.move(self._ainstTmpPkgDir, self._ainstPkgDir):
            Log.cout(Log.ERROR,
                     'Move pkg %s to packages dir failed' % self._pkg)
            self.undo()
            return False

        self._success = True
        Log.cout(Log.DEBUG, 'Install pkg %s success' % self._pkg)
        return True
    def undo(self):
        if not self._executed:
            return False

        if self._bakSettings is not None:
            settingPath = self._ainstRoot.getRootVarAinstDir('settings') + self._pkg.name
            if not SettingStreamer().dump(self._bakSettings, settingPath):
                Log.cout(Log.ERROR, 'Rollback settings of pkg %s failed' % self._pkg.name)
                return False

        for src, dest in self._confBakDict.values():
            if not file_util.move(src, dest):
                return False
        
        self._confBakDict = {}
        self._bakSettings = None
        self._executed = False
        return True
    def execute(self):
        Log.cout(Log.INFO, 'Install pkg %s ...' % self._pkg)
        if self._dryrun:
            return True

        if self._executed:
            Log.cout(Log.DEBUG, 'Install %s has always executed' % self._pkg)
            return False
        self._executed = True

        ret, self._rootInitTrace = self._ainstRoot.init()
        if not ret:
            Log.cout(Log.ERROR, 'Init ainst root failed')
            return False

        if not self._ainstRoot.checkRoot():
            Log.cout(Log.ERROR, 'Check ainst root failed')
            return False

        pkgDirName = PackageUtil.getPkgNameVersion(self._pkg)
        if not pkgDirName:
            Log.cout(Log.ERROR, 'Get pkg %s dir name failed' % self._pkg)
            return False

        self._ainstPkgDir = self._ainstRoot.getRootVarAinstDir('packages') + pkgDirName
        if file_util.isDir(self._ainstPkgDir):
            Log.cout(Log.INFO, 'Package %s is always installed' % self._pkg)
            return True

        self._ainstTmpPkgDir = self._ainstRoot.getRootVarAinstDir('tmp') + pkgDirName
        if not self._installInTmpDir(self._pkg, self._ainstTmpPkgDir):
            Log.cout(Log.ERROR, 'Install pkg %s to tmp dir failed' % pkgDirName)
            self.undo()
            return False

        if not file_util.move(self._ainstTmpPkgDir, self._ainstPkgDir):
            Log.cout(Log.ERROR, 'Move pkg %s to packages dir failed' % self._pkg)
            self.undo()
            return False

        self._success = True
        Log.cout(Log.DEBUG, 'Install pkg %s success' % self._pkg)
        return True
    def undo(self):
        if not self._executed:
            return False

        if self._bakSettings is not None:
            settingPath = self._ainstRoot.getRootVarAinstDir(
                'settings') + self._pkg.name
            if not SettingStreamer().dump(self._bakSettings, settingPath):
                Log.cout(Log.ERROR,
                         'Rollback settings of pkg %s failed' % self._pkg.name)
                return False

        for src, dest in self._confBakDict.values():
            if not file_util.move(src, dest):
                return False

        self._confBakDict = {}
        self._bakSettings = None
        self._executed = False
        return True