서버, 클라이언트 config 분리
This commit is contained in:
@ -2,7 +2,10 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -27,6 +30,15 @@ import (
|
|||||||
sigar "github.com/cloudfoundry/gosigar"
|
sigar "github.com/cloudfoundry/gosigar"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type clientConfig struct {
|
||||||
|
GrpcAddress string `json:"grpc_server_address"`
|
||||||
|
HttpAddress string `json:"http_server_address"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type totalConfig struct {
|
||||||
|
Client clientConfig `json:"houston_client"`
|
||||||
|
}
|
||||||
|
|
||||||
type HoustonClient interface {
|
type HoustonClient interface {
|
||||||
SetReportMetrics(map[string]float32)
|
SetReportMetrics(map[string]float32)
|
||||||
Shutdown()
|
Shutdown()
|
||||||
@ -152,8 +164,25 @@ func (hc *houstonClient) makeOperationQueryRequest() *protos.OperationQueryReque
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) {
|
func NewClient() (HoustonClient, error) {
|
||||||
client, err := grpc.Dial(grpcAddr, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
bt, err := os.ReadFile("config.json")
|
||||||
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var config totalConfig
|
||||||
|
if err := json.Unmarshal(bt, &config); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(config.Client.GrpcAddress) == 0 {
|
||||||
|
return nil, errors.New("client.grpc_server_address is missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(config.Client.HttpAddress) == 0 {
|
||||||
|
return nil, errors.New("client.http_server_address is missing")
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := grpc.Dial(config.Client.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -184,7 +213,7 @@ func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) {
|
|||||||
client: client,
|
client: client,
|
||||||
extraMetrics: unsafe.Pointer(&map[string]float32{}),
|
extraMetrics: unsafe.Pointer(&map[string]float32{}),
|
||||||
deploys: deploys,
|
deploys: deploys,
|
||||||
httpAddr: httpAddr,
|
httpAddr: config.Client.HttpAddress,
|
||||||
timestamp: exefi.ModTime().String(),
|
timestamp: exefi.ModTime().String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,7 +291,11 @@ func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) {
|
|||||||
err := hc.withdraw(&wr)
|
err := hc.withdraw(&wr)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
prog := gatherDeployedPrograms(wr.Name)
|
prog := gatherDeployedPrograms(wr.Name)
|
||||||
|
if len(prog) == 0 {
|
||||||
|
delete(hc.deploys, wr.Name)
|
||||||
|
} else {
|
||||||
hc.deploys[wr.Name] = prog
|
hc.deploys[wr.Name] = prog
|
||||||
|
}
|
||||||
op.Refresh(ctx, hc.makeOperationQueryRequest())
|
op.Refresh(ctx, hc.makeOperationQueryRequest())
|
||||||
} else {
|
} else {
|
||||||
logger.Println(err)
|
logger.Println(err)
|
||||||
|
|||||||
@ -1 +1,9 @@
|
|||||||
{}
|
{
|
||||||
|
"houston_client" : {
|
||||||
|
"grpc_server_address" : "192.168.9.32:8080",
|
||||||
|
"http_server_address" : "http://192.168.9.32/commandcenter"
|
||||||
|
},
|
||||||
|
"houston_server" : {
|
||||||
|
"grpc_port" : 8080
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
3
main.go
3
main.go
@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
var runAsClient = flag.Bool("client", false, "")
|
var runAsClient = flag.Bool("client", false, "")
|
||||||
var runAsServer = flag.Bool("server", false, "")
|
var runAsServer = flag.Bool("server", false, "")
|
||||||
var port = flag.Int("port", 8080, "")
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if !flag.Parsed() {
|
if !flag.Parsed() {
|
||||||
@ -23,7 +22,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if *runAsClient {
|
if *runAsClient {
|
||||||
hc, err := client.NewClient("192.168.9.32:8080", "http://192.168.9.32/commandcenter")
|
hc, err := client.NewClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -1,9 +1,12 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||||
"repositories.action2quare.com/ayo/houston/shared"
|
"repositories.action2quare.com/ayo/houston/shared"
|
||||||
"repositories.action2quare.com/ayo/houston/shared/protos"
|
"repositories.action2quare.com/ayo/houston/shared/protos"
|
||||||
|
|
||||||
@ -12,11 +15,19 @@ import (
|
|||||||
|
|
||||||
// protoc --go_out=. --go-grpc_out=. protos/*.proto
|
// protoc --go_out=. --go-grpc_out=. protos/*.proto
|
||||||
type HoustonServer interface {
|
type HoustonServer interface {
|
||||||
Start(port int) error
|
Start() error
|
||||||
Stop()
|
Stop()
|
||||||
Operation() Operation
|
Operation() Operation
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type serverConfig struct {
|
||||||
|
GrpcPort int `json:"grpc_port"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type totalConfig struct {
|
||||||
|
Server serverConfig `json:"houston_server"`
|
||||||
|
}
|
||||||
|
|
||||||
type DeployRequest struct {
|
type DeployRequest struct {
|
||||||
shared.DeployRequest
|
shared.DeployRequest
|
||||||
hostnames []string
|
hostnames []string
|
||||||
@ -100,6 +111,15 @@ type Operation interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewServer() HoustonServer {
|
func NewServer() HoustonServer {
|
||||||
|
port := 8080
|
||||||
|
if bt, err := os.ReadFile("config.json"); err == nil {
|
||||||
|
var config totalConfig
|
||||||
|
if err := json.Unmarshal(bt, &config); err == nil {
|
||||||
|
if config.Server.GrpcPort == 0 {
|
||||||
|
port = config.Server.GrpcPort
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var opts []grpc.ServerOption
|
var opts []grpc.ServerOption
|
||||||
grpcServer := grpc.NewServer(opts...)
|
grpcServer := grpc.NewServer(opts...)
|
||||||
@ -113,6 +133,7 @@ func NewServer() HoustonServer {
|
|||||||
rpcServer: grpcServer,
|
rpcServer: grpcServer,
|
||||||
os: os,
|
os: os,
|
||||||
ms: ms,
|
ms: ms,
|
||||||
|
port: port,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,10 +141,12 @@ type houstonServer struct {
|
|||||||
rpcServer *grpc.Server
|
rpcServer *grpc.Server
|
||||||
os *operationServer
|
os *operationServer
|
||||||
ms *monitorServer
|
ms *monitorServer
|
||||||
|
port int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hs *houstonServer) Start(port int) error {
|
func (hs *houstonServer) Start() error {
|
||||||
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
|
logger.Println("houston server is started at port", hs.port)
|
||||||
|
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", hs.port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user