-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate.go
417 lines (365 loc) · 10.5 KB
/
update.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
"archive/zip"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"sync"
"time"
"github.com/anacrolix/torrent"
"github.com/pkg/errors"
"github.com/anacrolix/torrent/metainfo"
)
const (
// UUIDApk is the UUID of updates that uses APK (Alpine Package Management)
// for deployment.
// Generated by invoking:
// $ uuidgen --sha1 --namespace @oid --name /sbin/apk
UUIDApk = "5ee3a38d-a8dc-514e-9c74-42ab160648aa"
// UUIDShell is the UUID of updates that uses shell script for deployment.
// Generated by invoking:
// $ uuidgen --sha1 --namespace @oid --name /bin/sh
UUIDShell = "f5adf0cb-b0e1-5a22-97f1-09092f566438"
// DeployFailsLimit is the maximum fails of deployment. Exceeding this value
// means that the update should not be deployed.
DeployFailsLimit = 5
// ShellExecutionTimeout is the maximum execution time of a shell script
// before timeout.
ShellExecutionTimeout = 600 // in seconds
)
// Update represents a system update that should be downloaded and deployed on
// the system. It also has to be distributed to other peers.
type Update struct {
sync.RWMutex
Notification Notification `json:"notification"`
Deployed time.Time `json:"deployed"`
Source string `json:"source"`
Stopped bool `json:"stopped"`
Sent bool `json:"sent"`
DeployFails int `json:"deploy-fails"`
Missing int64 `json:"missing"`
torrent *torrent.Torrent
agent *Agent
}
// NewUpdate returns an Update instance from given notification and agent.
func NewUpdate(n Notification, a *Agent) *Update {
return &Update{
Notification: n,
Stopped: true,
Sent: false,
agent: a,
}
}
// LoadUpdateFromFile loads Update description from given filename.
func LoadUpdateFromFile(filename string, a *Agent) (*Update, error) {
u := Update{
Stopped: true,
Sent: false,
agent: a,
}
f, err := os.Open(filename)
if err != nil {
return nil, err
}
return &u, json.NewDecoder(f).Decode(&u)
}
// MetadataFilename returns the name of the update metadata file.
func (u *Update) MetadataFilename() string {
filename := fmt.Sprintf("%s-v%d", u.Notification.UUID, u.Notification.Version)
return filepath.Join(u.agent.metadataDir, filename)
}
// Save writes Update metadata to file.
func (u *Update) Save() error {
f, err := os.OpenFile(u.MetadataFilename(), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0640)
if err != nil {
return err
}
return u.Write(f)
}
// Write writes this Update instance to Writer 'w'.
func (u *Update) Write(w io.Writer) error {
u.RLock()
defer u.RUnlock()
return json.NewEncoder(w).Encode(u)
}
// Verify verifies the update. It returns an error if the verification fails,
// otherwise nil.
func (u *Update) Verify(a *Agent) error {
if err := u.Notification.Verify(a.PublicKey); err != nil {
log.Printf("verification failed: %v", err)
return errUpdateVerificationFailed
}
return nil
}
// Start starts the update's lifecycle.
func (u *Update) Start(a *Agent) error {
u.Lock()
defer u.Unlock()
var (
mi *metainfo.MetaInfo
old *Update
err error
)
if err = u.Verify(a); err != nil {
return err
}
// Remove existing update that has the same UUID. If the existing update
// is newer, then return an error.
if old, err = a.addUpdate(u); err != nil {
return err
}
if old == nil {
log.Printf("older update of uuid:%s does not exist", u.Notification.UUID)
} else {
old.Stop()
if err = old.Delete(); err != nil {
log.Printf("WARNING: failed to delete update uuid:%s version:%d - %v",
old.Notification.UUID, old.Notification.Version, err)
}
}
// activate torrent
log.Printf("starting update: %s", u.String())
if mi, err = u.Notification.torrentMetainfo(); err != nil {
return fmt.Errorf("failed generating torrent metainfo: %v", err)
}
if u.torrent, err = a.torrentClient.AddTorrent(mi); err != nil {
return fmt.Errorf("failed adding torrent: %v", err)
}
u.Stopped = false
log.Printf("started update: %s", u.String())
// spawn a go-routine that monitors torrent's status
go u.monitor(a)
return nil
}
func (u *Update) monitor(a *Agent) {
toSave := true
for {
time.Sleep(5 * time.Second)
u.Lock()
if u.Stopped || u.torrent == nil {
break
}
if !u.Sent {
if err := u.Notification.Write(a.Overlay); err != nil {
log.Printf("failed sending update uuid:%s version:%d : %v",
u.Notification.UUID, u.Notification.Version, err)
} else {
u.Sent = true
toSave = true
}
}
u.Missing = u.torrent.BytesMissing()
if u.Missing > 0 {
<-u.torrent.GotInfo()
u.torrent.DownloadAll()
} else if !a.Config.Proxy && u.Deployed.Year() < 2000 {
u.deploy()
toSave = true
}
log.Println(u.String())
u.Unlock()
if toSave {
u.Save()
toSave = false
}
}
}
// Stop stops the lifecycle of the update.
func (u *Update) Stop() {
u.Lock()
defer u.Unlock()
log.Printf("stopping update: %v", u.String())
u.Stopped = true
if u.torrent != nil {
u.torrent.Drop()
<-u.torrent.Closed()
u.torrent = nil
}
log.Printf("stopped update: %v", u.String())
}
// Delete deletes this update files.
func (u *Update) Delete() error {
u.Lock()
defer u.Unlock()
log.Printf("deleting update: %v", u.String())
if !u.Stopped {
return fmt.Errorf("update has not been stopped")
}
filename := filepath.Join(u.agent.dataDir, u.Notification.Info.Name)
if err := os.RemoveAll(filename); err != nil {
log.Printf("WARNING: failed removing update file %s", filename)
}
filename = u.MetadataFilename()
if err := os.RemoveAll(filename); err != nil {
return errors.Wrapf(err, "failed deleting update uuid:%s version:%d",
u.Notification.UUID, u.Notification.Version)
}
log.Printf("deleted update: %v", u.String())
return nil
}
func (u *Update) String() string {
var b bytes.Buffer
b.WriteString(fmt.Sprintf("uuid:%v version:%d", u.Notification.UUID, u.Notification.Version))
if u.torrent != nil {
b.WriteString(fmt.Sprintf(" completed/missing:%v/%v",
u.torrent.BytesCompleted(), u.torrent.BytesMissing()))
stats := u.torrent.Stats()
b.WriteString(
fmt.Sprintf(" seeding:%v peers(total/active):%v/%v read/write:%v/%v",
u.torrent.Seeding(), stats.TotalPeers, stats.ActivePeers,
stats.BytesRead, stats.BytesWritten))
s := u.torrent.PieceState(0)
b.WriteString(
fmt.Sprintf(" piece[0]checking:%v complete:%v ok:%v partial:%v priority:%v",
s.Checking, s.Complete, s.Ok, s.Partial, s.Priority))
}
return b.String()
}
func (u *Update) deploy() {
if u.DeployFails > DeployFailsLimit {
log.Printf("Too many deployment failures:%d uuid:%s version:%d",
u.DeployFails, u.Notification.UUID, u.Notification.Version)
return
}
var (
apk ApkDeployer
shell ShellDeployer
err error
)
log.Printf("deploying update uuid:%s version:%d", u.Notification.UUID, u.Notification.Version)
switch u.Notification.UUID {
case UUIDApk:
err = u.deployWith(apk)
case UUIDShell:
err = u.deployWith(shell)
default:
u.DeployFails++
log.Printf("ERROR: Unrecognized uuid:%s", u.Notification.UUID)
return
}
if err != nil {
u.DeployFails++
} else {
u.DeployFails = 0
u.Deployed = time.Now()
}
}
func (u *Update) deployWith(d Deployer) error {
for _, f := range u.torrent.Files() {
script := filepath.Join(u.agent.dataDir, f.Path())
log.Printf("executing update shell uuid:%s version:%d file:%s",
u.Notification.UUID, u.Notification.Version, script)
if err := d.deploy(script, ShellExecutionTimeout*time.Second); err != nil {
log.Printf("ERROR: executed update shell with error uuid:%s version:%d file:%s - %v",
u.Notification.UUID, u.Notification.Version, f.Path(), err)
return err
}
log.Printf("executed update shell script uuid:%s version:%d file:%s",
u.Notification.UUID, u.Notification.Version, f.Path())
}
return nil
}
// Deployer is an interface of update deployer.
type Deployer interface {
deploy(filename string, d time.Duration) error
}
// ShellDeployer is an update deployer using system shell.
type ShellDeployer struct{}
func (sh ShellDeployer) deploy(filename string, d time.Duration) error {
st, err := os.Stat(filename)
if err != nil {
return err
}
if st.IsDir() {
return sh.deployDir(filename, d)
}
if strings.ToLower(filepath.Ext(filename)) == ".zip" {
return sh.deployZip(filename, d)
}
return sh.deployFile(filename, d)
}
func (ShellDeployer) deployFile(filename string, d time.Duration) error {
cmd := exec.Command("/bin/sh", filename)
if err := cmd.Start(); err != nil {
return err
}
timer := time.AfterFunc(d, func() {
cmd.Process.Kill()
})
err := cmd.Wait()
timer.Stop()
return err
}
func (sh ShellDeployer) deployZip(filename string, d time.Duration) error {
dir := path.Join(os.TempDir(), filename)
defer os.RemoveAll(dir)
_, err := Unzip(filename, dir)
if err != nil {
return fmt.Errorf("failed unzipping %s: %v", filename, err)
}
return sh.deployDir(dir, d)
}
// Unzip will decompress a zip archive, moving all files and folders
// within the zip file (parameter 1) to an output directory (parameter 2).
//
// References:
// - https://golangcode.com/unzip-files-in-go/
// - https://stackoverflow.com/questions/20357223/easy-way-to-unzip-file-with-golang
func Unzip(src string, dest string) ([]string, error) {
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return filenames, err
}
defer rc.Close()
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)
} else {
// Make File
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
// Close the file without defer to close before next iteration of loop
outFile.Close()
if err != nil {
return filenames, err
}
}
}
return filenames, nil
}
func (sh ShellDeployer) deployDir(filename string, d time.Duration) error {
main := fmt.Sprintf("%s/main.sh", filename)
if _, err := os.Stat(main); err != nil {
return err
}
return sh.deployFile(main, d)
}
// ApkDeployer is an update deployer using APK (Alpine Package Management).
type ApkDeployer struct{}
func (ApkDeployer) deploy(filename string, d time.Duration) error {
// TODO: implement
return fmt.Errorf("not implemented")
}