def init(self): if self._inited: return True, None if not self._root: return False, None markFilePath = self._root + '/' + self._ainstRootMarkFile initTrace = [] if not file_util.isDir(self._root): if not file_util.makeDir(self._root): self.clearInit(initTrace) return False, None initTrace.append(self._root) if not file_util.writeToFile(markFilePath, ''): self.clearInit(initTrace) return False, None initTrace.append(markFilePath) else: if not file_util.isFile(markFilePath): subDirs = file_util.listDir(self._root) if subDirs and len(subDirs) > 0: self.clearInit(initTrace) return False, None if not file_util.writeToFile(markFilePath, ''): self.clearInit(initTrace) return False, None initTrace.append(markFilePath) if not self._initRoot(initTrace): return False, None os.environ[self._installRootEnvKey] = self._root self._inited = True return True, initTrace
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 isAvailableAinstRoot(self): if not file_util.isDir(self._root): return True subDirs = file_util.listDir(self._root) if subDirs is not None and len(subDirs) == 0: return True return self.isValidAinstRoot()
def rpm2dir(rpmPath, destDir, timeout=600): currentWorkdir = os.getcwd() if not file_util.isDir(destDir) and not file_util.makeDir(destDir): Log.cout(Log.ERROR, 'Make rpm dir %s failed' % destDir) return False try: os.chdir(destDir) except OSError, e: return False
def getInstallPackages(self): installPkgs = [] installDir = self._ainstRoot.getRootVarAinstDir('packages') subDirs = file_util.listDir(installDir) if subDirs: for subDir in subDirs: if file_util.isDir(installDir + subDir): installPkgs.append(subDir) return installPkgs
def _initRoot(self, initTrace): for name in self._rootDirs: if file_util.isDir(self._rootDirs[name]): continue if not file_util.makeDir(self._rootDirs[name]): self.clearInit(initTrace) return False initTrace.append(self._rootDirs[name]) for name in self._rootVarDirs: if file_util.isDir(self._rootVarDirs[name]): continue if not file_util.makeDir(self._rootVarDirs[name]): self.clearInit(initTrace) return False initTrace.append(self._rootVarDirs[name]) for name in self._rootVarAinstDirs: if file_util.isDir(self._rootVarAinstDirs[name]): continue if not file_util.makeDir(self._rootVarAinstDirs[name]): self.clearInit(initTrace) return False initTrace.append(self._rootVarAinstDirs[name]) initRootState = self._rootVarAinstDirs['save'] + 'root-state-0' if not file_util.exists(initRootState): state = RootState(time.time(), '', self._root, common.AINST_VERSION, [], {}) content = RootStateStreamer().toString(state) if not content or not file_util.writeToFile(initRootState, content): self.clearInit(initTrace) return False initTrace.append(initRootState) if not self._clearTmp: if not file_util.remove(self._rootVarAinstDirs['tmp']) or\ not file_util.makeDir(self._rootVarAinstDirs['tmp']): self.clearInit(initTrace) return False self._clearTmp = True 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 getInstallPkgMetas(self): 'return list [(pkgVer, rpmFilePath)]' installPkgMetas = [] pkgDir = self._ainstRoot.getRootVarAinstDir('packages') subDirs = file_util.listDir(pkgDir) if subDirs is None: return None for subDir in subDirs: pkgPath = pkgDir + subDir if file_util.isDir(pkgPath): rpmPath = pkgPath + self._ainstRoot.getBackRpmPath() installPkgMetas.append((subDir, rpmPath)) return installPkgMetas
def checkRoot(self): if not self._root: return False if not file_util.isFile(self._root + '/' + self._ainstRootMarkFile): return False for name in self._rootDirs: if not file_util.isDir(self._rootDirs[name]): return False for name in self._rootVarDirs: if not file_util.isDir(self._rootVarDirs[name]): return False for name in self._rootVarAinstDirs: if not file_util.isDir(self._rootVarAinstDirs[name]): return False initRootState = self._rootVarAinstDirs['save'] + 'root-state-0' if not file_util.exists(initRootState): return False os.environ[self._installRootEnvKey] = self._root 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 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 execute(self): Log.cout(Log.INFO, 'Activate pkg %s ...' % self._pkg) if self._dryrun: return True if self._executed: return False self._executed = True 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 #get active dir and package dir self._ainstActivePkg = self._ainstRoot.getRootVarAinstDir( 'active') + self._pkg.name if self._isActive(self._ainstActivePkg, pkgDirName): Log.cout(Log.INFO, 'Package %s is always active' % self._pkg) return True self._ainstPkgDir = self._ainstRoot.getRootVarAinstDir( 'packages') + pkgDirName if not file_util.isDir(self._ainstPkgDir): Log.cout(Log.ERROR, 'Package %s is not installed' % self._pkg) return False #get aicf info ainstDirPath = self._ainstPkgDir + '/ainst/' self._ainstPathEnv = {self._ainstDirKey: ainstDirPath} aicfFile = ainstDirPath + self._pkg.name + '.aicf' if file_util.isFile(aicfFile): self._aicfInfo = AicfParser().parse(aicfFile) if not self._aicfInfo or not AicfInfoWrapper().removeConfigPrefix(self._aicfInfo) \ or not self._checkAicfInfo(self._aicfInfo): Log.cout(Log.ERROR, 'Aicf info of pkg %s is illegal' % self._pkg) return False #calc settings settingPath = self._ainstRoot.getRootVarAinstDir( 'settings') + self._pkg.name backSettings = SettingStreamer().parse(settingPath) if backSettings is None: Log.cout(Log.ERROR, 'Parse settings of pkg %s failed' % self._pkg) return False useSettings = {} useSettings.update(backSettings) self._mergeSettings(useSettings, self._aicfInfo, self._cliSettings, self._unsetKeySet) self._settingsEnv = self._generateSettingsEnv(useSettings) #get rpm file infos bakRpmFile = self._ainstPkgDir + self._ainstRoot.getBackRpmPath() rpmFileInfoList = RpmFileWrapper().convert(bakRpmFile) if rpmFileInfoList is None: Log.cout(Log.ERROR, 'Get rpm file info failed: %s', bakRpmFile) return False if not self._processScriptByName('pre-activate'): return False #link common files and makedir if needed if not self._linkPkgToRoot(rpmFileInfoList, self._linkList, self._mkdirList): Log.cout(Log.ERROR, 'Link pkg %s dir to root failed' % self._pkg) self.undo() return False #generate a new config file and copy to root dir self._exportToEnv(self._ainstPathEnv) self._exportToEnv(self._settingsEnv) settingMap = {} settingMap.update(useSettings) settingMap[self._ainstRoot.getInstallRootEnvKey( )] = self._ainstRoot.getRoot() settingMap.update(self._ainstPathEnv) ret = self._generateConfigToRoot(rpmFileInfoList, self._aicfInfo, settingMap, self._confSet) self._removeFromEnv(self._settingsEnv) self._removeFromEnv(self._ainstPathEnv) if not ret: Log.cout(Log.ERROR, 'Generate config of pkg %s failed' % self._pkg) self.undo() return False #seralize to setting files if len(useSettings) > 0: if not SettingStreamer().dump(useSettings, settingPath): Log.cout(Log.ERROR, 'Save settings of pkg %s failed' % self._pkg) self.undo() return False self._backSettings = backSettings self._modifyDb = self._addFileToDb() if not self._modifyDb: Log.cout(Log.ERROR, 'Modify db failed') self.undo() return False if not self._generateCrontabFile(): Log.cout(Log.ERROR, 'Generate crontab failed') self.undo() return False #do symbol link from package dir to active dir relativePkgPath = self._getActiveRelativePath(pkgDirName) self._linkToActive, self._lastActivePkg = self._symLinkToActive( relativePkgPath, self._ainstActivePkg) if not self._linkToActive: Log.cout(Log.ERROR, 'SymLink pkg %s dir to active failed' % self._pkg) self.undo() return False if not self._processScriptByName('post-activate'): self.undo() return False self._processStartScript(useSettings) self._success = True Log.cout(Log.DEBUG, 'Activate pkg %s success' % self._pkg) return True