2025-01-16 15:16:00 +01:00
|
|
|
|
using CodexOpenApi;
|
|
|
|
|
|
using Logging;
|
2024-04-02 14:53:45 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2023-10-19 11:18:59 +02:00
|
|
|
|
using Utils;
|
2025-01-16 11:31:50 +01:00
|
|
|
|
using WebUtils;
|
2023-04-12 13:53:55 +02:00
|
|
|
|
|
2025-01-16 11:31:50 +01:00
|
|
|
|
namespace CodexClient
|
2023-04-12 13:53:55 +02:00
|
|
|
|
{
|
2024-06-19 10:39:14 +02:00
|
|
|
|
public class CodexAccess
|
2023-04-12 13:53:55 +02:00
|
|
|
|
{
|
2024-06-12 10:48:52 +02:00
|
|
|
|
private readonly ILog log;
|
2025-01-16 11:31:50 +01:00
|
|
|
|
private readonly IHttpFactory httpFactory;
|
2025-01-15 15:43:50 +01:00
|
|
|
|
private readonly IProcessControl processControl;
|
|
|
|
|
|
private ICodexInstance instance;
|
2024-03-26 08:58:16 +01:00
|
|
|
|
private readonly Mapper mapper = new Mapper();
|
2023-04-30 10:08:32 +02:00
|
|
|
|
|
2025-01-16 13:24:57 +01:00
|
|
|
|
public CodexAccess(ILog log, IHttpFactory httpFactory, IProcessControl processControl, ICodexInstance instance)
|
2023-04-12 13:53:55 +02:00
|
|
|
|
{
|
2025-01-16 11:31:50 +01:00
|
|
|
|
this.log = log;
|
|
|
|
|
|
this.httpFactory = httpFactory;
|
2025-01-15 15:43:50 +01:00
|
|
|
|
this.processControl = processControl;
|
2025-01-15 15:00:25 +01:00
|
|
|
|
this.instance = instance;
|
2023-04-18 15:33:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-15 15:43:50 +01:00
|
|
|
|
public void Stop(bool waitTillStopped)
|
|
|
|
|
|
{
|
2025-01-16 13:24:57 +01:00
|
|
|
|
processControl.Stop(waitTillStopped);
|
2025-01-15 15:43:50 +01:00
|
|
|
|
// Prevents accidental use after stop:
|
|
|
|
|
|
instance = null!;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 10:15:02 +01:00
|
|
|
|
public IDownloadedLog DownloadLog(string additionalName = "")
|
|
|
|
|
|
{
|
2025-01-29 14:44:19 +01:00
|
|
|
|
var file = log.CreateSubfile(GetName() + additionalName);
|
2025-01-31 14:24:55 +01:00
|
|
|
|
Log($"Downloading logs to '{file.Filename}'");
|
2025-01-29 14:44:19 +01:00
|
|
|
|
return processControl.DownloadLog(file);
|
2025-01-16 10:15:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-15 15:00:25 +01:00
|
|
|
|
public string GetImageName()
|
|
|
|
|
|
{
|
|
|
|
|
|
return instance.ImageName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public DateTime GetStartUtc()
|
|
|
|
|
|
{
|
|
|
|
|
|
return instance.StartUtc;
|
|
|
|
|
|
}
|
2023-05-11 12:44:53 +02:00
|
|
|
|
|
2024-03-26 08:58:16 +01:00
|
|
|
|
public DebugInfo GetDebugInfo()
|
2023-05-04 11:34:43 +02:00
|
|
|
|
{
|
2024-03-26 15:12:28 +01:00
|
|
|
|
return mapper.Map(OnCodex(api => api.GetDebugInfoAsync()));
|
2023-06-29 16:07:49 +02:00
|
|
|
|
}
|
2023-05-04 11:34:43 +02:00
|
|
|
|
|
2025-03-03 16:44:04 +01:00
|
|
|
|
public void SetLogLevel(string logLevel)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
OnCodex(async api =>
|
|
|
|
|
|
{
|
|
|
|
|
|
await api.SetDebugLogLevelAsync(logLevel);
|
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exc)
|
|
|
|
|
|
{
|
|
|
|
|
|
log.Error("Failed to set log level: " + exc);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 14:17:57 +01:00
|
|
|
|
public string GetSpr()
|
|
|
|
|
|
{
|
|
|
|
|
|
return CrashCheck(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var endpoint = GetEndpoint();
|
|
|
|
|
|
var json = endpoint.HttpGetString("spr");
|
|
|
|
|
|
var response = JsonConvert.DeserializeObject<SprResponse>(json);
|
|
|
|
|
|
return response!.Spr;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class SprResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
public string Spr { get; set; } = string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-26 10:31:49 +01:00
|
|
|
|
public DebugPeer GetDebugPeer(string peerId)
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2024-03-26 10:31:49 +01:00
|
|
|
|
// Cannot use openAPI: debug/peer endpoint is not specified there.
|
2024-06-14 09:05:56 +02:00
|
|
|
|
return CrashCheck(() =>
|
2023-05-04 11:34:43 +02:00
|
|
|
|
{
|
2024-06-14 09:05:56 +02:00
|
|
|
|
var endpoint = GetEndpoint();
|
|
|
|
|
|
var str = endpoint.HttpGetString($"debug/peer/{peerId}");
|
|
|
|
|
|
|
|
|
|
|
|
if (str.ToLowerInvariant() == "unable to find peer!")
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2024-06-14 09:05:56 +02:00
|
|
|
|
return new DebugPeer
|
|
|
|
|
|
{
|
|
|
|
|
|
IsPeerFound = false
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2023-06-29 16:07:49 +02:00
|
|
|
|
|
2024-06-14 09:05:56 +02:00
|
|
|
|
var result = endpoint.Deserialize<DebugPeer>(str);
|
|
|
|
|
|
result.IsPeerFound = true;
|
|
|
|
|
|
return result;
|
|
|
|
|
|
});
|
2023-06-29 16:07:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-26 12:14:02 +01:00
|
|
|
|
public void ConnectToPeer(string peerId, string[] peerMultiAddresses)
|
|
|
|
|
|
{
|
|
|
|
|
|
OnCodex(api =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Time.Wait(api.ConnectPeerAsync(peerId, peerMultiAddresses));
|
|
|
|
|
|
return Task.FromResult(string.Empty);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 11:31:50 +01:00
|
|
|
|
public string UploadFile(UploadInput uploadInput)
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2025-01-16 11:31:50 +01:00
|
|
|
|
return OnCodex(api => api.UploadAsync(uploadInput.ContentType, uploadInput.ContentDisposition, uploadInput.FileStream));
|
2023-06-29 16:07:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 11:31:50 +01:00
|
|
|
|
public Stream DownloadFile(string contentId)
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2025-01-16 11:31:50 +01:00
|
|
|
|
var fileResponse = OnCodex(api => api.DownloadNetworkStreamAsync(contentId));
|
2024-03-26 11:39:59 +01:00
|
|
|
|
if (fileResponse.StatusCode != 200) throw new Exception("Download failed with StatusCode: " + fileResponse.StatusCode);
|
|
|
|
|
|
return fileResponse.Stream;
|
2023-11-10 08:20:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 11:28:18 +01:00
|
|
|
|
public LocalDataset DownloadStreamless(ContentId cid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = OnCodex(api => api.DownloadNetworkAsync(cid.Id));
|
|
|
|
|
|
return mapper.Map(response);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public LocalDataset DownloadManifestOnly(ContentId cid)
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = OnCodex(api => api.DownloadNetworkManifestAsync(cid.Id));
|
|
|
|
|
|
return mapper.Map(response);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-26 14:07:06 +01:00
|
|
|
|
public LocalDatasetList LocalFiles()
|
2023-11-10 08:20:08 +01:00
|
|
|
|
{
|
2025-02-26 11:59:50 +01:00
|
|
|
|
return mapper.Map(OnCodex(api => api.ListDataAsync()));
|
2023-06-29 16:07:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-26 11:39:59 +01:00
|
|
|
|
public StorageAvailability SalesAvailability(StorageAvailability request)
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2024-03-26 15:12:28 +01:00
|
|
|
|
var body = mapper.Map(request);
|
2024-10-16 14:05:02 +02:00
|
|
|
|
var read = OnCodex(api => api.OfferStorageAsync(body));
|
2024-03-26 15:12:28 +01:00
|
|
|
|
return mapper.Map(read);
|
2023-06-29 16:07:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-23 10:52:12 +02:00
|
|
|
|
public StorageAvailability[] GetAvailabilities()
|
|
|
|
|
|
{
|
2024-10-16 14:05:02 +02:00
|
|
|
|
var collection = OnCodex(api => api.GetAvailabilitiesAsync());
|
2024-09-23 10:52:12 +02:00
|
|
|
|
return mapper.Map(collection);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-26 11:39:59 +01:00
|
|
|
|
public string RequestStorage(StoragePurchaseRequest request)
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2024-03-26 15:12:28 +01:00
|
|
|
|
var body = mapper.Map(request);
|
2024-10-16 14:05:02 +02:00
|
|
|
|
return OnCodex(api => api.CreateStorageRequestAsync(request.ContentId.Id, body));
|
2023-06-29 16:07:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-05 09:20:00 +02:00
|
|
|
|
public CodexSpace Space()
|
|
|
|
|
|
{
|
2024-10-16 14:05:02 +02:00
|
|
|
|
var space = OnCodex(api => api.SpaceAsync());
|
2024-06-05 09:20:00 +02:00
|
|
|
|
return mapper.Map(space);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-03 16:27:10 +01:00
|
|
|
|
public StoragePurchase? GetPurchaseStatus(string purchaseId)
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2025-04-08 13:07:55 +02:00
|
|
|
|
var purchase = OnCodex(api => api.GetPurchaseAsync(purchaseId));
|
|
|
|
|
|
return mapper.Map(purchase);
|
2023-06-29 16:07:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-24 11:32:32 +02:00
|
|
|
|
public string GetName()
|
|
|
|
|
|
{
|
2025-01-15 15:00:25 +01:00
|
|
|
|
return instance.Name;
|
2023-08-24 11:32:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-15 15:00:25 +01:00
|
|
|
|
public Address GetDiscoveryEndpoint()
|
2023-11-06 14:33:47 +01:00
|
|
|
|
{
|
2025-01-15 15:00:25 +01:00
|
|
|
|
return instance.DiscoveryEndpoint;
|
2023-11-06 14:33:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-15 16:05:57 +01:00
|
|
|
|
public Address GetApiEndpoint()
|
|
|
|
|
|
{
|
|
|
|
|
|
return instance.ApiEndpoint;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Address GetListenEndpoint()
|
|
|
|
|
|
{
|
|
|
|
|
|
return instance.ListenEndpoint;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 13:24:57 +01:00
|
|
|
|
public bool HasCrashed()
|
|
|
|
|
|
{
|
|
|
|
|
|
return processControl.HasCrashed();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-15 15:43:50 +01:00
|
|
|
|
public Address? GetMetricsEndpoint()
|
|
|
|
|
|
{
|
2025-01-16 10:15:02 +01:00
|
|
|
|
return instance.MetricsEndpoint;
|
2025-01-15 15:43:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public EthAccount? GetEthAccount()
|
|
|
|
|
|
{
|
2025-01-16 10:15:02 +01:00
|
|
|
|
return instance.EthAccount;
|
2025-01-15 15:43:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-15 15:00:25 +01:00
|
|
|
|
public void DeleteDataDirFolder()
|
2024-06-13 08:51:52 +02:00
|
|
|
|
{
|
2025-01-16 13:24:57 +01:00
|
|
|
|
processControl.DeleteDataDirFolder();
|
2024-06-13 08:51:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 15:16:00 +01:00
|
|
|
|
private T OnCodex<T>(Func<CodexApiClient, Task<T>> action)
|
2023-06-29 16:07:49 +02:00
|
|
|
|
{
|
2025-01-16 13:24:57 +01:00
|
|
|
|
var result = httpFactory.CreateHttp(GetHttpId(), h => CheckContainerCrashed()).OnClient(client => CallCodex(client, action));
|
2024-06-05 09:20:00 +02:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 15:16:00 +01:00
|
|
|
|
private T OnCodex<T>(Func<CodexApiClient, Task<T>> action, Retry retry)
|
2024-06-05 09:20:00 +02:00
|
|
|
|
{
|
2025-01-16 13:24:57 +01:00
|
|
|
|
var result = httpFactory.CreateHttp(GetHttpId(), h => CheckContainerCrashed()).OnClient(client => CallCodex(client, action), retry);
|
2024-03-25 15:46:45 +01:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2024-03-25 11:37:41 +01:00
|
|
|
|
|
2025-01-16 15:16:00 +01:00
|
|
|
|
private T CallCodex<T>(HttpClient client, Func<CodexApiClient, Task<T>> action)
|
2024-06-05 09:20:00 +02:00
|
|
|
|
{
|
|
|
|
|
|
var address = GetAddress();
|
2025-01-16 15:16:00 +01:00
|
|
|
|
var api = new CodexApiClient(client);
|
2024-06-05 09:20:00 +02:00
|
|
|
|
api.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1";
|
2024-06-14 09:05:56 +02:00
|
|
|
|
return CrashCheck(() => Time.Wait(action(api)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private T CrashCheck<T>(Func<T> action)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return action();
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
2025-01-16 13:24:57 +01:00
|
|
|
|
CheckContainerCrashed();
|
2024-06-14 09:05:56 +02:00
|
|
|
|
}
|
2024-06-05 09:20:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-26 10:31:49 +01:00
|
|
|
|
private IEndpoint GetEndpoint()
|
|
|
|
|
|
{
|
2025-01-16 11:31:50 +01:00
|
|
|
|
return httpFactory
|
2025-01-16 13:24:57 +01:00
|
|
|
|
.CreateHttp(GetHttpId(), h => CheckContainerCrashed())
|
2025-01-15 15:00:25 +01:00
|
|
|
|
.CreateEndpoint(GetAddress(), "/api/codex/v1/", GetName());
|
2024-03-26 10:31:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-19 11:18:59 +02:00
|
|
|
|
private Address GetAddress()
|
|
|
|
|
|
{
|
2025-01-15 15:00:25 +01:00
|
|
|
|
return instance.ApiEndpoint;
|
2023-10-10 18:08:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-25 10:10:11 +02:00
|
|
|
|
private string GetHttpId()
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetAddress().ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 13:24:57 +01:00
|
|
|
|
private void CheckContainerCrashed()
|
2023-08-15 11:01:18 +02:00
|
|
|
|
{
|
2025-01-16 13:24:57 +01:00
|
|
|
|
if (processControl.HasCrashed()) throw new Exception($"Container {GetName()} has crashed.");
|
2023-08-15 11:01:18 +02:00
|
|
|
|
}
|
2024-06-05 09:20:00 +02:00
|
|
|
|
|
|
|
|
|
|
private void Throw(Failure failure)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw failure.Exception;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-12 10:48:52 +02:00
|
|
|
|
private void Log(string msg)
|
|
|
|
|
|
{
|
2025-01-31 14:24:55 +01:00
|
|
|
|
log.Log($"({GetName()}) {msg}");
|
2024-06-12 10:48:52 +02:00
|
|
|
|
}
|
2023-04-18 15:33:12 +02:00
|
|
|
|
}
|
2024-10-30 08:34:41 +01:00
|
|
|
|
|
|
|
|
|
|
public class UploadInput
|
|
|
|
|
|
{
|
|
|
|
|
|
public UploadInput(string contentType, string contentDisposition, FileStream fileStream)
|
|
|
|
|
|
{
|
|
|
|
|
|
ContentType = contentType;
|
|
|
|
|
|
ContentDisposition = contentDisposition;
|
|
|
|
|
|
FileStream = fileStream;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string ContentType { get; }
|
|
|
|
|
|
public string ContentDisposition { get; }
|
|
|
|
|
|
public FileStream FileStream { get; }
|
|
|
|
|
|
}
|
2023-04-12 13:53:55 +02:00
|
|
|
|
}
|