forked from flynn/flynn
/
update.go
177 lines (157 loc) · 3.78 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
package main
import (
"bytes"
"compress/gzip"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
cfg "github.com/flynn/flynn/cli/config"
"github.com/flynn/flynn/pkg/random"
"github.com/flynn/flynn/pkg/tufutil"
"github.com/flynn/flynn/pkg/version"
tuf "github.com/flynn/go-tuf/client"
tufdata "github.com/flynn/go-tuf/data"
"github.com/kardianos/osext"
"gopkg.in/inconshreveable/go-update.v0"
)
const upcktimePath = "cktime"
var updateDir = filepath.Join(cfg.Dir(), "update")
var updater *Updater
func runUpdate() error {
if updater == nil || !version.Tagged() {
return errors.New("Dev builds don't support auto-updates")
}
return updater.update()
}
type Updater struct {
repo string
rootKeys []*tufdata.Key
}
func (u *Updater) backgroundRun() {
if u == nil {
return
}
if !u.wantUpdate() {
return
}
self, err := osext.Executable()
if err != nil {
// fail update, couldn't figure out path to self
return
}
// TODO(titanous): logger isn't on Windows. Replace with proper error reports.
l := exec.Command("logger", "-tflynn")
c := exec.Command(self, "update")
if w, err := l.StdinPipe(); err == nil && l.Start() == nil {
c.Stdout = w
c.Stderr = w
}
c.Start()
}
func (u *Updater) wantUpdate() bool {
path := filepath.Join(updateDir, upcktimePath)
if !version.Tagged() || readTime(path).After(time.Now()) {
return false
}
wait := 12*time.Hour + randDuration(8*time.Hour)
return writeTime(path, time.Now().Add(wait))
}
func (u *Updater) update() error {
up := update.New()
if err := up.CanUpdate(); err != nil {
return err
}
if err := os.MkdirAll(updateDir, 0755); err != nil {
return err
}
local, err := tuf.FileLocalStore(filepath.Join(updateDir, "tuf.db"))
if err != nil {
return err
}
plat := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)
opts := &tuf.HTTPRemoteOptions{
UserAgent: fmt.Sprintf("flynn-cli/%s %s", version.String(), plat),
}
remote, err := tuf.HTTPRemoteStore(u.repo, opts)
if err != nil {
return err
}
client := tuf.NewClient(local, remote)
if err := u.updateTUFClient(client); err != nil {
return err
}
name := fmt.Sprintf("/flynn-%s.gz", plat)
latestVersion, err := tufutil.GetVersion(client, name)
if err != nil {
return err
}
if latestVersion == version.String() {
return nil
}
bin := &tufBuffer{}
if err := client.Download(name, bin); err != nil {
return err
}
gr, err := gzip.NewReader(bin)
if err != nil {
return err
}
err, errRecover := up.FromStream(gr)
if errRecover != nil {
return fmt.Errorf("update and recovery errors: %q %q", err, errRecover)
}
if err != nil {
return err
}
log.Printf("Updated %s -> %s.", version.String(), latestVersion)
return nil
}
// updateTUFClient updates the given client, initializing and re-running the
// update if ErrNoRootKeys is returned.
func (u *Updater) updateTUFClient(client *tuf.Client) error {
_, err := client.Update()
if err == nil || tuf.IsLatestSnapshot(err) {
return nil
}
if err == tuf.ErrNoRootKeys {
if err := client.Init(u.rootKeys, len(u.rootKeys)); err != nil {
return err
}
return u.updateTUFClient(client)
}
return err
}
// returns a random duration in [0,n).
func randDuration(n time.Duration) time.Duration {
return time.Duration(random.Math.Int63n(int64(n)))
}
func readTime(path string) time.Time {
p, err := ioutil.ReadFile(path)
if os.IsNotExist(err) {
return time.Time{}
}
if err != nil {
return time.Now().Add(1000 * time.Hour)
}
t, err := time.Parse(time.RFC3339, string(p))
if err != nil {
return time.Now().Add(1000 * time.Hour)
}
return t
}
func writeTime(path string, t time.Time) bool {
return ioutil.WriteFile(path, []byte(t.Format(time.RFC3339)), 0644) == nil
}
type tufBuffer struct {
bytes.Buffer
}
func (b *tufBuffer) Delete() error {
b.Reset()
return nil
}