-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdeploy_handler.go
309 lines (277 loc) · 7.81 KB
/
deploy_handler.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"regexp"
"strings"
"sync"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/gengo/goship/lib/auth"
"github.com/gengo/goship/lib/config"
"github.com/gengo/goship/lib/notification"
"github.com/gengo/goship/lib/revision"
"github.com/golang/glog"
"golang.org/x/net/context"
)
type DeployHandler struct {
ecl *etcd.Client
ctrl revision.Control
hub *notification.Hub
}
func (h DeployHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
c, err := config.Load(h.ecl)
if err != nil {
glog.Errorf("Failed to fetch latest configuration: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u, err := auth.CurrentUser(r)
if err != nil {
glog.Errorf("Failed to fetch current user: %v", err)
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
var (
user = u.Name
projName, envName string
deploy RevRange
src = RevRange{
From: revision.Revision(r.FormValue("from_source_revision")),
To: revision.Revision(r.FormValue("to_source_revision")),
}
)
for _, spec := range []struct {
name string
value *string
}{
{name: "project", value: &projName},
{name: "environment", value: &envName},
{name: "from_revision", value: (*string)(&deploy.From)},
{name: "to_revision", value: (*string)(&deploy.To)},
} {
*spec.value = r.FormValue(spec.name)
if *spec.value == "" {
glog.Errorf("%s not specified", spec.name)
http.Error(w, fmt.Sprintf("%s not specified", spec.name), http.StatusBadRequest)
return
}
}
proj, err := config.ProjectFromName(c.Projects, projName)
if err != nil {
http.Error(w, "no such project", http.StatusNotFound)
return
}
env, err := config.EnvironmentFromName(c.Projects, projName, envName)
if err != nil {
http.Error(w, "no such project/environment", http.StatusNotFound)
return
}
h.deploy(ctx, w, c, user, proj, *env, deploy, src)
}
func (h DeployHandler) deploy(ctx context.Context, w http.ResponseWriter, c config.Config, user string, proj config.Project, env config.Environment, deploy, src RevRange) {
if c.Notify != "" {
err := startNotify(c.Notify, user, proj.Name, env.Name)
if err != nil {
glog.Errorf("Failed to notify start-deployment event of %s (%s): %v", proj.Name, env.Name, err)
}
}
deployTime := time.Now()
success := true
command := deployCommand(env)
cmd := exec.Command(command[0], command[1:]...)
stdout, err := cmd.StdoutPipe()
if err != nil {
glog.Errorf("Could not get stdout of command: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
stderr, err := cmd.StderrPipe()
if err != nil {
glog.Errorf("Could not get stderr of command: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
repo := proj.SourceRepo()
glog.Infof("Starting deployment of %s-%s (%s/%s) from %s to %s; requested by %s", proj.Name, env.Name, repo.RepoOwner, repo.RepoName, deploy.From, deploy.To, user)
if err = cmd.Start(); err != nil {
glog.Errorf("Could not run deployment command: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var wg sync.WaitGroup
wg.Add(2)
go h.sendOutput(&wg, bufio.NewScanner(stdout), proj.Name, env.Name, deployTime)
go h.sendOutput(&wg, bufio.NewScanner(stderr), proj.Name, env.Name, deployTime)
wg.Wait()
err = cmd.Wait()
if err != nil {
success = false
glog.Errorf("Deployment of %s failed: %v", proj.Name, err)
} else {
glog.Infof("Successfully deployed %s", proj.Name)
}
if c.Notify != "" {
err = endNotify(c.Notify, proj.Name, env.Name, success)
if err != nil {
glog.Errorf("Failed to notify start-deployment event of %s (%s): %v", proj.Name, env.Name, err)
}
}
if (c.Pivotal.Token != "") && success {
err := config.PostToPivotal(c.Pivotal, env.Name, repo.RepoOwner, repo.RepoName, string(deploy.From), string(deploy.To))
if err != nil {
glog.Errorf("Failed to post to pivotal: %v", err)
} else {
glog.Infof("Pivotal Token: %s", c.Pivotal.Token)
}
}
err = h.insertEntry(ctx, proj, env, deploy, src, user, success, deployTime)
if err != nil {
glog.Errorf("Failed to insert an entry: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h DeployHandler) sendOutput(wg *sync.WaitGroup, scanner *bufio.Scanner, p, e string, deployTime time.Time) {
defer wg.Done()
for scanner.Scan() {
t := scanner.Text()
msg := struct {
Project string
Environment string
StdoutLine string
}{p, e, stripANSICodes(strings.TrimSpace(t))}
cmdOutput, err := json.Marshal(msg)
if err != nil {
glog.Errorf("Failed to marshal output into JSON: %v", err)
}
h.hub.Broadcast(string(cmdOutput))
go appendDeployOutput(fmt.Sprintf("%s-%s", p, e), t, deployTime)
}
if err := scanner.Err(); err != nil {
glog.Errorf("Failed to scan deploy output: %v", err)
return
}
}
func stripANSICodes(t string) string {
ansi := regexp.MustCompile(`\x1B\[[0-9;]{1,4}[mK]`)
return ansi.ReplaceAllString(t, "")
}
func appendDeployOutput(env string, output string, timestamp time.Time) {
logDir := path.Join(*dataPath, env)
path := path.Join(logDir, timestamp.String()+".log")
if _, err := os.Stat(logDir); err != nil {
if os.IsNotExist(err) {
err := os.Mkdir(logDir, 0755)
if err != nil {
fmt.Printf("ERROR: %s", err)
}
}
}
out, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
fmt.Printf("ERROR: %s", err)
}
defer out.Close()
io.WriteString(out, output+"\n")
}
func startNotify(n, user, p, env string) error {
msg := fmt.Sprintf("%s is deploying %s to *%s*.", user, p, env)
err := notify(n, msg)
if err != nil {
return err
}
return nil
}
func notify(n, msg string) error {
cmd := exec.Command(n, msg)
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func endNotify(n, p, env string, success bool) error {
msg := fmt.Sprintf("%s successfully deployed to *%s*.", p, env)
if !success {
msg = fmt.Sprintf("%s deployment to *%s* failed.", p, env)
}
err := notify(n, msg)
if err != nil {
return err
}
return nil
}
// deployCommand returns the deployment command for a given
// environment as a string slice that has been split on spaces.
func deployCommand(e config.Environment) []string {
// TODO(yugui) better handling of shell escape
return strings.Split(e.Deploy, " ")
}
func (h DeployHandler) insertEntry(ctx context.Context, proj config.Project, env config.Environment, deploy, src RevRange, user string, success bool, time time.Time) error {
basename := fmt.Sprintf("%s-%s", proj.Name, env.Name)
path := path.Join(*dataPath, basename+".json")
err := prepareDataFiles(path)
if err != nil {
return err
}
e, err := readEntries(basename)
if err != nil {
return err
}
repo := proj.SourceRepo()
var msg string
if src.To != "" {
msg, err = h.ctrl.SourceRevMessage(ctx, proj, src.To)
if err != nil {
glog.Errorf("Failed to get commit %s (%s/%s): %v", src.To, repo.RepoOwner, repo.RepoName, err)
msg = ""
}
}
var diffURL string
if src.From != "" && src.To != "" {
diffURL = h.ctrl.SourceDiffURL(proj, src.From, src.To)
}
d := DeployLogEntry{
Range: deploy,
DiffURL: diffURL,
ToRevisionMsg: msg,
User: user,
Time: time,
Success: success,
}
e = append(e, d)
err = writeJSON(e, path)
if err != nil {
return err
}
return nil
}
func prepareDataFiles(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
_, err := os.Create(path)
if err != nil {
return err
}
err = writeJSON([]DeployLogEntry{}, path)
if err != nil {
return err
}
}
return nil
}
func writeJSON(d []DeployLogEntry, file string) error {
b, err := json.Marshal(d)
if err != nil {
return err
}
return ioutil.WriteFile(file, b, 0755)
}