updater: include build date, check against updates

build time is included in the binary, so the buildrone release date is
compared to it when deciding if something is an update or not.
This commit is contained in:
Harvey Tindall 2023-06-23 14:16:36 +01:00
parent 7caa5c5d57
commit f083d6b53f
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 34 additions and 24 deletions

View File

@ -49,7 +49,7 @@ builds:
env:
- CGO_ENABLED=0
ldflags:
- -s -w -X main.version={{.Env.JFA_GO_VERSION}} -X main.commit={{.ShortCommit}} -X main.updater=binary -X main.cssVersion={{.Env.JFA_GO_CSS_VERSION}}
- -s -w -X main.version={{.Env.JFA_GO_VERSION}} -X main.commit={{.ShortCommit}} -X main.updater=binary -X main.cssVersion={{.Env.JFA_GO_CSS_VERSION}} -X main.buildTimeUnix={{.Env.JFA_GO_BUILD_TIME}}
goos:
- linux
- darwin

View File

@ -11,9 +11,10 @@ CSSVERSION ?= v3
VERSION ?= $(shell git describe --exact-match HEAD 2> /dev/null || echo vgit)
VERSION := $(shell echo $(VERSION) | sed 's/v//g')
COMMIT ?= $(shell git rev-parse --short HEAD || echo unknown)
BUILDTIME ?= $(shell date +%s)
UPDATER ?= off
LDFLAGS := -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.cssVersion=$(CSSVERSION)
LDFLAGS := -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.cssVersion=$(CSSVERSION) -X main.buildTimeUnix=$(BUILDTIME)
ifeq ($(UPDATER), on)
LDFLAGS := $(LDFLAGS) -X main.updater=binary
else ifneq ($(UPDATER), off)

View File

@ -2,4 +2,4 @@
# sets version environment variable for goreleaser to use
# scripts/version.sh goreleaser ...
JFA_GO_VERSION=$(git describe --exact-match HEAD 2> /dev/null || echo 'vgit')
JFA_GO_CSS_VERSION="v3" JFA_GO_NFPM_EPOCH=$(git rev-list --all --count) JFA_GO_VERSION="$(echo $JFA_GO_VERSION | sed 's/v//g')" $@
JFA_GO_CSS_VERSION="v3" JFA_GO_NFPM_EPOCH=$(git rev-list --all --count) JFA_GO_BUILD_TIME=$(date +%s) JFA_GO_VERSION="$(echo $JFA_GO_VERSION | sed 's/v//g')" $@

View File

@ -25,6 +25,12 @@ const (
repo = "jfa-go"
)
var buildTimeUnix string
var buildTime time.Time = func() time.Time {
i, _ := strconv.ParseInt(buildTimeUnix, 10, 64)
return time.Unix(i, 0)
}()
type GHRelease struct {
HTMLURL string `json:"html_url"`
ID int `json:"id"`
@ -106,14 +112,14 @@ var goarch = map[string]string{
"arm": "armv6",
}
// func newDockerBuild() Update {
// var tag string
// if version == "git" {
// tag = "docker-unstable"
// } else {
// tag = "docker-latest"
// }
// }
// func newDockerBuild() Update {
// var tag string
// if version == "git" {
// tag = "docker-unstable"
// } else {
// tag = "docker-latest"
// }
// }
type Updater struct {
version, commit, tag, url, namespace, name string
stable bool
@ -124,6 +130,7 @@ type Updater struct {
}
func newUpdater(buildroneURL, namespace, repo, version, commit, buildType string) *Updater {
// fmt.Printf(`Updater intializing with "%s", "%s", "%s", "%s", "%s", "%s"\n`, buildroneURL, namespace, repo, version, commit, buildType)
bType := off
tag := ""
switch buildType {
@ -184,6 +191,7 @@ func (ud *Updater) GetTag() (Tag, int, error) {
return Tag{}, -1, nil
}
url := fmt.Sprintf("%s/repo/%s/%s/tag/latest/%s", ud.url, ud.namespace, ud.name, ud.tag)
// fmt.Printf("Pinging URL \"%s\" for updates\n", url)
req, _ := http.NewRequest("GET", url, nil)
resp, err := ud.httpClient.Do(req)
defer ud.timeoutHandler()
@ -205,7 +213,8 @@ func (ud *Updater) GetTag() (Tag, int, error) {
}
func (t *Tag) IsNew() bool {
return t.Version[:7] != commit && t.Ready
// fmt.Printf("Build Time: %+v, Release Date: %+v", buildTime, t.ReleaseDate)
return t.Version[:7] != commit && t.Ready && t.ReleaseDate.After(buildTime)
}
func (ud *Updater) getRelease() (release GHRelease, status int, err error) {
@ -526,18 +535,18 @@ func (ud *Updater) pullInternal(url string) (applyUpdate ApplyUpdate, status int
// func newInternalBuild() Update {
// tag := "internal"
// func update(path string) err {
// if
// fp, err := os.Executable()
// if err != nil {
// return err
// }
// fullPath, err := filepath.EvalSymlinks(fp)
// if err != nil {
// return err
// }
// newBinary,
// }
// func update(path string) err {
// if
// fp, err := os.Executable()
// if err != nil {
// return err
// }
// fullPath, err := filepath.EvalSymlinks(fp)
// if err != nil {
// return err
// }
// newBinary,
// }
func (app *appContext) checkForUpdates() {
for {
go func() {