public async Task <ActionResult> RunImage([FromBody] RunImageConfiguration runImageConfig) { try { await _containerService.RunImageAsync(runImageConfig); return(Ok(JsonConvert.SerializeObject("Started image successfully"))); } catch (InexistentTaskDefinition ex) { return(BadRequest(ex.Message)); } catch (AmazonECSException ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
/// <summary> /// Runs the image on ECS. /// </summary> /// <param name="name"></param> /// <returns></returns> public async System.Threading.Tasks.Task RunImageAsync(RunImageConfiguration runImageConfig) { var taskDefinitionsResponse = await _ecsClient.ListTaskDefinitionsAsync(new ListTaskDefinitionsRequest { FamilyPrefix = runImageConfig.ConfigName }); if (!taskDefinitionsResponse.TaskDefinitionArns.Any()) { throw new InexistentTaskDefinition($"Configuration does not exists: {runImageConfig.ConfigName}"); } var clustersResponse = await _ecsClient.ListClustersAsync(new ListClustersRequest()); string clusterArn; try { clusterArn = clustersResponse.ClusterArns.First(); } catch (Exception) { throw new InexistentCluster("No ECS clusters have been found"); } try { await _ecsClient.RunTaskAsync(new RunTaskRequest { Cluster = clusterArn, Group = runImageConfig.TaskGroupName, StartedBy = "StreamAnalysis User", Count = 1, LaunchType = Amazon.ECS.LaunchType.EC2, TaskDefinition = $"{runImageConfig.ConfigName}" }); } catch (AmazonECSException) { throw; } }