diff --git a/Framework/Utils/PluginPathUtils.cs b/Framework/Utils/PluginPathUtils.cs new file mode 100644 index 0000000..ced65f3 --- /dev/null +++ b/Framework/Utils/PluginPathUtils.cs @@ -0,0 +1,41 @@ +namespace Utils +{ + public static class PluginPathUtils + { + private const string ProjectPluginsFolderName = "ProjectPlugins"; + private static string projectPluginsDir = string.Empty; + + public static string ProjectPluginsDir + { + get + { + if (string.IsNullOrEmpty(projectPluginsDir)) projectPluginsDir = FindProjectPluginsDir(); + return projectPluginsDir; + } + } + + private static string FindProjectPluginsDir() + { + var current = Directory.GetCurrentDirectory(); + while (true) + { + var localFolders = Directory.GetDirectories(current); + var projectPluginsFolders = localFolders.Where(l => l.EndsWith(ProjectPluginsFolderName)).ToArray(); + if (projectPluginsFolders.Length == 1) + { + return projectPluginsFolders.Single(); + } + + var parent = Directory.GetParent(current); + if (parent == null) + { + var msg = $"Unable to locate '{ProjectPluginsFolderName}' folder. Travelled up from: '{Directory.GetCurrentDirectory()}'"; + Console.WriteLine(msg); + throw new Exception(msg); + } + + current = parent.FullName; + } + } + } +} diff --git a/ProjectPlugins/CodexContractsPlugin/SelfUpdater.cs b/ProjectPlugins/CodexContractsPlugin/SelfUpdater.cs index 1f612fb..2e66385 100644 --- a/ProjectPlugins/CodexContractsPlugin/SelfUpdater.cs +++ b/ProjectPlugins/CodexContractsPlugin/SelfUpdater.cs @@ -1,4 +1,6 @@ -namespace CodexContractsPlugin +using Utils; + +namespace CodexContractsPlugin { public class SelfUpdater { @@ -41,24 +43,10 @@ private string GetMarketplaceFilePath() { - var here = Directory.GetCurrentDirectory(); - while (true) - { - var path = GetMarketplaceFile(here); - if (path != null) return path; - - var parent = Directory.GetParent(here); - var up = parent?.FullName; - if (up == null || up == here) throw new Exception("Unable to locate ProjectPlugins folder. Unable to update contracts."); - here = up; - } - } - - private string? GetMarketplaceFile(string root) - { - var path = Path.Combine(root, "ProjectPlugins", "CodexContractsPlugin", "Marketplace", "Marketplace.cs"); - if (File.Exists(path)) return path; - return null; + var projectPluginDir = PluginPathUtils.ProjectPluginsDir; + var path = Path.Combine(projectPluginDir, "CodexContractsPlugin", "Marketplace", "Marketplace.cs"); + if (!File.Exists(path)) throw new Exception("Marketplace file not found. Expected: " + path); + return path; } private string GenerateContent(string abi, string bytecode) diff --git a/ProjectPlugins/CodexPlugin/ApiChecker.cs b/ProjectPlugins/CodexPlugin/ApiChecker.cs index 34737db..4e9a135 100644 --- a/ProjectPlugins/CodexPlugin/ApiChecker.cs +++ b/ProjectPlugins/CodexPlugin/ApiChecker.cs @@ -3,13 +3,14 @@ using KubernetesWorkflow.Types; using Logging; using System.Security.Cryptography; using System.Text; +using Utils; namespace CodexPlugin { public class ApiChecker { // - private const string OpenApiYamlHash = "6B-94-24-A4-D5-01-6F-12-E9-34-74-36-80-57-7A-3A-79-8C-E8-02-68-B7-05-DA-50-A0-5C-B1-02-B9-AE-C6"; + private const string OpenApiYamlHash = "8B-C5-3F-BF-E6-6C-6A-4F-1C-70-29-19-46-AA-E6-71-DC-56-0A-A0-BC-73-A5-11-9E-66-CE-09-1E-86-20-FC"; private const string OpenApiFilePath = "/codex/openapi.yaml"; private const string DisableEnvironmentVariable = "CODEXPLUGIN_DISABLE_APICHECK"; @@ -21,8 +22,9 @@ namespace CodexPlugin private const string Failure = "Codex API compatibility check failed! " + - "openapi.yaml used by CodexPlugin does not match openapi.yaml in Codex container. Please update the openapi.yaml in " + - "'ProjectPlugins/CodexPlugin' and rebuild this project. If you wish to disable API compatibility checking, please set " + + "openapi.yaml used by CodexPlugin does not match openapi.yaml in Codex container. The openapi.yaml in " + + "'ProjectPlugins/CodexPlugin' has been overwritten with the container one. " + + "Please and rebuild this project. If you wish to disable API compatibility checking, please set " + $"the environment variable '{DisableEnvironmentVariable}' or set the disable bool in 'ProjectPlugins/CodexPlugin/ApiChecker.cs'."; private static bool checkPassed = false; @@ -71,10 +73,23 @@ namespace CodexPlugin return; } + OverwriteOpenApiYaml(containerApi); + log.Error(Failure); throw new Exception(Failure); } + private void OverwriteOpenApiYaml(string containerApi) + { + Log("API compatibility check failed. Updating CodexPlugin..."); + var openApiFilePath = Path.Combine(PluginPathUtils.ProjectPluginsDir, "CodexPlugin", "openapi.yaml"); + if (!File.Exists(openApiFilePath)) throw new Exception("Unable to locate CodexPlugin/openapi.yaml. Expected: " + openApiFilePath); + + File.Delete(openApiFilePath); + File.WriteAllText(openApiFilePath, containerApi); + Log("CodexPlugin/openapi.yaml has been updated."); + } + private string Hash(string file) { var fileBytes = Encoding.ASCII.GetBytes(file diff --git a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs index cd833f6..900f7fa 100644 --- a/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs +++ b/ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs @@ -7,7 +7,7 @@ namespace CodexPlugin { public class CodexContainerRecipe : ContainerRecipeFactory { - private const string DefaultDockerImage = "codexstorage/nim-codex:0.1.4"; + private const string DefaultDockerImage = "codexstorage/nim-codex:0.1.5-dist-tests"; public const string ApiPortTag = "codex_api_port"; public const string ListenPortTag = "codex_listen_port"; public const string MetricsPortTag = "codex_metrics_port"; diff --git a/ProjectPlugins/CodexPlugin/openapi.yaml b/ProjectPlugins/CodexPlugin/openapi.yaml index 9a2056c..674bbba 100644 --- a/ProjectPlugins/CodexPlugin/openapi.yaml +++ b/ProjectPlugins/CodexPlugin/openapi.yaml @@ -83,33 +83,12 @@ components: id: $ref: "#/components/schemas/PeerId" - ErasureParameters: - type: object - properties: - totalChunks: - type: integer - - PoRParameters: - description: Parameters for Proof of Retrievability - type: object - properties: - u: - type: string - publicKey: - type: string - name: - type: string - Content: type: object description: Parameters specifying the content properties: cid: $ref: "#/components/schemas/Cid" - erasure: - $ref: "#/components/schemas/ErasureParameters" - por: - $ref: "#/components/schemas/PoRParameters" DebugInfo: type: object @@ -813,4 +792,4 @@ paths: content: application/json: schema: - $ref: "#/components/schemas/DebugInfo" + $ref: "#/components/schemas/DebugInfo" \ No newline at end of file diff --git a/ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj b/ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj index 91b464a..a8ee135 100644 --- a/ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj +++ b/ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/ProjectPlugins/CodexPluginPrebuild/Program.cs b/ProjectPlugins/CodexPluginPrebuild/Program.cs index bdd4716..3df9f23 100644 --- a/ProjectPlugins/CodexPluginPrebuild/Program.cs +++ b/ProjectPlugins/CodexPluginPrebuild/Program.cs @@ -1,5 +1,6 @@ using System.Security.Cryptography; using System.Text; +using Utils; public static class Program { @@ -40,32 +41,9 @@ public static class Program private static string FindCodexPluginFolder() { - var current = Directory.GetCurrentDirectory(); - - while (true) - { - var localFolders = Directory.GetDirectories(current); - var projectPluginsFolders = localFolders.Where(l => l.EndsWith(ProjectPluginsFolderName)).ToArray(); - if (projectPluginsFolders.Length == 1) - { - return Path.Combine(projectPluginsFolders.Single(), CodexPluginFolderName); - } - var codexPluginFolders = localFolders.Where(l => l.EndsWith(CodexPluginFolderName)).ToArray(); - if (codexPluginFolders.Length == 1) - { - return codexPluginFolders.Single(); - } - - var parent = Directory.GetParent(current); - if (parent == null) - { - var msg = $"Unable to locate '{CodexPluginFolderName}' folder. Travelled up from: '{Directory.GetCurrentDirectory()}'"; - Console.WriteLine(msg); - throw new Exception(msg); - } - - current = parent.FullName; - } + var folder = Path.Combine(PluginPathUtils.ProjectPluginsDir, "CodexPlugin"); + if (!Directory.Exists(folder)) throw new Exception("CodexPlugin folder not found. Expected: " + folder); + return folder; } private static string CreateHash(string openApiFile)