Update check cleanup, gofmt and gocyclo = 3
This commit is contained in:
parent
4bd2be6317
commit
a4def7571c
2
Makefile
2
Makefile
|
|
@ -15,7 +15,7 @@ daemon-codestyle:
|
||||||
go fmt ./...
|
go fmt ./...
|
||||||
go vet ./...
|
go vet ./...
|
||||||
golint ./...
|
golint ./...
|
||||||
gocyclo -over 4 cmd internal
|
gocyclo -over 3 cmd internal
|
||||||
|
|
||||||
daemon-unittests:
|
daemon-unittests:
|
||||||
mkdir -p reports
|
mkdir -p reports
|
||||||
|
|
|
||||||
|
|
@ -60,7 +60,7 @@ func main() {
|
||||||
|
|
||||||
log.Debugf("Config: %+v", cfg)
|
log.Debugf("Config: %+v", cfg)
|
||||||
|
|
||||||
go updatecheck.CheckForUpdate(version, commit, cfg)
|
go updatecheck.StartUpdateChecker(version, commit, cfg)
|
||||||
|
|
||||||
go grpcapi.Start(cfg)
|
go grpcapi.Start(cfg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import ()
|
||||||
|
|
||||||
// ActionButton represents a button that is shown in the webui.
|
// ActionButton represents a button that is shown in the webui.
|
||||||
type ActionButton struct {
|
type ActionButton struct {
|
||||||
Id string
|
ID string
|
||||||
Title string
|
Title string
|
||||||
Icon string
|
Icon string
|
||||||
Shell string
|
Shell string
|
||||||
|
|
@ -32,6 +32,7 @@ type Config struct {
|
||||||
LogLevel string
|
LogLevel string
|
||||||
ActionButtons []ActionButton `mapstructure:"actions"`
|
ActionButtons []ActionButton `mapstructure:"actions"`
|
||||||
Entities []Entity `mapstructure:"omitempty"`
|
Entities []Entity `mapstructure:"omitempty"`
|
||||||
|
CheckForUpdates bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultConfig gets a new Config structure with sensible default values.
|
// DefaultConfig gets a new Config structure with sensible default values.
|
||||||
|
|
@ -43,6 +44,7 @@ func DefaultConfig() *Config {
|
||||||
config.ListenAddressGrpcActions = "localhost:1339"
|
config.ListenAddressGrpcActions = "localhost:1339"
|
||||||
config.ListenAddressWebUI = "localhost:1340"
|
config.ListenAddressWebUI = "localhost:1340"
|
||||||
config.LogLevel = "INFO"
|
config.LogLevel = "INFO"
|
||||||
|
config.CheckForUpdates = true
|
||||||
|
|
||||||
return &config
|
return &config
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,6 @@ import (
|
||||||
|
|
||||||
// ExecAction executes an action.
|
// ExecAction executes an action.
|
||||||
func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
|
func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
|
||||||
res := &pb.StartActionResponse{}
|
|
||||||
res.TimedOut = false
|
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"actionName": action,
|
"actionName": action,
|
||||||
}).Infof("StartAction")
|
}).Infof("StartAction")
|
||||||
|
|
@ -24,8 +21,18 @@ func ExecAction(cfg *config.Config, action string) *pb.StartActionResponse {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error finding action %s, %s", err, action)
|
log.Errorf("Error finding action %s, %s", err, action)
|
||||||
return res
|
|
||||||
|
return &pb.StartActionResponse{
|
||||||
|
TimedOut: false,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return execAction(cfg, actualAction)
|
||||||
|
}
|
||||||
|
|
||||||
|
func execAction(cfg *config.Config, actualAction *config.ActionButton) *pb.StartActionResponse {
|
||||||
|
res := &pb.StartActionResponse{}
|
||||||
|
res.TimedOut = false
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"title": actualAction.Title,
|
"title": actualAction.Title,
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ package grpcapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
ctx "context"
|
ctx "context"
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
pb "github.com/jamesread/OliveTin/gen/grpc"
|
pb "github.com/jamesread/OliveTin/gen/grpc"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"net"
|
"net"
|
||||||
"crypto/md5"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
config "github.com/jamesread/OliveTin/internal/config"
|
config "github.com/jamesread/OliveTin/internal/config"
|
||||||
executor "github.com/jamesread/OliveTin/internal/executor"
|
executor "github.com/jamesread/OliveTin/internal/executor"
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,27 @@
|
||||||
package updatecheck
|
package updatecheck
|
||||||
|
|
||||||
import (
|
import (
|
||||||
config "github.com/jamesread/OliveTin/internal/config"
|
"bytes"
|
||||||
log "github.com/sirupsen/logrus"
|
"encoding/json"
|
||||||
"github.com/denisbrodbeck/machineid"
|
"github.com/denisbrodbeck/machineid"
|
||||||
"github.com/go-co-op/gocron"
|
"github.com/go-co-op/gocron"
|
||||||
"runtime"
|
config "github.com/jamesread/OliveTin/internal/config"
|
||||||
"net/http"
|
log "github.com/sirupsen/logrus"
|
||||||
"encoding/json"
|
|
||||||
"bytes"
|
|
||||||
"time"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateRequest struct {
|
type updateRequest struct {
|
||||||
CurrentVersion string
|
CurrentVersion string
|
||||||
CurrentCommit string
|
CurrentCommit string
|
||||||
OS string
|
OS string
|
||||||
Arch string
|
Arch string
|
||||||
MachineId string
|
MachineID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func machineId() string {
|
func machineID() string {
|
||||||
v, err := machineid.ProtectedID("OliveTin")
|
v, err := machineid.ProtectedID("OliveTin")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -29,16 +29,23 @@ func machineId() string {
|
||||||
return "?"
|
return "?"
|
||||||
}
|
}
|
||||||
|
|
||||||
return v;
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckForUpdate(currentVersion string, currentCommit string, cfg *config.Config) {
|
// StartUpdateChecker will start a job that runs periodically, checking
|
||||||
payload := UpdateRequest {
|
// for updates.
|
||||||
|
func StartUpdateChecker(currentVersion string, currentCommit string, cfg *config.Config) {
|
||||||
|
if !cfg.CheckForUpdates {
|
||||||
|
log.Warn("Update checking is disabled")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := updateRequest{
|
||||||
CurrentVersion: currentVersion,
|
CurrentVersion: currentVersion,
|
||||||
CurrentCommit: currentCommit,
|
CurrentCommit: currentCommit,
|
||||||
OS: runtime.GOOS,
|
OS: runtime.GOOS,
|
||||||
Arch: runtime.GOARCH,
|
Arch: runtime.GOARCH,
|
||||||
MachineId: machineId(),
|
MachineID: machineID(),
|
||||||
}
|
}
|
||||||
|
|
||||||
s := gocron.NewScheduler(time.UTC)
|
s := gocron.NewScheduler(time.UTC)
|
||||||
|
|
@ -50,7 +57,7 @@ func CheckForUpdate(currentVersion string, currentCommit string, cfg *config.Con
|
||||||
s.StartAsync()
|
s.StartAsync()
|
||||||
}
|
}
|
||||||
|
|
||||||
func actualCheckForUpdate(payload UpdateRequest) {
|
func actualCheckForUpdate(payload updateRequest) {
|
||||||
jsonUpdateRequest, err := json.Marshal(payload)
|
jsonUpdateRequest, err := json.Marshal(payload)
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewReader(jsonUpdateRequest))
|
req, err := http.NewRequest("POST", "http://update-check.olivetin.app", bytes.NewReader(jsonUpdateRequest))
|
||||||
|
|
@ -66,14 +73,14 @@ func actualCheckForUpdate(payload UpdateRequest) {
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Update check failed %v", err)
|
log.Errorf("Update check failed %v", err)
|
||||||
} else {
|
return
|
||||||
newVersion, _ := ioutil.ReadAll(resp.Body)
|
|
||||||
|
|
||||||
log.WithFields(log.Fields {
|
|
||||||
"NewVersion": string(newVersion),
|
|
||||||
}).Infof("Update check complete");
|
|
||||||
|
|
||||||
defer resp.Body.Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newVersion, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"NewVersion": string(newVersion),
|
||||||
|
}).Infof("Update check complete")
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue