Skip to content

Commit 960b0c3

Browse files
committed
Automatic updating of CodexPlugin openapi.yaml.
1 parent 9ca4bf8 commit 960b0c3

File tree

7 files changed

+76
-71
lines changed

7 files changed

+76
-71
lines changed

Framework/Utils/PluginPathUtils.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Utils
2+
{
3+
public static class PluginPathUtils
4+
{
5+
private const string ProjectPluginsFolderName = "ProjectPlugins";
6+
private static string projectPluginsDir = string.Empty;
7+
8+
public static string ProjectPluginsDir
9+
{
10+
get
11+
{
12+
if (string.IsNullOrEmpty(projectPluginsDir)) projectPluginsDir = FindProjectPluginsDir();
13+
return projectPluginsDir;
14+
}
15+
}
16+
17+
private static string FindProjectPluginsDir()
18+
{
19+
var current = Directory.GetCurrentDirectory();
20+
while (true)
21+
{
22+
var localFolders = Directory.GetDirectories(current);
23+
var projectPluginsFolders = localFolders.Where(l => l.EndsWith(ProjectPluginsFolderName)).ToArray();
24+
if (projectPluginsFolders.Length == 1)
25+
{
26+
return projectPluginsFolders.Single();
27+
}
28+
29+
var parent = Directory.GetParent(current);
30+
if (parent == null)
31+
{
32+
var msg = $"Unable to locate '{ProjectPluginsFolderName}' folder. Travelled up from: '{Directory.GetCurrentDirectory()}'";
33+
Console.WriteLine(msg);
34+
throw new Exception(msg);
35+
}
36+
37+
current = parent.FullName;
38+
}
39+
}
40+
}
41+
}

ProjectPlugins/CodexContractsPlugin/SelfUpdater.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace CodexContractsPlugin
1+
using Utils;
2+
3+
namespace CodexContractsPlugin
24
{
35
public class SelfUpdater
46
{
@@ -41,24 +43,10 @@ public void Update(string abi, string bytecode)
4143

4244
private string GetMarketplaceFilePath()
4345
{
44-
var here = Directory.GetCurrentDirectory();
45-
while (true)
46-
{
47-
var path = GetMarketplaceFile(here);
48-
if (path != null) return path;
49-
50-
var parent = Directory.GetParent(here);
51-
var up = parent?.FullName;
52-
if (up == null || up == here) throw new Exception("Unable to locate ProjectPlugins folder. Unable to update contracts.");
53-
here = up;
54-
}
55-
}
56-
57-
private string? GetMarketplaceFile(string root)
58-
{
59-
var path = Path.Combine(root, "ProjectPlugins", "CodexContractsPlugin", "Marketplace", "Marketplace.cs");
60-
if (File.Exists(path)) return path;
61-
return null;
46+
var projectPluginDir = PluginPathUtils.ProjectPluginsDir;
47+
var path = Path.Combine(projectPluginDir, "CodexContractsPlugin", "Marketplace", "Marketplace.cs");
48+
if (!File.Exists(path)) throw new Exception("Marketplace file not found. Expected: " + path);
49+
return path;
6250
}
6351

6452
private string GenerateContent(string abi, string bytecode)

ProjectPlugins/CodexPlugin/ApiChecker.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
using Logging;
44
using System.Security.Cryptography;
55
using System.Text;
6+
using Utils;
67

78
namespace CodexPlugin
89
{
910
public class ApiChecker
1011
{
1112
// <INSERT-OPENAPI-YAML-HASH>
12-
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";
13+
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";
1314
private const string OpenApiFilePath = "/codex/openapi.yaml";
1415
private const string DisableEnvironmentVariable = "CODEXPLUGIN_DISABLE_APICHECK";
1516

@@ -21,8 +22,9 @@ public class ApiChecker
2122

2223
private const string Failure =
2324
"Codex API compatibility check failed! " +
24-
"openapi.yaml used by CodexPlugin does not match openapi.yaml in Codex container. Please update the openapi.yaml in " +
25-
"'ProjectPlugins/CodexPlugin' and rebuild this project. If you wish to disable API compatibility checking, please set " +
25+
"openapi.yaml used by CodexPlugin does not match openapi.yaml in Codex container. The openapi.yaml in " +
26+
"'ProjectPlugins/CodexPlugin' has been overwritten with the container one. " +
27+
"Please and rebuild this project. If you wish to disable API compatibility checking, please set " +
2628
$"the environment variable '{DisableEnvironmentVariable}' or set the disable bool in 'ProjectPlugins/CodexPlugin/ApiChecker.cs'.";
2729

2830
private static bool checkPassed = false;
@@ -71,10 +73,23 @@ public void CheckCompatibility(RunningPod[] containers)
7173
return;
7274
}
7375

76+
OverwriteOpenApiYaml(containerApi);
77+
7478
log.Error(Failure);
7579
throw new Exception(Failure);
7680
}
7781

82+
private void OverwriteOpenApiYaml(string containerApi)
83+
{
84+
Log("API compatibility check failed. Updating CodexPlugin...");
85+
var openApiFilePath = Path.Combine(PluginPathUtils.ProjectPluginsDir, "CodexPlugin", "openapi.yaml");
86+
if (!File.Exists(openApiFilePath)) throw new Exception("Unable to locate CodexPlugin/openapi.yaml. Expected: " + openApiFilePath);
87+
88+
File.Delete(openApiFilePath);
89+
File.WriteAllText(openApiFilePath, containerApi);
90+
Log("CodexPlugin/openapi.yaml has been updated.");
91+
}
92+
7893
private string Hash(string file)
7994
{
8095
var fileBytes = Encoding.ASCII.GetBytes(file

ProjectPlugins/CodexPlugin/CodexContainerRecipe.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace CodexPlugin
77
{
88
public class CodexContainerRecipe : ContainerRecipeFactory
99
{
10-
private const string DefaultDockerImage = "codexstorage/nim-codex:0.1.4";
10+
private const string DefaultDockerImage = "codexstorage/nim-codex:0.1.5-dist-tests";
1111
public const string ApiPortTag = "codex_api_port";
1212
public const string ListenPortTag = "codex_listen_port";
1313
public const string MetricsPortTag = "codex_metrics_port";

ProjectPlugins/CodexPlugin/openapi.yaml

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,12 @@ components:
8383
id:
8484
$ref: "#/components/schemas/PeerId"
8585

86-
ErasureParameters:
87-
type: object
88-
properties:
89-
totalChunks:
90-
type: integer
91-
92-
PoRParameters:
93-
description: Parameters for Proof of Retrievability
94-
type: object
95-
properties:
96-
u:
97-
type: string
98-
publicKey:
99-
type: string
100-
name:
101-
type: string
102-
10386
Content:
10487
type: object
10588
description: Parameters specifying the content
10689
properties:
10790
cid:
10891
$ref: "#/components/schemas/Cid"
109-
erasure:
110-
$ref: "#/components/schemas/ErasureParameters"
111-
por:
112-
$ref: "#/components/schemas/PoRParameters"
11392

11493
DebugInfo:
11594
type: object
@@ -813,4 +792,4 @@ paths:
813792
content:
814793
application/json:
815794
schema:
816-
$ref: "#/components/schemas/DebugInfo"
795+
$ref: "#/components/schemas/DebugInfo"

ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<ProjectReference Include="..\..\Framework\Utils\Utils.csproj" />
12+
</ItemGroup>
13+
1014
</Project>

ProjectPlugins/CodexPluginPrebuild/Program.cs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Security.Cryptography;
22
using System.Text;
3+
using Utils;
34

45
public static class Program
56
{
@@ -40,32 +41,9 @@ public static void Main(string[] args)
4041

4142
private static string FindCodexPluginFolder()
4243
{
43-
var current = Directory.GetCurrentDirectory();
44-
45-
while (true)
46-
{
47-
var localFolders = Directory.GetDirectories(current);
48-
var projectPluginsFolders = localFolders.Where(l => l.EndsWith(ProjectPluginsFolderName)).ToArray();
49-
if (projectPluginsFolders.Length == 1)
50-
{
51-
return Path.Combine(projectPluginsFolders.Single(), CodexPluginFolderName);
52-
}
53-
var codexPluginFolders = localFolders.Where(l => l.EndsWith(CodexPluginFolderName)).ToArray();
54-
if (codexPluginFolders.Length == 1)
55-
{
56-
return codexPluginFolders.Single();
57-
}
58-
59-
var parent = Directory.GetParent(current);
60-
if (parent == null)
61-
{
62-
var msg = $"Unable to locate '{CodexPluginFolderName}' folder. Travelled up from: '{Directory.GetCurrentDirectory()}'";
63-
Console.WriteLine(msg);
64-
throw new Exception(msg);
65-
}
66-
67-
current = parent.FullName;
68-
}
44+
var folder = Path.Combine(PluginPathUtils.ProjectPluginsDir, "CodexPlugin");
45+
if (!Directory.Exists(folder)) throw new Exception("CodexPlugin folder not found. Expected: " + folder);
46+
return folder;
6947
}
7048

7149
private static string CreateHash(string openApiFile)

0 commit comments

Comments
 (0)