Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispruitt authored Jan 17, 2019
1 parent 18001d1 commit 2d24708
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
VERSION=`go run main.go -version`
default:
env GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o build/get-service-versions.$(VERSION).windows-amd64.exe
env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/get-service-versions.$(VERSION).linux-amd64
env GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o build/get-service-versions.$(VERSION).darwin-amd64

build: default

tag:
git tag $(VERSION)
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# get-service-versions
A script to get versions of services in a cluster and the difference between two clusters
The version is pulled from the SSM VERSION parameter with the following syntax "/<cluster>/<service-name>/VERSION"


Usage:
get-service-versions [flags]

Flags:
--cluster string cluster to get list services and versions (default "dev")
--profile string override default AWS profile (default will discover default AWS profile)
--diffCluster string cluster to compare difference
--diffProfile string override AWS profile for '-diffCluster' only if a cluster exists in a different AWS account
--help help for get-service-versions
--version show version for get-service-versions


## Examples

Get service versions for every service running in a cluster

get-service-versions -cluster dev

Diff service versions for each service running in either cluster

get-service-versions -cluster dev -cluster tst

Diff service versions for each service in two clusters existing on different AWS accounts

get-service-versions -cluster tst -cluster prd -diffProfile my-prd-aws-profile
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/chrispruitt/get-service-versions

require (
github.com/aws/aws-sdk-go v1.16.20
github.com/fatih/color v1.7.0
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/aws/aws-sdk-go v1.16.20 h1:fNQRk/PXr8B4rU3olSLeJPu2w6y79U+bTKTF6JRI5C8=
github.com/aws/aws-sdk-go v1.16.20/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
147 changes: 147 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package main

import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/fatih/color"
"log"
"os"
"sort"
"strings"
)

var (
clusterPtr = flag.String("cluster", "dev", "cluster to get list services and versions")
profilePtr = flag.String("profile", "", "set AWS profile, default will discover default profile")
diffClusterPtr = flag.String("diffCluster", "", "cluster to compare difference")
diffProfilePtr = flag.String("diffProfile", "", "override AWS profile for '-diffCluster' only if a cluster exists in a different AWS account")
versionPtr = flag.Bool("version", false, "show current version")
)

func main() {
flag.Parse()

if *versionPtr {
fmt.Println("v0.0.0")
os.Exit(0)
}

if *diffProfilePtr == "" {*diffProfilePtr = *profilePtr}

serviceMap := getServiceVersions(*clusterPtr, *profilePtr)

if *diffClusterPtr != "" {
diffServiceMap := getServiceVersions(*diffClusterPtr, *diffProfilePtr)

fmt.Println(strings.ToUpper(*clusterPtr), " => ", strings.ToUpper(*diffClusterPtr))
printDiff(serviceMap, diffServiceMap)
} else {
fmt.Println(strings.ToUpper(*clusterPtr))
printMap(serviceMap)
}
}



func printDiff(x map[string]string, y map[string]string) {
emptyStr := ""

allKeys := append(getMapKeys(x), getMapKeys(y)...)
sort.Strings(allKeys)

for _, key := range allKeys {

if _, ok := x[key]; !ok {
x[key] = emptyStr
}

if _, ok := y[key]; !ok {
y[key] = emptyStr
}

if x[key] == emptyStr && y[key] != emptyStr {
color.Red(fmt.Sprint("- ", key, ": ", y[key]))
} else if x[key] != emptyStr && y[key] == emptyStr {
color.Green(fmt.Sprint("+ ", key, ": ", x[key]))
} else if x[key] != y[key] {
color.Yellow(fmt.Sprint("~ ", key, ": ", x[key], " => ", y[key]))
} else {
color.White(fmt.Sprint(" ", key, ": ", x[key], " = ", y[key]))
//fmt.Printf(" ", key, ": ", x[key], " = ", y[key])
}
}
}

func printMap(m map[string]string) {
for key, value := range m {
fmt.Println("\t" + key + ":", value)
}
}

func check(err error) {
if err != nil {
log.Fatal(err)
}
}

func parseServiceArnList(service_arn_list []*string) []string {
var result []string
for _, ptrs := range service_arn_list {
result = append(result, strings.Split(*ptrs, "/")[1])
}
return result
}

func getMapKeys(x map[string]string) []string {
i := 0
keys := make([]string, len(x))
for key := range x {
keys[i] = key
i++
}
return keys
}

func getServiceVersions(cluster string, profile string) map[string]string {
resultMap := make(map[string]string)

sessOpts := session.Options{
SharedConfigState: session.SharedConfigEnable,
}

if profile != "" {
fmt.Println("Using Profile", profile, "for cluster", cluster)
sessOpts.Profile = profile
}

sess := session.Must(session.NewSessionWithOptions(sessOpts))

svcECS := ecs.New(sess)
svcSSM := ssm.New(sess)

input := &ecs.ListServicesInput{
Cluster: aws.String(cluster),
}

result, err := svcECS.ListServices(input)
check(err)

services := parseServiceArnList(result.ServiceArns)
sort.Strings(services)

for _, service := range services {
ssmOpts := ssm.GetParameterInput{
Name: aws.String(fmt.Sprintf("/%s/%s/VERSION", cluster, service)),
}
ssmResponse, err := svcSSM.GetParameter(&ssmOpts)
check(err)

resultMap[service] = *ssmResponse.Parameter.Value
}

return resultMap
}

0 comments on commit 2d24708

Please sign in to comment.