// Copyright Epic Games, Inc. All Rights Reserved. using EpicGames.Core; using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; 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 Release; /// /// The Kubernetes namespace /// [TaskParameter(Optional = true)] public string Namespace; /// /// The kubectl context /// [TaskParameter(Optional = true)] public string KubeContext; /// /// Values to set for running the chart /// [TaskParameter(Optional = true)] public List Values; /// /// 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 BaseDir; } /// /// Spawns Helm and waits for it to complete. /// [TaskElement("Helm", typeof(HelmTaskParameters))] public class HelmTask : CustomTask { /// /// 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 void Execute(JobContext Job, HashSet BuildProducts, Dictionary> TagNameToFileSet) { FileReference HelmExe = CommandUtils.FindToolInPath("helm"); if(HelmExe == null) { throw new AutomationException("Unable to find path to Helm. Check you have it installed, and it is on your PATH."); } Dictionary Environment = new Dictionary(); if (Parameters.EnvironmentFile != null) { } // Switch Kubernetes config if (Parameters.KubeContext != null) { FileReference KubectlExe = CommandUtils.FindToolInPath("kubectl"); if (KubectlExe == null) { throw new AutomationException("Unable to find path to Kubectl. Check you have it installed, and it is on your PATH."); } IProcessResult KubectlResult = CommandUtils.Run(KubectlExe.FullName, $"config use-context {Parameters.KubeContext}", null, WorkingDir: Parameters.BaseDir); if (KubectlResult.ExitCode != 0) { throw new AutomationException("Kubectl terminated with an exit code indicating an error ({0})", KubectlResult.ExitCode); } } // Build the argument list List Arguments = new List(); Arguments.Add("upgrade"); Arguments.Add(Parameters.Release); Arguments.Add(Parameters.Chart); Arguments.Add("--install"); Arguments.Add("--reset-values"); if(Parameters.Namespace != null) { Arguments.Add("--namespace"); Arguments.Add(Parameters.Namespace); } foreach (string Value in Parameters.Values) { Arguments.Add("--set"); Arguments.Add(Value); } Arguments.Add("--dry-run"); IProcessResult Result = CommandUtils.Run(HelmExe.FullName, CommandLineArguments.Join(Arguments), WorkingDir: Parameters.BaseDir); if (Result.ExitCode != 0) { throw new AutomationException("Helm terminated with an exit code indicating an error ({0})", Result.ExitCode); } } /// /// 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; } } }