mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 15:35:17 +03:00
create register agent in agent
This commit is contained in:
+6
@@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<profile version="1.0">
|
||||
<option name="myName" value="Project Default" />
|
||||
<inspection_tool class="SqlNoDataSourceInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
||||
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AutoImportSettings">
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="abe4019d-78ad-4b09-9aa4-c503fa264179" name="Changes" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="FileTemplateManagerImpl">
|
||||
<option name="RECENT_TEMPLATES">
|
||||
<list>
|
||||
<option value="Go File" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="GOROOT" url="file:///usr/lib/go" />
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectColorInfo">{
|
||||
"associatedIndex": 6
|
||||
}</component>
|
||||
<component name="ProjectId" id="3Bz7LjOuV8KsTCrmtXpgkPdosIV" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"DefaultGoTemplateProperty": "Go File",
|
||||
"ModuleVcsDetector.initialDetectionPerformed": "true",
|
||||
"RunOnceActivity.GoLinterPluginOnboardingV2": "true",
|
||||
"RunOnceActivity.GoLinterPluginStorageMigration": "true",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
|
||||
"RunOnceActivity.git.unshallow": "true",
|
||||
"RunOnceActivity.go.analysis.ui.options.defaults": "true",
|
||||
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
||||
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
||||
"RunOnceActivity.typescript.service.memoryLimit.init": "true",
|
||||
"git-widget-placeholder": "feat/register-agent",
|
||||
"go.sdk.automatically.set": "true",
|
||||
"last_opened_file_path": "/home/lorsan/projects/HomeOps-Hub",
|
||||
"node.js.detected.package.eslint": "true",
|
||||
"node.js.selected.package.eslint": "(autodetect)",
|
||||
"nodejs_package_manager_path": "npm"
|
||||
}
|
||||
}]]></component>
|
||||
<component name="SharedIndexes">
|
||||
<attachedChunks>
|
||||
<set>
|
||||
<option value="bundled-gosdk-72a9cf600ed8-dfa3e7267ae0-org.jetbrains.plugins.go.sharedIndexes.bundled-GO-253.30387.193" />
|
||||
<option value="bundled-js-predefined-d6986cc7102b-9b0f141eb926-JavaScript-GO-253.30387.193" />
|
||||
</set>
|
||||
</attachedChunks>
|
||||
</component>
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="abe4019d-78ad-4b09-9aa4-c503fa264179" name="Changes" comment="" />
|
||||
<created>1775479991162</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1775479991162</updated>
|
||||
</task>
|
||||
<servers />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
<option name="version" value="3" />
|
||||
</component>
|
||||
</project>
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import "github.com/lorsanstand/HomeOps-Hub/internal/hub/app"
|
||||
import "github.com/lorsanstand/HomeOps-Hub/internal/agent/app"
|
||||
|
||||
func main() {
|
||||
start := app.NewApp()
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -30,3 +30,7 @@ func (h *HubHandler) Ping(ctx context.Context, _ *emptypb.Empty) (*pb.PongRespon
|
||||
h.log.Info().Msg("pong request")
|
||||
return &pb.PongResponse{Pong: "Pong"}, nil
|
||||
}
|
||||
|
||||
func (h *HubHandler) RegisterAgent(ctx context.Context, request *pb.RegisterAgentRequest) (*pb.RegisterAgentResponse, error) {
|
||||
return &pb.RegisterAgentResponse{AgentId: "12234", HeartbeatIntervalSecond: 2}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user