Created
July 25, 2017 11:44
-
-
Save flou/fcde8b664142113195e2e3a1d0fa8b8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/ecs" | |
) | |
type EcsService struct { | |
ecs.Service | |
} | |
func main() { | |
sess := session.Must(session.NewSession()) | |
ecsClient := ecs.New(sess) | |
clusters, _ := listClusters(ecsClient) | |
for _, cluster := range clusters { | |
services, _ := listServices(ecsClient, &cluster) | |
split := strings.Split(cluster, "/") | |
clusterName := strings.ToUpper(split[len(split)-1]) | |
fmt.Println(fmt.Sprintf("---- %s : %d services", clusterName, len(services))) | |
for _, svc := range services { | |
svc.IsUp() | |
} | |
fmt.Println("") | |
} | |
} | |
func listClusters(ecsClient *ecs.ECS) ([]string, error) { | |
clusters := []string{} | |
ecsClient.ListClustersPages(&ecs.ListClustersInput{}, func(page *ecs.ListClustersOutput, lastPage bool) bool { | |
for _, arn := range page.ClusterArns { | |
clusters = append(clusters, *arn) | |
} | |
return true | |
}) | |
return clusters, nil | |
} | |
func listServices(ecsClient *ecs.ECS, cluster *string) ([]EcsService, error) { | |
services := []EcsService{} | |
input := &ecs.ListServicesInput{Cluster: cluster} | |
ecsClient.ListServicesPages(input, func(page *ecs.ListServicesOutput, lastPage bool) bool { | |
result, _ := ecsClient.DescribeServices(&ecs.DescribeServicesInput{Cluster: cluster, Services: page.ServiceArns}) | |
for _, service := range result.Services { | |
services = append(services, EcsService{*service}) | |
} | |
return true | |
}) | |
return services, nil | |
} | |
// IsUp checks that the service in ECS is running and stable | |
func (s *EcsService) IsUp() bool { | |
lastMessage := *s.Events[0].Message | |
status := false | |
if *s.DesiredCount == *s.RunningCount && strings.HasSuffix(lastMessage, "has reached a steady state.") { | |
status = true | |
} | |
return status | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment