Skip to content

Commit

Permalink
Automatic updating of CodexPlugin openapi.yaml.
Browse files Browse the repository at this point in the history
  • Loading branch information
benbierens committed Oct 8, 2024
1 parent 9ca4bf8 commit 960b0c3
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 71 deletions.
41 changes: 41 additions & 0 deletions Framework/Utils/PluginPathUtils.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
26 changes: 7 additions & 19 deletions ProjectPlugins/CodexContractsPlugin/SelfUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CodexContractsPlugin
using Utils;

namespace CodexContractsPlugin
{
public class SelfUpdater
{
Expand Down Expand Up @@ -41,24 +43,10 @@ public void Update(string abi, string bytecode)

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)
Expand Down
21 changes: 18 additions & 3 deletions ProjectPlugins/CodexPlugin/ApiChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
using Logging;
using System.Security.Cryptography;
using System.Text;
using Utils;

namespace CodexPlugin
{
public class ApiChecker
{
// <INSERT-OPENAPI-YAML-HASH>
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";

Expand All @@ -21,8 +22,9 @@ public class ApiChecker

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;
Expand Down Expand Up @@ -71,10 +73,23 @@ public void CheckCompatibility(RunningPod[] containers)
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
Expand Down
2 changes: 1 addition & 1 deletion ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
23 changes: 1 addition & 22 deletions ProjectPlugins/CodexPlugin/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -813,4 +792,4 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/DebugInfo"
$ref: "#/components/schemas/DebugInfo"
4 changes: 4 additions & 0 deletions ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Framework\Utils\Utils.csproj" />
</ItemGroup>

</Project>
30 changes: 4 additions & 26 deletions ProjectPlugins/CodexPluginPrebuild/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Security.Cryptography;
using System.Text;
using Utils;

public static class Program
{
Expand Down Expand Up @@ -40,32 +41,9 @@ public static void Main(string[] args)

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)
Expand Down

0 comments on commit 960b0c3

Please sign in to comment.