@@ -122,7 +122,7 @@ func getContainerInstanceArnsForService(ecs_obj *ecs.ECS, cluster string, servic
122122 }
123123
124124 if len (list_tasks_resp .TaskArns ) <= 0 {
125- fmt .Println ("No ECS tasks found with specified filter - cluster:" , cluster , ",service:" , service )
125+ fmt .Println ("No ECS tasks found with specified filter - cluster: " , cluster , ", service:" , service )
126126 os .Exit (1 )
127127 }
128128
@@ -239,21 +239,29 @@ func getEc2PrivateIpsFromInstanceIds(ec2_obj *ec2.EC2, instance_ids []string, de
239239 return result
240240}
241241
242- func doMain (current_cluster bool , cluster_name string , ecs_service string , debug bool ) {
243- // Get the metadata from the ECS agent on the local Docker host.
244- local_ecs_agent_metadata := getEcsAgentMetadata ()
242+ func doMain (current_cluster bool , aws_region string , cluster_name string , ecs_service string , debug bool ) {
243+ var local_ecs_agent_metadata EcsAgentMetadata
244+ if current_cluster == true {
245+ // Get the metadata from the ECS agent on the local Docker host.
246+ local_ecs_agent_metadata = getEcsAgentMetadata ()
247+ }
245248
246- // Discover the region which this instance resides.
247- metadata := ec2metadata .New (session .New ())
248- region , err := metadata .Region ()
249- if err != nil {
250- fmt .Println ("Cannot retrieve AWS region from EC2 Metadata Service:" )
251- formatAwsError (err )
252- os .Exit (1 )
249+ var region string
250+ if aws_region != "" {
251+ region = aws_region
252+ } else {
253+ // Discover the region which this instance resides.
254+ metadata := ec2metadata .New (session .New ())
255+ use_region , err := metadata .Region ()
256+ if err != nil {
257+ fmt .Println ("Cannot retrieve AWS region from EC2 Metadata Service:" )
258+ formatAwsError (err )
259+ os .Exit (1 )
260+ }
261+ region = use_region
253262 }
254263
255264 // Reusable config session object for AWS services with current region attached.
256- // TODOLATER: support other regions via a flag.
257265 aws_config_session := session .New (& aws.Config {Region : aws .String (region )})
258266
259267 // Create an ECS service object.
@@ -279,8 +287,12 @@ func doMain(current_cluster bool, cluster_name string, ecs_service string, debug
279287 // readme states ports are outside the scope for now.
280288
281289 // Get all tasks for the given service, in this ECS cluster. We exclude the current container instance in the result,
282- // as we only need to know about all other instances.
283- container_instances := getContainerInstanceArnsForService (ecs_obj , ecs_cluster , ecs_service , local_ecs_agent_metadata .ContainerInstanceArn , debug )
290+ // as we only need to know about all other instances. The exclusion only occurs when we are working on the current cluster.
291+ current_container_instance_arn := "NONE"
292+ if current_cluster == true {
293+ current_container_instance_arn = local_ecs_agent_metadata .ContainerInstanceArn
294+ }
295+ container_instances := getContainerInstanceArnsForService (ecs_obj , ecs_cluster , ecs_service , current_container_instance_arn , debug )
284296 if debug == true {
285297 fmt .Println ("container_instances:" , strings .Join (container_instances , "," ))
286298 }
@@ -300,30 +312,38 @@ func doMain(current_cluster bool, cluster_name string, ecs_service string, debug
300312 fmt .Println (strings .Join (instance_private_ips , "," ))
301313}
302314
303- func parseFlags (c * cli.Context ) (bool , string , string , bool ) {
315+ // current_cluster, aws_region, cluster, service, debug
316+ func parseFlags (c * cli.Context ) (bool , string , string , string , bool ) {
304317 current_cluster := false
305318 cluster := ""
319+ aws_region := ""
306320 if c .String ("c" ) == "" {
307321 current_cluster = true
308322 } else {
309323 cluster = c .String ("c" )
324+ if c .String ("r" ) == "" {
325+ fmt .Printf ("Error: If Cluster (-c) is specified, AWS Region (-r) is also required. Cannot proceed.\n \n " )
326+ cli .ShowAppHelp (c )
327+ os .Exit (1 )
328+ }
329+ aws_region = c .String ("r" )
310330 }
311331 if c .String ("s" ) == "" {
312332 fmt .Printf ("Error: Service (-s) must not be empty. Cannot proceed.\n \n " )
313333 cli .ShowAppHelp (c )
314334 os .Exit (1 )
315335 }
316- return current_cluster , cluster , c .String ("s" ), c .Bool ("d" )
336+ return current_cluster , aws_region , cluster , c .String ("s" ), c .Bool ("d" )
317337}
318338
319339func main () {
320340 app := cli .NewApp ()
321341 app .Name = "ecs-discoverer"
322- app .Version = "0.3.1 "
342+ app .Version = "0.3.2 "
323343 app .Usage = "Discovery tool for Private IPs of ECS EC2 Container Instances for a given Service/Cluster"
324344 app .Action = func (c * cli.Context ) {
325- current_cluster , cluster , service , debug := parseFlags (c )
326- doMain (current_cluster , cluster , service , debug )
345+ current_cluster , aws_region , cluster , service , debug := parseFlags (c )
346+ doMain (current_cluster , aws_region , cluster , service , debug )
327347 }
328348 app .Flags = []cli.Flag {
329349 cli.StringFlag {
@@ -335,6 +355,11 @@ func main() {
335355 Name : "d" ,
336356 Usage : "Debug Mode" ,
337357 },
358+ cli.StringFlag {
359+ Name : "r" ,
360+ Value : "" ,
361+ Usage : "AWS Region (Optional - defaults to the location of this ECS Cluster instance)" ,
362+ },
338363 cli.StringFlag {
339364 Name : "s" ,
340365 Value : "" ,
0 commit comments