/
ecs.go
125 lines (108 loc) · 4.48 KB
/
ecs.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2020 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ecs
import (
"context"
"fmt"
"io/ioutil"
"time"
"go.uber.org/zap"
provider "github.com/pipe-cd/pipe/pkg/app/piped/cloudprovider/ecs"
"github.com/pipe-cd/pipe/pkg/app/piped/planner"
"github.com/pipe-cd/pipe/pkg/model"
)
// Planner plans the deployment pipeline for ECS application.
type Planner struct {
}
type registerer interface {
Register(k model.ApplicationKind, p planner.Planner) error
}
// Register registers this planner into the given registerer.
func Register(r registerer) {
r.Register(model.ApplicationKind_ECS, &Planner{})
}
// Plan decides which pipeline should be used for the given input.
func (p *Planner) Plan(ctx context.Context, in planner.Input) (out planner.Output, err error) {
ds, err := in.TargetDSP.Get(ctx, ioutil.Discard)
if err != nil {
err = fmt.Errorf("error while preparing deploy source data (%v)", err)
return
}
cfg := ds.DeploymentConfig.ECSDeploymentSpec
if cfg == nil {
err = fmt.Errorf("missing ECSDeploymentSpec in deployment configuration")
return
}
// Determine application version from the task definition
if version, err := determineVersion(ds.AppDir, cfg.Input.TaskDefinitionFile); err == nil {
out.Version = version
} else {
out.Version = "unknown"
in.Logger.Warn("unable to determine target version", zap.Error(err))
}
// If the deployment was triggered by forcing via web UI,
// we rely on the user's decision.
switch in.Deployment.Trigger.SyncStrategy {
case model.SyncStrategy_QUICK_SYNC:
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Quick sync to deploy image %s and configure all traffic to it (forced via web)", out.Version)
return
case model.SyncStrategy_PIPELINE:
if cfg.Pipeline == nil {
err = fmt.Errorf("unable to force sync with pipeline because no pipeline was specified")
return
}
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Sync with pipeline to deploy image %s (forced via web)", out.Version)
return
}
// If this is the first time to deploy this application or it was unable to retrieve last successful commit,
// we perform the quick sync strategy.
if in.MostRecentSuccessfulCommitHash == "" {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Quick sync to deploy image %s and configure all traffic to it (it seems this is the first deployment)", out.Version)
return
}
// When no pipeline was configured, perform the quick sync.
if cfg.Pipeline == nil || len(cfg.Pipeline.Stages) == 0 {
out.SyncStrategy = model.SyncStrategy_QUICK_SYNC
out.Stages = buildQuickSyncPipeline(cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Quick sync to deploy image %s and configure all traffic to it (pipeline was not configured)", out.Version)
return
}
// Load service manifest at the last deployed commit to decide running version.
ds, err = in.RunningDSP.Get(ctx, ioutil.Discard)
if err == nil {
if lastVersion, e := determineVersion(ds.AppDir, cfg.Input.TaskDefinitionFile); e == nil {
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, time.Now())
out.Summary = fmt.Sprintf("Sync with pipeline to update image from %s to %s", lastVersion, out.Version)
return
}
}
out.SyncStrategy = model.SyncStrategy_PIPELINE
out.Stages = buildProgressivePipeline(cfg.Pipeline, cfg.Input.AutoRollback, time.Now())
out.Summary = "Sync with the specified pipeline"
return
}
func determineVersion(appDir, taskDefinitonFile string) (string, error) {
taskDefinition, err := provider.LoadTaskDefinition(appDir, taskDefinitonFile)
if err != nil {
return "", err
}
return provider.FindImageTag(taskDefinition)
}