From 9c89a2fe904c78d66d250b44272806deab979e6c Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 5 Dec 2017 17:21:47 +0900 Subject: [PATCH] ing --- client/{ => central}/data/client.go | 0 client/{ => central}/data/client_handlers.go | 0 client/{ => central}/data/socket_builders.go | 0 client/{ => central}/data/socket_handlers.go | 0 client/{ => central}/file/client.go | 0 client/{ => central}/file/client_handlers.go | 0 client/{ => central}/file/socket_builders.go | 0 client/{ => central}/file/socket_handlers.go | 0 client/{ => central}/probe/client.go | 0 client/{ => central}/probe/client_handlers.go | 0 client/{ => central}/probe/socket_builders.go | 0 client/{ => central}/probe/socket_handlers.go | 0 client/container/client_handlers.go | 30 +++++++ client/container/container.go | 16 ++++ client/container/socket_handlers.go | 26 ++++++ glide.yaml | 4 - manager/container/container.go | 85 +++++++++++++++++++ service/CrawlerService.go | 16 +++- service/DiscoveryService.go | 31 ++++--- 19 files changed, 189 insertions(+), 19 deletions(-) rename client/{ => central}/data/client.go (100%) rename client/{ => central}/data/client_handlers.go (100%) rename client/{ => central}/data/socket_builders.go (100%) rename client/{ => central}/data/socket_handlers.go (100%) rename client/{ => central}/file/client.go (100%) rename client/{ => central}/file/client_handlers.go (100%) rename client/{ => central}/file/socket_builders.go (100%) rename client/{ => central}/file/socket_handlers.go (100%) rename client/{ => central}/probe/client.go (100%) rename client/{ => central}/probe/client_handlers.go (100%) rename client/{ => central}/probe/socket_builders.go (100%) rename client/{ => central}/probe/socket_handlers.go (100%) create mode 100644 client/container/client_handlers.go create mode 100644 client/container/container.go create mode 100644 client/container/socket_handlers.go create mode 100644 manager/container/container.go diff --git a/client/data/client.go b/client/central/data/client.go similarity index 100% rename from client/data/client.go rename to client/central/data/client.go diff --git a/client/data/client_handlers.go b/client/central/data/client_handlers.go similarity index 100% rename from client/data/client_handlers.go rename to client/central/data/client_handlers.go diff --git a/client/data/socket_builders.go b/client/central/data/socket_builders.go similarity index 100% rename from client/data/socket_builders.go rename to client/central/data/socket_builders.go diff --git a/client/data/socket_handlers.go b/client/central/data/socket_handlers.go similarity index 100% rename from client/data/socket_handlers.go rename to client/central/data/socket_handlers.go diff --git a/client/file/client.go b/client/central/file/client.go similarity index 100% rename from client/file/client.go rename to client/central/file/client.go diff --git a/client/file/client_handlers.go b/client/central/file/client_handlers.go similarity index 100% rename from client/file/client_handlers.go rename to client/central/file/client_handlers.go diff --git a/client/file/socket_builders.go b/client/central/file/socket_builders.go similarity index 100% rename from client/file/socket_builders.go rename to client/central/file/socket_builders.go diff --git a/client/file/socket_handlers.go b/client/central/file/socket_handlers.go similarity index 100% rename from client/file/socket_handlers.go rename to client/central/file/socket_handlers.go diff --git a/client/probe/client.go b/client/central/probe/client.go similarity index 100% rename from client/probe/client.go rename to client/central/probe/client.go diff --git a/client/probe/client_handlers.go b/client/central/probe/client_handlers.go similarity index 100% rename from client/probe/client_handlers.go rename to client/central/probe/client_handlers.go diff --git a/client/probe/socket_builders.go b/client/central/probe/socket_builders.go similarity index 100% rename from client/probe/socket_builders.go rename to client/central/probe/socket_builders.go diff --git a/client/probe/socket_handlers.go b/client/central/probe/socket_handlers.go similarity index 100% rename from client/probe/socket_handlers.go rename to client/central/probe/socket_handlers.go diff --git a/client/container/client_handlers.go b/client/container/client_handlers.go new file mode 100644 index 0000000..79c7b28 --- /dev/null +++ b/client/container/client_handlers.go @@ -0,0 +1,30 @@ +package container + +import ( + crc "git.loafle.net/commons_go/rpc/client" + crr "git.loafle.net/commons_go/rpc/registry" + oopcc "git.loafle.net/overflow/overflow_probe_container/client" +) + +type ClientHandlers struct { + oopcc.ClientHandler +} + +func (ch *ClientHandlers) Init(clientCTX crc.ClientContext) error { + + return ch.ClientHandler.Init(clientCTX) +} + +func (ch *ClientHandlers) Destroy(clientCTX crc.ClientContext) { + ch.ClientHandler.Destroy(clientCTX) +} + +func (ch *ClientHandlers) Validate() { + ch.ClientHandler.Validate() +} + +func NewClientHandler(rpcInvoker crr.RPCInvoker) oopcc.ClientHandler { + ch := &ClientHandlers{} + ch.ClientHandler = oopcc.NewClientHandler(rpcInvoker) + return ch +} diff --git a/client/container/container.go b/client/container/container.go new file mode 100644 index 0000000..37e7cd1 --- /dev/null +++ b/client/container/container.go @@ -0,0 +1,16 @@ +package container + +import ( + crc "git.loafle.net/commons_go/rpc/client" + crr "git.loafle.net/commons_go/rpc/registry" + oopcc "git.loafle.net/overflow/overflow_probe_container/client" +) + +func New(addr string, rpcInvoker crr.RPCInvoker) crc.Client { + ch := oopcc.NewClientHandler(rpcInvoker) + socketHandler := NewSocketHandler() + + c := oopcc.New(addr, ch, socketHandler) + + return c +} diff --git a/client/container/socket_handlers.go b/client/container/socket_handlers.go new file mode 100644 index 0000000..04b927f --- /dev/null +++ b/client/container/socket_handlers.go @@ -0,0 +1,26 @@ +package container + +import ( + "net" + + csc "git.loafle.net/commons_go/server/client" +) + +type SocketHandlers struct { + csc.SocketHandler +} + +func (sh *SocketHandlers) OnConnect(socketContext csc.SocketContext, conn net.Conn) { + // no op +} + +func (sh *SocketHandlers) OnDisconnect(soc csc.Socket) { + // no op +} + +func (sh *SocketHandlers) Validate() { +} + +func NewSocketHandler() csc.SocketHandler { + return &SocketHandlers{} +} diff --git a/glide.yaml b/glide.yaml index 392c4a3..781049c 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,12 +2,8 @@ package: git.loafle.net/overflow/overflow_probes import: - package: git.loafle.net/commons_go/config - package: git.loafle.net/commons_go/logging -- package: github.com/gorilla/websocket - version: v1.2.0 - package: git.loafle.net/commons_go/util - package: github.com/shirou/gopsutil version: v2.17.08 -- package: github.com/takama/daemon - version: 0.9.1 - package: github.com/dgrijalva/jwt-go version: v3.1.0 diff --git a/manager/container/container.go b/manager/container/container.go new file mode 100644 index 0000000..054549c --- /dev/null +++ b/manager/container/container.go @@ -0,0 +1,85 @@ +package container + +import ( + "fmt" + "os/exec" + "time" + + "git.loafle.net/commons_go/logging" + crc "git.loafle.net/commons_go/rpc/client" + oopcc "git.loafle.net/overflow/overflow_probes/client/container" + uuid "github.com/satori/go.uuid" +) + +type ContainerManager interface { + GetClient(name string) crc.Client +} + +type containerManager struct { + containerClients map[string]*containerState +} + +type containerState struct { + socketName string + pid int + client crc.Client +} + +func (cm *containerManager) GetClient(name string) crc.Client { + return nil +} + +func (cm *containerManager) CheckClient(name string) bool { + cs, ok := cm.containerClients[name] + if !ok || nil == cs || nil == cs.client { + return false + } + + stateOk := false + if err := cs.client.Call(&stateOk, "StateService.State"); nil != err { + logging.Logger().Error(fmt.Sprintf("Probe: Call[%s(%s)] err %v", name, "StateService.State", err)) + return false + } + + return stateOk +} + +func (cm *containerManager) ConnectClient(name string) error { + + return nil +} + +func (cm *containerManager) runProcess(name string) error { + sockFile := uuid.NewV4().String() + sockArg := fmt.Sprintf("-sock \"%s\"", sockFile) + + cmd := exec.Command("", sockArg) + if err := cmd.Start(); nil != err { + logging.Logger().Error(fmt.Sprintf("Probe: To run container(%s) failed err %v", name, err)) + return err + } + time.Sleep(time.Duration(time.Second * 2)) + + cs := &containerState{ + socketName: sockFile, + pid: cmd.Process.Pid, + } + cs.client = oopcc.New(sockFile, nil) + // write pid file + cm.containerClients[name] = cs + + return nil +} + +// func (cm *containerManager) { + +// } +// func (cm *containerManager) { + +// } +// func (cm *containerManager) { + +// } +// func (cm *containerManager) { + +// } diff --git a/service/CrawlerService.go b/service/CrawlerService.go index 7e2b2d8..de0b85b 100644 --- a/service/CrawlerService.go +++ b/service/CrawlerService.go @@ -1,20 +1,28 @@ package service +import ( + configM "git.loafle.net/overflow/overflow_commons_go/modules/config/model" + oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client" + oopmc "git.loafle.net/overflow/overflow_probes/manager/container" +) + type CrawlerService struct { + ProbeClient oogwc.Client `annotation:"@inject{name:probeClient}"` + ContainerManager oopmc.ContainerManager `annotation:"@inject{name:containerManager}"` } func (cs *CrawlerService) Install() error { - + return nil } func (cs *CrawlerService) Uninstall() error { - + return nil } func (cs *CrawlerService) Update() error { - + return nil } func (cs *CrawlerService) Authenticate(crawler *configM.Crawler, target *configM.Target) error { - + return nil } diff --git a/service/DiscoveryService.go b/service/DiscoveryService.go index 25c47c9..efe8cdf 100644 --- a/service/DiscoveryService.go +++ b/service/DiscoveryService.go @@ -1,36 +1,45 @@ package service +import ( + discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model" + oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client" + oopmc "git.loafle.net/overflow/overflow_probes/manager/container" +) + type DiscoveryService struct { + ProbeClient oogwc.Client `annotation:"@inject{name:probeClient}"` + ContainerManager oopmc.ContainerManager `annotation:"@inject{name:containerManager}"` } func (ds *DiscoveryService) DiscoverZone(dz *discoveryM.DiscoveryZone) error { - return nil + return ds.ContainerManager.GetClient().Send("DiscoveryService.DiscoverZone", dz) } -func (ds *DiscoveryService) DiscoverHost(zone *discoveryM.Zone, dz *discoveryM.DiscoveryHost) error { - return nil +func (ds *DiscoveryService) DiscoverHost(zone *discoveryM.Zone, dh *discoveryM.DiscoveryHost) error { + return ds.ContainerManager.GetClient().Send("DiscoveryService.DiscoverHost", zone, dh) } -func (ds *DiscoveryService) DiscoverPort(host *discoveryM.Host, dz *discoveryM.DiscoveryPort) error { - return nil +func (ds *DiscoveryService) DiscoverPort(host *discoveryM.Host, dp *discoveryM.DiscoveryPort) error { + return ds.ContainerManager.GetClient().Send("DiscoveryService.DiscoverPort", host, dp) } -func (ds *DiscoveryService) DiscoverService(port *discoveryM.Port, dz *discoveryM.DiscoveryService) error { - return nil +func (ds *DiscoveryService) DiscoverService(port *discoveryM.Port, ds *discoveryM.DiscoveryService) error { + return ds.ContainerManager.GetClient().Send("DiscoveryService.DiscoverZone", port, ds) } +// use by discovery func (ds *DiscoveryService) DiscoveredZone(zone *discoveryM.Zone) error { - return nil + return ds.ProbeClient.Send("DiscoveryService.DiscoveredZone", zone) } func (ds *DiscoveryService) DiscoveredHost(host *discoveryM.Host) error { - return nil + return ds.ProbeClient.Send("DiscoveryService.DiscoveredHost", host) } func (ds *DiscoveryService) DiscoveredPort(port *discoveryM.Port) error { - return nil + return ds.ProbeClient.Send("DiscoveryService.DiscoveredPort", port) } func (ds *DiscoveryService) DiscoveredService(service *discoveryM.Service) error { - return nil + return ds.ProbeClient.Send("DiscoveryService.DiscoveredService", service) }