mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 20:05:17 +03:00
create register agent in agent
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
standartlog "log"
|
||||
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/rpc"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/service/agent_service"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/service/collector"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/service/docker_service"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/utils/config_yaml"
|
||||
log2 "github.com/lorsanstand/HomeOps-Hub/internal/shared/log"
|
||||
"github.com/rs/zerolog"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
@@ -20,6 +23,7 @@ type App struct {
|
||||
}
|
||||
|
||||
func NewApp() *App {
|
||||
|
||||
cfg, err := config_yaml.NewConfig()
|
||||
if err != nil {
|
||||
standartlog.Fatalf("failed get config: %v", err)
|
||||
@@ -31,7 +35,9 @@ func NewApp() *App {
|
||||
}
|
||||
|
||||
func (a *App) Run() {
|
||||
GRPCConn, err := grpc.NewClient(a.cfg.GetGRPCAddress())
|
||||
ctx := context.Background()
|
||||
|
||||
GRPCConn, err := grpc.NewClient(a.cfg.GetGRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msg("failed to get hub connections")
|
||||
return
|
||||
@@ -50,7 +56,8 @@ func (a *App) Run() {
|
||||
DockerService = docker_service.NewDockerService(DockerClient, a.log)
|
||||
}
|
||||
|
||||
Collector := collector.NewCollector(DockerService, a.log)
|
||||
collect := collector.NewCollector(DockerService, a.log)
|
||||
|
||||
Collector.GatherInfoSystem()
|
||||
agent := agent_service.NewAgentService(collect, conn, "", a.cfg, a.log)
|
||||
agent.RegisterAgentConn(ctx)
|
||||
}
|
||||
|
||||
@@ -33,6 +33,10 @@ func toGRPCCapability(caps []domain.Capability) []*pb.Capability {
|
||||
}
|
||||
|
||||
func toAgentRegisterDataResponse(response *pb.RegisterAgentResponse) domain.RegisterAgentDataResponse {
|
||||
if response == nil {
|
||||
return domain.RegisterAgentDataResponse{}
|
||||
}
|
||||
|
||||
return domain.RegisterAgentDataResponse{
|
||||
AgentID: response.AgentId,
|
||||
Heartbeat: int(response.HeartbeatIntervalSecond),
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package agent_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/domain"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/utils/config_yaml"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type Collector interface {
|
||||
GatherInfoSystem() (domain.HostInfo, []domain.Capability)
|
||||
}
|
||||
|
||||
type HubConnection interface {
|
||||
RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentData) (domain.RegisterAgentDataResponse, error)
|
||||
}
|
||||
|
||||
type AgentService struct {
|
||||
collect Collector
|
||||
conn HubConnection
|
||||
log zerolog.Logger
|
||||
cfg *config_yaml.AgentConfig
|
||||
heartBeat int
|
||||
agentID string
|
||||
}
|
||||
|
||||
func NewAgentService(
|
||||
collector Collector,
|
||||
conn HubConnection,
|
||||
AgentID string,
|
||||
cfg *config_yaml.AgentConfig,
|
||||
logger zerolog.Logger,
|
||||
) *AgentService {
|
||||
logger = logger.With().Str("component", "agent.service.agent_serivce").Logger()
|
||||
|
||||
return &AgentService{collect: collector, conn: conn, cfg: cfg, log: logger, agentID: AgentID}
|
||||
}
|
||||
|
||||
func (a *AgentService) RegisterAgentConn(ctx context.Context) {
|
||||
info, caps := a.collect.GatherInfoSystem()
|
||||
AgentID := a.agentID
|
||||
AgentName := a.cfg.AppName
|
||||
AgentData := domain.RegisterAgentData{AgentId: AgentID, AgentName: AgentName, Host: info, Capabilities: caps}
|
||||
|
||||
data, err := a.conn.RegisterAgent(ctx, AgentData)
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msg("failed register agent")
|
||||
return
|
||||
}
|
||||
fmt.Println(data)
|
||||
}
|
||||
@@ -18,7 +18,7 @@ type AgentConfig struct {
|
||||
}
|
||||
|
||||
func NewConfig() (*AgentConfig, error) {
|
||||
yamlFile, err := os.ReadFile("config.yaml")
|
||||
yamlFile, err := os.ReadFile("agent.dev.yaml")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed open file: %v", err)
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func (c *AgentConfig) GetLogLevel() zerolog.Level {
|
||||
}
|
||||
|
||||
func (c *AgentConfig) GetMode() string {
|
||||
return "PROD"
|
||||
return "DEV"
|
||||
}
|
||||
|
||||
func (c *AgentConfig) GetGRPCAddress() string {
|
||||
|
||||
Reference in New Issue
Block a user