Update notification in logs, see docs for more info

This commit is contained in:
jamesread 2021-06-22 00:27:01 +01:00
parent 5d01183198
commit 4bd2be6317
3 changed files with 85 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
grpcapi "github.com/jamesread/OliveTin/internal/grpcapi" grpcapi "github.com/jamesread/OliveTin/internal/grpcapi"
updatecheck "github.com/jamesread/OliveTin/internal/updatecheck"
"github.com/jamesread/OliveTin/internal/httpservers" "github.com/jamesread/OliveTin/internal/httpservers"
@ -57,7 +58,9 @@ func init() {
func main() { func main() {
log.Info("OliveTin started") log.Info("OliveTin started")
log.Debugf("%+v", cfg) log.Debugf("Config: %+v", cfg)
go updatecheck.CheckForUpdate(version, commit, cfg)
go grpcapi.Start(cfg) go grpcapi.Start(cfg)

2
go.mod
View File

@ -3,6 +3,8 @@ module github.com/jamesread/OliveTin
go 1.15 go 1.15
require ( require (
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/go-co-op/gocron v1.6.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.4.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.4.0
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/spf13/viper v1.7.1 github.com/spf13/viper v1.7.1

View File

@ -0,0 +1,79 @@
package updatecheck
import (
config "github.com/jamesread/OliveTin/internal/config"
log "github.com/sirupsen/logrus"
"github.com/denisbrodbeck/machineid"
"github.com/go-co-op/gocron"
"runtime"
"net/http"
"encoding/json"
"bytes"
"time"
"io/ioutil"
)
type UpdateRequest struct {
CurrentVersion string
CurrentCommit string
OS string
Arch string
MachineId string
}
func machineId() string {
v, err := machineid.ProtectedID("OliveTin")
if err != nil {
log.Warnf("Error getting machine ID: %v", err)
return "?"
}
return v;
}
func CheckForUpdate(currentVersion string, currentCommit string, cfg *config.Config) {
payload := UpdateRequest {
CurrentVersion: currentVersion,
CurrentCommit: currentCommit,
OS: runtime.GOOS,
Arch: runtime.GOARCH,
MachineId: machineId(),
}
s := gocron.NewScheduler(time.UTC)
s.Every(7).Days().Do(func() {
actualCheckForUpdate(payload)
})
s.StartAsync()
}
func actualCheckForUpdate(payload UpdateRequest) {
jsonUpdateRequest, err := json.Marshal(payload)
req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewReader(jsonUpdateRequest))
if err != nil {
log.Errorf("Update check failed %v", err)
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorf("Update check failed %v", err)
} else {
newVersion, _ := ioutil.ReadAll(resp.Body)
log.WithFields(log.Fields {
"NewVersion": string(newVersion),
}).Infof("Update check complete");
defer resp.Body.Close();
}
}