// 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 an AWS CLI task /// public class AwsTaskParameters { /// /// AWS command line arguments /// [TaskParameter(Optional = true)] public string Arguments; /// /// Base directory for running the command /// [TaskParameter(Optional = true)] public string BaseDir; /// /// The minimum exit code, which is treated as an error. /// [TaskParameter(Optional = true)] public int ErrorLevel = 1; /// /// Whether or not to show the console output from the AWS CLI command. /// [TaskParameter(Optional = true)] public bool Verbose = false; /// /// Path to a file containing the credentials needed to run the AWS command /// [TaskParameter(Optional = true)] public string CredentialsFile; /// /// Path to a file to store any JSON output by the command. /// [TaskParameter(Optional = true)] public string OutputFile; } /// /// Spawns AWS CLI and waits for it to complete. /// [TaskElement("Aws", typeof(AwsTaskParameters))] public class AwsTask : CustomTask { /// /// Parameters for this task /// AwsTaskParameters Parameters; /// /// Construct an AWS CLI task /// /// Parameters for the task public AwsTask(AwsTaskParameters 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 ToolFile = CommandUtils.FindToolInPath("aws"); if(ToolFile == null) { throw new AutomationException("Unable to find path to AWS CLI. Check you have it installed, and it is on your PATH."); } string AWSArgs = Parameters.Arguments; Dictionary EnvVars = new Dictionary(); if (!string.IsNullOrWhiteSpace(Parameters.CredentialsFile)) { if (!File.Exists(Parameters.CredentialsFile)) { throw new AutomationException("Credentials file {0} could not be found.", Parameters.CredentialsFile); } string[] CredentialTokens = File.ReadAllText(Parameters.CredentialsFile).Split(':'); EnvVars.Add("AWS_ACCESS_KEY_ID", CredentialTokens[0]); EnvVars.Add("AWS_SECRET_ACCESS_KEY", CredentialTokens[1]); } CommandUtils.ERunOptions Options = CommandUtils.ERunOptions.AppMustExist; if (Parameters.Verbose) { Options |= CommandUtils.ERunOptions.AllowSpew; } IProcessResult Result = CommandUtils.Run(ToolFile.FullName, AWSArgs, WorkingDir: Parameters.BaseDir, Env: EnvVars, Options: Options); if (!string.IsNullOrWhiteSpace(Parameters.OutputFile)) { File.WriteAllText(Parameters.OutputFile, Result.Output); } if (Result.ExitCode < 0 || Result.ExitCode >= Parameters.ErrorLevel) { throw new AutomationException("AWS 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; } } }