Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
asdek committed Mar 22, 2021
0 parents commit 3730596
Show file tree
Hide file tree
Showing 19 changed files with 1,456 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/

# Visual Studio Code directory
.vscode/

# VXAgent artefacts
build/*
agent.log

!.gitkeep
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Dockerfile for publishing build to repo
FROM debian:buster-slim

RUN mkdir -p /opt/vxagent/bin
RUN mkdir -p /opt/vxagent/data
RUN mkdir -p /opt/vxagent/logs

ADD preparing.sh /opt/vxagent/bin/
ADD build/vxagent /opt/vxagent/bin/

WORKDIR /opt/vxagent

RUN chmod +x /opt/vxagent/bin/preparing.sh
RUN /opt/vxagent/bin/preparing.sh

RUN apt update
RUN apt install -y ca-certificates
RUN apt clean

ENTRYPOINT ["/opt/vxagent/bin/vxagent"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 VXControl

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# vxagent
VXMonitor agent
1 change: 1 addition & 0 deletions build-linux32.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P=linux32 LF="-Wl,--whole-archive" LD="-Wl,--no-whole-archive -pthread -lluajit -lm -ldl -lstdc++" T="vxagent" ./build.sh
1 change: 1 addition & 0 deletions build-linux64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P=linux64 LF="-Wl,--whole-archive" LD="-Wl,--no-whole-archive -pthread -lluajit -lm -ldl -lstdc++" T="vxagent" ./build.sh
1 change: 1 addition & 0 deletions build-mingw32.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P=mingw32 LF="-static -Wl,--export-all-symbols -Wl,--whole-archive" LD="-Wl,--no-whole-archive -lgdi32 -lmsimg32 -lopengl32 -lwinmm -lws2_32 -lole32 -lpsapi -lMpr -lluajit -lstdc++" T="vxagent.exe" ./build.sh
1 change: 1 addition & 0 deletions build-mingw64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
P=mingw64 LF="-static -Wl,--export-all-symbols -Wl,--whole-archive" LD="-Wl,--no-whole-archive -lgdi32 -lmsimg32 -lopengl32 -lwinmm -lws2_32 -lole32 -lpsapi -lMpr -lluajit -lstdc++" T="vxagent.exe" ./build.sh
8 changes: 8 additions & 0 deletions build-osx32.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if [ `uname` = Linux ]; then
export CC=o32-clang
export CXX=o32-clang++
else
export CC=clang
export CXX=clang++
fi
CGO_ENABLED=1 GOOS=darwin GOARCH=386 P=osx32 LF="-Wl,-all_load" LD="-pthread -lluajit -lm -ldl -lstdc++" T="vxagent" ./build.sh
8 changes: 8 additions & 0 deletions build-osx64.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if [ `uname` = Linux ]; then
export CC=o64-clang
export CXX=o64-clang++
else
export CC=clang
export CXX=clang++
fi
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 P=osx64 LF="-Wl,-all_load" LD="-pthread -lluajit -lm -ldl -lstdc++" T="vxagent" ./build.sh
11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ "$PACKAGE_VER" ] || PACKAGE_VER=$(git describe --tags `git rev-list --tags --max-count=1`)
[ "$PACKAGE_REV" ] || PACKAGE_REV=$(git rev-parse --short HEAD)

export BASE_PREFIX=../vxcommon/lib
if [ ! -d "$BASE_PREFIX" ]; then
export VXCOMMON=`go list -m github.com/vxcontrol/vxcommon | sed 's/ /@/g'`
export BASE_PREFIX=`go env | grep GOPATH | cut -d'"' -f2 | xargs -I {} echo "{}/pkg/mod/$VXCOMMON/lib"`
fi
# go get
# for debugging -gcflags="all=-N -l"
go build -ldflags "-X main.PackageVer=$PACKAGE_VER -X main.PackageRev=$PACKAGE_REV -L $BASE_PREFIX/$P -extldflags '$LF $BASE_PREFIX/$P/libluab.a $LD'" -o build/$T main.go
Empty file added build/.gitkeep
Empty file.
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/vxcontrol/vxagent

go 1.13

require (
github.com/golang/protobuf v1.5.1
github.com/judwhite/go-svc v1.2.1
github.com/sirupsen/logrus v1.8.1
github.com/takama/daemon v1.0.0
github.com/vxcontrol/vxcommon v1.0.0
)
450 changes: 450 additions & 0 deletions go.sum

Large diffs are not rendered by default.

238 changes: 238 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package main

import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sync"
"time"

"github.com/judwhite/go-svc"
"github.com/sirupsen/logrus"
"github.com/takama/daemon"

"github.com/vxcontrol/vxagent/mmodule"
"github.com/vxcontrol/vxcommon/utils"
)

const (
name = "vxagent"
description = "VXAgent service to the OS control"
)

// PackageVer is semantic version of vxagent
var PackageVer string

// PackageRev is revision of vxagent build
var PackageRev string

// Agent implements daemon structure
type Agent struct {
debug bool
service bool
logDir string
agentID string
connect string
command string
module *mmodule.MainModule
wg sync.WaitGroup
svc daemon.Daemon
}

// Init for preparing agent main module struct
func (a *Agent) Init(env svc.Environment) (err error) {
utils.RemoveUnusedTempDir()
a.module = mmodule.New(a.connect, a.agentID)
if a.module == nil {
err = fmt.Errorf("failed to create new main module")
logrus.WithError(err).Error("failed to initialize")
return
}

return
}

// Start logic of agent main module
func (a *Agent) Start() (err error) {
logrus.Info("vxagent is starting...")
a.wg.Add(1)
go func() {
defer a.wg.Done()
err = a.module.Start()
}()

// Wait a little time to catch error on start
time.Sleep(time.Second)
logrus.Info("vxagent started")

return
}

// Stop logic of agent main module
func (a *Agent) Stop() (err error) {
logrus.Info("vxagent is stopping...")
if err = a.module.Stop(); err != nil {
return
}
logrus.Info("vxagent is waiting of modules release...")
a.wg.Wait()
logrus.Info("vxagent stopped")

return
}

// Manage by daemon commands or run the daemon
func (a *Agent) Manage() (string, error) {
switch a.command {
case "install":
opts := []string{
"-service",
"-agent", a.agentID,
"-connect", a.connect,
"-logdir", a.logDir,
}
if a.debug {
opts = append(opts, "-debug")
}
return a.svc.Install(opts...)
case "uninstall":
return a.svc.Remove()
case "start":
return a.svc.Start()
case "stop":
return a.svc.Stop()
case "status":
return a.svc.Status()
}

if err := svc.Run(a); err != nil {
logrus.WithError(err).Error("vxagent executing failed")
return "vxagent running failed", err
}

logrus.Info("vxagent exited normaly")
return "vxagent exited normaly", nil
}

func main() {
var agent Agent
var version bool
flag.StringVar(&agent.connect, "connect", "ws://localhost:8080", "Connection string")
flag.StringVar(&agent.agentID, "agent", "testid", "Agent ID for connection to server")
flag.StringVar(&agent.command, "command", "", `Command to service control (not required):
install - install the service to the system
uninstall - uninstall the service from the system
start - start the service
stop - stop the service
status - status of the service`)
flag.StringVar(&agent.logDir, "logdir", "", "System option to define log directory to vxagent")
flag.BoolVar(&agent.debug, "debug", false, "System option to run vxagent in debug mode")
flag.BoolVar(&agent.service, "service", false, "System option to run vxagent as a service")
flag.BoolVar(&version, "version", false, "Print current version of vxagent and exit")
flag.Parse()

if version {
fmt.Printf("vxagent version is ")
if PackageVer != "" {
fmt.Printf("%s", PackageVer)
} else {
fmt.Printf("develop")
}
if PackageRev != "" {
fmt.Printf("-%s\n", PackageRev)
} else {
fmt.Printf("\n")
}

os.Exit(0)
}

switch agent.command {
case "install":
case "uninstall":
case "start":
case "stop":
case "status":
case "":
default:
fmt.Println("invalid value of 'command' argument: ", agent.command)
flag.PrintDefaults()
os.Exit(1)
}

if os.Getenv("CONNECT") != "" {
agent.connect = os.Getenv("CONNECT")
}
if os.Getenv("AGENT_ID") != "" {
agent.agentID = os.Getenv("AGENT_ID")
}
if os.Getenv("LOG_DIR") != "" {
agent.logDir = os.Getenv("LOG_DIR")
}
if os.Getenv("DEBUG") != "" {
agent.debug = true
}

if agent.logDir == "" {
agent.logDir = filepath.Dir(os.Args[0])
}
logDir, err := filepath.Abs(agent.logDir)
if err != nil {
fmt.Println("invalid value of 'logdir' argument: ", agent.logDir)
os.Exit(1)
} else {
agent.logDir = logDir
}

if agent.debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}

logPath := filepath.Join(agent.logDir, "agent.log")
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println("failed to open log file: ", logPath)
os.Exit(1)
}
if agent.service {
logrus.SetOutput(logFile)
} else {
logrus.SetOutput(io.MultiWriter(os.Stdout, logFile))
}

kind := daemon.SystemDaemon
var dependencies []string
switch runtime.GOOS {
case "linux":
dependencies = []string{"multi-user.target", "sockets.target"}
case "windows":
dependencies = []string{"tcpip"}
case "darwin":
if os.Geteuid() == 0 {
kind = daemon.GlobalDaemon
} else {
kind = daemon.UserAgent
}
default:
logrus.Error("unsupported OS type")
os.Exit(1)
}

agent.svc, err = daemon.New(name, description, kind, dependencies...)
if err != nil {
logrus.WithError(err).Error("vxagent service creating failed")
os.Exit(1)
}

status, err := agent.Manage()
if err != nil {
fmt.Println(status, "\n", err.Error())
os.Exit(1)
}
fmt.Println(status)
}
9 changes: 9 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import "testing"

func TestMain(t *testing.T) {
if true != true {
t.Errorf("Test error\n")
}
}
Loading

0 comments on commit 3730596

Please sign in to comment.