This commit is contained in:
crusader 2018-04-18 23:56:13 +09:00
parent 92d7926d3f
commit d2d34699aa
25 changed files with 606 additions and 271 deletions

4
_build/bin/auth.json Normal file
View File

@ -0,0 +1,4 @@
{
"tempKey": "38dc61ec42de11e8b0460242ac120002",
"acceptedDate": "2018-04-18T16:57:23.23885679+09:00"
}

View File

@ -17,9 +17,6 @@
} }
}, },
"probe": { "probe": {
"key": "d3eb9b99424511e8b0460242ac120002" "key": "7771f55d42de11e8b0460242ac120002"
},
"paths": {
"root": "/project/overFlow/probe"
} }
} }

View File

@ -1,4 +0,0 @@
{
"tempKey": "cb8ef3c8424511e8b0460242ac120002",
"acceptedDate": "2018-04-17T22:45:11.147531244+09:00"
}

View File

@ -3,7 +3,6 @@ package auth
import ( import (
"context" "context"
"fmt" "fmt"
"path"
"sync" "sync"
"git.loafle.net/commons/configuration-go" "git.loafle.net/commons/configuration-go"
@ -14,11 +13,10 @@ import (
"git.loafle.net/overflow/probe/auth/annotation" "git.loafle.net/overflow/probe/auth/annotation"
"git.loafle.net/overflow/probe/auth/service" "git.loafle.net/overflow/probe/auth/service"
"git.loafle.net/overflow/probe/config"
) )
type Authenticator struct { type Authenticator struct {
ConfigDir string
authConfig ocnc.Auth authConfig ocnc.Auth
services []interface{} services []interface{}
@ -33,13 +31,12 @@ func (a *Authenticator) EndableStart() (<-chan error, error) {
return nil, fmt.Errorf("already running. Stop it before starting it again") return nil, fmt.Errorf("already running. Stop it before starting it again")
} }
authConfigPath := path.Join(a.ConfigDir, ocnc.ConfigFileName)
conf := configuration.New() conf := configuration.New()
if configuration.Exists(authConfigPath) { if configuration.Exists(config.NoAuthProbeConfigFilePath()) {
if err := conf.Load(&a.authConfig, authConfigPath); nil != err { if err := conf.Load(&a.authConfig, config.NoAuthProbeConfigFilePath()); nil != err {
logging.Logger().Errorf("%s %v", err) logging.Logger().Errorf("%s %v", err)
return nil, fmt.Errorf("loading of auth config file[%s] failed", authConfigPath) return nil, fmt.Errorf("loading of auth config file[%s] failed", config.NoAuthProbeConfigFilePath())
} }
} }

View File

@ -2,7 +2,6 @@ package service
import ( import (
"context" "context"
"path"
"reflect" "reflect"
"time" "time"
@ -12,7 +11,6 @@ import (
logging "git.loafle.net/commons/logging-go" logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client" crc "git.loafle.net/commons/rpc-go/client"
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config" ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
ocpc "git.loafle.net/overflow/commons-go/probe/config"
"git.loafle.net/overflow/probe/client/central" "git.loafle.net/overflow/probe/client/central"
"git.loafle.net/overflow/probe/config" "git.loafle.net/overflow/probe/config"
@ -29,7 +27,6 @@ func init() {
type NoAuthProbeService struct { type NoAuthProbeService struct {
cda.TypeAnnotation `annotation:"@overflow:AuthRPCService()"` cda.TypeAnnotation `annotation:"@overflow:AuthRPCService()"`
ConfigDir string `annotation:"@Resource(name='ConfigDir')"`
Config *config.Config `annotation:"@Resource(name='Config')"` Config *config.Config `annotation:"@Resource(name='Config')"`
AuthConfig *ocnc.Auth `annotation:"@Resource(name='AuthConfig')"` AuthConfig *ocnc.Auth `annotation:"@Resource(name='AuthConfig')"`
AuthDoneChan chan error `annotation:"@Resource(name='AuthDoneChan')"` AuthDoneChan chan error `annotation:"@Resource(name='AuthDoneChan')"`
@ -70,14 +67,14 @@ func (s *NoAuthProbeService) Accept(probeKey string) error {
n := time.Now() n := time.Now()
s.AuthConfig.AcceptedDate = &n s.AuthConfig.AcceptedDate = &n
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err { if err := configuration.Save(s.AuthConfig, config.NoAuthProbeConfigFilePath(), true); nil != err {
logging.Logger().Error(err) logging.Logger().Error(err)
s.AuthDoneChan <- err s.AuthDoneChan <- err
return nil return nil
} }
s.Config.Probe.Key = &probeKey s.Config.Probe.Key = &probeKey
if err := configuration.Save(s.Config, path.Join(s.ConfigDir, ocpc.ConfigFileName), true); nil != err { if err := configuration.Save(s.Config, config.ProbeConfigFilePath(), true); nil != err {
logging.Logger().Error(err) logging.Logger().Error(err)
s.AuthDoneChan <- err s.AuthDoneChan <- err
return nil return nil
@ -91,7 +88,7 @@ func (s *NoAuthProbeService) Deny() error {
logging.Logger().Infof("denied by central") logging.Logger().Infof("denied by central")
n := time.Now() n := time.Now()
s.AuthConfig.DeniedDate = &n s.AuthConfig.DeniedDate = &n
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err { if err := configuration.Save(s.AuthConfig, config.NoAuthProbeConfigFilePath(), true); nil != err {
logging.Logger().Error(err) logging.Logger().Error(err)
s.AuthDoneChan <- err s.AuthDoneChan <- err
return nil return nil
@ -104,7 +101,7 @@ func (s *NoAuthProbeService) Deny() error {
func (s *NoAuthProbeService) HandleTempKey(tempKey string) { func (s *NoAuthProbeService) HandleTempKey(tempKey string) {
logging.Logger().Infof("registered by central") logging.Logger().Infof("registered by central")
s.AuthConfig.TempKey = &tempKey s.AuthConfig.TempKey = &tempKey
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err { if err := configuration.Save(s.AuthConfig, config.NoAuthProbeConfigFilePath(), true); nil != err {
logging.Logger().Error(err) logging.Logger().Error(err)
s.AuthDoneChan <- err s.AuthDoneChan <- err
return return

View File

@ -8,6 +8,7 @@ import (
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go" logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client" crc "git.loafle.net/commons/rpc-go/client"
csc "git.loafle.net/commons/server-go/client"
cur "git.loafle.net/commons/util-go/reflect" cur "git.loafle.net/commons/util-go/reflect"
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config" ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants" ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
@ -58,6 +59,10 @@ func NewAuth(tempKeyHandler func(tempKey string), services ...interface{}) (*crc
} }
} }
connector.OnDisconnected = func(connector csc.Connector) {
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
}
return newClient("Auth", connector, services), nil return newClient("Auth", connector, services), nil
} }

View File

@ -4,7 +4,9 @@ import (
"fmt" "fmt"
"net/http" "net/http"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client" crc "git.loafle.net/commons/rpc-go/client"
csc "git.loafle.net/commons/server-go/client"
ocpc "git.loafle.net/overflow/commons-go/probe/constants" ocpc "git.loafle.net/overflow/commons-go/probe/constants"
"git.loafle.net/overflow/probe/config" "git.loafle.net/overflow/probe/config"
) )
@ -28,6 +30,9 @@ func NewData() (*crc.Client, error) {
} }
connector.ResponseHandler = func(res *http.Response) { connector.ResponseHandler = func(res *http.Response) {
} }
connector.OnDisconnected = func(connector csc.Connector) {
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
}
return newClient("Data", connector, nil), nil return newClient("Data", connector, nil), nil
} }

View File

@ -4,7 +4,9 @@ import (
"fmt" "fmt"
"net/http" "net/http"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client" crc "git.loafle.net/commons/rpc-go/client"
csc "git.loafle.net/commons/server-go/client"
ocpc "git.loafle.net/overflow/commons-go/probe/constants" ocpc "git.loafle.net/overflow/commons-go/probe/constants"
"git.loafle.net/overflow/probe/config" "git.loafle.net/overflow/probe/config"
) )
@ -32,6 +34,8 @@ func NewProbe(encryptionKeyHandler func(encryptionKey string), services ...inter
encryptionKeyHandler(encryptionKey) encryptionKeyHandler(encryptionKey)
} }
} }
connector.OnDisconnected = func(connector csc.Connector) {
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
}
return newClient("Probe", connector, services), nil return newClient("Probe", connector, services), nil
} }

View File

@ -1,29 +1,36 @@
package container package container
import ( import (
"fmt"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client" crc "git.loafle.net/commons/rpc-go/client"
crpj "git.loafle.net/commons/rpc-go/protocol/json" crpj "git.loafle.net/commons/rpc-go/protocol/json"
crr "git.loafle.net/commons/rpc-go/registry" crr "git.loafle.net/commons/rpc-go/registry"
csc "git.loafle.net/commons/server-go/client" csc "git.loafle.net/commons/server-go/client"
cssnc "git.loafle.net/commons/server-go/socket/net/client" cssnc "git.loafle.net/commons/server-go/socket/net/client"
ocpcc "git.loafle.net/overflow/commons-go/probe/constants"
) )
func newConnector(name string) (*cssnc.Connectors, error) { func newConnector(name string, port int) (*cssnc.Connectors, error) {
connector := &cssnc.Connectors{ connector := &cssnc.Connectors{
Network: "tcp4", Network: "tcp4",
Address: "", Address: fmt.Sprintf("127.0.0.1:%d", port),
} }
connector.ReconnectInterval = 5 connector.ReconnectInterval = 5
connector.ReconnectTryTime = 10 connector.ReconnectTryTime = 2
connector.MaxMessageSize = 4096 connector.MaxMessageSize = 4096
connector.ReadBufferSize = 4096 connector.ReadBufferSize = 4096
connector.WriteBufferSize = 4096 connector.WriteBufferSize = 4096
connector.PongTimeout = 60 connector.PongTimeout = 60
connector.PingTimeout = 10 connector.PingTimeout = 10
connector.PingPeriod = 9 connector.PingPeriod = 9
connector.Name = name connector.Name = name
connector.OnDisconnected = func(connector csc.Connector) {
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
}
return connector, nil return connector, nil
} }
@ -43,3 +50,12 @@ func newClient(name string, connector csc.Connector, services []interface{}) *cr
Name: name, Name: name,
} }
} }
func NewClient(containerType ocpcc.ContainerType, port int, services []interface{}) (*crc.Client, error) {
connector, err := newConnector(containerType.String(), port)
if nil != err {
return nil, err
}
return newClient(containerType.String(), connector, services), nil
}

View File

@ -1,22 +0,0 @@
package container
import (
"fmt"
crc "git.loafle.net/commons/rpc-go/client"
"git.loafle.net/overflow/probe/config"
)
func NewDiscovery(services ...interface{}) (*crc.Client, error) {
config := config.GetConfig()
if nil == config {
return nil, fmt.Errorf("Config is not available")
}
connector, err := newConnector("Probe")
if nil != err {
return nil, err
}
return newClient("Probe", connector, services), nil
}

View File

@ -10,15 +10,13 @@ import (
) )
const ( const (
ConfigKey = "Config" ConfigKey = "Config"
ConfigDirKey = "ConfigDir"
) )
type Config struct { type Config struct {
Account *ocpc.Account `required:"true" json:"account" yaml:"account" toml:"account"` Account *ocpc.Account `required:"true" json:"account" yaml:"account" toml:"account"`
Central *ocpc.Central `required:"true" json:"central" yaml:"central" toml:"central"` Central *ocpc.Central `required:"true" json:"central" yaml:"central" toml:"central"`
Probe *ocpc.Probe `required:"true" json:"probe" yaml:"probe" toml:"probe"` Probe *ocpc.Probe `required:"true" json:"probe" yaml:"probe" toml:"probe"`
Paths map[string]string `required:"true" json:"paths" yaml:"paths" toml:"paths"`
} }
func GetConfig() *Config { func GetConfig() *Config {
@ -36,19 +34,3 @@ func GetConfig() *Config {
return config return config
} }
func GetConfigDir() string {
_configDir, err := cdr.GetInstanceByName(ConfigDirKey)
if nil != err {
logging.Logger().Error(err)
return ""
}
configDir, ok := _configDir.(string)
if !ok {
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_configDir))
logging.Logger().Errorf("Cannot convert [%s]%s to string type", pkg, n)
return ""
}
return configDir
}

21
config/path-container.go Normal file
View File

@ -0,0 +1,21 @@
package config
import (
"fmt"
"path"
"strings"
ocpcc "git.loafle.net/overflow/commons-go/probe/constants"
)
func ContainerPIDFilePath(containerType ocpcc.ContainerType) string {
return path.Join(PIDDir(), fmt.Sprintf("%s.pid", strings.ToLower(containerType.String())))
}
func ContainerBinFilePath(containerType ocpcc.ContainerType) string {
return path.Join(BinDir(), ocpcc.ContainerBinFileName[containerType])
}
func ContainerLoggingConfigFilePath(containerType ocpcc.ContainerType) string {
return path.Join(BinDir(), ocpcc.ContainerLoggingConfigFileName[containerType])
}

View File

@ -0,0 +1,15 @@
package config
import (
"path"
ocscm "git.loafle.net/overflow/commons-go/sensorconfig/model"
)
func SensorConfigContainerDir(name string) string {
return path.Join(ConfigDir(), name)
}
func SensorConfigFilePath(sensorConfig *ocscm.SensorConfig) string {
return path.Join(SensorConfigContainerDir(sensorConfig.Crawler.Container), sensorConfig.Crawler.Name, sensorConfig.ID.String())
}

70
config/path.go Normal file
View File

@ -0,0 +1,70 @@
package config
import (
"fmt"
"os"
"path"
"path/filepath"
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
ocpcc "git.loafle.net/overflow/commons-go/probe/constants"
)
var (
rootDir string
)
func init() {
exePath, err := os.Executable()
if err != nil {
panic(err)
}
binDir := filepath.Dir(exePath)
if "debug" == filepath.Base(exePath) {
rootDir = path.Join(binDir, "_build")
} else {
rootDir = filepath.Clean(fmt.Sprintf("%s/..", binDir))
}
}
func RootDir() string {
return rootDir
}
func BinDir() string {
return path.Join(RootDir(), ocpcc.PathBin)
}
func ConfigDir() string {
return path.Join(RootDir(), ocpcc.PathConfig)
}
func JREDir() string {
return path.Join(RootDir(), ocpcc.PathJRE)
}
func JavaBinPath() string {
return path.Join(JREDir(), "bin", "java")
}
func LogsDir() string {
return path.Join(RootDir(), ocpcc.PathLogs)
}
func PIDDir() string {
return path.Join(RootDir(), ocpcc.PathPID)
}
func ProbeConfigFilePath() string {
return path.Join(BinDir(), ocpcc.ConfigFileName)
}
func ProbeLoggingConfigFilePath() string {
return path.Join(BinDir(), ocpcc.LoggingConfigFileName)
}
func NoAuthProbeConfigFilePath() string {
return path.Join(BinDir(), ocncc.ConfigFileName)
}

19
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"flag"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
@ -18,35 +17,23 @@ import (
"git.loafle.net/overflow/probe/probe" "git.loafle.net/overflow/probe/probe"
) )
var (
configDir *string
)
func init() { func init() {
configDir = flag.String("config-dir", "./", "Config directory") logging.InitializeLogger(config.ProbeLoggingConfigFilePath())
logConfigPath := flag.String("log-config", "", "logging config path")
flag.Parse()
logging.InitializeLogger(*logConfigPath)
} }
func main() { func main() {
_config := &config.Config{} _config := &config.Config{}
configuration.SetConfigPath(*configDir) if err := configuration.Load(_config, config.ProbeConfigFilePath()); nil != err {
if err := configuration.Load(_config, ocpc.ConfigFileName); nil != err {
logging.Logger().Panic(err) logging.Logger().Panic(err)
} }
cdr.RegisterResource(config.ConfigKey, _config) cdr.RegisterResource(config.ConfigKey, _config)
cdr.RegisterResource(config.ConfigDirKey, *configDir)
var instance interface{} var instance interface{}
go func() { go func() {
if ocpc.ProbeStateTypeNotAuthorized == _config.Probe.State() { if ocpc.ProbeStateTypeNotAuthorized == _config.Probe.State() {
instance = &auth.Authenticator{ instance = &auth.Authenticator{}
ConfigDir: *configDir,
}
doneChan, err := instance.(occi.EndableStarter).EndableStart() doneChan, err := instance.(occi.EndableStarter).EndableStart()
if nil != err { if nil != err {
logging.Logger().Error(err) logging.Logger().Error(err)

View File

@ -1,10 +1,25 @@
package service package service
import ( import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"reflect" "reflect"
"strconv"
"time"
cda "git.loafle.net/commons/di-go/annotation" cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
"git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
csc "git.loafle.net/commons/server-go/client"
ocpcc "git.loafle.net/overflow/commons-go/probe/constants"
"git.loafle.net/overflow/probe/client/container"
"git.loafle.net/overflow/probe/config"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation" _ "git.loafle.net/overflow/commons-go/core/annotation"
) )
@ -16,26 +31,225 @@ func init() {
type ContainerService struct { type ContainerService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"` cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
discoveryService *DiscoveryService
rpcServiceMap map[ocpcc.ContainerType][]interface{}
containerStates map[ocpcc.ContainerType]*containerState
connectorMap map[csc.Connector]*containerState
} }
func (s *ContainerService) InitService() error { func (s *ContainerService) InitService() error {
s.containerStates = make(map[ocpcc.ContainerType]*containerState)
s.rpcServiceMap = make(map[ocpcc.ContainerType][]interface{})
s.connectorMap = make(map[csc.Connector]*containerState)
return nil return nil
} }
func (s *ContainerService) StartService() error { func (s *ContainerService) StartService() error {
s.rpcServiceMap[ocpcc.ContainerDiscovery] = []interface{}{
s.discoveryService,
}
s.rpcServiceMap[ocpcc.ContainerNetwork] = []interface{}{}
s.rpcServiceMap[ocpcc.ContainerGenernal] = []interface{}{}
return nil return nil
} }
func (s *ContainerService) StopService() { func (s *ContainerService) StopService() {
for containerType := range s.containerStates {
s.removeContainerState(containerType)
}
} }
func (s *ContainerService) DestroyService() { func (s *ContainerService) DestroyService() {
} }
func (s *ContainerService) Accept() error { func (s *ContainerService) Call(containerType ocpcc.ContainerType, result interface{}, method string, params ...interface{}) error {
client, err := s.getClient(containerType)
if nil != err {
return err
}
return client.Call(result, method, params...)
}
func (s *ContainerService) Send(containerType ocpcc.ContainerType, method string, params ...interface{}) error {
client, err := s.getClient(containerType)
if nil != err {
return err
}
return client.Send(method, params...)
}
func (s *ContainerService) getClient(containerType ocpcc.ContainerType) (*crc.Client, error) {
cs := s.checkContainer(containerType)
if nil == cs {
_cs, err := s.runContainer(containerType)
if nil != err {
return nil, err
}
cs = _cs
s.containerStates[containerType] = cs
}
if nil == cs.client {
client, err := container.NewClient(containerType, cs.port, s.rpcServiceMap[containerType])
if nil != err {
s.removeContainerState(containerType)
return nil, err
}
cs.client = client
cs.client.Connector.SetOnDisconnected(s.onDisconnected)
s.connectorMap[cs.client.Connector] = cs
}
return cs.client, nil
}
func (s *ContainerService) onDisconnected(connector csc.Connector) {
cs, ok := s.connectorMap[connector]
if !ok || nil == cs {
return
}
logging.Logger().Debugf("Client[%s] has been disconnected", cs.containerType.String())
}
func (s *ContainerService) runContainer(containerType ocpcc.ContainerType) (*containerState, error) {
if cs := s.checkContainer(containerType); nil != cs {
return cs, nil
}
cmd, pidFilePath := cotainerCommand(containerType)
removePidFile(pidFilePath)
if err := cmd.Start(); nil != err {
logging.Logger().Errorf("to run Container[%s] failed err %v", containerType.String(), err)
return nil, err
}
port, err := watchPidFileCreate(pidFilePath, time.Duration(time.Second*2))
if nil != err {
return nil, err
}
go func(containerType ocpcc.ContainerType, cmd *exec.Cmd) {
if err := cmd.Wait(); nil != err {
logging.Logger().Error(err)
}
logging.Logger().Infof("Container[%s] has been stopped", containerType.String())
}(containerType, cmd)
cs := &containerState{
containerType: containerType,
cmd: cmd,
port: port,
}
return cs, nil
}
func (s *ContainerService) checkContainer(containerType ocpcc.ContainerType) *containerState {
cs, ok := s.containerStates[containerType]
if !ok || nil == cs {
return nil
}
if nil != cs.cmd.ProcessState && cs.cmd.ProcessState.Exited() {
s.removeContainerState(containerType)
return nil
}
return cs
}
func (s *ContainerService) killContainer(containerType ocpcc.ContainerType) error {
cs, ok := s.containerStates[containerType]
if !ok || nil == cs {
return nil
}
if nil == cs.cmd.ProcessState || !cs.cmd.ProcessState.Exited() {
if err := cs.cmd.Process.Kill(); nil != err {
return err
}
}
return nil return nil
} }
func (s *ContainerService) removeContainerState(containerType ocpcc.ContainerType) {
cs, ok := s.containerStates[containerType]
if !ok || nil == cs {
return
}
delete(s.connectorMap, cs.client.Connector)
cs.client.Stop(context.Background())
s.killContainer(containerType)
delete(s.containerStates, containerType)
}
func cotainerCommand(containerType ocpcc.ContainerType) (cmd *exec.Cmd, pidFilePath string) {
pidFilePath = config.ContainerPIDFilePath(containerType)
loggingConfigFilePath := config.ContainerLoggingConfigFilePath(containerType)
binFilePath := config.ContainerBinFilePath(containerType)
switch containerType {
case ocpcc.ContainerDiscovery, ocpcc.ContainerNetwork:
args := []string{
fmt.Sprintf("-%s=%s", ocpcc.FlagPidFilePathName, pidFilePath),
fmt.Sprintf("-%s=%s", ocpcc.FlagLoggingConfigFilePathName, loggingConfigFilePath),
}
cmd = exec.Command(binFilePath, args...)
case ocpcc.ContainerGenernal:
args := []string{
"-jar",
binFilePath,
pidFilePath,
loggingConfigFilePath,
}
cmd = exec.Command(config.JavaBinPath(), args...)
}
return
}
func removePidFile(pidFilePath string) {
if _, err := os.Stat(pidFilePath); err == nil {
if err := os.Remove(pidFilePath); nil != err {
logging.Logger().Errorf("removing pid file has been failed [%v]", err)
}
}
}
func watchPidFileCreate(pidFilePath string, waitTime time.Duration) (int, error) {
startTime := time.Now()
for {
if _, err := os.Stat(pidFilePath); err == nil {
buf, err := ioutil.ReadFile(pidFilePath)
if nil != err {
return 0, err
}
portNumber, err := strconv.ParseInt(string(buf), 10, 32)
if nil != err {
return 0, err
}
return int(portNumber), nil
}
if time.Since(startTime) > waitTime {
return 0, fmt.Errorf("pid file not exist")
}
time.Sleep(time.Duration(time.Millisecond * 100))
}
}
type containerState struct {
containerType ocpcc.ContainerType
cmd *exec.Cmd
port int
client *crc.Client
}

View File

@ -1,13 +1,10 @@
package service package service
import ( import (
"context"
"reflect" "reflect"
cda "git.loafle.net/commons/di-go/annotation" cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
_ "git.loafle.net/overflow/commons-go/core/annotation" _ "git.loafle.net/overflow/commons-go/core/annotation"
"git.loafle.net/overflow/probe/client/central" "git.loafle.net/overflow/probe/client/central"
) )
@ -19,22 +16,30 @@ func init() {
} }
type DataClientService struct { type DataClientService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"` RPCClientService
client *crc.Client cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
} }
func (s *DataClientService) InitService() error { func (s *DataClientService) InitService() error {
if err := s.RPCClientService.InitService(); nil != err {
return err
}
return nil
}
func (s *DataClientService) StartService() error {
if err := s.RPCClientService.StartService(); nil != err {
return err
}
client, err := central.NewData() client, err := central.NewData()
if nil != err { if nil != err {
return err return err
} }
s.client = client s.client = client
return nil
}
func (s *DataClientService) StartService() error {
if err := s.client.Start(); nil != err { if err := s.client.Start(); nil != err {
return err return err
} }
@ -43,11 +48,10 @@ func (s *DataClientService) StartService() error {
} }
func (s *DataClientService) StopService() { func (s *DataClientService) StopService() {
if err := s.client.Stop(context.Background()); nil != err { s.RPCClientService.StopService()
logging.Logger().Error(err)
}
} }
func (s *DataClientService) DestroyService() { func (s *DataClientService) DestroyService() {
s.client = nil s.RPCClientService.DestroyService()
} }

View File

@ -1,52 +0,0 @@
package service
import (
"context"
"reflect"
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation"
)
var DiscoveryClientServiceType = reflect.TypeOf((*DiscoveryClientService)(nil))
func init() {
cdr.RegisterType(DiscoveryClientServiceType)
}
type DiscoveryClientService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
DiscoveryService *DiscoveryService `annotation:"@Inject()"`
EncryptionKey string
client *crc.Client
}
func (s *DiscoveryClientService) InitService() error {
return nil
}
func (s *DiscoveryClientService) StartService() error {
return nil
}
func (s *DiscoveryClientService) StopService() {
if nil != s.client {
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
}
func (s *DiscoveryClientService) DestroyService() {
s.client = nil
}

View File

@ -5,6 +5,10 @@ import (
cda "git.loafle.net/commons/di-go/annotation" cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
ocdm "git.loafle.net/overflow/commons-go/discovery/model"
ocpcc "git.loafle.net/overflow/commons-go/probe/constants"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation" _ "git.loafle.net/overflow/commons-go/core/annotation"
) )
@ -16,9 +20,15 @@ func init() {
type DiscoveryService struct { type DiscoveryService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"` cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
ContainerService *ContainerService `annotation:"@Inject()"`
ProbeClientService *ProbeClientService `annotation:"@Inject()"`
} }
func (s *DiscoveryService) InitService() error { func (s *DiscoveryService) InitService() error {
s.ProbeClientService.discoveryService = s
s.ContainerService.discoveryService = s
return nil return nil
} }
@ -35,7 +45,35 @@ func (s *DiscoveryService) DestroyService() {
} }
func (s *DiscoveryService) Accept() error { func (s *DiscoveryService) DiscoverZone(requesterID string, dz *ocdm.DiscoveryZone) error {
return s.ContainerService.Send(ocpcc.ContainerDiscovery, "DiscoveryService.DiscoverZone", requesterID, dz)
return nil }
func (s *DiscoveryService) DiscoverHost(requesterID string, zone *ocdm.Zone, dh *ocdm.DiscoveryHost) error {
return s.ContainerService.Send(ocpcc.ContainerDiscovery, "DiscoveryService.DiscoverHost", requesterID, zone, dh)
}
func (s *DiscoveryService) DiscoverPort(requesterID string, host *ocdm.Host, dp *ocdm.DiscoveryPort) error {
return s.ContainerService.Send(ocpcc.ContainerDiscovery, "DiscoveryService.DiscoverPort", requesterID, host, dp)
}
func (s *DiscoveryService) DiscoverService(requesterID string, port *ocdm.Port, dService *ocdm.DiscoveryService) error {
return s.ContainerService.Send(ocpcc.ContainerDiscovery, "DiscoveryService.DiscoverZone", requesterID, port, dService)
}
// use by discovery
func (s *DiscoveryService) DiscoveredZone(requesterID string, zone *ocdm.Zone) error {
return s.ProbeClientService.Send("DiscoveryService.discoveredZone", requesterID, zone)
}
func (s *DiscoveryService) DiscoveredHost(requesterID string, host *ocdm.Host) error {
return s.ProbeClientService.Send("DiscoveryService.discoveredHost", requesterID, host)
}
func (s *DiscoveryService) DiscoveredPort(requesterID string, port *ocdm.Port) error {
return s.ProbeClientService.Send("DiscoveryService.discoveredPort", requesterID, port)
}
func (s *DiscoveryService) DiscoveredService(requesterID string, service *ocdm.Service) error {
return s.ProbeClientService.Send("DiscoveryService.discoveredService", requesterID, service)
} }

View File

@ -1,48 +0,0 @@
package service
import (
"context"
"reflect"
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation"
)
var GeneralClientServiceType = reflect.TypeOf((*GeneralClientService)(nil))
func init() {
cdr.RegisterType(GeneralClientServiceType)
}
type GeneralClientService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
EncryptionKey string
client *crc.Client
}
func (s *GeneralClientService) InitService() error {
return nil
}
func (s *GeneralClientService) StartService() error {
return nil
}
func (s *GeneralClientService) StopService() {
if nil != s.client {
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
}
func (s *GeneralClientService) DestroyService() {
s.client = nil
}

View File

@ -1,48 +0,0 @@
package service
import (
"context"
"reflect"
cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation"
)
var NetworkClientServiceType = reflect.TypeOf((*NetworkClientService)(nil))
func init() {
cdr.RegisterType(NetworkClientServiceType)
}
type NetworkClientService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
EncryptionKey string
client *crc.Client
}
func (s *NetworkClientService) InitService() error {
return nil
}
func (s *NetworkClientService) StartService() error {
return nil
}
func (s *NetworkClientService) StopService() {
if nil != s.client {
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
}
func (s *NetworkClientService) DestroyService() {
s.client = nil
}

View File

@ -1,13 +1,10 @@
package service package service
import ( import (
"context"
"reflect" "reflect"
cda "git.loafle.net/commons/di-go/annotation" cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
// For annotation // For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation" _ "git.loafle.net/overflow/commons-go/core/annotation"
@ -21,26 +18,34 @@ func init() {
} }
type ProbeClientService struct { type ProbeClientService struct {
RPCClientService
cda.TypeAnnotation `annotation:"@overflow:RPCService()"` cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
DiscoveryService *DiscoveryService `annotation:"@Inject()"` discoveryService *DiscoveryService
EncryptionKey string EncryptionKey string
client *crc.Client
} }
func (s *ProbeClientService) InitService() error { func (s *ProbeClientService) InitService() error {
client, err := central.NewProbe(s.HandleEncryptionKey, s.DiscoveryService) if err := s.RPCClientService.InitService(); nil != err {
if nil != err {
return err return err
} }
s.client = client
return nil return nil
} }
func (s *ProbeClientService) StartService() error { func (s *ProbeClientService) StartService() error {
if err := s.RPCClientService.StartService(); nil != err {
return err
}
client, err := central.NewProbe(s.HandleEncryptionKey, s.discoveryService)
if nil != err {
return err
}
s.client = client
if err := s.client.Start(); nil != err { if err := s.client.Start(); nil != err {
return err return err
} }
@ -49,17 +54,13 @@ func (s *ProbeClientService) StartService() error {
} }
func (s *ProbeClientService) StopService() { func (s *ProbeClientService) StopService() {
if err := s.client.Stop(context.Background()); nil != err { s.RPCClientService.StopService()
logging.Logger().Error(err)
}
} }
func (s *ProbeClientService) DestroyService() { func (s *ProbeClientService) DestroyService() {
s.client = nil s.RPCClientService.DestroyService()
} }
func (s *ProbeClientService) HandleEncryptionKey(encryptionKey string) { func (s *ProbeClientService) HandleEncryptionKey(encryptionKey string) {
logging.Logger().Debugf("encryptionKey arrived %s", encryptionKey)
s.EncryptionKey = encryptionKey s.EncryptionKey = encryptionKey
} }

View File

@ -0,0 +1,45 @@
package service
import (
"context"
"fmt"
"git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
)
type RPCClientService struct {
client *crc.Client
}
func (s *RPCClientService) InitService() error {
return nil
}
func (s *RPCClientService) StartService() error {
return nil
}
func (s *RPCClientService) StopService() {
if err := s.client.Stop(context.Background()); nil != err {
logging.Logger().Error(err)
}
}
func (s *RPCClientService) DestroyService() {
s.client = nil
}
func (s *RPCClientService) Call(result interface{}, method string, params ...interface{}) error {
if nil == s.client {
return fmt.Errorf("rpc client is not valid")
}
return s.client.Call(result, method, params...)
}
func (s *RPCClientService) Send(method string, params ...interface{}) error {
if nil == s.client {
return fmt.Errorf("rpc client is not valid")
}
return s.client.Send(method, params...)
}

View File

@ -1,10 +1,19 @@
package service package service
import ( import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"reflect" "reflect"
cda "git.loafle.net/commons/di-go/annotation" cda "git.loafle.net/commons/di-go/annotation"
cdr "git.loafle.net/commons/di-go/registry" cdr "git.loafle.net/commons/di-go/registry"
ocscm "git.loafle.net/overflow/commons-go/sensorconfig/model"
"git.loafle.net/overflow/probe/config"
// For annotation
_ "git.loafle.net/overflow/commons-go/core/annotation" _ "git.loafle.net/overflow/commons-go/core/annotation"
) )
@ -16,13 +25,20 @@ func init() {
type SensorConfigService struct { type SensorConfigService struct {
cda.TypeAnnotation `annotation:"@overflow:RPCService()"` cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
sensorConfigs map[string]*ocscm.SensorConfig
} }
func (s *SensorConfigService) InitService() error { func (s *SensorConfigService) InitService() error {
s.sensorConfigs = make(map[string]*ocscm.SensorConfig)
return nil return nil
} }
func (s *SensorConfigService) StartService() error { func (s *SensorConfigService) StartService() error {
if err := s.loadConfigAll(); nil != err {
return err
}
return nil return nil
} }
@ -34,3 +50,97 @@ func (s *SensorConfigService) StopService() {
func (s *SensorConfigService) DestroyService() { func (s *SensorConfigService) DestroyService() {
} }
func (s *SensorConfigService) AddConfig(tempFilePath string) error {
sc, buf, err := s.loadConfigFile(tempFilePath)
if nil != err {
return err
}
targetPath := config.SensorConfigFilePath(sc)
ioutil.WriteFile(targetPath, buf, 0644)
// tempfile remove
err = os.Remove(tempFilePath)
if nil != err {
return err
}
s.sensorConfigs[sc.ID.String()] = sc
return nil
}
func (s *SensorConfigService) RemoveConfig(sensorConfigID string) error {
sc, ok := s.sensorConfigs[sensorConfigID]
if !ok {
return fmt.Errorf("SensorConfig[%s] is not exist", sensorConfigID)
}
targetPath := config.SensorConfigFilePath(sc)
err := os.Remove(targetPath)
if nil != err {
return err
}
delete(s.sensorConfigs, sensorConfigID)
return nil
}
func (s *SensorConfigService) loadConfigAll() error {
configDirPath := config.ConfigDir()
files, err := ioutil.ReadDir(configDirPath)
if nil != err {
return err
}
for _, file := range files {
if file.IsDir() == true {
if err := s.loadConfigDir(path.Join(configDirPath, file.Name())); nil != err {
return err
}
}
}
return nil
}
func (s *SensorConfigService) loadConfigDir(dirPath string) error {
files, err := ioutil.ReadDir(dirPath)
if nil != err {
return err
}
for _, file := range files {
filePath := path.Join(dirPath, file.Name())
if file.IsDir() == true {
if err := s.loadConfigDir(filePath); nil != err {
return err
}
} else {
sc, _, err := s.loadConfigFile(filePath)
if nil != err {
return err
}
s.sensorConfigs[file.Name()] = sc
}
}
return nil
}
func (s *SensorConfigService) loadConfigFile(filePath string) (*ocscm.SensorConfig, []byte, error) {
buf, err := ioutil.ReadFile(filePath)
if nil != err {
return nil, nil, err
}
var m = &ocscm.SensorConfig{}
if err := json.Unmarshal(buf, m); nil != err {
return nil, nil, err
}
return m, buf, nil
}

View File

@ -6,9 +6,6 @@ var (
OrderedServices = []reflect.Type{ OrderedServices = []reflect.Type{
ProbeClientServiceType, ProbeClientServiceType,
DataClientServiceType, DataClientServiceType,
DiscoveryClientServiceType,
GeneralClientServiceType,
NetworkClientServiceType,
SensorConfigServiceType, SensorConfigServiceType,
ContainerServiceType, ContainerServiceType,
CrawlerServiceType, CrawlerServiceType,