-
Notifications
You must be signed in to change notification settings - Fork 2
/
up.go
59 lines (55 loc) · 2.27 KB
/
up.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cage
import (
"context"
"github.com/apex/log"
"github.com/aws/aws-sdk-go-v2/service/ecs"
ecstypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/loilo-inc/canarycage/awsiface"
"github.com/loilo-inc/canarycage/env"
"github.com/loilo-inc/canarycage/key"
"github.com/loilo-inc/canarycage/types"
"golang.org/x/xerrors"
)
func (c *cage) Up(ctx context.Context) (*types.UpResult, error) {
td, err := c.CreateNextTaskDefinition(ctx)
if err != nil {
return nil, err
}
env := c.di.Get(key.Env).(*env.Envars)
ecsCli := c.di.Get(key.EcsCli).(awsiface.EcsClient)
log.Infof("checking existence of service '%s'", env.Service)
if o, err := ecsCli.DescribeServices(ctx, &ecs.DescribeServicesInput{
Cluster: &env.Cluster,
Services: []string{env.Service},
}); err != nil {
return nil, xerrors.Errorf("couldn't describe service: %w", err)
} else if len(o.Services) > 0 {
svc := o.Services[0]
if *svc.Status != "INACTIVE" {
return nil, xerrors.Errorf("service '%s' already exists. Use 'cage rollout' instead", env.Service)
}
}
env.ServiceDefinitionInput.TaskDefinition = td.TaskDefinitionArn
if service, err := c.createService(ctx, env.ServiceDefinitionInput); err != nil {
return nil, err
} else {
return &types.UpResult{TaskDefinition: td, Service: service}, nil
}
}
func (c *cage) createService(ctx context.Context, serviceDefinitionInput *ecs.CreateServiceInput) (*ecstypes.Service, error) {
env := c.di.Get(key.Env).(*env.Envars)
ecsCli := c.di.Get(key.EcsCli).(awsiface.EcsClient)
log.Infof("creating service '%s' with task-definition '%s'...", *serviceDefinitionInput.ServiceName, *serviceDefinitionInput.TaskDefinition)
o, err := ecsCli.CreateService(ctx, serviceDefinitionInput)
if err != nil {
return nil, xerrors.Errorf("failed to create service '%s': %w", *serviceDefinitionInput.ServiceName, err)
}
log.Infof("waiting for service '%s' to be STABLE", *serviceDefinitionInput.ServiceName)
if err := ecs.NewServicesStableWaiter(ecsCli).Wait(ctx, &ecs.DescribeServicesInput{
Cluster: &env.Cluster,
Services: []string{*serviceDefinitionInput.ServiceName},
}, env.GetServiceStableWait()); err != nil {
return nil, xerrors.Errorf("failed to wait for service '%s' to be STABLE: %w", *serviceDefinitionInput.ServiceName, err)
}
return o.Service, nil
}