Jelajahi Sumber

Merge branch 'main' of ssh://github.com/OliveTin/OliveTin

jamesread 3 tahun lalu
induk
melakukan
8ed3db0170

+ 12 - 0
OliveTin.proto

@@ -93,6 +93,12 @@ message WhoAmIResponse {
 	string authenticatedUser = 1;
 }
 
+message SosReportRequest {}
+
+message SosReportResponse {
+	string alert = 1;
+}
+
 service OliveTinApi {
 	rpc GetDashboardComponents(GetDashboardComponentsRequest) returns (GetDashboardComponentsResponse) {
 		option (google.api.http) = {
@@ -125,4 +131,10 @@ service OliveTinApi {
 			get: "/api/WhoAmI"
 		};
 	}
+
+	rpc SosReport(SosReportRequest) returns (SosReportResponse) {
+		option (google.api.http) = {
+			get: "/api/sosreport"
+		};
+	}
 }

+ 6 - 0
cmd/OliveTin/main.go

@@ -7,6 +7,7 @@ import (
 
 	"github.com/OliveTin/OliveTin/internal/executor"
 	grpcapi "github.com/OliveTin/OliveTin/internal/grpcapi"
+	"github.com/OliveTin/OliveTin/internal/installationinfo"
 	"github.com/OliveTin/OliveTin/internal/oncron"
 	"github.com/OliveTin/OliveTin/internal/onstartup"
 	updatecheck "github.com/OliveTin/OliveTin/internal/updatecheck"
@@ -76,6 +77,11 @@ func init() {
 
 	warnIfPuidGuid()
 
+	installationinfo.Config = cfg
+	installationinfo.Build.Version = version
+	installationinfo.Build.Commit = commit
+	installationinfo.Build.Date = date
+
 	log.Info("Init complete")
 }
 

+ 11 - 0
internal/grpcapi/grpcApi.go

@@ -11,6 +11,7 @@ import (
 	acl "github.com/OliveTin/OliveTin/internal/acl"
 	config "github.com/OliveTin/OliveTin/internal/config"
 	executor "github.com/OliveTin/OliveTin/internal/executor"
+	installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo"
 )
 
 var (
@@ -108,6 +109,16 @@ func (api *oliveTinAPI) WhoAmI(ctx ctx.Context, req *pb.WhoAmIRequest) (*pb.WhoA
 	return res, nil
 }
 
+func (api *oliveTinAPI) SosReport(ctx ctx.Context, req *pb.SosReportRequest) (*pb.SosReportResponse, error) {
+	res := &pb.SosReportResponse{
+		Alert: "Your SOS Report has been logged to OliveTin logs.",
+	}
+
+	log.Infof("\n" + installationinfo.GetSosReport())
+
+	return res, nil
+}
+
 // Start will start the GRPC API.
 func Start(globalConfig *config.Config, ex *executor.Executor) {
 	cfg = globalConfig

+ 9 - 0
internal/installationinfo/buildinfo.go

@@ -0,0 +1,9 @@
+package installationinfo
+
+type buildInfo struct {
+	Commit  string
+	Version string
+	Date    string
+}
+
+var Build = &buildInfo{}

+ 55 - 0
internal/installationinfo/runtimeinfo.go

@@ -0,0 +1,55 @@
+package installationinfo
+
+import (
+	"bufio"
+	"errors"
+	"os"
+	"runtime"
+	"strings"
+)
+
+type runtimeInfo struct {
+	OS                   string
+	OSReleasePrettyName  string
+	Arch                 string
+	InContainer          bool
+	LastBrowserUserAgent string
+}
+
+func isInContainer() bool {
+	if _, err := os.Stat("/.dockerenv"); errors.Is(err, os.ErrNotExist) {
+		return false
+	}
+
+	return true
+}
+
+func getOsReleasePrettyName() string {
+	handle, err := os.Open("/etc/os-release")
+
+	if err != nil {
+		return ""
+	}
+
+	scanner := bufio.NewScanner(handle)
+	scanner.Split(bufio.ScanLines)
+
+	for scanner.Scan() {
+		line := scanner.Text()
+
+		if strings.Contains(line, "PRETTY_NAME") {
+			return line
+		}
+	}
+
+	handle.Close()
+
+	return "notfound"
+}
+
+var Runtime = &runtimeInfo{
+	OS:                  runtime.GOOS,
+	Arch:                runtime.GOARCH,
+	InContainer:         isInContainer(),
+	OSReleasePrettyName: getOsReleasePrettyName(),
+}

+ 39 - 0
internal/installationinfo/sosreport.go

@@ -0,0 +1,39 @@
+package installationinfo
+
+import (
+	"fmt"
+	config "github.com/OliveTin/OliveTin/internal/config"
+	"gopkg.in/yaml.v3"
+)
+
+var Config *config.Config
+
+type sosReportConfig struct {
+	CountOfActions int
+	LogLevel       string
+}
+
+func configToSosreport(cfg *config.Config) *sosReportConfig {
+	return &sosReportConfig{
+		CountOfActions: len(cfg.Actions),
+		LogLevel:       cfg.LogLevel,
+	}
+}
+
+func GetSosReport() string {
+	ret := ""
+
+	ret += "### SOSREPORT START (copy all text to SOSREPORT END)\n"
+
+	out, _ := yaml.Marshal(Build)
+	ret += fmt.Sprintf("# Build: \n%+v\n", string(out))
+
+	out, _ = yaml.Marshal(Runtime)
+	ret += fmt.Sprintf("# Runtime:\n%+v\n", string(out))
+
+	out, _ = yaml.Marshal(configToSosreport(Config))
+	ret += fmt.Sprintf("# Config:\n%+v\n", string(out))
+	ret += "### SOSREPORT END  (copy all text from SOSREPORT START)\n"
+
+	return ret
+}

+ 4 - 13
internal/updatecheck/updateCheck.go

@@ -3,15 +3,14 @@ package updatecheck
 import (
 	"bytes"
 	"encoding/json"
-	"errors"
 	config "github.com/OliveTin/OliveTin/internal/config"
+	installationinfo "github.com/OliveTin/OliveTin/internal/installationinfo"
 	"github.com/google/uuid"
 	"github.com/robfig/cron/v3"
 	log "github.com/sirupsen/logrus"
 	"io/ioutil"
 	"net/http"
 	"os"
-	"runtime"
 )
 
 type updateRequest struct {
@@ -63,14 +62,6 @@ func installationID(filename string) string {
 	return content
 }
 
-func isInContainer() bool {
-	if _, err := os.Stat("/.dockerenv"); errors.Is(err, os.ErrNotExist) {
-		return false
-	}
-
-	return true
-}
-
 // StartUpdateChecker will start a job that runs periodically, checking
 // for updates.
 func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config, configDir string) {
@@ -84,10 +75,10 @@ func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config
 	payload := updateRequest{
 		CurrentVersion: currentVersion,
 		CurrentCommit:  currentCommit,
-		OS:             runtime.GOOS,
-		Arch:           runtime.GOARCH,
+		OS:             installationinfo.Runtime.OS,
+		Arch:           installationinfo.Runtime.Arch,
 		InstallationID: installationID(configDir + "/installation-id.txt"),
-		InContainer:    isInContainer(),
+		InContainer:    installationinfo.Runtime.InContainer,
 	}
 
 	s := cron.New(cron.WithSeconds())