feature: No more tracking in update checking! :-) (#93) (#332)

This commit is contained in:
James Read 2024-06-04 13:21:11 +01:00 committed by GitHub
parent c82beb61a9
commit 362a97c59e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 45 additions and 86 deletions

View File

@ -149,7 +149,7 @@ func main() {
entityfiles.AddListener(executor.RebuildActionMap) entityfiles.AddListener(executor.RebuildActionMap)
go entityfiles.SetupEntityFileWatchers(cfg) go entityfiles.SetupEntityFileWatchers(cfg)
go updatecheck.StartUpdateChecker(version, commit, cfg, cfg.GetDir()) go updatecheck.StartUpdateChecker(cfg)
go grpcapi.Start(cfg, executor) go grpcapi.Start(cfg, executor)

View File

@ -10,8 +10,8 @@ import (
"path/filepath" "path/filepath"
config "github.com/OliveTin/OliveTin/internal/config" config "github.com/OliveTin/OliveTin/internal/config"
installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo"
sv "github.com/OliveTin/OliveTin/internal/stringvariables" sv "github.com/OliveTin/OliveTin/internal/stringvariables"
updatecheck "github.com/OliveTin/OliveTin/internal/updatecheck"
) )
var ( var (
@ -106,8 +106,8 @@ func generateWebUISettings(w http.ResponseWriter, r *http.Request) {
ShowFooter: cfg.ShowFooter, ShowFooter: cfg.ShowFooter,
ShowNavigation: cfg.ShowNavigation, ShowNavigation: cfg.ShowNavigation,
ShowNewVersions: cfg.ShowNewVersions, ShowNewVersions: cfg.ShowNewVersions,
AvailableVersion: updatecheck.AvailableVersion, AvailableVersion: installationinfo.Runtime.AvailableVersion,
CurrentVersion: updatecheck.CurrentVersion, CurrentVersion: installationinfo.Build.Version,
PageTitle: cfg.PageTitle, PageTitle: cfg.PageTitle,
SectionNavigationStyle: cfg.SectionNavigationStyle, SectionNavigationStyle: cfg.SectionNavigationStyle,
DefaultIconForBack: cfg.DefaultIconForBack, DefaultIconForBack: cfg.DefaultIconForBack,

View File

@ -19,6 +19,7 @@ type runtimeInfo struct {
User string User string
Uid string Uid string
FoundSshKey string FoundSshKey string
AvailableVersion string
} }
var Runtime = &runtimeInfo{ var Runtime = &runtimeInfo{

View File

@ -1,11 +1,9 @@
package updatecheck package updatecheck
import ( import (
"bytes"
"encoding/json" "encoding/json"
config "github.com/OliveTin/OliveTin/internal/config" config "github.com/OliveTin/OliveTin/internal/config"
installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo" "github.com/OliveTin/OliveTin/internal/installationinfo"
"github.com/google/uuid"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io" "io"
@ -13,99 +11,63 @@ import (
"os" "os"
) )
type updateRequest struct { type versionMapType struct {
CurrentVersion string ApiVersion int
CurrentCommit string Latest string
OS string History map[string]string
Arch string
InstallationID string
InContainer bool
}
// AvailableVersion is updated when checking with the update service.
var AvailableVersion = "none"
// CurrentVersion is set by the main cmd (which is in tern set as a compile constant)
var CurrentVersion = "?"
func installationID(filename string) string {
var content string
contentBytes, err := os.ReadFile(filename)
if err != nil {
fileHandle, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Warnf("Could not read + create installation ID file: %v", err)
return "cant-create"
}
content = uuid.NewString()
fileHandle.WriteString(content)
fileHandle.Close()
} else {
content = string(contentBytes)
_, err := uuid.Parse(content)
if err != nil {
log.Errorf("Invalid installation ID, %v", err)
content = "invalid-installation-id"
}
}
log.WithFields(log.Fields{
"content": content,
"from": filename,
}).Infof("Installation ID")
return content
} }
// StartUpdateChecker will start a job that runs periodically, checking // StartUpdateChecker will start a job that runs periodically, checking
// for updates. // for updates.
func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config, configDir string) { func StartUpdateChecker(cfg *config.Config) {
CurrentVersion = currentVersion
if !cfg.CheckForUpdates { if !cfg.CheckForUpdates {
installationinfo.Runtime.AvailableVersion = "none"
log.Warn("Update checking is disabled") log.Warn("Update checking is disabled")
return return
} }
payload := updateRequest{ s := cron.New()
CurrentVersion: currentVersion,
CurrentCommit: currentCommit,
OS: installationinfo.Runtime.OS,
Arch: installationinfo.Runtime.Arch,
InstallationID: installationID(configDir + "/installation-id.txt"),
InContainer: installationinfo.Runtime.InContainer,
}
s := cron.New(cron.WithSeconds())
// Several values have been tried here. // Several values have been tried here.
// 1st: Every 24h - very spammy. // 1st: Every 24h - very spammy.
// 2nd: Every 7d - (168 hours - much more reasonable, but it checks in at the same time/day each week. // 2nd: Every 7d - (168 hours - much more reasonable, but it checks in at the same time/day each week.
// Current: Every 100h is not so spammy, and has the advantage that the checkin time "shifts" hours. // Current: Every 100h is not so spammy, and has the advantage that the checkin time "shifts" hours.
s.AddFunc("@every 100h", func() { s.AddFunc("@every 100h", func() {
actualCheckForUpdate(payload) actualCheckForUpdate()
}) })
go actualCheckForUpdate(payload) // On startup go actualCheckForUpdate() // On startup
go s.Start() go s.Start()
} }
func doRequest(jsonUpdateRequest []byte) string { func parseVersion(input []byte) string {
req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewBuffer(jsonUpdateRequest)) versionMap := &versionMapType{}
err := json.Unmarshal(input, &versionMap)
if err != nil {
log.Warnf("Update check unmarshal failure: %v", err)
return "error-during-check"
} else {
log.Infof("Update check remote version: %+v", versionMap)
if installationinfo.Build.Version != versionMap.Latest {
return versionMap.Latest
} else {
return "none"
}
}
}
func doRequest() string {
req, err := http.NewRequest("GET", "http://update-check.olivetin.app/versions.json", nil)
if err != nil { if err != nil {
log.Errorf("Update check failed %v", err) log.Errorf("Update check failed %v", err)
return "" return ""
} }
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
if err != nil { if err != nil {
@ -113,26 +75,22 @@ func doRequest(jsonUpdateRequest []byte) string {
return "" return ""
} }
newVersion, _ := io.ReadAll(resp.Body) versionMap, _ := io.ReadAll(resp.Body)
defer resp.Body.Close() defer resp.Body.Close()
return string(newVersion) return parseVersion(versionMap)
} }
func actualCheckForUpdate(payload updateRequest) { func actualCheckForUpdate() {
jsonUpdateRequest, err := json.Marshal(payload) if installationinfo.Build.Version == "dev" && os.Getenv("OLIVETIN_FORCE_UPDATE_CHECK") == "" {
installationinfo.Runtime.AvailableVersion = "you-are-using-a-dev-build"
log.Debugf("Update request payload: %+v", payload) } else {
installationinfo.Runtime.AvailableVersion = doRequest()
if err != nil {
log.Errorf("Update check failed %v", err)
return
} }
AvailableVersion = doRequest(jsonUpdateRequest)
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"NewVersion": AvailableVersion, "CurrentVersion": installationinfo.Build.Version,
"NewVersion": installationinfo.Runtime.AvailableVersion,
}).Infof("Update check complete") }).Infof("Update check complete")
} }