def _autoRecover(self): """ private method to recover the right cache.dat based on the patch installed in the running VistA instance. """ from GitUtils import getCommitInfo mExtractConfig = self._config['M_Extract'] mRepo = mExtractConfig['M_repo'] mRepoBranch = mExtractConfig.get('M_repo_branch', None) if not mRepoBranch: mRepoBranch = 'master' # default is the master branch commitInfo = getCommitInfo(mRepo, mRepoBranch) if not commitInfo: logger.error("Can not read commit info from %s branch %s" % (mRepo, mRepoBranch)) return -1 logger.debug(commitInfo) """ convert datetime to VistA T- format """ from datetime import datetime commitDate = datetime.fromtimestamp(int(commitInfo['%ct'])) timeDiff = datetime.now() - commitDate days = timeDiff.days + 30 # extra 30 days logger.debug("Totol dates to query is %s" % days) installNames = None idx = commitInfo['%s'].find('Install: ') if idx >= 0: installNames = commitInfo['%s'][len('Install: '):].split(', ') logger.info("installNames is %s" % installNames) if installNames is None: logger.error("Can not find patch installed after") return -1 """ check to see what and when is the last patch installed """ testClient = self._createTestClient() """ make sure cache instance is up and running """ startCache(self._instance, self._useSudo) with testClient: patchInfoFetch = VistAPackageInfoFetcher(testClient) output = patchInfoFetch.getAllPatchInstalledAfterByTime("T-%s" % days) if not output: # must be an error or something, skip backup logger.error("Can not get patch installation information from VistA") return -1 logger.debug(output) """ logic to check if we need to recover from cache backup data """ found = False for idx in xrange(0,len(output)): if output[idx][0] == installNames[-1]: found = True break if found and idx == len(output) - 1: """ last patch is the same as last in the commit """ logger.info("No need to recover.") return 0 if not found or idx < len(output) - 1: """ check to see if cache.dat exist in the backup dir""" backupConfig = self._config.get('Backup') backupDir = backupConfig['backup_dir'] if not os.path.exists(backupDir): logger.error("%s does not exist" % backupDir) return -4 cacheDir = backupConfig['cache_dat_dir'] origDir = os.path.join(cacheDir, "CACHE.DAT") """ identify the exists of backup file in the right format """ commitHash = commitInfo['%H'] cacheBackupFile = os.path.join(backupDir, getCacheBackupNameByHash(commitHash)) if not os.path.exists(cacheBackupFile): logger.error("backup file %s does not exist" % cacheBackupFile) return -5 logger.info("Need to restore from backup data %s" % cacheBackupFile) restoreCacheData(self._instance, cacheBackupFile, cacheDir, self._useSudo) startCache(self._instance, self._useSudo) return 0 return -1
def _autoRecover(self): """ private method to recover the right cache.dat based on the patch installed in the running VistA instance. """ from GitUtils import getCommitInfo mExtractConfig = self._config['M_Extract'] mRepo = mExtractConfig['M_repo'] mRepoBranch = mExtractConfig.get('M_repo_branch', None) if not mRepoBranch: mRepoBranch = 'master' # default is the master branch commitInfo = getCommitInfo(mRepo, mRepoBranch) if not commitInfo: logger.error("Can not read commit info from %s branch %s" % (mRepo, mRepoBranch)) return -1 logger.debug(commitInfo) """ convert datetime to VistA T- format """ from datetime import datetime commitDate = datetime.fromtimestamp(int(commitInfo['%ct'])) timeDiff = datetime.now() - commitDate days = timeDiff.days + 30 # extra 30 days logger.debug("Totol dates to query is %s" % days) installNames = None idx = commitInfo['%s'].find('Install: ') if idx >= 0: installNames = commitInfo['%s'][len('Install: '):].split(', ') logger.info("installNames is %s" % installNames) if installNames is None: logger.error("Can not find patch installed after") return -1 """ check to see what and when is the last patch installed """ testClient = self._createTestClient() """ make sure cache instance is up and running """ startCache(self._instance, self._useSudo) with testClient: patchInfoFetch = VistAPackageInfoFetcher(testClient) output = patchInfoFetch.getAllPatchInstalledAfterByTime("T-%s" % days) if not output: # must be an error or something, skip backup logger.error( "Can not get patch installation information from VistA") return -1 logger.debug(output) """ logic to check if we need to recover from cache backup data """ found = False for idx in xrange(0, len(output)): if output[idx][0] == installNames[-1]: found = True break if found and idx == len(output) - 1: """ last patch is the same as last in the commit """ logger.info("No need to recover.") return 0 if not found or idx < len(output) - 1: """ check to see if cache.dat exist in the backup dir""" backupConfig = self._config.get('Backup') backupDir = backupConfig['backup_dir'] if not os.path.exists(backupDir): logger.error("%s does not exist" % backupDir) return -4 cacheDir = backupConfig['cache_dat_dir'] origDir = os.path.join(cacheDir, "CACHE.DAT") """ identify the exists of backup file in the right format """ commitHash = commitInfo['%H'] cacheBackupFile = os.path.join( backupDir, getCacheBackupNameByHash(commitHash)) if not os.path.exists(cacheBackupFile): logger.error("backup file %s does not exist" % cacheBackupFile) return -5 logger.info("Need to restore from backup data %s" % cacheBackupFile) restoreCacheData(self._instance, cacheBackupFile, cacheDir, self._useSudo) startCache(self._instance, self._useSudo) return 0 return -1