-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
110 lines (91 loc) · 2.46 KB
/
plugin.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
package ecs
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/service/ecs"
cred "github.com/carash/ecs-deploy/credential"
)
type ServicePlugin struct {
AWSCredential cred.Credential
Service Service
}
type TaskPlugin struct {
AWSCredential cred.Credential
TaskDefinition TaskDefinition
}
func (p *ServicePlugin) DeployService() error {
svc := ecs.New(p.AWSCredential.NewSession())
_, err := p.Service.Deploy(svc)
return err
}
func (p *ServicePlugin) UpdateService(timeout int64) error {
svc := ecs.New(p.AWSCredential.NewSession())
service, err := p.Service.Update(svc)
if err != nil {
return err
}
start := time.Now()
check := make(chan error)
td, _ := parseFamilyRevision(*service.TaskDefinition)
go func() {
for {
go func() {
taskout, err := svc.ListTasks(&ecs.ListTasksInput{
Cluster: p.Service.Cluster,
ServiceName: &p.Service.Service,
})
if err != nil {
check <- err
}
if len(taskout.TaskArns) == 0 {
return
}
detout, err := svc.DescribeTasks(&ecs.DescribeTasksInput{
Cluster: p.Service.Cluster,
Tasks: taskout.TaskArns,
})
if err != nil {
check <- err
}
healthy := int64(0)
for _, t := range detout.Tasks {
taskDefinition, _ := parseFamilyRevision(*t.TaskDefinitionArn)
fmt.Printf("Status of [%s] -> %s\n", taskDefinition, *t.HealthStatus)
if *t.TaskDefinitionArn == *service.TaskDefinition && *t.HealthStatus == "HEALTHY" {
healthy += 1
}
}
fmt.Println()
if int64(len(taskout.TaskArns)) != *service.DesiredCount {
return
}
if healthy == *service.DesiredCount {
fmt.Printf("Task [%s] is HEALTHY after %d seconds\n\n", td, int64(time.Now().Sub(start).Seconds()))
check <- nil
return
}
}()
time.Sleep(10 * time.Second)
fmt.Printf("Waiting for Task [%s] to be HEALTHY, %ds...\n", td, int64(time.Now().Sub(start).Seconds()))
}
}()
select {
case err := <-check:
if err != nil {
return err
}
case <-time.After(time.Duration(timeout) * time.Second):
return fmt.Errorf("Timed out after %ds while wating for Task [%s] to deploy\n\n", int64(time.Now().Sub(start).Seconds()), td)
}
return nil
}
func (p *TaskPlugin) RegisterTask() error {
svc := ecs.New(p.AWSCredential.NewSession())
_, err := p.TaskDefinition.Register(svc)
return err
}
func (p *TaskPlugin) UpdateTask() error {
svc := ecs.New(p.AWSCredential.NewSession())
_, err := p.TaskDefinition.Update(svc)
return err
}