Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions agent/updater/configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@ type Environment struct {
Branch string `yaml:"branch"`
}

func ReadEnv() (*Environment, error) {
// ReadEnv reads the environment file
// If the file does not exist, it returns a default environment with release branch
// If the file exists, it returns the environment
func ReadEnv() (string, error) {
var env Environment
path, err := utils.GetMyPath()
if err != nil {
return nil, err
return "", err
}

path = filepath.Join(path, "env.yml")

if _, err = os.Stat(path); os.IsNotExist(err) {
return &Environment{Branch: "release"}, nil
return "release", nil
} else {
err = utils.ReadYAML(path, &env)
if err != nil {
return nil, err
return "", err
}
}
return &env, nil
return env.Branch, nil
}
70 changes: 70 additions & 0 deletions agent/updater/configuration/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package configuration

import (
"path/filepath"
"runtime"
"time"

"github.com/utmstack/UTMStack/agent/updater/utils"
)

const (
SERV_NAME = "UTMStackUpdater"
SERV_LOG = "utmstack_updater.log"
SERV_LOCK = "utmstack_updater.lock"
MASTERVERSIONENDPOINT = "/management/info"
Bucket = "https://cdn.utmstack.com/agent_updates/"
CHECK_EVERY = 5 * time.Minute
)

type ServiceAttribt struct {
ServName string
ServBin string
ServLock string
}

// GetServAttr returns a map of ServiceAttribt
func GetServAttr() map[string]ServiceAttribt {
serAttr := map[string]ServiceAttribt{
"agent": {ServName: "UTMStackAgent", ServBin: "utmstack_agent_service", ServLock: "utmstack_agent.lock"},
"updater": {ServName: "UTMStackUpdater", ServBin: "utmstack_updater_self", ServLock: "utmstack_updater.lock"},
"redline": {ServName: "UTMStackRedline", ServBin: "utmstack_redline_service", ServLock: "utmstack_redline.lock"},
}

switch runtime.GOOS {
case "windows":
for code, att := range serAttr {
att.ServBin += ".exe"
serAttr[code] = att
}
}

return serAttr
}

// GetAgentBin returns the agent binary name
func GetAgentBin() string {
bin := "utmstack_agent_service"
if runtime.GOOS == "windows" {
bin = bin + ".exe"
}
return bin
}

// GetCertPath returns the path to the certificate
func GetCertPath() string {
path, _ := utils.GetMyPath()
return filepath.Join(path, "certs", "utm.crt")
}

// GetKeyPath returns the path to the key
func GetKeyPath() string {
path, _ := utils.GetMyPath()
return filepath.Join(path, "certs", "utm.key")
}

// GetCaPath returns the path to the CA
func GetCaPath() string {
path, _ := utils.GetMyPath()
return filepath.Join(path, "certs", "ca.crt")
}
73 changes: 0 additions & 73 deletions agent/updater/constants/const.go

This file was deleted.

34 changes: 16 additions & 18 deletions agent/updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,54 @@ import (
"time"

"github.com/quantfall/holmes"
"github.com/utmstack/UTMStack/agent/updater/constants"
"github.com/utmstack/UTMStack/agent/updater/configuration"
"github.com/utmstack/UTMStack/agent/updater/serv"
"github.com/utmstack/UTMStack/agent/updater/utils"
)

var h = holmes.New("debug", constants.SERV_NAME)
var utmLogger = holmes.New("debug", configuration.SERV_NAME)

func main() {
// Get current path
path, err := utils.GetMyPath()
if err != nil {
fmt.Printf("Failed to get current path: %v", err)
h.FatalError("Failed to get current path: %v", err)
fmt.Printf("failed to get current path: %v", err)
utmLogger.FatalError("failed to get current path: %v", err)
}

// Configuring log saving
var logger = utils.CreateLogger(filepath.Join(path, "logs", constants.SERV_LOG))
var logger = utils.CreateLogger(filepath.Join(path, "logs", configuration.SERV_LOG))
defer logger.Close()
log.SetOutput(logger)

if len(os.Args) > 1 {
mode := os.Args[1]
switch mode {
case "run":
serv.RunService(h)
serv.RunService(utmLogger)
case "install":
h.Info("Installing UTMStack Updater service...")
utmLogger.Info("Installing UTMStack Updater service...")

if isInstalled, err := utils.CheckIfServiceIsInstalled(constants.SERV_NAME); err != nil {
h.FatalError("error checking %s service: %v", constants.SERV_NAME, err)
if isInstalled, err := utils.CheckIfServiceIsInstalled(configuration.SERV_NAME); err != nil {
utmLogger.FatalError("error checking %s service: %v", configuration.SERV_NAME, err)
} else if isInstalled {
h.FatalError("%s is already installed", constants.SERV_NAME)
utmLogger.FatalError("%s is already installed", configuration.SERV_NAME)
}

serv.InstallService(h)
h.Info("UTMStack Updater service installed correctly.")
serv.InstallService(utmLogger)
utmLogger.Info("UTMStack Updater service installed correctly.")
time.Sleep(5 * time.Second)
os.Exit(0)

case "uninstall":
h.Info("Uninstalling UTMStack Updater service...")
serv.UninstallService(h)
h.Info("UTMStack Updater service uninstalled correctly.")
utmLogger.Info("Uninstalling UTMStack Updater service...")
serv.UninstallService(utmLogger)
utmLogger.Info("UTMStack Updater service uninstalled correctly.")
time.Sleep(5 * time.Second)
os.Exit(0)

default:
fmt.Println("unknown option")
}
} else {
serv.RunService(h)
serv.RunService(utmLogger)
}
}
4 changes: 2 additions & 2 deletions agent/updater/serv/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package serv

import (
"github.com/kardianos/service"
"github.com/utmstack/UTMStack/agent/updater/constants"
"github.com/utmstack/UTMStack/agent/updater/configuration"
)

// GetConfigServ creates and returns a pointer to a service configuration structure.
func GetConfigServ() *service.Config {
svcConfig := &service.Config{
Name: constants.SERV_NAME,
Name: configuration.SERV_NAME,
DisplayName: "UTMStack Updater",
Description: "UTMStack Updater Service",
}
Expand Down
9 changes: 5 additions & 4 deletions agent/updater/serv/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@ import (
"github.com/quantfall/holmes"
)

func InstallService(h *holmes.Logger) {
// InstallService installs the service in the system and starts it
func InstallService(utmLogger *holmes.Logger) {
svcConfig := GetConfigServ()
prg := new(program)
newService, err := service.New(prg, svcConfig)
if err != nil {
fmt.Printf("error creating new service: %v", err)
h.FatalError("error creating new service: %v", err)
utmLogger.FatalError("error creating new service: %v", err)
}
err = newService.Install()
if err != nil {
fmt.Printf("error installing new service: %v", err)
h.FatalError("error installing new service: %v", err)
utmLogger.FatalError("error installing new service: %v", err)
}

// Start the service after installing it
err = newService.Start()
if err != nil {
fmt.Printf("error starting new service: %v", err)
h.FatalError("error starting new service: %v", err)
utmLogger.FatalError("error starting new service: %v", err)
}
}
7 changes: 4 additions & 3 deletions agent/updater/serv/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"github.com/quantfall/holmes"
)

func RunService(h *holmes.Logger) {
// RunService runs the service in the system
func RunService(utmLogger *holmes.Logger) {
svcConfig := GetConfigServ()
prg := new(program)
newService, err := service.New(prg, svcConfig)
if err != nil {
h.FatalError("error creating new service: %v", err)
utmLogger.FatalError("error creating new service: %v", err)
}
err = newService.Run()
if err != nil {
h.FatalError("error running new service: %v", err)
utmLogger.FatalError("error running new service: %v", err)
}
}
8 changes: 3 additions & 5 deletions agent/updater/serv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"github.com/kardianos/service"
"github.com/quantfall/holmes"
"github.com/utmstack/UTMStack/agent/updater/configuration"
"github.com/utmstack/UTMStack/agent/updater/constants"
"github.com/utmstack/UTMStack/agent/updater/updates"
"github.com/utmstack/UTMStack/agent/updater/utils"
)

var h = holmes.New("debug", constants.SERV_NAME)
var h = holmes.New("debug", configuration.SERV_NAME)

type program struct{}

Expand All @@ -30,10 +29,10 @@ func (p *program) Stop(s service.Service) error {

func (p *program) run() {
for {
isActive, err := utils.CheckIfServiceIsActive(constants.GetServAttr()["agent"].ServName)
isActive, err := utils.CheckIfServiceIsActive(configuration.GetServAttr()["agent"].ServName)
if err != nil {
time.Sleep(time.Second * 5)
h.Error("error checking if %s service is active: %v", constants.GetServAttr()["agent"].ServName, err)
h.Error("error checking if %s service is active: %v", configuration.GetServAttr()["agent"].ServName, err)
continue
} else if !isActive {
time.Sleep(time.Second * 5)
Expand All @@ -47,7 +46,6 @@ func (p *program) run() {
h.FatalError("failed to get current path: %v", err)
}

// Read config.yml
var cnf configuration.Config
err = utils.ReadYAML(filepath.Join(path, "config.yml"), &cnf)
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions agent/updater/serv/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package serv

import (
"github.com/quantfall/holmes"
"github.com/utmstack/UTMStack/agent/updater/constants"
"github.com/utmstack/UTMStack/agent/updater/configuration"
"github.com/utmstack/UTMStack/agent/updater/utils"
)

func UninstallService(h *holmes.Logger) {
// UninstallService uninstalls the service in the system
func UninstallService(utmLogger *holmes.Logger) {
// Uninstall service
err := utils.StopService(constants.SERV_NAME)
err := utils.StopService(configuration.SERV_NAME)
if err != nil {
h.FatalError("error stopping %s: %v", constants.SERV_NAME, err)
utmLogger.FatalError("error stopping %s: %v", configuration.SERV_NAME, err)
}
err = utils.UninstallService(constants.SERV_NAME)
err = utils.UninstallService(configuration.SERV_NAME)
if err != nil {
h.FatalError("error uninstalling %s: %v", constants.SERV_NAME, err)
utmLogger.FatalError("error uninstalling %s: %v", configuration.SERV_NAME, err)
}
}
Loading