This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
handle_deploy.go
90 lines (72 loc) · 1.81 KB
/
handle_deploy.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
package http
import (
"encoding/json"
"errors"
"io"
"time"
"github.com/mcuadros/dockership/core"
"gopkg.in/igm/sockjs-go.v2/sockjs"
)
var ErrProjectNotFound = errors.New("Project not found")
type DeployResult struct {
Done bool
Elapsed time.Duration
Errors []error `json:",omitempty"`
}
func (s *server) HandleDeploy(msg Message, session sockjs.Session) {
force := true
project, ok := msg.Request["project"]
if !ok {
core.Error("Missing project", "request", "deploy")
return
}
environment, ok := msg.Request["environment"]
if !ok {
core.Error("Missing environment", "request", "deploy")
return
}
now := time.Now()
writer := NewSockJSWriter(s.sockjs, "deploy")
writer.SetFormater(func(raw []byte) []byte {
str, _ := json.Marshal(map[string]string{
"environment": environment,
"project": project,
"date": now.String(),
"log": string(raw),
})
return str
})
go func(session sockjs.Session) {
time.Sleep(50 * time.Millisecond)
s.EmitProjects(session)
}(session)
s.DoDeploy(writer, project, environment, force)
s.EmitProjects(session)
}
func (s *server) DoDeploy(w io.Writer, project, environment string, force bool) *DeployResult {
start := time.Now()
r := &DeployResult{}
defer func() {
r.Elapsed = time.Since(start)
}()
core.Info(
"Starting deploy",
"project", project, "environment", environment, "force", force,
)
p, ok := s.config.Projects[project]
if !ok {
core.Error("Project not found", "project", p)
r.Errors = []error{ErrProjectNotFound}
return r
}
r.Errors = p.Deploy(environment, w, force)
if len(r.Errors) == 0 {
r.Done = true
core.Info("Deploy success", "project", p, "environment", environment)
} else {
for _, e := range r.Errors {
core.Critical(e.Error(), "project", p, "environment", environment)
}
}
return r
}