-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
337 lines (284 loc) · 6.8 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
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"runtime"
"github.com/cloud66/cxlogger"
"github.com/inconshreveable/go-update"
"github.com/kardianos/osext"
)
type GockerDownload struct {
Version string `json:"version"`
Platform string `json:"platform"`
Arch string `json:"architecture"`
SHA string `json:"sha"`
File string `json:"file"`
}
type GockerLatest struct {
Version string `json:"latest"`
}
var (
flagForcedVersion string
currentPlatform string
currentArch string
)
var ErrHashMismatch = errors.New("mismatch SHA")
var ErrNoUpdateAvailable = errors.New("no update available")
const (
DOWNLOAD_URL = "http://downloads.cloud66.com.s3.amazonaws.com/gocker/"
)
func init() {
if os.Getenv("GOCKER_PLATFORM") == "" {
currentPlatform = runtime.GOOS
} else {
currentPlatform = os.Getenv("GOCKER_PLATFORM")
}
if os.Getenv("GOCKER_ARCH") == "" {
currentArch = runtime.GOARCH
} else {
currentArch = os.Getenv("GOCKER_ARCH")
}
}
func runUpdate() bool {
updateIt, err := needUpdate()
if err != nil {
cxlogger.Info(err)
return false
}
if !updateIt {
cxlogger.Info("No need for update")
return false
}
// houston we have an update. which one do we need?
download, err := getVersionManifest(flagForcedVersion)
if err != nil {
cxlogger.Info(err)
}
if download == nil {
cxlogger.Info("Found no matching download for the current OS and ARCH")
return false
}
err = download.update()
if err != nil {
cxlogger.Info("Failed to update")
cxlogger.Info(err)
return false
}
return true
}
func needUpdate() (bool, error) {
// get the latest version from remote
cxlogger.Info("Checking for latest version")
latest, err := findLatestVersion()
if err != nil {
return false, err
}
cxlogger.Debugf("Found %s as the latest version\n", latest.Version)
if flagForcedVersion != "" {
cxlogger.Debugf("Forcing update to %s\n", flagForcedVersion)
cxlogger.Info(err)
return true, nil
} else {
flagForcedVersion = latest.Version
}
if VERSION == latest.Version {
return false, nil
}
return true, nil
}
func getVersionManifest(version string) (*GockerDownload, error) {
resp, err := http.Get(DOWNLOAD_URL + "gocker_" + version + ".json")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error fetching version manifest: %d", resp.StatusCode)
}
var manifest []GockerDownload
if err = json.NewDecoder(resp.Body).Decode(&manifest); err != nil {
return nil, err
}
// find our OS and ARCH
for _, download := range manifest {
if download.Platform == currentPlatform && download.Arch == currentArch {
return &download, nil
}
}
return nil, nil
}
func backgroundRun() {
b, err := needUpdate()
if err != nil {
return
}
if b {
self, err := osext.Executable()
if err != nil {
// fail update, couldn't figure out path to self
return
}
l := exec.Command("logger", "-tgocker")
c := exec.Command(self, "update")
if w, err := l.StdinPipe(); err == nil && l.Start() == nil {
c.Stdout = w
c.Stderr = w
}
c.Start()
}
}
func (download *GockerDownload) update() error {
bin, err := download.fetchAndVerify()
if err != nil {
return err
}
err, errRecover := update.New().FromStream(bytes.NewBuffer(bin))
if errRecover != nil {
return fmt.Errorf("update and recovery errors: %q %q\n", err, errRecover)
}
if err != nil {
return err
}
fmt.Printf("Updated v%s -> v%s.\n", VERSION, download.Version)
return nil
}
func (download *GockerDownload) fetchAndVerify() ([]byte, error) {
bin, err := download.fetchBin()
if err != nil {
return nil, err
}
return bin, nil
}
func (download *GockerDownload) fetchBin() ([]byte, error) {
r, err := fetch(DOWNLOAD_URL + download.File)
if err != nil {
return nil, err
}
defer r.Close()
buf, err := download.decompress(r)
if err != nil {
return nil, err
}
return buf, nil
}
func (download *GockerDownload) decompress(r io.ReadCloser) ([]byte, error) {
// for darwin and windows the files are zipped
if download.Platform == "windows" || download.Platform == "darwin" {
cxlogger.Debugf("Decompressing for %s\n", download.Platform)
// write it to disk and unzip from there
dest, err := ioutil.TempFile("", "gocker")
defer os.Remove(dest.Name())
if err != nil {
return nil, err
}
cxlogger.Debugf("Using temp file %s\n", dest.Name())
writer, err := os.Create(dest.Name())
if err != nil {
return nil, err
}
defer writer.Close()
io.Copy(writer, r)
// now unzip it
zipper, err := zip.OpenReader(dest.Name())
if err != nil {
return nil, err
}
defer r.Close()
for _, f := range zipper.File {
cxlogger.Debugf("Zipped file %s\n", f.Name)
var targetFile string
if download.Platform == "windows" {
targetFile = "gocker.exe"
} else {
targetFile = "gocker_" + flagForcedVersion + "_" + currentPlatform + "_" + currentArch + "/gocker"
}
if f.Name == targetFile {
rc, err := f.Open()
if err != nil {
return nil, err
}
defer rc.Close()
buf := new(bytes.Buffer)
if _, err = io.Copy(buf, rc); err != nil {
return nil, err
}
// we are done
return buf.Bytes(), nil
}
}
}
// for linux they are tarred and gzipped
if download.Platform == "linux" {
buf := new(bytes.Buffer)
gz, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
if _, err = io.Copy(buf, gz); err != nil {
return nil, err
}
untar := new(bytes.Buffer)
// now untar
tr := tar.NewReader(buf)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
cxlogger.Debugf("Gziped file %s\n", hdr.Name)
if hdr.Name == "gocker_"+flagForcedVersion+"_linux_"+currentArch+"/gocker" {
// this is the executable
if _, err := io.Copy(untar, tr); err != nil {
return nil, err
}
}
}
return untar.Bytes(), nil
}
panic("unreached")
}
func fetch(url string) (io.ReadCloser, error) {
cxlogger.Debugf("Downloading %s\n", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.Body, nil
case 401, 403, 404:
return nil, ErrNoUpdateAvailable
default:
return nil, fmt.Errorf("bad http status from %s: %v", url, resp.Status)
}
}
func findLatestVersion() (*GockerLatest, error) {
path := DOWNLOAD_URL + "gocker_latest.json"
cxlogger.Debugf("Dowloading gocker manifest from %s\n", path)
resp, err := http.Get(path)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error fetching latest version manifest: %d", resp.StatusCode)
}
var latest GockerLatest
if err = json.NewDecoder(resp.Body).Decode(&latest); err != nil {
return nil, err
}
return &latest, nil
}