113 lines
3.4 KiB
C#
113 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using ln.build.repositories;
|
|
using ln.http;
|
|
using ln.http.client;
|
|
using ln.http.router;
|
|
using ln.json;
|
|
using ln.logging;
|
|
using ln.threading;
|
|
using ln.type;
|
|
|
|
namespace ln.build
|
|
{
|
|
public class CIService
|
|
{
|
|
public Pool buildPool;
|
|
public string ReportsDirectory { get; set; } = "./builds";
|
|
public Endpoint HttpEndpoint { get; } = new Endpoint(IPv6.ANY, 1888);
|
|
|
|
Logger webLogger;
|
|
SimpleRouter httpRouter;
|
|
SimpleRouter hookRouter;
|
|
HTTPServer httpServer;
|
|
|
|
List<RepositoryInterface> repositoryInterfaces = new List<RepositoryInterface>();
|
|
|
|
HashSet<PipeLine> pipelines = new HashSet<PipeLine>();
|
|
public IEnumerable<PipeLine> PipeLines => pipelines;
|
|
|
|
public CIService()
|
|
{
|
|
buildPool = new Pool(2);
|
|
ReportsDirectory = Path.GetFullPath(ReportsDirectory);
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
Directory.CreateDirectory(ReportsDirectory);
|
|
|
|
InitializeHttpServer();
|
|
}
|
|
|
|
private void InitializeHttpServer()
|
|
{
|
|
httpRouter = new SimpleRouter();
|
|
httpRouter.AddSimpleRoute("/builds/*", new StaticRouter(ReportsDirectory));
|
|
|
|
hookRouter = new SimpleRouter();
|
|
httpRouter.AddSimpleRoute("/hooks/*", hookRouter);
|
|
|
|
webLogger = new Logger(Logger.ConsoleLogger);
|
|
webLogger.Backends.Add(new FileLogger("ln.build.http.log"));
|
|
|
|
httpServer = new HTTPServer(HttpEndpoint, new LoggingRouter(httpRouter, webLogger));
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
buildPool.Start();
|
|
httpServer.Start();
|
|
}
|
|
|
|
public void AddPipeline(PipeLine pipeLine)
|
|
{
|
|
pipelines.Add(pipeLine);
|
|
}
|
|
|
|
public void AddRepositoryInterface(RepositoryInterface repositoryInterface)
|
|
{
|
|
hookRouter.AddSimpleRoute(String.Format("/{0}", repositoryInterface.WebHookName), (HttpRoutingContext context,HttpRequest request) => repositoryInterface.WebHookHandler(this, request));
|
|
repositoryInterfaces.Add(repositoryInterface);
|
|
}
|
|
|
|
|
|
public void Enqueue(CIJob job)
|
|
{
|
|
job.CurrentCIService = this;
|
|
buildPool.Enqueue(job);
|
|
}
|
|
|
|
public void CreateReport(CIJob job)
|
|
{
|
|
JSONObject report = new JSONObject();
|
|
JSONArray steps = new JSONArray();
|
|
report["steps"] = steps;
|
|
report["state"] = new JSONString(job.BuildState.ToString());
|
|
|
|
string reportDirectory = Path.Combine(ReportsDirectory, job.JobID);
|
|
Directory.CreateDirectory(reportDirectory);
|
|
|
|
foreach (string logName in job.GetLogStreamNames())
|
|
{
|
|
string logFileName = Path.Combine(reportDirectory, logName);
|
|
using (FileStream fileStream = new FileStream(logFileName, FileMode.CreateNew))
|
|
{
|
|
job.GetLogStream(logName).Position = 0;
|
|
job.GetLogStream(logName).CopyTo(fileStream);
|
|
fileStream.Flush();
|
|
fileStream.Close();
|
|
}
|
|
|
|
steps.Add(new JSONString(logName));
|
|
}
|
|
|
|
using (StreamWriter writer = new StreamWriter(Path.Combine(reportDirectory, "build.json")))
|
|
writer.Write(report.ToString());
|
|
}
|
|
|
|
|
|
|
|
}
|
|
} |