Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mattevans committed Dec 18, 2016
0 parents commit 63e84f0
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Matt Evans

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ecr-cleanse
101 changes: 101 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package main

import (
"flag"
"fmt"

"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/mattevans/ecr-cleaner-master/services"
)

var log = logrus.New()
var logGroup = []string{}

func main() {
// Assign our flags.
var (
region = flag.String("aws-region", "us-west-2", "AWS region")
dry = flag.Bool("dry-run", false, "Executes without deleting any images")
err error
)

// Parse our flags.
flag.Parse()

// Intialize ECS and ECR clients
ecsc := services.NewECSClient(*region)
ecrc := services.NewECRClient(*region)

// Find our repositories.
repositories, err := ecrc.ListRepositories()
if err != nil {
log.WithError(err).Error("Error listing ECR repositories")
}

// Find image identities currenlty in use on active tasks.
runningImages, err := ecsc.FindActiveImages()
if err != nil {
log.WithError(err).Error("Error finding `running` tasks within clusters")
}

// Log some info for the user.
logGroup = append(logGroup, []string{
fmt.Sprintf("Dry Run: %v", *dry),
fmt.Sprintf("AWS Region: %v", *region),
fmt.Sprintf("Repositories Found: %v", len(repositories)),
fmt.Sprintf("Active Images Found: %v", len(runningImages)),
}...)
outputLog()

// Range repo's, find stale images and remove them.
for _, repository := range repositories {
// Get our images within given repository.
images, err := ecrc.ListImages(repository)
if err != nil {
log.Errorf("Error retrieving images for `%v` repository (%v)", repository, err)
}
logGroup = append(logGroup, fmt.Sprintf("Repository: %v", repository))

// Build a slice of stale images.
stale := []*ecr.ImageIdentifier{}
for _, image := range images {
match := false
for _, running := range runningImages {
// Handle case where we have an image without a tag.
if image.ImageTag == nil {
break
}
// Tag matches an image running a container. Ignore!
if running == *image.ImageTag {
match = true
break
}
}
// No match, image is stale.
if match == false {
stale = append(stale, image)
}
}

// Purge our stale images if not a dry-run.
if *dry {
logGroup = append(logGroup, fmt.Sprintf("[DRY RUN] `%v` images would be purged", len(stale)))
} else {
err = ecrc.PurgeImages(repository, stale)
if err != nil {
log.Errorf("Error purging stale images for repo %v: %v", repository, err)
}
logGroup = append(logGroup, fmt.Sprintf("[PURGED] `%v` images in repository `%v`", len(stale), repository))
}
outputLog()
}
}

func outputLog() {
for _, value := range logGroup {
log.Info(value)
}
log.Info("----------------------------------------------------------------")
logGroup = []string{}
}
95 changes: 95 additions & 0 deletions services/ecr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package services

import (
"fmt"

"github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
)

var log = logrus.New()

// ECRClient holds a connection to AWS ECR.
type ECRClient struct {
client *ecr.ECR
region string
}

// NewECRClient initializes an ECRClient.
func NewECRClient(region string) *ECRClient {
return &ECRClient{
client: ecr.New(session.New(), aws.NewConfig().WithRegion(region)),
region: region,
}
}

// ListRepositories will return all ECR repositories as []string.
func (c *ECRClient) ListRepositories() ([]string, error) {
allRepositories, err := c.client.DescribeRepositories(&ecr.DescribeRepositoriesInput{})
if err != nil {
return []string{}, err
}

repositories := make([]string, 0, len(allRepositories.Repositories))
for _, repo := range allRepositories.Repositories {
repositories = append(repositories, *repo.RepositoryName)
}
return repositories, nil
}

// ListImages will return all image identifiers for a given repository.
func (c *ECRClient) ListImages(repository string) ([]*ecr.ImageIdentifier, error) {
var token *string
var imageIDs []*ecr.ImageIdentifier

for {
resp, err := c.client.ListImages(&ecr.ListImagesInput{
RepositoryName: aws.String(repository),
NextToken: token,
})
if err != nil {
return nil, err
}

imageIDs = append(imageIDs, resp.ImageIds...)
if token = resp.NextToken; token == nil {
break
}
}
return imageIDs, nil
}

// PurgeImages will batch delete images by image identitfier in sets of 100.
func (c *ECRClient) PurgeImages(repository string, images []*ecr.ImageIdentifier) error {
// No images found, back-out.
if len(images) <= 0 {
return nil
}
// Purge the images in batches of 100.
i := 0
for i = 0; i < int(len(images)/100); i++ {
err := c.BatchPurge(repository, images[i*100:(i+1)*100])
if err != nil {
return fmt.Errorf("Failed purging images in repository `%v` (%v)", repository, err)
}
}
err := c.BatchPurge(repository, images[i*100:])
if err != nil {
return fmt.Errorf("Failed purging images in repository `%v` (%v)", repository, err)
}
return err
}

// BatchPurge will batch delete images by the image identifiers.
func (c *ECRClient) BatchPurge(repository string, images []*ecr.ImageIdentifier) error {
_, err := c.client.BatchDeleteImage(&ecr.BatchDeleteImageInput{
RepositoryName: aws.String(repository),
ImageIds: images,
})
if err != nil {
return err
}
return nil
}
78 changes: 78 additions & 0 deletions services/ecs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package services

import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
)

// ECSClient holds a connection to AWS ECS.
type ECSClient struct {
client *ecs.ECS
region string
}

// NewECSClient initializes an ECSClient.
func NewECSClient(region string) *ECSClient {
return &ECSClient{
client: ecs.New(session.New(), aws.NewConfig().WithRegion(region)),
region: region,
}
}

// FindActiveImages will initiate a connection with ECS, parsing out image names
// from all running tasks/clusters, returning them as a []string.
func (c *ECSClient) FindActiveImages() ([]string, error) {
// List ECS clusters.
var token *string
clusters, err := c.client.ListClusters(&ecs.ListClustersInput{
NextToken: token,
})
if err != nil {
return nil, err
}

// Slice to store our running images name(s).
runningImages := []string{}

// Range cluster ARNs and parse out the image ID from the running task.
clusterARNs := make([]string, 0, len(clusters.ClusterArns))
for _, arn := range clusters.ClusterArns {
clusterARNs = append(clusterARNs, *arn)

// List all 'running' tasks from cluster.
running := ecs.DesiredStatusRunning
runningTasks, err := c.client.ListTasks(&ecs.ListTasksInput{
Cluster: arn,
DesiredStatus: &running,
})
if err != nil {
return nil, err
}

// Describe cluster tasks.
tasks, err := c.client.DescribeTasks(&ecs.DescribeTasksInput{
Tasks: runningTasks.TaskArns,
Cluster: arn,
})
if err != nil {
return nil, err
}

// Range the tasks, parsing/storing the running ECI name.
for _, task := range tasks.Tasks {
tasks, err := c.client.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: task.TaskDefinitionArn,
})
if err != nil {
return nil, err
}
for _, container := range tasks.TaskDefinition.ContainerDefinitions {
runningImages = append(runningImages, strings.Split(*container.Image, ":")[1])
}
}
}
return runningImages, nil
}

0 comments on commit 63e84f0

Please sign in to comment.