public void ResetCache(string bucketName, ModuleInfo moduleInfo) { // remove cached module var moduleLocation = new ModuleLocation(bucketName, moduleInfo, "<MISSING>"); var cachedManifestFilePath = GetCachedManifestFilePath(moduleLocation); if (cachedManifestFilePath != null) { try { File.Delete(cachedManifestFilePath); } catch { // nothing to do } } // remove cached list versions var cachedManifestFolder = GetCachedManifestDirectory(bucketName, moduleInfo.Origin ?? bucketName, moduleInfo.Namespace, moduleInfo.Name); if (cachedManifestFolder != null) { try { File.Delete(Path.Combine(cachedManifestFolder, "versions.json")); } catch { // nothing to do } } }
public async Task <ModuleManifest> LoadManifestFromLocationAsync(ModuleLocation moduleLocation, bool errorIfMissing = true) { // download cloudformation template var cloudformationText = await GetS3ObjectContentsAsync(moduleLocation.SourceBucketName, moduleLocation.ModuleTemplateKey); if (cloudformationText == null) { if (errorIfMissing) { LogError($"could not load CloudFormation template for {moduleLocation.ModuleInfo}"); } return(null); } // extract manifest var cloudformation = JsonConvert.DeserializeObject <JObject>(cloudformationText); var manifest = GetManifest(cloudformation); if (manifest == null) { return(null); } // validate manifest if (manifest.Version != ModuleManifest.CurrentVersion) { LogError($"Incompatible LambdaSharp manifest version (found: {manifest.Version ?? "<null>"}, expected: {ModuleManifest.CurrentVersion})"); return(null); } return(manifest); }
public async Task <ModuleManifest> LoadManifestFromLocationAsync(ModuleLocation moduleLocation, bool errorIfMissing = true, bool allowCaching = false) { var stopwatch = Stopwatch.StartNew(); var cached = false; try { var cachedManifest = Path.Combine(Settings.GetOriginCacheDirectory(moduleLocation.ModuleInfo), moduleLocation.ModuleInfo.Version.ToString()); if (allowCaching && Settings.AllowCaching && !moduleLocation.ModuleInfo.Version.IsPreRelease() && File.Exists(cachedManifest)) { cached = true; return(JsonConvert.DeserializeObject <ModuleManifest>(await File.ReadAllTextAsync(cachedManifest))); } // download cloudformation template var cloudformationText = await GetS3ObjectContentsAsync(moduleLocation.SourceBucketName, moduleLocation.ModuleTemplateKey); if (cloudformationText == null) { if (errorIfMissing) { LogError($"could not load CloudFormation template for {moduleLocation.ModuleInfo}"); } return(null); } // extract manifest var cloudformation = JsonConvert.DeserializeObject <JObject>(cloudformationText); var manifest = GetManifest(cloudformation); if (manifest == null) { return(null); } // validate manifest if (manifest.Version != ModuleManifest.CurrentVersion) { LogError($"Incompatible LambdaSharp manifest version (found: {manifest.Version ?? "<null>"}, expected: {ModuleManifest.CurrentVersion})"); return(null); } return(manifest); } finally { LogInfoPerformance($"LoadManifestFromLocationAsync() for {moduleLocation.ModuleInfo}", stopwatch.Elapsed, cached); } }
private string GetCachedManifestFilePath(ModuleLocation moduleLocation) { // never cache pre-release versions or when the module origin is not set if ( moduleLocation.ModuleInfo.Version.IsPreRelease() || (moduleLocation.ModuleInfo.Origin is null) ) { return(null); } // ensure directory exists since it will be used var cachedManifestFolder = GetCachedManifestDirectory(moduleLocation.SourceBucketName, moduleLocation.ModuleInfo.Origin, moduleLocation.ModuleInfo.Namespace, moduleLocation.ModuleInfo.Name); if (cachedManifestFolder == null) { return(null); } return(Path.Combine(cachedManifestFolder, moduleLocation.ModuleInfo.Version.ToString())); }
public async Task <ModuleNameMappings> GetNameMappingsFromLocationAsync(ModuleLocation moduleLocation) { var template = await GetS3ObjectContentsAsync(moduleLocation.SourceBucketName, moduleLocation.ModuleTemplateKey); return(GetNameMappingsFromTemplate(template)); }
public async Task <(ModuleManifest Manifest, string Reason)> LoadManifestFromLocationAsync(ModuleLocation moduleLocation) { StartLogPerformance($"LoadManifestFromLocationAsync() for {moduleLocation.ModuleInfo}"); var cached = false; try { // attempt to load manifest from cache var cachedManifestFilePath = GetCachedManifestFilePath(moduleLocation); if (!Settings.ForceRefresh && (cachedManifestFilePath is not null) && File.Exists(cachedManifestFilePath)) { ModuleManifest result = null; try { result = JsonSerializer.Deserialize <ModuleManifest>(await File.ReadAllTextAsync(cachedManifestFilePath), Settings.JsonSerializerOptions); cached = true; return(Manifest : result, Reason : null); } catch { // cached manifest file is either corrupted or inaccessible; attempt to delete it try { File.Delete(cachedManifestFilePath); } catch { // nothing to do } } } // download manifest from S3 var manifestText = await GetS3ObjectContentsAsync(moduleLocation.SourceBucketName, moduleLocation.ModuleInfo.VersionPath); if (manifestText == null) { return(Manifest : null, Reason : $"could not load module manifest for {moduleLocation.ModuleInfo}"); } var manifest = JsonSerializer.Deserialize <ModuleManifest>(manifestText, Settings.JsonSerializerOptions); // validate manifest if (manifest.Version != ModuleManifest.CurrentVersion) { return(Manifest : null, Reason : $"incompatible LambdaSharp manifest version (found: {manifest.Version ?? "<null>"}, expected: {ModuleManifest.CurrentVersion})"); } // keep manifest if we have a valid file path for it if (cachedManifestFilePath is not null) { try { await File.WriteAllTextAsync(cachedManifestFilePath, manifestText); } catch { // cached manifest file could not be written; nothing to do } } return(Manifest : manifest, Reason : null); } finally { StopLogPerformance(cached); } }