implement auto update
This commit is contained in:
parent
eedff55287
commit
629ae4ad1f
7 changed files with 121 additions and 1 deletions
1
go.mod
1
go.mod
|
|
@ -5,6 +5,7 @@ go 1.23.4
|
|||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.0
|
||||
github.com/glebarez/sqlite v1.11.0
|
||||
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/nekomeowww/gorm-logger-logrus v1.0.8
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -15,6 +15,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
|||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
|
||||
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
|
||||
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
|
||||
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
|
|
|
|||
14
main.go
14
main.go
|
|
@ -8,11 +8,25 @@ import (
|
|||
"jonasled.dev/jonasled/fms32-pro-mqtt/log"
|
||||
"jonasled.dev/jonasled/fms32-pro-mqtt/mqttclient"
|
||||
"jonasled.dev/jonasled/fms32-pro-mqtt/mqttserver"
|
||||
"jonasled.dev/jonasled/fms32-pro-mqtt/update"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.Init()
|
||||
PrintVersion()
|
||||
|
||||
if os.Getenv("UPDATE_CHECK") != "false" {
|
||||
updateAvailable, err := update.CheckForUpdates(BuildNumber, UpdateJson)
|
||||
if err != nil {
|
||||
log.Log.Error("Failed checking for updates: ", err.Error())
|
||||
} else if updateAvailable && os.Getenv("UPDATE_INSTALL") == "true" {
|
||||
err := update.DoUpdate(UpdateJson)
|
||||
if err != nil {
|
||||
log.Log.Error("Failed installing update: ", err.Error())
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
database.Init()
|
||||
|
||||
if os.Getenv("MQTT_ENABLED") == "true" {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,28 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UpdateInfo struct {
|
||||
DownloadUrl string `json:"downloadUrl"`
|
||||
BuildNumber string `json:"buildNumber"`
|
||||
RawTime string `json:"time"`
|
||||
}
|
||||
|
||||
func (u UpdateInfo) GetUpdateUrl() string {
|
||||
goos := runtime.GOOS
|
||||
goarch := runtime.GOARCH
|
||||
fileExtension := ""
|
||||
|
||||
if goos == "windows" {
|
||||
fileExtension = ".exe"
|
||||
}
|
||||
|
||||
downloadUrl := strings.ReplaceAll(u.DownloadUrl, "%GOOS%", goos)
|
||||
downloadUrl = strings.ReplaceAll(downloadUrl, "%GOARCH%", goarch)
|
||||
downloadUrl = strings.ReplaceAll(downloadUrl, "%FILEXTENSION%", fileExtension)
|
||||
|
||||
return downloadUrl
|
||||
}
|
||||
|
|
|
|||
54
update/check.go
Normal file
54
update/check.go
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
package update
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"jonasled.dev/jonasled/fms32-pro-mqtt/log"
|
||||
"jonasled.dev/jonasled/fms32-pro-mqtt/types"
|
||||
)
|
||||
|
||||
func getUpdateJson(updateUrl string) (*types.UpdateInfo, error) {
|
||||
resp, err := http.Get(updateUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch URL: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("unexpected HTTP status: %s", resp.Status)
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
var updateJson types.UpdateInfo
|
||||
if err := json.Unmarshal(body, &updateJson); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse JSON: %w", err)
|
||||
}
|
||||
return &updateJson, nil
|
||||
}
|
||||
|
||||
func CheckForUpdates(currentBuild string, updateUrl string) (bool, error) {
|
||||
if currentBuild == "git" { // disable update check for custom builds
|
||||
log.Log.Warn("Found custom build, update check is skipped")
|
||||
return false, nil
|
||||
}
|
||||
|
||||
updateJson, err := getUpdateJson(updateUrl)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
currentBuildNum, _ := strconv.Atoi(currentBuild)
|
||||
latestBuildNum, _ := strconv.Atoi(updateJson.BuildNumber)
|
||||
if currentBuildNum < latestBuildNum {
|
||||
log.Log.Infof("New Update availabl: build %s, date: %s", updateJson.BuildNumber, updateJson.RawTime)
|
||||
}
|
||||
return currentBuildNum < latestBuildNum, nil
|
||||
}
|
||||
29
update/execute.go
Normal file
29
update/execute.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package update
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/inconshreveable/go-update"
|
||||
"jonasled.dev/jonasled/fms32-pro-mqtt/log"
|
||||
)
|
||||
|
||||
func DoUpdate(updateUrl string) error {
|
||||
updateJson, err := getUpdateJson(updateUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Log.Info("Downloading update")
|
||||
resp, err := http.Get(updateJson.GetUpdateUrl())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
log.Log.Info("Installing update")
|
||||
err = update.Apply(resp.Body, update.Options{})
|
||||
if err != nil {
|
||||
// error handling
|
||||
}
|
||||
return err
|
||||
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
package update
|
||||
Loading…
Add table
Add a link
Reference in a new issue