deploy

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package deploy provides simple functions for deploy ECS.

Usage:

import "github.com/h3poteto/ecs-goploy/deploy"

Service update

When you want to update service in ECS, please use this package as follows.

Construct a new Service, then use deploy functions.

s, err := deploy.NewService("cluster", "service-name", "nginx:stable", nil, 5 * time.Minute, true, "", "", true)
if err != nil {
    log.Fatalf("[ERROR] %v", err)
}

// deploy new image
if err := s.Deploy(); err != nil {
    log.Fatalf("[ERROR] %v", err)
}

Or you can write a custom deploy recipe as you like.

For example:

s, err := deploy.NewService("cluster", "service-name", "nginx:stable", nil, 5 * time.Minute, true, "", "", true)
if err != nil {
    log.Fatal(err)
}

// get the current service
service, err := s.DescribeService()
if err != nil {
    log.Fatal(err)
}
currentTaskDefinition, err := s.TaskDefinition.DescribeTaskDefinition(service)
if err != nil {
    log.Fatal(err)
}

newTaskDefinition, err := s.RegisterTaskDefinition(currentTaskDefinition, s.NewImage)
if err != nil {
    log.Fatal(err)
}

// Do something

err = s.UpdateService(service, newTaskDefinition)
if err != nil {
    // Do something
}
log.Println("[INFO] Deploy success")

TaskDefinition update

You can create a new revision of the task definition. Please use this task definition at `Task` and `ScheduledTask`.

For example:

taskDefinition := ecsdeploy.NewTaskDefinition("", "", true)
t, err := taskDefinition.Create("sample-task-definition:revision", "nginx:stable")
if err != nil {
    log.Fatal(err)
}
log.Println(*t.TaskDefinitionArn)

Run task

When you want to run task on ECS at once, plese use this package as follows.

For example:

task, err := ecsdeploy.NewTask("cluster", "container-name", "echo hoge", "sample-task-definition:2", (5 * time.Minute), "", "", true)
if err != nil {
    log.Fatal(err)
}
if _, err := task.Run(); err != nil {
    log.Fatal(err)
}
log.Println("[INFO] Task success")

ScheduledTask update

When you update the ECS Scheduled Task, please use this package.

For example:

scheduledTask := ecsdeploy.NewScheduledTask("", "", true)
scheduledTask("schedule-name", "sample-task-definition:2", 1)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Image

type Image struct {

	// Docker image repository.
	Repository string

	// Docker image tag.
	Tag string
}

Image has repository and tag string of docker image.

type ScheduledTask added in v0.4.0

type ScheduledTask struct {

	// TaskDefinition struct to call aws API.
	TaskDefinition *TaskDefinition
	// contains filtered or unexported fields
}

ScheduledTask has target task definition information and client of aws-sdk-go.

func NewScheduledTask added in v0.4.0

func NewScheduledTask(profile, region string, verbose bool) *ScheduledTask

NewScheduledTask returns a nwe ScheduledTask struct, and initialize aws cloudwatchevents API client.

func (*ScheduledTask) DescribeRule added in v0.4.0

func (s *ScheduledTask) DescribeRule(name string) (*events.DescribeRuleOutput, error)

DescribeRule finds an event rule.

func (*ScheduledTask) ListsEventTargets added in v0.4.0

func (s *ScheduledTask) ListsEventTargets(ruleName *string) ([]*events.Target, error)

ListsEventTargets list up event targets based on rule name.

func (*ScheduledTask) Update added in v0.4.0

func (s *ScheduledTask) Update(name string, taskDefinition *string, count int64) error

Update update the cloudwatch event with provided task definition.

func (*ScheduledTask) UpdateTargets added in v0.4.0

func (s *ScheduledTask) UpdateTargets(taskCount int64, taskDefinition *ecs.TaskDefinition, name string) error

UpdateTargets updates all event targets related the rule.

type Service

type Service struct {

	// Name of ECS cluster.
	Cluster string

	// Name of ECS service.
	Name string

	// Name of base task definition of deploy.
	BaseTaskDefinition *string

	// TaskDefinition struct to call aws API.
	TaskDefinition *TaskDefinition

	// New image for deploy.
	NewImage *Image

	// Wait time when update service.
	// This script monitors ECS service for new task definition to be running after call update service API.
	Timeout time.Duration

	// If deploy failed, rollback to current task definition.
	EnableRollback bool

	// When check whether deploy completed, confirm only new task status.
	// If this flag is true, confirm service deployments status.
	SkipCheckDeployments bool
	// contains filtered or unexported fields
}

Service has target ECS information, client of aws-sdk-go, tasks information and timeout seconds.

func NewService

func NewService(cluster, name, imageWithTag string, baseTaskDefinition *string, timeout time.Duration, enableRollback bool, skipCheckDeployments bool, profile, region string, verbose bool) (*Service, error)

NewService returns a new Service struct, and initialize aws ecs API client. Separates imageWithTag into repository and tag, then sets a NewImage for deploy.

func (*Service) Deploy

func (s *Service) Deploy() error

Deploy runs deploy commands and handle errors.

func (*Service) DescribeService

func (s *Service) DescribeService() (*ecs.Service, error)

DescribeService gets a current service in the cluster.

func (*Service) Rollback

func (s *Service) Rollback(service *ecs.Service, currentTaskDefinition *ecs.TaskDefinition) error

Rollback updates the service with current task definition. This method call update-service API and does not wait for execution to end.

func (*Service) UpdateService

func (s *Service) UpdateService(service *ecs.Service, taskDefinition *ecs.TaskDefinition) error

UpdateService updates the service with a new task definition, and wait during update action.

type Task

type Task struct {

	// Name of ECS cluster.
	Cluster string

	// Name of the container for override task definition.
	Name string

	// Name of base task definition for run task.
	BaseTaskDefinition string

	// TaskDefinition struct to call aws API.
	TaskDefinition *TaskDefinition

	// Task command which run on ECS.
	Command []*string

	// Wait time when run task.
	// This script monitors ECS task for new task definition to be running after call run task API.
	Timeout time.Duration
	// EC2 or Fargate
	LaunchType string
	// If you set Fargate as launch type, you have to set your subnet IDs.
	// Because Fargate demands awsvpc as network configuration, so subnet IDs are required.
	Subnets []*string
	// If you want to attach the security groups to ENI of the task, please set this.
	SecurityGroups []*string
	// If you don't enable this flag, the task access the internet throguth NAT gateway.
	// Please read more information: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html
	AssignPublicIP string
	// contains filtered or unexported fields
}

Task has target ECS information, client of aws-sdk-go, command and timeout seconds.

func NewTask

func NewTask(cluster, name, command, baseTaskDefinition string, fargate bool, subnetIDs, securityGroupIDs string, timeout time.Duration, profile, region string, verbose bool) (*Task, error)

NewTask returns a new Task struct, and initialize aws ecs API client. If you want to run the task as Fargate, please provide fargate flag to true, and your subnet IDs for awsvpc. If you don't want to run the task as Fargate, please provide empty string for subnetIDs.

func (*Task) Run

func (t *Task) Run() ([]*ecs.Task, error)

Run run task on ECS based on provided task definition.

func (*Task) RunTask

func (t *Task) RunTask(taskDefinition *ecs.TaskDefinition) ([]*ecs.Task, error)

RunTask calls run-task API.

type TaskDefinition

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

TaskDefinition has image and task definition information.

func NewTaskDefinition

func NewTaskDefinition(profile, region string, verbose bool) *TaskDefinition

NewTaskDefinition initializes aws ecs API client, and returns a task definition struct.

func (*TaskDefinition) Create added in v0.4.0

func (n *TaskDefinition) Create(base *string, dockerImage string) (*ecs.TaskDefinition, error)

Create creates a new revision of the task definition.

func (*TaskDefinition) DescribeTaskDefinition

func (d *TaskDefinition) DescribeTaskDefinition(taskDefinitionName string) (*ecs.TaskDefinition, error)

DescribeTaskDefinition gets a task definition. The family for the latest ACTIVE revision, family and revision (family:revision) for a specific revision in the family, or full Amazon Resource Name (ARN) of the task definition to describe.

func (*TaskDefinition) NewContainerDefinition

func (d *TaskDefinition) NewContainerDefinition(baseDefinition *ecs.ContainerDefinition, newImage *Image) (*ecs.ContainerDefinition, error)

NewContainerDefinition updates image tag in the given container definition. If the container definition is not target container, returns the givien definition.

func (*TaskDefinition) RegisterTaskDefinition

func (d *TaskDefinition) RegisterTaskDefinition(baseDefinition *ecs.TaskDefinition, newImage *Image) (*ecs.TaskDefinition, error)

RegisterTaskDefinition registers new task definition if needed. If newTask is not set, returns a task definition which same as the given task definition.

Jump to

Keyboard shortcuts

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