2024-03-25 14:46:45 +00:00
using CodexOpenApi ;
using Core ;
2023-09-11 09:59:33 +00:00
using KubernetesWorkflow ;
2023-11-12 09:07:23 +00:00
using KubernetesWorkflow.Types ;
2023-10-19 09:18:59 +00:00
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-12 08:31:55 +00:00
private readonly IPluginTools tools ;
2024-03-26 07:58:16 +00:00
private readonly Mapper mapper = new Mapper ( ) ;
2023-08-15 09:01:18 +00:00
private bool hasContainerCrashed ;
2023-04-30 08:08:32 +00:00
2023-09-20 10:55:09 +00:00
public CodexAccess ( IPluginTools tools , RunningContainer container , CrashWatcher crashWatcher )
2023-04-12 11:53:55 +00:00
{
2023-09-12 08:31:55 +00:00
this . tools = tools ;
2023-06-29 14:07:49 +00:00
Container = container ;
2023-09-20 10:55:09 +00:00
CrashWatcher = crashWatcher ;
2023-08-15 09:01:18 +00:00
hasContainerCrashed = false ;
2023-09-20 10:55:09 +00:00
CrashWatcher . Start ( this ) ;
2023-04-18 13:33:12 +00:00
}
2023-06-21 06:28:40 +00:00
public RunningContainer Container { get ; }
2023-09-20 10:55:09 +00:00
public CrashWatcher CrashWatcher { get ; }
2023-05-11 10:44:53 +00:00
2024-03-26 07:58:16 +00:00
public DebugInfo GetDebugInfo ( )
2023-05-04 09:34:43 +00:00
{
2024-03-26 07:58:16 +00:00
return Map ( OnCodex ( api = > api . GetDebugInfoAsync ( ) ) ) ;
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
2023-10-24 07:41:37 +00:00
var result = http . Deserialize < CodexDebugPeerResponse > ( str ) ;
2023-06-29 14:07:49 +00:00
result . IsPeerFound = true ;
return result ;
}
2023-10-09 10:33:35 +00:00
public CodexDebugBlockExchangeResponse GetDebugBlockExchange ( )
{
return Http ( ) . HttpGetJson < CodexDebugBlockExchangeResponse > ( "debug/blockexchange" ) ;
}
2023-10-10 15:54:19 +00:00
public CodexDebugRepoStoreResponse [ ] GetDebugRepoStore ( )
{
2023-10-10 16:08:21 +00:00
return LongHttp ( ) . HttpGetJson < CodexDebugRepoStoreResponse [ ] > ( "debug/repostore" ) ;
2023-10-10 15:54:19 +00:00
}
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 )
{
2023-11-10 07:20:08 +00:00
return Http ( ) . HttpPostStream ( "data" , fileStream ) ;
2023-06-29 14:07:49 +00:00
}
public Stream DownloadFile ( string contentId )
{
2023-11-23 12:32:58 +00:00
return Http ( ) . HttpGetStream ( "data/" + contentId + "/network" ) ;
2023-11-10 07:20:08 +00:00
}
public CodexLocalDataResponse [ ] LocalFiles ( )
{
2023-11-23 12:43:19 +00:00
return Http ( ) . HttpGetJson < CodexLocalDataResponse [ ] > ( "data" ) ;
2023-06-29 14:07:49 +00:00
}
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-11-06 13:33:47 +00:00
public PodInfo GetPodInfo ( )
{
var workflow = tools . CreateWorkflow ( ) ;
return workflow . GetPodInfo ( Container ) ;
}
2024-03-26 07:58:16 +00:00
private dynamic Map ( dynamic input )
{
return mapper . Map ( input ) ;
}
2024-03-25 14:46:45 +00:00
private T OnCodex < T > ( Func < CodexApi , Task < T > > action )
2023-06-29 14:07:49 +00:00
{
2024-03-25 10:37:41 +00:00
var address = GetAddress ( ) ;
2024-03-25 14:46:45 +00:00
var result = tools . CreateHttp ( ) . OnClient ( client = >
{
var api = new CodexApi ( client ) ;
api . BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1" ;
return Time . Wait ( action ( api ) ) ;
} ) ;
return result ;
}
2024-03-25 10:37:41 +00:00
2024-03-25 14:46:45 +00:00
//private IHttp Http()
//{
// var address = GetAddress();
// var api = new CodexOpenApi.CodexApi(new HttpClient());
// api.BaseUrl = $"{address.Host}:{address.Port}/api/codex/v1";
2024-03-25 10:37:41 +00:00
2024-03-25 14:46:45 +00:00
// var debugInfo = Time.Wait(api.GetDebugInfoAsync());
2024-03-25 10:37:41 +00:00
2024-03-25 14:46:45 +00:00
// using var stream = File.OpenRead("C:\\Users\\thatb\\Desktop\\Collect\\Wallpapers\\demerui_djinn_illuminatus_fullbody_full_body_view_in_the_style__86ea9491-1fe1-44ab-8577-a3636cad1b21.png");
// var cid = Time.Wait(api.UploadAsync(stream));
2024-03-25 10:37:41 +00:00
2024-03-25 14:46:45 +00:00
// var file = Time.Wait(api.DownloadNetworkAsync(cid));
// while (file.IsPartial) Thread.Sleep(100);
// using var outfile = File.OpenWrite("C:\\Users\\thatb\\Desktop\\output.png");
// file.Stream.CopyTo(outfile);
2023-08-15 09:01:18 +00:00
2024-03-25 14:46:45 +00:00
// return tools.CreateHttp(GetAddress(), baseUrl: "/api/codex/v1", CheckContainerCrashed, Container.Name);
//}
//private IHttp LongHttp()
//{
// return tools.CreateHttp(GetAddress(), baseUrl: "/api/codex/v1", CheckContainerCrashed, new LongTimeSet(), Container.Name);
//}
2023-10-19 09:18:59 +00:00
private Address GetAddress ( )
{
2023-11-07 11:02:17 +00:00
return Container . GetAddress ( tools . GetLog ( ) , CodexContainerRecipe . ApiPortTag ) ;
2023-10-10 16:08:21 +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 )
{
2023-09-12 08:31:55 +00:00
var log = tools . GetLog ( ) ;
2023-08-15 09:01:18 +00:00
var file = log . CreateSubfile ( ) ;
log . Log ( $"Container {Container.Name} has crashed. Downloading crash log to '{file.FullFilename}'..." ) ;
2023-10-16 09:19:57 +00:00
file . Write ( $"Container Crash Log for {Container.Name}." ) ;
2023-08-15 09:01:18 +00:00
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
}