diff --git a/client/client.go b/client/client.go index 76b3169..c702181 100644 --- a/client/client.go +++ b/client/client.go @@ -2,7 +2,10 @@ package client import ( "context" + "encoding/json" + "errors" "io" + "io/fs" "os" "os/exec" "os/signal" @@ -27,6 +30,15 @@ import ( 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 { SetReportMetrics(map[string]float32) Shutdown() @@ -152,8 +164,25 @@ func (hc *houstonClient) makeOperationQueryRequest() *protos.OperationQueryReque } } -func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) { - client, err := grpc.Dial(grpcAddr, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials())) +func NewClient() (HoustonClient, error) { + 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 { return nil, err } @@ -184,7 +213,7 @@ func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) { client: client, extraMetrics: unsafe.Pointer(&map[string]float32{}), deploys: deploys, - httpAddr: httpAddr, + httpAddr: config.Client.HttpAddress, timestamp: exefi.ModTime().String(), } @@ -262,7 +291,11 @@ func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) { err := hc.withdraw(&wr) if err == nil { prog := gatherDeployedPrograms(wr.Name) - hc.deploys[wr.Name] = prog + if len(prog) == 0 { + delete(hc.deploys, wr.Name) + } else { + hc.deploys[wr.Name] = prog + } op.Refresh(ctx, hc.makeOperationQueryRequest()) } else { logger.Println(err) diff --git a/config_template.json b/config_template.json index 9e26dfe..f3c021a 100644 --- a/config_template.json +++ b/config_template.json @@ -1 +1,9 @@ -{} \ No newline at end of file +{ + "houston_client" : { + "grpc_server_address" : "192.168.9.32:8080", + "http_server_address" : "http://192.168.9.32/commandcenter" + }, + "houston_server" : { + "grpc_port" : 8080 + } +} diff --git a/main.go b/main.go index fbe1d2f..5b86b08 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( var runAsClient = flag.Bool("client", false, "") var runAsServer = flag.Bool("server", false, "") -var port = flag.Int("port", 8080, "") func main() { if !flag.Parsed() { @@ -23,7 +22,7 @@ func main() { } if *runAsClient { - hc, err := client.NewClient("192.168.9.32:8080", "http://192.168.9.32/commandcenter") + hc, err := client.NewClient() if err != nil { logger.Fatal(err) return diff --git a/server/server.go b/server/server.go index 76312f1..5a259ce 100644 --- a/server/server.go +++ b/server/server.go @@ -1,9 +1,12 @@ package server import ( + "encoding/json" "fmt" "net" + "os" + "repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/houston/shared" "repositories.action2quare.com/ayo/houston/shared/protos" @@ -12,11 +15,19 @@ import ( // protoc --go_out=. --go-grpc_out=. protos/*.proto type HoustonServer interface { - Start(port int) error + Start() error Stop() Operation() Operation } +type serverConfig struct { + GrpcPort int `json:"grpc_port"` +} + +type totalConfig struct { + Server serverConfig `json:"houston_server"` +} + type DeployRequest struct { shared.DeployRequest hostnames []string @@ -100,6 +111,15 @@ type Operation interface { } 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 grpcServer := grpc.NewServer(opts...) @@ -113,6 +133,7 @@ func NewServer() HoustonServer { rpcServer: grpcServer, os: os, ms: ms, + port: port, } } @@ -120,10 +141,12 @@ type houstonServer struct { rpcServer *grpc.Server os *operationServer ms *monitorServer + port int } -func (hs *houstonServer) Start(port int) error { - lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port)) +func (hs *houstonServer) Start() error { + 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 { return err }