esu

package module
v0.0.0-...-88c42f6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

ESU (ECS Service Utility)

From the department of nested acronyms, comes a set of utilities for locating and monitoring ECS services.

Status: Experimental

This repository contains a Go library that wraps the AWS SDK and offers utilities for querying and monitoring ECS tasks. The original purpose was to facilitate service discovery in lieu of an ELB in front of each service, but there are many other purposes.

To list or locate tasks one off, use TaskFinder:

tf := esu.NewTaskFinder(sess, *cluster)
tasks, err := tf.Tasks("website")

To monitor the status of a service, use TaskMonitor:

tm := esu.NewTaskMonitor(sess, "sites", "website")
tm.OnTaskChange = func(tasks []esu.TaskInfo) { ... }
tm.OnError = func(err error) { ... }
tm.Monitor()

The data return about a Task, aggregated from several API calls is represented by the TaskInfo struct:

type TaskInfo struct {
  TaskDefinition   string
  DesiredStatus    ECSTaskStatus  // RUNNING, PENDING, STOPPED
  LastStatus       ECSTaskStatus
  StartedAt        time.Time
  Port             int
  PublicDNSName    string
  PublicIPAddress  string
  PrivateDNSName   string
  PrivateIPAddress string
}

Full API docs can be found here: https://godoc.org/github.com/dpup/esu

Tools

List Tasks - Show all services and tasks running on an ECS cluster

go run cmd/listtasks/listtasks.go --cluster=sites

Monitor Service - Monitors status of a service, printing out status changes to runing tasks.

go run cmd/monitor/monitor.go --cluster=sites --service=website

Update Task - Updates the container image of a service and waits for the tasks to be updated.

go run cmd/updatetask/updatetask.go --world=prod --service=website --tag=build-38

(This task assumes the cluster is named prod-cluster and the task definition for the servies is prod-website.)

(Make sure credentials are available in the environment)

Contributing

Questions, comments, bug reports, and pull requests are all welcome. Submit them on the project issue tracker.

License

Copyright 2016 Daniel Pupius. Licensed under the Apache License, Version 2.0.

Documentation

Overview

Package esu provides wrappers around the AWS-SDK for locating ECS tasks. It can be used for basic service discovery in lieu of using ELBs infront of your containerized tasks.

An assumption is that each task has one canonical container, for example a web server. For multi-container tasks, the canonical container's name should match the service name.

Index

Constants

View Source
const DefaultPollFreq = time.Second * 5

DefaultPollFreq specifies how often to check ECS for task changes, under normal operation.

View Source
const DefaultVolatilePollFreq = time.Second * 1

DefaultVolatilePollFreq specifies how often to check ECS for task changes, when a task has been noted as being in a pending state or soon to be stopped.

Variables

This section is empty.

Functions

This section is empty.

Types

type ARN

type ARN struct {
	Prefix   string // Region + Account + Resource Type
	Resource string
	Revision string
}

ARN contains fields within an ARN used by ECS services.

arn:aws:ecs:region:account-id:task-definition/task-definition-family-name:task-definition-revision-number arn:aws:ecs:region:account-id:container/container-id arn:aws:ecr:region:account-id:repository/repo:tag

func ParseARN

func ParseARN(arn string) ARN

ParseARN parses an ARN.

func (ARN) ShortName

func (td ARN) ShortName() string

ShortName returns "Resource:Revision"

func (ARN) String

func (td ARN) String() string

String returns the full ARN (if fields are set).

type ECSTaskStatus

type ECSTaskStatus string

ECSTaskStatus are the different task states. See http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_life_cycle.html

const (
	// ECSTaskStatusRunning indicates the task is running and taking traffic.
	ECSTaskStatusRunning ECSTaskStatus = "RUNNING"
	// ECSTaskStatusPending indicates the task has been started.
	ECSTaskStatusPending ECSTaskStatus = "PENDING"
	// ECSTaskStatusStopped indicates the task has been stopped.
	ECSTaskStatusStopped ECSTaskStatus = "STOPPED"
)

type TaskFinder

type TaskFinder struct {
	// contains filtered or unexported fields
}

TaskFinder provides a wrapper around the AWS-SDK for locating ECS tasks.

func NewTaskFinder

func NewTaskFinder(sess *session.Session, cluster string) *TaskFinder

NewTaskFinder returns a new task finder. It is as thread-safe as the underlying AWS SDK :)

func (*TaskFinder) Services

func (f *TaskFinder) Services() ([]string, error)

Services returns a list of ARNs for all services active on a cluster.

func (*TaskFinder) Tasks

func (f *TaskFinder) Tasks(service string) ([]TaskInfo, error)

Tasks returns information about a service's running tasks, sorted first by public DNS name and then port.

type TaskInfo

type TaskInfo struct {
	TaskDefinition   string
	DesiredStatus    ECSTaskStatus
	LastStatus       ECSTaskStatus
	StartedAt        time.Time
	Port             int
	EC2InstanceID    string
	PublicDNSName    string
	PublicIPAddress  string
	PrivateDNSName   string
	PrivateIPAddress string
}

TaskInfo specifies information about a task running on ECS. A service may have multiple tasks associated with it.

func (TaskInfo) String

func (ti TaskInfo) String() string

type TaskMonitor

type TaskMonitor struct {
	Service          string
	PollFreq         time.Duration
	VolatilePollFreq time.Duration
	OnStatusChange   func([]TaskInfo)
	OnTaskChange     func([]TaskInfo)
	OnError          func(error)
	// contains filtered or unexported fields
}

TaskMonitor is

func NewTaskMonitor

func NewTaskMonitor(sess *session.Session, cluster string, service string) *TaskMonitor

NewTaskMonitor returns a new task monitor.

func (*TaskMonitor) AllTasks

func (tm *TaskMonitor) AllTasks() []TaskInfo

AllTasks returns a list of all tasks, including pending and stopped.

func (*TaskMonitor) IsVolatile

func (tm *TaskMonitor) IsVolatile() bool

IsVolatile returns true if any tasks have a desired status that doesn't match last status, or an error was encountered recently.

func (*TaskMonitor) Monitor

func (tm *TaskMonitor) Monitor() chan<- bool

Monitor polls for changes in running tasks, relevant callbacks are executed when changes are detected. There should only be one active Monitor per instance.

func (*TaskMonitor) RunningTasks

func (tm *TaskMonitor) RunningTasks() []TaskInfo

RunningTasks returns a list of currently running tasks.

func (*TaskMonitor) Update

func (tm *TaskMonitor) Update() bool

Update queries ECS for the latest tasks and returns true if there were any changes in the number of running tasks.

Directories

Path Synopsis
cmd
findinstances
Sample application that lists all services and tasks running on a cluster.
Sample application that lists all services and tasks running on a cluster.
listtasks
Sample application that lists all services and tasks running on a cluster.
Sample application that lists all services and tasks running on a cluster.
monitor
Sample application that monitors the state of a service's tasks.
Sample application that monitors the state of a service's tasks.
updatetask
Command which updates a task definition and waits for the service to become stable.
Command which updates a task definition and waits for the service to become stable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL