-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
303 lines (266 loc) · 7.31 KB
/
main.go
File metadata and controls
303 lines (266 loc) · 7.31 KB
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
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
flag "github.com/spf13/pflag"
"gopkg.in/go-playground/webhooks.v5/github"
)
// These variables will be overriden on build.
var (
revisionID = "unknown"
buildTimestamp = "unknown"
)
func main() {
stackName := ""
workDir := ""
listenHostPort := ":8080"
servePath := "/stack-deployments"
secret := ""
flag.StringVar(&stackName, "stack", "",
"The name of the stack.")
flag.StringVar(&workDir, "workdir", "",
"If provided, swhook will use this directory to checkout the "+
"repository. By default, temporary directory will be used.")
flag.StringVar(&listenHostPort, "listen", listenHostPort,
"Where should the service listen to.")
flag.StringVar(&secret, "secret", "",
"The secret used to validate the "+
"requests comming to the hook.")
flag.Parse()
fmt.Fprintf(os.Stderr, "swhook revision %s built at %s\n",
revisionID, buildTimestamp)
svc, err := NewStackDeploymentService(stackName, workDir, secret)
if err != nil {
log.Fatalf("Unable to initialize service: %v", err)
}
http.HandleFunc(servePath, svc.postStackDeployments)
log.Printf("Starting HTTP server at %s ...", listenHostPort)
err = http.ListenAndServe(listenHostPort, nil)
if err != nil {
log.Fatalf("Unable to start the HTTP server: %v", err)
}
}
// NewStackDeploymentService returns a new instance of StackDeploymentService.
func NewStackDeploymentService(
stackName string,
workDir string,
secret string,
) (*StackDeploymentService, error) {
if stackName == "" {
return nil, errors.New("stack name must not be empty")
}
opts := []github.Option{}
if secret != "" {
opts = append(opts, github.Options.Secret(secret))
}
ghHook, err := github.New(opts...)
if err != nil {
log.Fatalf("GitHub hook handler instantiation: %v", err)
}
log.Printf("Creating service for stack %q, with working directory %q ...",
stackName, workDir)
return &StackDeploymentService{
githubHook: ghHook,
stackName: stackName,
workDir: workDir,
}, nil
}
// StackDeploymentService represents a service for a stack deployment.
type StackDeploymentService struct {
githubHook *github.Webhook
stackName string
workDir string
}
func (svc *StackDeploymentService) postStackDeployments(w http.ResponseWriter, r *http.Request) {
payload, err := svc.githubHook.Parse(r, github.PushEvent)
if err != nil {
if err == github.ErrEventNotFound {
w.WriteHeader(http.StatusOK)
return
}
}
switch eventData := payload.(type) {
case github.PushPayload:
revision := eventData.After
repoURL := eventData.Repository.SSHURL
err := svc.updateDeployment(repoURL, revision)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusInternalServerError)
}
func (svc *StackDeploymentService) updateDeployment(repoURL string, revision string) error {
log.Printf("stack %q rev %s: Preparing new deployment...",
svc.stackName, revision[:12])
action := NewStackDeploymentAction(svc.stackName, svc.workDir, repoURL, revision)
go func() {
defer func() {
if recData := recover(); recData != nil {
log.Printf("stack %q rev %s: Deployment update caused panic: %v",
svc.stackName, revision[:12], recData)
}
}()
err := action.Run()
if err != nil {
log.Printf("stack %q rev %s: Deployment update failed: %v",
svc.stackName, revision[:12], err)
return
}
log.Printf("stack %q rev %s: Deployment update complete.",
svc.stackName, revision[:12])
}()
return nil
}
// NewStackDeploymentAction creates an action for updating the deployment.
func NewStackDeploymentAction(
stackName string,
workDir string,
composeRepoURL string,
composeRevision string,
) *StackDeploymentAction {
return &StackDeploymentAction{
stackName: stackName,
workDir: workDir,
workDirSpecified: workDir != "",
composeRepoURL: composeRepoURL,
composeRevision: composeRevision,
}
}
// StackDeploymentAction holds information required for an update.
type StackDeploymentAction struct {
stackName string
workDir string
workDirSpecified bool
composeRepoURL string
composeRevision string
composeFilename string
}
// Run runs the action.
func (action *StackDeploymentAction) Run() error {
var err error
if !action.workDirSpecified {
action.workDir, err = ioutil.TempDir("", "swhook_")
if err != nil {
return fmt.Errorf("temp dir: %w", err)
}
defer os.RemoveAll(action.workDir)
}
err = action.checkout()
if err != nil {
return err
}
err = action.execHook("pre-deploy")
if err != nil {
return err
}
err = action.deploy()
if err != nil {
return err
}
err = action.execHook("post-deploy")
if err != nil {
return err
}
return nil
}
func (action *StackDeploymentAction) checkout() error {
workDirExists := false
if action.workDirSpecified {
workDirExists = dirExists(filepath.Join(action.workDir, ".git"))
}
if !workDirExists {
cmd := exec.Command("git", "clone", action.composeRepoURL, action.workDir)
outBytes, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("git clone: %w\n%s", err, outBytes)
}
} else {
cmd := exec.Command("git", "pull")
cmd.Dir = action.workDir
outBytes, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("git pull: %w\n%s", err, outBytes)
}
}
cmd := exec.Command("git", "reset", "--hard", action.composeRevision)
cmd.Dir = action.workDir
outBytes, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("git reset: %w\n%s", err, outBytes)
}
return nil
}
func (action *StackDeploymentAction) execHook(hookName string) error {
hookFilename := filepath.Join(action.workDir, ".swhook", "hooks", hookName)
cmd := exec.Command(hookFilename)
cmd.Dir = action.workDir
outBytes, err := cmd.CombinedOutput()
if err != nil {
if pathErr, ok := err.(*os.PathError); ok {
if pathErr.Op == "fork/exec" && os.IsNotExist(err) && pathErr.Path == hookFilename {
return nil
}
}
return fmt.Errorf("exec %s hook: %w\n%s", hookName, err, outBytes)
}
return nil
}
func (action *StackDeploymentAction) deploy() error {
stackName := action.stackName
composeFilename := action.composeFilename
if composeFilename == "" {
suffixes := []string{".yml", ".yaml", "-stack.yml", "-stack.yaml"}
for _, sfx := range suffixes {
fname := stackName + sfx
if fileExists(fname) {
composeFilename = fname
break
}
}
if composeFilename == "" {
composeFilename = stackName + "-stack.yaml"
}
}
var cmdEnv []string
if action.workDirSpecified {
cmdEnv = append(os.Environ(), "SWHOOK_WORKDIR="+action.workDir)
}
log.Printf("stack %q rev %s: Executing stack update with compose file %q ...",
stackName, action.composeRevision[:12], composeFilename)
cmd := exec.Command("docker", "stack", "deploy",
"--with-registry-auth",
"--prune",
"--compose-file", composeFilename,
stackName)
cmd.Dir = action.workDir
cmd.Env = cmdEnv
outBytes, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("docker stack deploy: %w\n%s", err, outBytes)
}
return nil
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func dirExists(dirname string) bool {
info, err := os.Stat(dirname)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}