2023-09-11 09:59:33 +00:00
|
|
|
|
using DistTestCore;
|
|
|
|
|
using KubernetesWorkflow;
|
2023-06-29 14:07:49 +00:00
|
|
|
|
using Logging;
|
|
|
|
|
using Utils;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-09-11 09:59:33 +00:00
|
|
|
|
namespace CodexPlugin
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-08-15 09:01:18 +00:00
|
|
|
|
public class CodexAccess : ILogHandler
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-09-11 14:57:57 +00:00
|
|
|
|
private readonly ILog log;
|
2023-06-29 14:07:49 +00:00
|
|
|
|
private readonly ITimeSet timeSet;
|
2023-08-15 09:01:18 +00:00
|
|
|
|
private bool hasContainerCrashed;
|
2023-04-30 08:08:32 +00:00
|
|
|
|
|
2023-09-11 14:57:57 +00:00
|
|
|
|
public CodexAccess(ILog log, RunningContainer container, ITimeSet timeSet, Address address)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-06-29 14:07:49 +00:00
|
|
|
|
this.log = log;
|
|
|
|
|
Container = container;
|
|
|
|
|
this.timeSet = timeSet;
|
|
|
|
|
Address = address;
|
2023-08-15 09:01:18 +00:00
|
|
|
|
hasContainerCrashed = false;
|
|
|
|
|
|
|
|
|
|
if (container.CrashWatcher != null) container.CrashWatcher.Start(this);
|
2023-04-18 13:33:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 06:28:40 +00:00
|
|
|
|
public RunningContainer Container { get; }
|
2023-06-29 14:07:49 +00:00
|
|
|
|
public Address Address { get; }
|
2023-05-11 10:44:53 +00:00
|
|
|
|
|
2023-06-29 14:07:49 +00:00
|
|
|
|
public CodexDebugResponse GetDebugInfo()
|
2023-05-04 09:34:43 +00:00
|
|
|
|
{
|
2023-07-17 13:21:10 +00:00
|
|
|
|
return Http().HttpGetJson<CodexDebugResponse>("debug/info");
|
2023-06-29 14:07:49 +00:00
|
|
|
|
}
|
2023-05-04 09:34:43 +00:00
|
|
|
|
|
2023-06-29 14:07:49 +00:00
|
|
|
|
public CodexDebugPeerResponse GetDebugPeer(string peerId)
|
|
|
|
|
{
|
2023-07-17 13:21:10 +00:00
|
|
|
|
var http = Http();
|
2023-06-29 14:07:49 +00:00
|
|
|
|
var str = http.HttpGetString($"debug/peer/{peerId}");
|
|
|
|
|
|
|
|
|
|
if (str.ToLowerInvariant() == "unable to find peer!")
|
2023-05-04 09:34:43 +00:00
|
|
|
|
{
|
2023-06-29 14:07:49 +00:00
|
|
|
|
return new CodexDebugPeerResponse
|
|
|
|
|
{
|
|
|
|
|
IsPeerFound = false
|
|
|
|
|
};
|
2023-05-04 09:34:43 +00:00
|
|
|
|
}
|
2023-06-29 14:07:49 +00:00
|
|
|
|
|
|
|
|
|
var result = http.TryJsonDeserialize<CodexDebugPeerResponse>(str);
|
|
|
|
|
result.IsPeerFound = true;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 06:52:53 +00:00
|
|
|
|
public CodexDebugThresholdBreaches GetDebugThresholdBreaches()
|
|
|
|
|
{
|
|
|
|
|
return Http().HttpGetJson<CodexDebugThresholdBreaches>("debug/loop");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 14:07:49 +00:00
|
|
|
|
public string UploadFile(FileStream fileStream)
|
|
|
|
|
{
|
|
|
|
|
return Http().HttpPostStream("upload", fileStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Stream DownloadFile(string contentId)
|
|
|
|
|
{
|
|
|
|
|
return Http().HttpGetStream("download/" + contentId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CodexSalesAvailabilityResponse SalesAvailability(CodexSalesAvailabilityRequest request)
|
|
|
|
|
{
|
|
|
|
|
return Http().HttpPostJson<CodexSalesAvailabilityRequest, CodexSalesAvailabilityResponse>("sales/availability", request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId)
|
|
|
|
|
{
|
2023-08-31 09:19:53 +00:00
|
|
|
|
return Http().HttpPostJson($"storage/request/{contentId}", request);
|
2023-06-29 14:07:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CodexStoragePurchase GetPurchaseStatus(string purchaseId)
|
|
|
|
|
{
|
|
|
|
|
return Http().HttpGetJson<CodexStoragePurchase>($"storage/purchases/{purchaseId}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ConnectToPeer(string peerId, string peerMultiAddress)
|
|
|
|
|
{
|
|
|
|
|
return Http().HttpGetString($"connect/{peerId}?addrs={peerMultiAddress}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 09:32:32 +00:00
|
|
|
|
public string GetName()
|
|
|
|
|
{
|
|
|
|
|
return Container.Name;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-17 13:21:10 +00:00
|
|
|
|
private Http Http()
|
2023-06-29 14:07:49 +00:00
|
|
|
|
{
|
2023-08-29 08:09:48 +00:00
|
|
|
|
return new Http(log, timeSet, Address, baseUrl: "/api/codex/v1", CheckContainerCrashed, Container.Name);
|
2023-05-04 09:34:43 +00:00
|
|
|
|
}
|
2023-08-15 09:01:18 +00:00
|
|
|
|
|
2023-08-29 08:09:48 +00:00
|
|
|
|
private void CheckContainerCrashed(HttpClient client)
|
2023-08-15 09:01:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (hasContainerCrashed) throw new Exception("Container has crashed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Log(Stream crashLog)
|
|
|
|
|
{
|
|
|
|
|
var file = log.CreateSubfile();
|
|
|
|
|
log.Log($"Container {Container.Name} has crashed. Downloading crash log to '{file.FullFilename}'...");
|
|
|
|
|
|
|
|
|
|
using var reader = new StreamReader(crashLog);
|
|
|
|
|
var line = reader.ReadLine();
|
|
|
|
|
while (line != null)
|
|
|
|
|
{
|
|
|
|
|
file.Write(line);
|
|
|
|
|
line = reader.ReadLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Log("Crash log successfully downloaded.");
|
|
|
|
|
hasContainerCrashed = true;
|
|
|
|
|
}
|
2023-04-18 13:33:12 +00:00
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|