Skip to content

Commit

Permalink
Clean up unused ECS tasks (#2)
Browse files Browse the repository at this point in the history
* add release workflow

* initialize go program

* add structure for cobra cli application

* add successful prototype of ecs connection

* tidy deps

* add quick'n'dirty readme

* add automatic vendoring as pre-commit hook

* linter fixes

* collect services across clusters

* describe services

* collect task definitions

* filter out active tasks

* finalize task definitions to be deregistered

* wire up flags (includes workaround for apparent issue where cobra's pflags can't output anything other than strings)

* add parallel flag

* successfully deregister task definitions

* create properly-informative readme

* add region flag
  • Loading branch information
tlake authored Nov 15, 2019
1 parent 48824ec commit 56fbf2b
Show file tree
Hide file tree
Showing 236 changed files with 58,870 additions and 1 deletion.
76 changes: 76 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Release Workflow
on:
push:
tags:
- "v*"

jobs:
manage_release:
name: Manage Release
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.13
id: go
uses: actions/setup-go@v1
with:
go-version: 1.13

- name: Checkout code
uses: actions/checkout@v1

- name: Get dependencies
run: go mod download

- name: Build artifacts
run: |
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -X main.Version=TEST" -a -o build/Linux/go-ecs-cleaner .
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -X main.Version=TEST" -a -o build/macOS/go-ecs-cleaner .
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -X main.Version=TEST" -a -o build/Windows/go-ecs-cleaner.exe .
- name: Create release
id: create_release
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false

- name: Upload Linux binary
id: upload-linux-binary
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
# This pulls from the `Create release` step above, referencing its ID to get its outputs object,
# which includes an `upload_url`. See this blog post for more info:
# https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: ./build/Linux/go-ecs-cleaner
asset_name: go-ecs-cleaner-linux
asset_content_type: application/zip

- name: Upload macOS binary
id: upload-macos-binary
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./build/macOS/go-ecs-cleaner
asset_name: go-ecs-cleaner-darwin
asset_content_type: application/zip

- name: Upload Windows binary
id: upload-windows-binary
uses: actions/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./build/Windows/go-ecs-cleaner.exe
asset_name: go-ecs-cleaner-windows.exe
asset_content_type: application/zip
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# go-ecs-cleaner
# go-ecs-cleaner

A Go tool for cleaning up ECS resources in your AWS account.
CLI built using the [`cobra`](https://github.com/spf13/cobra) library.

## Installation

Download a binary appropriate for your OS from the [releases](https://github.com/quintilesims/go-ecs-cleaner/releases).

To build from source yourself, clone the repo, then build and use a binary, or run `main.go` directly:

- `go build && ./go-ecs-cleaner ecs-task`
- `go run main.go ecs-task`

## Usage

At present, `go-ecs-cleaner` isn't built to configure its own connection to AWS beyond specifying a region.
You'll need to use the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) - specifically, the `aws configure` command - to configure your credentials and connect to an account.

Once your environment is configured, you can run this tool!

`go-ecs-cleaner ecs-task --apply --region us-west-2`

Use the `-h, --help` flag to learn more about the tool's abilities.
38 changes: 38 additions & 0 deletions cmd/ecs_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cmd

import (
"github.com/quintilesims/go-ecs-cleaner/ecstask"
"github.com/spf13/cobra"
)

var ecsTaskCmd = &cobra.Command{
Use: "ecs-task",
Short: "Deregister unused task definitions (dry run by default).",
Long: `Deregister unused task definitions (dry run by default).
BEFORE RUNNING: Make sure that you've properly configured your environment with
the AWS CLI for the AWS account you want to clean up.`,
Run: func(cmd *cobra.Command, args []string) {
flags := map[string]interface{}{
"apply": applyFlag,
"cutoff": cutoffFlag,
"parallel": parallelFlag,
"region": regionFlag,
}

ecstask.Run(cmd, args, flags)
},
}

var applyFlag bool
var cutoffFlag int
var parallelFlag int
var regionFlag string

func init() {
ecsTaskCmd.Flags().BoolVarP(&applyFlag, "apply", "a", false, "actually perform task definition deregistration")
ecsTaskCmd.Flags().IntVarP(&cutoffFlag, "cutoff", "c", 5, "how many most-recent task definitions to keep around")
ecsTaskCmd.Flags().IntVarP(&parallelFlag, "parallel", "p", 10, "how many concurrent deregistration requests to make")
ecsTaskCmd.Flags().StringVarP(&regionFlag, "region", "r", "us-west-2", "the AWS region in which to operate")
rootCmd.AddCommand(ecsTaskCmd)
}
16 changes: 16 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"github.com/spf13/cobra"
)

// Execute executes the root command.
func Execute() error {
return rootCmd.Execute()
}

var rootCmd = &cobra.Command{
Use: "go-ecs-cleaner",
Short: "Clean up your ECS",
Long: "A Go tool to clean up your ECS account, based upon https://github.com/FernandoMiguel/ecs-cleaner",
}
Loading

0 comments on commit 56fbf2b

Please sign in to comment.