// Copyright Epic Games, Inc. All Rights Reserved.
using AutomationTool;
using EpicGames.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using UnrealBuildBase;
namespace AutomationTool.Tasks
{
///
/// Parameters for a Helm task
///
public class HelmTaskParameters
{
///
/// Helm command line arguments
///
[TaskParameter]
public string Chart;
///
/// Name of the release
///
[TaskParameter]
public string Deployment;
///
/// The Kubernetes namespace
///
[TaskParameter(Optional = true)]
public string Namespace;
///
/// The kubectl context
///
[TaskParameter(Optional = true)]
public string KubeContext;
///
/// The kubectl config file to use
///
[TaskParameter(Optional = true)]
public string KubeConfig;
///
/// Values to set for running the chart
///
[TaskParameter(Optional = true)]
public string Values;
///
/// Values to set for running the chart
///
[TaskParameter(Optional = true)]
public string ValuesFile;
///
/// Environment variables to set
///
[TaskParameter(Optional = true)]
public string Environment;
///
/// File to parse environment variables from
///
[TaskParameter(Optional = true)]
public string EnvironmentFile;
///
/// Additional arguments
///
[TaskParameter(Optional = true)]
public string Arguments;
///
/// Base directory for running the command
///
[TaskParameter(Optional = true)]
public string WorkingDir;
}
///
/// Spawns Helm and waits for it to complete.
///
[TaskElement("Helm", typeof(HelmTaskParameters))]
public class HelmTask : SpawnTaskBase
{
///
/// Parameters for this task
///
HelmTaskParameters Parameters;
///
/// Construct a Helm task
///
/// Parameters for the task
public HelmTask(HelmTaskParameters 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)
{
// Build the argument list
List Arguments = new List();
Arguments.Add("upgrade");
Arguments.Add(Parameters.Deployment);
Arguments.Add(new FileReference(Parameters.Chart).FullName);
Arguments.Add("--install");
Arguments.Add("--reset-values");
if(Parameters.Namespace != null)
{
Arguments.Add("--namespace");
Arguments.Add(Parameters.Namespace);
}
if (Parameters.KubeContext != null)
{
Arguments.Add("--kube-context");
Arguments.Add(Parameters.KubeContext);
}
if (Parameters.KubeConfig != null)
{
Arguments.Add("--kubeconfig");
Arguments.Add(Parameters.KubeConfig);
}
if (!string.IsNullOrEmpty(Parameters.Values))
{
foreach (string Value in SplitDelimitedList(Parameters.Values))
{
Arguments.Add("--set");
Arguments.Add(Value);
}
}
if (!String.IsNullOrEmpty(Parameters.ValuesFile))
{
foreach (FileReference ValuesFile in ResolveFilespec(Unreal.RootDirectory, Parameters.ValuesFile, TagNameToFileSet))
{
Arguments.Add("--values");
Arguments.Add(ValuesFile.FullName);
}
}
string AdditionalArguments = String.IsNullOrEmpty(Parameters.Arguments) ? String.Empty : $" {Parameters.Arguments}";
await SpawnTaskBase.ExecuteAsync("helm", CommandLineArguments.Join(Arguments) + AdditionalArguments, WorkingDir: Parameters.WorkingDir, EnvVars: ParseEnvVars(Parameters.Environment, Parameters.EnvironmentFile));
}
///
/// 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;
}
}
}