Przeglądaj źródła

feature: Switch from MachineID to InstanceID in update checks

jamesread 4 lat temu
rodzic
commit
cabf045202
3 zmienionych plików z 48 dodań i 13 usunięć
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 45 13
      internal/updatecheck/updateCheck.go

+ 1 - 0
go.mod

@@ -9,6 +9,7 @@ require (
 	github.com/fzipp/gocyclo v0.3.1
 	github.com/go-co-op/gocron v1.6.2
 	github.com/go-critic/go-critic v0.6.1
+	github.com/google/uuid v1.3.0 // indirect
 	github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0
 	github.com/sirupsen/logrus v1.8.1
 	github.com/spf13/viper v1.8.1

+ 2 - 0
go.sum

@@ -185,6 +185,8 @@ github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLe
 github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
 github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+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/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
 github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=

+ 45 - 13
internal/updatecheck/updateCheck.go

@@ -3,16 +3,16 @@ package updatecheck
 import (
 	"bytes"
 	"encoding/json"
-	"github.com/denisbrodbeck/machineid"
+	"errors"
 	"github.com/go-co-op/gocron"
+	"github.com/google/uuid"
 	config "github.com/jamesread/OliveTin/internal/config"
 	log "github.com/sirupsen/logrus"
 	"io/ioutil"
 	"net/http"
+	"os"
 	"runtime"
 	"time"
-	"errors"
-	"os"
 )
 
 type updateRequest struct {
@@ -20,7 +20,7 @@ type updateRequest struct {
 	CurrentCommit  string
 	OS             string
 	Arch           string
-	MachineID      string
+	InstallationID string
 	InContainer    bool
 }
 
@@ -30,23 +30,55 @@ var AvailableVersion = "none"
 // CurrentVersion is set by the main cmd (which is in tern set as a compile constant)
 var CurrentVersion = "?"
 
-func machineID() string {
-	v, err := machineid.ProtectedID("OliveTin")
+func getInstanceIDFilename() string {
+	directory := "./"
+
+	if _, err := os.Stat("/etc/OliveTin"); !os.IsNotExist(err) {
+		directory = "/etc/OliveTin/"
+	}
+
+	if _, err := os.Stat("/config"); !os.IsNotExist(err) {
+		directory = "/config/"
+	}
+
+	return directory + "installation-id.txt"
+
+}
+
+func installationID() string {
+	filename := getInstanceIDFilename()
+
+	content := "unset"
+	contentBytes, err := ioutil.ReadFile(filename)
 
 	if err != nil {
-		log.Warnf("Error getting machine ID: %v", err)
-		return "?"
+		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)
 	}
 
-	return v
+	log.WithFields(log.Fields{
+		"content": content,
+	}).Infof("Installation ID")
+
+	return content
 }
 
 func isInContainer() bool {
 	if _, err := os.Stat("/.dockerenv"); errors.Is(err, os.ErrNotExist) {
-		return false;
+		return false
 	}
 
-	return true;
+	return true
 }
 
 // StartUpdateChecker will start a job that runs periodically, checking
@@ -64,8 +96,8 @@ func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config
 		CurrentCommit:  currentCommit,
 		OS:             runtime.GOOS,
 		Arch:           runtime.GOARCH,
-		MachineID:      machineID(),
-		InContainer:	isInContainer(),
+		InstallationID: installationID(),
+		InContainer:    isInContainer(),
 	}
 
 	s := gocron.NewScheduler(time.UTC)