auto-self-update

This commit is contained in:
Alex Grintsvayg 2018-08-30 16:50:46 -04:00
parent 22f42d5559
commit 25ec93a6b9
5 changed files with 115 additions and 2 deletions

View file

@ -48,8 +48,6 @@ before_script:
# script always run to completion (set +e). All of these code checks are must haves
# in a modern Go project.
script:
# Build Prism successfully
- make
# Fail if a .go file hasn't been formatted with gofmt
- test -z $(gofmt -s -l $GO_FILES)
# Run unit tests
@ -69,6 +67,8 @@ script:
- unconvert ./...
# one last linter - ignore autogen code
#- golint -set_exit_status $(go list ./... | grep -v /vendor/ )
# Finally, build the binary
- make
deploy:
- provider: releases
@ -80,3 +80,15 @@ deploy:
condition: "$DEPLOY = true"
api_key:
secure: epAlhp3SUr8hhISarJ22n6tRw2TEa4s4oNFIvJUb5HGECVp1SYN7ao0ln5NoNLmfJS60pi911i/kMhhi21/uhZ0kCYlEhhIE2pc1zsiAxK9L9ENCssJ205HfVbe5grhwskLGzgjhU9OznO8WtmyOPWXr0it8M8RCTjx6rEC0A33Id3WMYyhP938Sj9CxEYeH4KS8wFvBXkgBVtrgaYwRTCIROFddHFXOb9jyNhqQ1RbfKtllsVtQhVk5WMlomheBNSS4vr6WMS4X4+2okFqnLtiSn1wrn5I/94UQbnrI1juVnQj0K+j32EyQbAOt4T2cLW3GtG0jhaYKyNMT9ycDCdVACPSDELlHWjeyoes9bnhUFftm6kDbQxwA1UsTF1yG8tMKXxBSmYyoT7qDloi6pBifZMrFXL61uTs6yhVB9LS/2oqg4sc0Ne87bRcn4OxsBeVCe3kbBHDTR/NTyF2gNPtRvgMAWULxTVcUm9VYdO0IWvAig5g4Row0DnFzEquD6CzezbRWD9WyZyV/AFyYHeeQ2PO7jTw0/3M7aDX33Fuhh34lehzmrC03cfgD/wZW+spxozIcQCYdiJqVw+u+/NvbNr0kkFzE9zW26JEmUFTyDvKxvnza1Kwtww3EgH6zaOL8r4yVbb54rePRvLw7pl93zlfJnEB2MCPqJOY5ZpU=
- provider: s3
access_key_id:
secure: "LFY+XFzaqsyM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AkmrK620P5/zHvBn9eOnbt2pvqdXC7HT4w/iVJvpvFvZIA0s/bEjXlI6gLSQrRxqerIiLiL5UjnvLlS0b2vpyahcBX5TWUAkzQHjh+kyX9mgd14Sv0ihCQy3VCd1Lrf+sFYHab+BOmnLghe1gKzFvkjm4KZPAw6h+k/xGpS1Ya2CG6hWKKvrq3iPlnw9N2CEaDoIAnoyz1xvOLQAVwofVf0DH2X1Wwmw0zzD5C/13ubsQ1/18xITjRDm4XTQDBKsC6RuVz+wu4fjuGCjj0ikbfeO0ec4jbtK1ORUZOA9MK0="
bucket: "lbry-reflector-binary"
skip_cleanup: true
# local_dir: ./bin
file: prism-bin
on:
repo: lbryio/reflector.go
condition: "$DEPLOY = true"

13
Gopkg.lock generated
View file

@ -193,6 +193,18 @@
pruneopts = ""
revision = "984a73625de3138f44deb38d00878fab39eb6447"
[[projects]]
branch = "master"
digest = "1:0d37c42156531a07a84812a47c27610947b849710ffab6f62be6e98c5112c140"
name = "github.com/inconshreveable/go-update"
packages = [
".",
"internal/binarydist",
"internal/osext",
]
pruneopts = ""
revision = "8152e7eb6ccf8679a64582a66b78519688d156ad"
[[projects]]
digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be"
name = "github.com/inconshreveable/mousetrap"
@ -415,6 +427,7 @@
"github.com/gorilla/rpc/v2",
"github.com/gorilla/rpc/v2/json",
"github.com/hashicorp/serf/serf",
"github.com/inconshreveable/go-update",
"github.com/johntdyer/slackrus",
"github.com/lbryio/errors.go",
"github.com/lbryio/lbry.go/crypto",

View file

@ -37,3 +37,7 @@
[[constraint]]
branch = "master"
name = "github.com/johntdyer/slackrus"
[[constraint]]
branch = "master"
name = "github.com/inconshreveable/go-update"

View file

@ -8,6 +8,7 @@ import (
"github.com/lbryio/lbry.go/errors"
"github.com/lbryio/lbry.go/util"
"github.com/lbryio/reflector.go/dht"
"github.com/lbryio/reflector.go/updater"
"github.com/johntdyer/slackrus"
"github.com/sirupsen/logrus"
@ -22,6 +23,8 @@ type Config struct {
BucketName string `json:"bucket_name"`
DBConn string `json:"db_conn"`
SlackHookURL string `json:"slack_hook_url"`
UpdateBinURL string `json:"update_bin_url"`
UpdateCmd string `json:"update_cmd"`
}
var verbose []string
@ -102,6 +105,14 @@ func preRun(cmd *cobra.Command, args []string) {
logrus.AddHook(hook)
debugLogger.AddHook(hook)
}
if globalConfig.UpdateBinURL != "" {
if globalConfig.UpdateCmd == "" {
logrus.Warnln("update_cmd is empty in conf file")
}
logrus.Println("starting update checker")
go updater.Run(globalConfig.UpdateBinURL, globalConfig.UpdateCmd)
}
}
func checkErr(err error) {

73
updater/updater.go Normal file
View file

@ -0,0 +1,73 @@
package updater
import (
"net/http"
"os/exec"
"strings"
"time"
"github.com/lbryio/reflector.go/meta"
"github.com/inconshreveable/go-update"
"github.com/sirupsen/logrus"
)
// TODO: this needs to be shut down cleanly
func Run(url, updateCmd string) {
for {
<-time.After(1 * time.Minute)
r, err := http.Head(url)
if err != nil {
logrus.Errorln(err)
continue
} else if r.StatusCode >= 300 {
logrus.Errorf("HEAD request returned status code %d", r.StatusCode)
continue
}
lm, err := time.Parse(time.RFC1123, r.Header.Get("Last-Modified"))
if err != nil {
logrus.Errorln(err)
continue
}
if !lm.After(meta.BuildTime.Add(5 * time.Minute)) { // buffer for compile and upload time
continue
}
logrus.Println("updating binary...")
err = updateBin(url)
if err != nil {
logrus.Errorln(err)
// todo: may need to recover here, if stuck without binary
continue
}
parts := strings.Fields(updateCmd)
err = exec.Command(parts[0], parts[1:]...).Start()
if err != nil {
logrus.Errorln(err)
continue
}
return // stop checking for updates in case restart takes a while
}
}
func updateBin(url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer func() {
err := resp.Body.Close()
if err != nil {
logrus.Errorln(err)
}
}()
return update.Apply(resp.Body, update.Options{})
}