-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenv.go
113 lines (103 loc) · 3.34 KB
/
env.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
package cage
import (
"encoding/json"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"golang.org/x/xerrors"
)
type Envars struct {
_ struct{} `type:"struct"`
CI bool `json:"ci" type:"bool"`
Region string `json:"region" type:"string"`
Cluster string `json:"cluster" type:"string" required:"true"`
Service string `json:"service" type:"string" required:"true"`
CanaryInstanceArn string
TaskDefinitionArn string `json:"nextTaskDefinitionArn" type:"string"`
TaskDefinitionInput *ecs.RegisterTaskDefinitionInput
ServiceDefinitionInput *ecs.CreateServiceInput
CanaryTaskIdleDuration int
}
// required
const ClusterKey = "CAGE_CLUSTER"
const ServiceKey = "CAGE_SERVICE"
// either required
const TaskDefinitionArnKey = "CAGE_TASK_DEFINITION_ARN"
// optional
const CanaryInstanceArnKey = "CAGE_CANARY_INSTANCE_ARN"
const RegionKey = "CAGE_REGION"
const CanaryTaskIdleDuration = "CAGE_CANARY_TASK_IDLE_DURATION"
const UpdateServiceKey = "CAGE_UPDATE_SERVIEC"
func EnsureEnvars(
dest *Envars,
) error {
// required
if dest.Cluster == "" {
return xerrors.Errorf("--cluster [%s] is required", ClusterKey)
} else if dest.Service == "" {
return xerrors.Errorf("--service [%s] is required", ServiceKey)
}
if dest.TaskDefinitionArn == "" && dest.TaskDefinitionInput == nil {
return xerrors.Errorf("--nextTaskDefinitionArn or deploy context must be provided")
}
if dest.Region == "" {
log.Fatalf("region must be specified. set --region flag or see also https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html")
}
return nil
}
func LoadDefinitionsFromFiles(dir string) (
*ecs.RegisterTaskDefinitionInput,
*ecs.CreateServiceInput,
error,
) {
svcPath := filepath.Join(dir, "service.json")
tdPath := filepath.Join(dir, "task-definition.json")
_, noSvc := os.Stat(svcPath)
_, noTd := os.Stat(tdPath)
var service ecs.CreateServiceInput
var td ecs.RegisterTaskDefinitionInput
if noSvc != nil || noTd != nil {
return nil, nil, xerrors.Errorf("roll out context specified at '%s' but no 'service.json' or 'task-definition.json'", dir)
}
if _, err := ReadAndUnmarshalJson(svcPath, &service); err != nil {
return nil, nil, xerrors.Errorf("failed to read and unmarshal service.json: %s", err)
}
if _, err := ReadAndUnmarshalJson(tdPath, &td); err != nil {
return nil, nil, xerrors.Errorf("failed to read and unmarshal task-definition.json: %s", err)
}
return &td, &service, nil
}
func MergeEnvars(dest *Envars, src *Envars) {
if src.Region != "" {
dest.Region = src.Region
}
if src.Cluster != "" {
dest.Cluster = src.Cluster
}
if src.Service != "" {
dest.Service = src.Service
}
if src.CanaryInstanceArn != "" {
dest.CanaryInstanceArn = src.CanaryInstanceArn
}
if src.TaskDefinitionArn != "" {
dest.TaskDefinitionArn = src.TaskDefinitionArn
}
if src.TaskDefinitionInput != nil {
dest.TaskDefinitionInput = src.TaskDefinitionInput
}
if src.ServiceDefinitionInput != nil {
dest.ServiceDefinitionInput = src.ServiceDefinitionInput
}
}
func ReadAndUnmarshalJson(path string, dest interface{}) ([]byte, error) {
if d, err := ReadFileAndApplyEnvars(path); err != nil {
return d, err
} else {
if err := json.Unmarshal(d, dest); err != nil {
return d, err
}
return d, nil
}
}