// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.IO; using System.Text.Json; using System.Threading.Tasks; using System.Xml; using EpicGames.Core; using EpicGames.Horde; using EpicGames.Horde.Server; using EpicGames.Horde.Storage; using EpicGames.Horde.Storage.Nodes; using EpicGames.Horde.Tools; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; #nullable enable namespace AutomationTool.Tasks { /// /// Parameters for a DeployTool task /// public class DeployToolTaskParameters { /// /// Identifier for the tool /// [TaskParameter] public string Id { get; set; } = String.Empty; /// /// Settings file to use for the deployment. Should be a JSON file containing server name and access token. /// [TaskParameter(Optional = true)] public string Settings { get; set; } = String.Empty; /// /// Version number for the new tool /// [TaskParameter] public string Version { get; set; } = String.Empty; /// /// Duration over which to roll out the tool, in minutes. /// [TaskParameter(Optional = true)] public int Duration { get; set; } = 0; /// /// Whether to create the deployment as paused /// [TaskParameter(Optional = true)] public bool Paused { get; set; } = false; /// /// Zip file containing files to upload /// [TaskParameter(Optional = true)] public string? File { get; set; } = null!; /// /// Directory to upload for the tool /// [TaskParameter(Optional = true)] public string? Directory { get; set; } = 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; } } readonly DeployToolTaskParameters _parameters; /// /// Construct a Helm task /// /// Parameters for the task public DeployToolTask(DeployToolTaskParameters parameters) { _parameters = parameters; } /// /// ExecuteAsync 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) { DeploySettings? settings = null; if (!String.IsNullOrEmpty(_parameters.Settings)) { 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 }; 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 => { if (!String.IsNullOrEmpty(settings?.Server)) { options.ServerUrl = new Uri(settings.Server); } if (!String.IsNullOrEmpty(settings?.Token)) { options.AccessToken = settings.Token; } }); serviceCollection.AddLogging(builder => builder.AddProvider(CommandUtils.ServiceProvider.GetRequiredService())); serviceCollection.AddHttpClient(); serviceCollection.AddHorde(); await using ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider(); IHordeClient hordeClient = (settings == null) ? CommandUtils.ServiceProvider.GetRequiredService() : serviceProvider.GetRequiredService(); using HordeHttpClient hordeHttpClient = hordeClient.CreateHttpClient(); GetServerInfoResponse infoResponse = await hordeHttpClient.GetServerInfoAsync(); Logger.LogInformation("Uploading {ToolId} to {ServerUrl} (Version: {Version}, API v{ApiVersion})...", toolId, hordeClient.ServerUrl, infoResponse.ServerVersion, (int)infoResponse.ApiVersion); BlobSerializerOptions serializerOptions = BlobSerializerOptions.Create(infoResponse.ApiVersion); IHashedBlobRef handle; IStorageNamespace storageNamespace = hordeClient.GetStorageNamespace(toolId); await using (IBlobWriter blobWriter = storageNamespace.CreateBlobWriter($"{toolId}", 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; } HashedBlobRefValue 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; } } }