-
-
Notifications
You must be signed in to change notification settings - Fork 89
/
create.go
111 lines (98 loc) · 3.2 KB
/
create.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package ecspresso
import (
"context"
"errors"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
)
func (d *App) createService(ctx context.Context, opt DeployOption) error {
d.Log("Starting create service %s", opt.DryRunString())
svd, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
if err != nil {
return err
}
td, err := d.LoadTaskDefinition(d.config.TaskDefinitionPath)
if err != nil {
return err
}
count := calcDesiredCount(svd, opt)
if count == nil && (svd.SchedulingStrategy != "" && svd.SchedulingStrategy == types.SchedulingStrategyReplica) {
count = aws.Int32(0) // Must provide desired count for replica scheduling strategy
}
if opt.DryRun {
d.Log("task definition:")
d.OutputJSONForAPI(os.Stderr, td)
d.Log("service definition:")
d.OutputJSONForAPI(os.Stderr, svd)
d.Log("DRY RUN OK")
return nil
}
var tdArn string
if opt.LatestTaskDefinition || opt.SkipTaskDefinition {
var err error
tdArn, err = d.findLatestTaskDefinitionArn(ctx, aws.ToString(td.Family))
if err != nil {
return err
}
d.Log("Using latest task definition %s", tdArn)
} else {
newTd, err := d.RegisterTaskDefinition(ctx, td)
if err != nil {
return err
}
tdArn = *newTd.TaskDefinitionArn
}
createServiceInput := &ecs.CreateServiceInput{
Cluster: aws.String(d.config.Cluster),
CapacityProviderStrategy: svd.CapacityProviderStrategy,
DeploymentConfiguration: svd.DeploymentConfiguration,
DeploymentController: svd.DeploymentController,
DesiredCount: count,
EnableECSManagedTags: svd.EnableECSManagedTags,
EnableExecuteCommand: svd.EnableExecuteCommand,
HealthCheckGracePeriodSeconds: svd.HealthCheckGracePeriodSeconds,
LaunchType: svd.LaunchType,
LoadBalancers: svd.LoadBalancers,
NetworkConfiguration: svd.NetworkConfiguration,
PlacementConstraints: svd.PlacementConstraints,
PlacementStrategy: svd.PlacementStrategy,
PlatformVersion: svd.PlatformVersion,
PropagateTags: svd.PropagateTags,
SchedulingStrategy: svd.SchedulingStrategy,
ServiceConnectConfiguration: svd.ServiceConnectConfiguration,
ServiceName: svd.ServiceName,
ServiceRegistries: svd.ServiceRegistries,
Tags: svd.Tags,
TaskDefinition: aws.String(tdArn),
VolumeConfigurations: svd.VolumeConfigurations,
}
if _, err := d.ecs.CreateService(ctx, createServiceInput); err != nil {
return fmt.Errorf("failed to create service: %w", err)
}
d.Log("Service is created")
if !opt.Wait {
return nil
}
time.Sleep(delayForServiceChanged) // wait for service created
sv, err := d.DescribeService(ctx)
if err != nil {
return err
}
doWait, err := d.WaitFunc(sv, nil)
if err != nil {
return err
}
if err := doWait(ctx, sv); err != nil {
if errors.As(err, &errNotFound) && sv.isCodeDeploy() {
d.Log("[INFO] %s", err)
return d.WaitTaskSetStable(ctx, sv)
}
return err
}
d.Log("Service is stable now. Completed!")
return nil
}