-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdewy.go
380 lines (327 loc) · 8.15 KB
/
dewy.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package dewy
import (
"bytes"
"context"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"syscall"
"time"
"github.com/carlescere/scheduler"
"github.com/cli/safeexec"
starter "github.com/lestrrat-go/server-starter"
"github.com/linyows/dewy/artifact"
"github.com/linyows/dewy/kvs"
"github.com/linyows/dewy/notify"
"github.com/linyows/dewy/registry"
)
const (
ISO8601 = "20060102T150405Z0700"
releaseDir = ISO8601
releasesDir = "releases"
symlinkDir = "current"
keepReleases = 7
// currentkeyName is a name whose value is the version of the currently running server application.
// For example, if you are using a file for the cache store, running `cat current` will show `v1.2.3--app_linux_amd64.tar.gz`, which is a combination of the tag and artifact.
// dewy uses this value as a key (**cachekeyName**) to manage the artifacts in the cache store.
currentkeyName = "current"
)
// Dewy struct.
type Dewy struct {
config Config
registry registry.Registry
artifact artifact.Artifact
cache kvs.KVS
isServerRunning bool
disableReport bool
root string
job *scheduler.Job
notify notify.Notify
sync.RWMutex
}
// New returns Dewy.
func New(c Config) (*Dewy, error) {
kv := &kvs.File{}
kv.Default()
wd, err := os.Getwd()
if err != nil {
return nil, err
}
su := strings.SplitN(c.Registry, "://", 2)
u, err := url.Parse(su[1])
if err != nil {
return nil, err
}
c.Registry = fmt.Sprintf("%s://%s", su[0], u.String())
return &Dewy{
config: c,
cache: kv,
isServerRunning: false,
root: wd,
}, nil
}
// Start dewy.
func (d *Dewy) Start(i int) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var err error
d.registry, err = registry.New(ctx, d.config.Registry)
if err != nil {
log.Printf("[ERROR] Registry failure: %#v", err)
}
d.notify, err = notify.New(ctx, d.config.Notify)
if err != nil {
log.Printf("[ERROR] Notify failure: %#v", err)
}
d.notify.Send(ctx, "Automatic shipping started by *Dewy*")
d.job, err = scheduler.Every(i).Seconds().Run(func() {
e := d.Run()
if e != nil {
log.Printf("[ERROR] Dewy run failure: %#v", e)
}
})
if err != nil {
log.Printf("[ERROR] Scheduler failure: %#v", err)
}
d.notify.Send(ctx, fmt.Sprintf("Stop receiving \"%s\" signal", d.waitSigs()))
}
func (d *Dewy) waitSigs() os.Signal {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sigReceived := <-sigCh
log.Printf("[DEBUG] PID %d received signal as %s", os.Getpid(), sigReceived)
d.job.Quit <- true
return sigReceived
}
// cachekeyName is "tag--artifact"
// example: v1.2.3--testapp_linux_amd64.tar.gz
func (d *Dewy) cachekeyName(res *registry.CurrentResponse) string {
u := strings.SplitN(res.ArtifactURL, "?", 2)
return fmt.Sprintf("%s--%s", res.Tag, filepath.Base(u[0]))
}
// Run dewy.
func (d *Dewy) Run() error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Get current
res, err := d.registry.Current(ctx, ®istry.CurrentRequest{
Arch: runtime.GOARCH,
OS: runtime.GOOS,
ArtifactName: d.config.ArtifactName,
})
if err != nil {
log.Printf("[ERROR] Current failure: %#v", err)
return err
}
// Check cache
cachekeyName := d.cachekeyName(res)
currentkeyValue, _ := d.cache.Read(currentkeyName)
found := false
list, err := d.cache.List()
if err != nil {
return err
}
for _, key := range list {
// same current version and already cached
if string(currentkeyValue) == cachekeyName && key == cachekeyName {
log.Print("[DEBUG] Deploy skipped")
if d.isServerRunning {
return nil
}
// when the server fails to start
break
}
// no current version but already cached
if key == cachekeyName {
found = true
if err := d.cache.Write(currentkeyName, []byte(cachekeyName)); err != nil {
return err
}
break
}
}
// Download artifact and cache
if !found {
buf := new(bytes.Buffer)
if d.artifact == nil {
d.artifact, err = artifact.New(ctx, res.ArtifactURL)
if err != nil {
return err
}
}
err := d.artifact.Download(ctx, buf)
d.artifact = nil
if err != nil {
return err
}
if err := d.cache.Write(cachekeyName, buf.Bytes()); err != nil {
return err
}
if err := d.cache.Write(currentkeyName, []byte(cachekeyName)); err != nil {
return err
}
log.Printf("[INFO] Cached as %s", cachekeyName)
}
d.notify.Send(ctx, fmt.Sprintf("Ready for `%s`", res.Tag))
if err := d.deploy(cachekeyName); err != nil {
return err
}
if d.config.Command == SERVER {
if d.isServerRunning {
err = d.restartServer()
if err == nil {
d.notify.Send(ctx, fmt.Sprintf("Server restarted for `%s`", res.Tag))
}
} else {
err = d.startServer()
if err == nil {
d.notify.Send(ctx, fmt.Sprintf("Server started for `%s`", res.Tag))
}
}
if err != nil {
log.Printf("[ERROR] Server failure: %#v", err)
}
}
if !d.disableReport {
log.Print("[DEBUG] Report shipping")
err := d.registry.Report(ctx, ®istry.ReportRequest{
ID: res.ID,
Tag: res.Tag,
})
if err != nil {
log.Printf("[ERROR] Report shipping failure: %#v", err)
}
}
log.Printf("[INFO] Keep releases as %d", keepReleases)
err = d.keepReleases()
if err != nil {
log.Printf("[ERROR] Keep releases failure: %#v", err)
}
return nil
}
func (d *Dewy) deploy(key string) (err error) {
if err := d.execHook(d.config.BeforeDeployHook); err != nil {
log.Printf("[ERROR] Before deploy hook failure: %#v", err)
return err
}
defer func() {
if err != nil {
return
}
// When deploy is success, run after deploy hook
if err := d.execHook(d.config.AfterDeployHook); err != nil {
log.Printf("[ERROR] After deploy hook failure: %#v", err)
}
}()
p := filepath.Join(d.cache.GetDir(), key)
linkFrom, err := d.preserve(p)
if err != nil {
log.Printf("[ERROR] Preserve failure: %#v", err)
return err
}
log.Printf("[INFO] Extract archive to %s", linkFrom)
linkTo := filepath.Join(d.root, symlinkDir)
if _, err := os.Lstat(linkTo); err == nil {
os.Remove(linkTo)
}
log.Printf("[INFO] Create symlink to %s from %s", linkTo, linkFrom)
if err := os.Symlink(linkFrom, linkTo); err != nil {
return err
}
return nil
}
func (d *Dewy) preserve(p string) (string, error) {
dst := filepath.Join(d.root, releasesDir, time.Now().UTC().Format(releaseDir))
if err := os.MkdirAll(dst, 0755); err != nil {
return "", err
}
if err := kvs.ExtractArchive(p, dst); err != nil {
return "", err
}
return dst, nil
}
func (d *Dewy) restartServer() error {
d.Lock()
defer d.Unlock()
p, _ := os.FindProcess(os.Getpid())
err := p.Signal(syscall.SIGHUP)
if err != nil {
return err
}
log.Print("[INFO] Send SIGHUP for server restart")
return nil
}
func (d *Dewy) startServer() error {
d.Lock()
defer d.Unlock()
d.isServerRunning = true
log.Print("[INFO] Start server")
ch := make(chan error)
go func() {
s, err := starter.NewStarter(d.config.Starter)
if err != nil {
log.Printf("[ERROR] Starter failure: %#v", err)
return
}
ch <- s.Run()
}()
return nil
}
func (d *Dewy) keepReleases() error {
dir := filepath.Join(d.root, releasesDir)
files, err := os.ReadDir(dir)
if err != nil {
return err
}
sort.Slice(files, func(i, j int) bool {
fi, err := files[i].Info()
if err != nil {
return false
}
fj, err := files[j].Info()
if err != nil {
return true
}
return fi.ModTime().Unix() > fj.ModTime().Unix()
})
for i, f := range files {
if i < keepReleases {
continue
}
if err := os.RemoveAll(filepath.Join(dir, f.Name())); err != nil {
return err
}
}
return nil
}
func (d *Dewy) execHook(cmd string) error {
if cmd == "" {
return nil
}
sh, err := safeexec.LookPath("sh")
if err != nil {
return err
}
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
c := exec.Command(sh, "-c", cmd)
c.Dir = d.root
c.Env = os.Environ()
c.Stdout = stdout
c.Stderr = stderr
defer func() {
log.Printf("[INFO] execute hook: command=%q stdout=%q stderr=%q", cmd, stdout.String(), stderr.String())
}()
if err := c.Run(); err != nil {
return err
}
return nil
}