This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
/
update.go
150 lines (122 loc) · 2.97 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
package autoupdate
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/Luzifer/gobuilder/builddb"
"github.com/inconshreveable/go-update"
)
// Updater is a helper for autoupdating go-binaries with their builds on GoBuilder.me
type Updater struct {
UpdateInterval time.Duration
SelfRestart bool
repository string
label string
runningFile string
currentHash string
liveHash string
goBuilderFilename string
}
// New instanciates a new Updater
func New(repo, label string) *Updater {
filename := fmt.Sprintf("%s_%s_%s-%s",
path.Base(repo),
label,
runtime.GOOS,
runtime.GOARCH,
)
if runtime.GOOS == "windows" {
filename = filename + ".exe"
}
return &Updater{
UpdateInterval: time.Minute * 60,
repository: repo,
label: label,
runningFile: os.Args[0],
goBuilderFilename: filename,
}
}
// Run starts a permanent loop (better start as a go-routine) looking for updates
// to the current binary and will execute the update if required. This is intended
// for long running processes like daemons
func (g *Updater) Run() error {
for range time.Tick(g.UpdateInterval) {
if err := g.SingleRun(); err != nil {
return err
}
}
return nil
}
// SingleRun checks for an update and updates the binary when required.
func (g *Updater) SingleRun() error {
hasUpdate, err := g.HasUpdate()
if err != nil {
return err
}
if hasUpdate {
if err := g.updateBinary(); err != nil {
return fmt.Errorf("Update failed: %s", err)
}
}
return nil
}
// HasUpdate checks whether a new version is available
func (g *Updater) HasUpdate() (bool, error) {
bin, err := ioutil.ReadFile(g.runningFile)
if err != nil {
return false, err
}
g.currentHash = fmt.Sprintf("%x", sha256.Sum256(bin))
liveHash, err := g.getGoBuilderHash()
if err != nil {
return false, err
}
return len(liveHash) == len(g.currentHash) && liveHash != g.currentHash, nil
}
func (g *Updater) getGoBuilderHash() (string, error) {
url := fmt.Sprintf("https://gobuilder.me/api/v1/%s/hashes/%s.json",
g.repository,
g.label,
)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("HTTP Status != 200")
}
out := builddb.HashDB{}
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return "", err
}
hashes, ok := out[g.goBuilderFilename]
if !ok {
return "", fmt.Errorf("Could not find hashes for %s", g.goBuilderFilename)
}
g.liveHash = hashes.SHA256
return strings.TrimSpace(hashes.SHA256), nil
}
func (g *Updater) updateBinary() error {
dlURL := fmt.Sprintf("https://gobuilder.me/get/%s/%s",
g.repository,
g.goBuilderFilename,
)
resp, err := http.Get(dlURL)
if err != nil {
return err
}
defer resp.Body.Close()
cs, _ := hex.DecodeString(g.liveHash)
return update.Apply(resp.Body, &update.Options{
Checksum: cs,
})
}