// Copyright Epic Games, Inc. All Rights Reserved.
using AutomationTool;
using EpicGames.Core;
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using UnrealBuildBase;
using Microsoft.Extensions.Logging;
using EpicGames.Horde.Storage.Bundles;
using EpicGames.Horde.Storage.Clients;
using EpicGames.Horde.Storage;
using EpicGames.Horde.Storage.Nodes;
using System.Threading;
using System.Data;
using EpicGames.Horde.Storage.Backends;
using Microsoft.Extensions.DependencyInjection;
using EpicGames.Horde;
using EpicGames.Horde.Tools;
using EpicGames.Horde.Server;
#nullable enable
namespace AutomationTool.Tasks
{
///
/// Parameters for a DeployTool task
///
public class DeployToolTaskParameters
{
///
/// Identifier for the tool
///
[TaskParameter]
public string Id = String.Empty;
///
/// Settings file to use for the deployment. Should be a JSON file containing server name and access token.
///
[TaskParameter]
public string Settings = String.Empty;
///
/// Version number for the new tool
///
[TaskParameter]
public string Version = String.Empty;
///
/// Duration over which to roll out the tool, in minutes.
///
[TaskParameter(Optional = true)]
public int Duration = 0;
///
/// Whether to create the deployment as paused
///
[TaskParameter(Optional = true)]
public bool Paused = false;
///
/// Zip file containing files to upload
///
[TaskParameter(Optional = true)]
public string? File = null!;
///
/// Directory to upload for the tool
///
[TaskParameter(Optional = true)]
public string? Directory = null!;
}
///
/// Deploys a tool update through Horde
///
[TaskElement("DeployTool", typeof(DeployToolTaskParameters))]
public class DeployToolTask : SpawnTaskBase
{
class DeploySettings
{
public string Server { get; set; } = String.Empty;
public string? Token { get; set; }
}
///
/// Options for a new deployment
///
class CreateDeploymentRequest
{
public string Version { get; set; } = "Unknown";
public double? Duration { get; set; }
public bool? CreatePaused { get; set; }
public string? Node { get; set; }
}
///
/// Parameters for this task
///
DeployToolTaskParameters Parameters;
///
/// Construct a Helm task
///
/// Parameters for the task
public DeployToolTask(DeployToolTaskParameters InParameters)
{
Parameters = InParameters;
}
///
/// Execute the task.
///
/// Information about the current job
/// Set of build products produced by this node.
/// Mapping from tag names to the set of files they include
public override async Task ExecuteAsync(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet)
{
FileReference settingsFile = ResolveFile(Parameters.Settings);
if (!FileReference.Exists(settingsFile))
{
throw new AutomationException($"Settings file '{settingsFile}' does not exist");
}
byte[] settingsData = await FileReference.ReadAllBytesAsync(settingsFile);
JsonSerializerOptions jsonOptions = new JsonSerializerOptions { AllowTrailingCommas = true, ReadCommentHandling = JsonCommentHandling.Skip, PropertyNameCaseInsensitive = true };
DeploySettings? settings = JsonSerializer.Deserialize(settingsData, jsonOptions);
if (settings == null)
{
throw new AutomationException($"Unable to read settings file {settingsFile}");
}
else if (settings.Server == null)
{
throw new AutomationException($"Missing 'server' key from {settingsFile}");
}
ToolId toolId = new ToolId(Parameters.Id);
ServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.Configure(options =>
{
options.ServerUrl = new Uri(settings.Server);
options.AccessToken = settings.Token;
});
serviceCollection.AddHttpClient();
serviceCollection.AddHorde();
await using ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
IHordeClient hordeClient = serviceProvider.GetRequiredService();
using HordeHttpClient hordeHttpClient = hordeClient.CreateHttpClient();
GetServerInfoResponse infoResponse = await hordeHttpClient.GetServerInfoAsync();
Logger.LogInformation("Uploading {ToolId} to {ServerUrl} (Version: {Version}, API v{ApiVersion})...", toolId, settings.Server, infoResponse.ServerVersion, (int)infoResponse.ApiVersion);
BlobSerializerOptions serializerOptions = BlobSerializerOptions.Create(infoResponse.ApiVersion);
IBlobRef handle;
using IStorageClient storageClient = hordeClient.CreateStorageClient(toolId);
await using (IBlobWriter blobWriter = storageClient.CreateBlobWriter(serializerOptions: serializerOptions))
{
DirectoryNode sandbox = new DirectoryNode();
if (Parameters.File != null)
{
using FileStream stream = FileReference.Open(ResolveFile(Parameters.File), FileMode.Open, FileAccess.Read);
await sandbox.CopyFromZipStreamAsync(stream, blobWriter, new ChunkingOptions());
}
else if (Parameters.Directory != null)
{
DirectoryInfo directoryInfo = ResolveDirectory(Parameters.Directory).ToDirectoryInfo();
await sandbox.AddFilesAsync(directoryInfo, blobWriter);
}
else
{
throw new AutomationException("Either File=... or Directory=... must be specified");
}
handle = await blobWriter.WriteBlobAsync(sandbox);
await blobWriter.FlushAsync();
}
double? duration = null;
if (Parameters.Duration != 0)
{
duration = Parameters.Duration;
}
bool? createPaused = null;
if (Parameters.Paused)
{
createPaused = true;
}
BlobRefValue locator = handle.GetRefValue();
ToolDeploymentId deploymentId = await hordeHttpClient.CreateToolDeploymentAsync(toolId, Parameters.Version, duration, createPaused, locator);
Logger.LogInformation("Created {ToolId} deployment {DeploymentId}", toolId, deploymentId);
}
///
/// Output this task out to an XML writer.
///
public override void Write(XmlWriter Writer)
{
Write(Writer, Parameters);
}
///
/// Find all the tags which are used as inputs to this task
///
/// The tag names which are read by this task
public override IEnumerable FindConsumedTagNames()
{
yield break;
}
///
/// Find all the tags which are modified by this task
///
/// The tag names which are modified by this task
public override IEnumerable FindProducedTagNames()
{
yield break;
}
}
}