in src/Amazon.ECS.Tools/Commands/DeployScheduledTaskCommand.cs [79:208]
protected override async Task<bool> PerformActionAsync()
{
var skipPush = this.GetBoolValueOrDefault(this.DeployScheduledTaskProperties.SkipImagePush, ECSDefinedCommandOptions.ARGUMENT_SKIP_IMAGE_PUSH, false).GetValueOrDefault();
var ecsContainer = this.GetStringValueOrDefault(this.TaskDefinitionProperties.ContainerName, ECSDefinedCommandOptions.ARGUMENT_CONTAINER_NAME, true);
var ecsTaskDefinition = this.GetStringValueOrDefault(this.TaskDefinitionProperties.TaskDefinitionName, ECSDefinedCommandOptions.ARGUMENT_TD_NAME, true);
var ecsPlatformVersion = this.GetStringValueOrDefault(this.TaskDefinitionProperties.TaskPlatformVersion, ECSDefinedCommandOptions.ARGUMENT_TD_PLATFORM_VERSION, false);
this.PushDockerImageProperties.DockerImageTag = GetDockerImageTag();
if (!this.PushDockerImageProperties.DockerImageTag.Contains(":"))
this.PushDockerImageProperties.DockerImageTag += ":latest";
if (skipPush)
{
this.PushDockerImageProperties.DockerImageTag = await ECSUtilities.ExpandImageTagIfNecessary(this.Logger, this.ECRClient, this.PushDockerImageProperties.DockerImageTag);
}
else
{
var pushCommand = new PushDockerImageCommand(this.Logger, this.WorkingDirectory, this.OriginalCommandLineArguments)
{
ConfigFile = this.ConfigFile,
DisableInteractive = this.DisableInteractive,
Credentials = this.Credentials,
ECRClient = this.ECRClient,
Profile = this.Profile,
ProfileLocation = this.ProfileLocation,
ProjectLocation = this.ProjectLocation,
Region = this.Region,
WorkingDirectory = this.WorkingDirectory,
PushDockerImageProperties = this.PushDockerImageProperties,
};
var success = await pushCommand.ExecuteAsync();
if (!success)
{
if (pushCommand.LastException != null)
{
throw pushCommand.LastException;
}
return false;
}
this.PushDockerImageProperties.DockerImageTag = pushCommand.PushedImageUri;
}
var taskDefinitionArn = await ECSUtilities.CreateOrUpdateTaskDefinition(this.Logger, this.ECSClient,
this, this.TaskDefinitionProperties, this.PushDockerImageProperties.DockerImageTag, IsFargateLaunch(this.ClusterProperties.LaunchType));
var ecsCluster = this.GetStringValueOrDefault(this.ClusterProperties.ECSCluster, ECSDefinedCommandOptions.ARGUMENT_ECS_CLUSTER, true);
await ECSUtilities.EnsureClusterExistsAsync(this.Logger, this.ECSClient, ecsCluster);
if (!ecsCluster.Contains(":"))
{
var arnPrefix = taskDefinitionArn.Substring(0, taskDefinitionArn.LastIndexOf(":task"));
ecsCluster = arnPrefix + ":cluster/" + ecsCluster;
}
var ruleName = this.GetStringValueOrDefault(this.DeployScheduledTaskProperties.ScheduleTaskRule, ECSDefinedCommandOptions.ARGUMENT_SCHEDULED_RULE_NAME, true);
var targetName = this.GetStringValueOrDefault(this.DeployScheduledTaskProperties.ScheduleTaskRuleTarget, ECSDefinedCommandOptions.ARGUMENT_SCHEDULED_RULE_TARGET, false);
if (string.IsNullOrEmpty(targetName))
targetName = ruleName;
var scheduleExpression = this.GetStringValueOrDefault(this.DeployScheduledTaskProperties.ScheduleExpression, ECSDefinedCommandOptions.ARGUMENT_SCHEDULE_EXPRESSION, true);
var cweRole = this.GetStringValueOrDefault(this.DeployScheduledTaskProperties.CloudWatchEventIAMRole, ECSDefinedCommandOptions.ARGUMENT_CLOUDWATCHEVENT_ROLE, true);
var desiredCount = this.GetIntValueOrDefault(this.DeployScheduledTaskProperties.DesiredCount, ECSDefinedCommandOptions.ARGUMENT_ECS_DESIRED_COUNT, false);
if (!desiredCount.HasValue)
desiredCount = 1;
var launchType = this.GetStringValueOrDefault(this.ClusterProperties.LaunchType, ECSDefinedCommandOptions.ARGUMENT_LAUNCH_TYPE, true);
var ecsParameters = new EcsParameters
{
TaskCount = desiredCount.Value,
TaskDefinitionArn = taskDefinitionArn,
LaunchType = launchType
};
if (IsFargateLaunch(this.ClusterProperties.LaunchType))
{
var networkConfiguration = new Amazon.CloudWatchEvents.Model.NetworkConfiguration();
await ECSUtilities.SetupAwsVpcNetworkConfigurationCloudwatchEventAsync(this, networkConfiguration);
ecsParameters.NetworkConfiguration = networkConfiguration;
ecsParameters.PlatformVersion = ecsPlatformVersion;
await this.AttemptToCreateServiceLinkRoleAsync();
}
string ruleArn = null;
try
{
ruleArn = (await this.CWEClient.PutRuleAsync(new PutRuleRequest
{
Name = ruleName,
ScheduleExpression = scheduleExpression,
State = RuleState.ENABLED
})).RuleArn;
this.Logger?.WriteLine($"Put CloudWatch Event rule {ruleName} with expression {scheduleExpression}");
}
catch(Exception e)
{
throw new DockerToolsException("Error creating CloudWatch Event rule: " + e.Message, DockerToolsException.ECSErrorCode.PutRuleFail);
}
try
{
await this.CWEClient.PutTargetsAsync(new PutTargetsRequest
{
Rule = ruleName,
Targets = new List<Target>
{
new Target
{
Arn = ecsCluster,
RoleArn = cweRole,
Id = targetName,
EcsParameters = ecsParameters
}
}
});
this.Logger?.WriteLine($"Put CloudWatch Event target {targetName}");
}
catch (Exception e)
{
throw new DockerToolsException("Error creating CloudWatch Event target: " + e.Message, DockerToolsException.ECSErrorCode.PutTargetFail);
}
return true;
}