feat: create save new agent in db

This commit is contained in:
2026-04-19 16:16:03 +03:00
parent 52766cf7e8
commit e289365ce8
14 changed files with 314 additions and 72 deletions
@@ -10,6 +10,8 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
const AgentVersion = "0.0"
type Collector interface { type Collector interface {
GatherInfoSystem() (domain.HostInfo, []domain.Capability) GatherInfoSystem() (domain.HostInfo, []domain.Capability)
} }
@@ -43,7 +45,7 @@ func (a *AgentService) RegisterAgentConn(ctx context.Context) {
info, caps := a.collect.GatherInfoSystem() info, caps := a.collect.GatherInfoSystem()
AgentID := a.settings.AgentID AgentID := a.settings.AgentID
AgentName := a.cfg.AppName AgentName := a.cfg.AppName
AgentData := domain.RegisterAgentRequest{AgentId: AgentID, AgentName: AgentName, Host: info, Capabilities: caps} AgentData := domain.RegisterAgentRequest{AgentId: AgentID, AgentName: AgentName, Host: info, Capabilities: caps, AgentVersion: AgentVersion}
data, err := a.conn.RegisterAgent(ctx, AgentData) data, err := a.conn.RegisterAgent(ctx, AgentData)
if err != nil { if err != nil {
@@ -0,0 +1,25 @@
package agent_service
import (
"context"
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
)
type CollectorMock struct {
host domain.HostInfo
caps []domain.Capability
}
func (c *CollectorMock) GatherInfoSystem() (domain.HostInfo, []domain.Capability) {
return c.host, c.caps
}
type ConnectionMock struct {
regAgentErr error
regResp domain.RegisterAgentResponse
}
func (c *ConnectionMock) RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) {
return c.regResp, c.regAgentErr
}
+17 -7
View File
@@ -36,55 +36,65 @@ func NewApp() *App {
func (a *App) Run() { func (a *App) Run() {
ctx := context.Background() ctx := context.Background()
a.log.Info().Str("host", a.cfg.DBHost).Int("port", a.cfg.DBPort).Msg("connecting to database")
migratePGConn, err := sql.Open("pgx", a.cfg.GetURLPostgres()) migratePGConn, err := sql.Open("pgx", a.cfg.GetURLPostgres())
if err != nil { if err != nil {
a.log.Error().Err(err).Msg("failed to connect to the database") a.log.Error().Err(err).Msg("failed to connect to the database for migrations")
return return
} }
defer migratePGConn.Close() defer migratePGConn.Close()
mgrt, err := migrator.NewMigrator(hubdir.MigrationsFS, "migrations") mgrt, err := migrator.NewMigrator(hubdir.MigrationsFS, "migrations")
if err != nil { if err != nil {
a.log.Error().Err(err).Msg("failed create migrator") a.log.Error().Err(err).Msg("failed to create migrator")
return return
} }
a.log.Info().Msg("applying database migrations")
if err = mgrt.ApplyMigrations(migratePGConn); err != nil { if err = mgrt.ApplyMigrations(migratePGConn); err != nil {
a.log.Error().Err(err).Msg("migrations were not applied") a.log.Error().Err(err).Msg("migrations failed to apply")
return return
} }
a.log.Info().Msg("migrations applied successfully")
migratePGConn.Close() migratePGConn.Close()
a.log.Info().Msg("creating database connection pool")
pool, err := pgxpool.New(ctx, a.cfg.GetURLPostgres()) pool, err := pgxpool.New(ctx, a.cfg.GetURLPostgres())
if err != nil { if err != nil {
a.log.Error().Err(err).Msg("failed create db pool") a.log.Error().Err(err).Msg("failed to create database connection pool")
return return
} }
defer pool.Close()
a.log.Info().Msg("database connection pool created")
hubStore := store.NewHubStore(pool) hubStore := store.NewHubStore(pool)
hubService := hub_service.NewHubService(hubStore, a.log) hubService := hub_service.NewHubService(hubStore, a.log)
a.log.Info().Msg("starting hub service")
err = a.hubServe(hubService) err = a.hubServe(hubService)
if err != nil { if err != nil {
a.log.Error().Err(err).Msg("failed to start the server") a.log.Error().Err(err).Msg("hub service failed to start")
return return
} }
} }
func (a *App) hubServe(hubService *hub_service.HubService) error { func (a *App) hubServe(hubService *hub_service.HubService) error {
address := fmt.Sprintf("0.0.0.0:%v", a.cfg.Port) address := fmt.Sprintf("0.0.0.0:%v", a.cfg.Port)
a.log.Info().Str("address", "http://"+address).Msg("start GRPC server") a.log.Info().Str("address", address).Msg("starting gRPC server")
server := grpcserv.NewHubHandler(hubService, a.log) server := grpcserv.NewHubHandler(hubService, a.log)
lis, err := net.Listen("tcp", address) lis, err := net.Listen("tcp", address)
if err != nil { if err != nil {
a.log.Error().Err(err).Str("address", address).Msg("failed to listen on address")
return err return err
} }
a.log.Info().Str("address", address).Msg("listening on address")
a.log.Info().Msg("gRPC server is running")
err = server.GrpcServer.Serve(lis) err = server.GrpcServer.Serve(lis)
if err != nil { if err != nil {
a.log.Error().Err(err).Msg("gRPC server error")
return err return err
} }
+29
View File
@@ -0,0 +1,29 @@
package domain
import (
"time"
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
)
type CreateAgentModel struct {
AgentID string
AgentName string
Architecture string
System string
Hostname string
Version string
Capabilities []domain.Capability
}
type AgentModel struct {
ID int
AgentID string
AgentName string
Architecture string
System string
Hostname string
Version string
Capabilities []domain.Capability
RegisteredAt time.Time
}
+8 -2
View File
@@ -33,12 +33,18 @@ func NewHubHandler(HubServ HubService, logger zerolog.Logger) *HubHandler {
} }
func (h *HubHandler) Ping(ctx context.Context, _ *emptypb.Empty) (*pb.PongResponse, error) { func (h *HubHandler) Ping(ctx context.Context, _ *emptypb.Empty) (*pb.PongResponse, error) {
h.log.Info().Msg("pong request") h.log.Debug().Msg("ping request received")
return &pb.PongResponse{Pong: "Pong"}, nil return &pb.PongResponse{Pong: "Pong"}, nil
} }
func (h *HubHandler) RegisterAgent(ctx context.Context, request *pb.RegisterAgentRequest) (*pb.RegisterAgentResponse, error) { func (h *HubHandler) RegisterAgent(ctx context.Context, request *pb.RegisterAgentRequest) (*pb.RegisterAgentResponse, error) {
h.log.Debug().Str("agentId", request.AgentId).Str("agentName", request.AgentName).Msg("register agent request received")
data := domain.ToDomainAgentRequest(request) data := domain.ToDomainAgentRequest(request)
resp, err := h.hub.RegisterAgent(ctx, data) resp, err := h.hub.RegisterAgent(ctx, data)
return domain.ToGRPCAgentResponse(resp), err if err != nil {
h.log.Error().Err(err).Str("agentId", request.AgentId).Msg("register agent request failed")
return domain.ToGRPCAgentResponse(resp), err
}
h.log.Debug().Str("agentId", resp.AgentID).Msg("register agent request completed")
return domain.ToGRPCAgentResponse(resp), nil
} }
+39 -22
View File
@@ -2,15 +2,20 @@ package hub_service
import ( import (
"context" "context"
"database/sql"
"errors"
"fmt"
"github.com/lorsanstand/HomeOps-Hub/internal/domain" "github.com/lorsanstand/HomeOps-Hub/internal/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store" domainHub "github.com/lorsanstand/HomeOps-Hub/internal/hub/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/utils/hasher" "github.com/lorsanstand/HomeOps-Hub/internal/hub/utils/hasher"
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
type Store interface { type Store interface {
NewAgent(ctx context.Context, agent store.Agent) error NewAgent(ctx context.Context, agent domainHub.CreateAgentModel) error
GetAgentByAgentID(ctx context.Context, AgentID string) (domainHub.AgentModel, error)
UpdateAgentByID(ctx context.Context, ID int, updateAgent domainHub.CreateAgentModel) error
} }
type HubService struct { type HubService struct {
@@ -23,31 +28,43 @@ func NewHubService(store Store, logger zerolog.Logger) *HubService {
} }
func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) { func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) {
h.log.Debug().Msg("registered new agent") h.log.Debug().Str("agentId", data.AgentId).Str("agentName", data.AgentName).Msg("started registering agent")
AgentID := data.AgentId agent, err := h.store.GetAgentByAgentID(ctx, data.AgentId)
if data.AgentId == "" { if err != nil && !errors.Is(err, sql.ErrNoRows) {
var err error h.log.Error().Err(err).Str("agentId", data.AgentId).Msg("failed to get agent from database")
AgentID, err = hasher.MakeID(data.Host, data.AgentName) return domain.RegisterAgentResponse{}, fmt.Errorf("failed select agent to db: %w", err)
if err != nil { }
h.log.Error().Err(err).Msg("failed create agent id")
AgentID = "" if data.AgentId != "" && !errors.Is(err, sql.ErrNoRows) {
h.log.Debug().Str("agentId", agent.AgentID).Str("agentName", data.AgentName).Msg("agent exists, updating")
data.AgentId = agent.AgentID
agentStore := toCreateAgentModel(data)
if err := h.store.UpdateAgentByID(ctx, agent.ID, agentStore); err != nil {
h.log.Error().Err(err).Str("agentId", agent.AgentID).Msg("failed to update agent in database")
return domain.RegisterAgentResponse{}, err
} }
h.log.Info().Str("agentId", agent.AgentID).Msg("agent updated successfully")
return domain.RegisterAgentResponse{AgentID: agent.AgentID, Heartbeat: 5}, nil
} }
agentStore := store.Agent{ AgentID, err := hasher.MakeID(data.Host, data.AgentName)
AgentID: AgentID, if err != nil {
AgentName: data.AgentName, h.log.Error().Err(err).Str("agentName", data.AgentName).Str("hostname", data.Host.Hostname).Msg("failed to generate agent id")
Architecture: data.Host.Arch,
System: data.Host.System,
Hostname: data.Host.Hostname,
Version: data.AgentVersion,
Capabilities: data.Capabilities,
}
if err := h.store.NewAgent(ctx, agentStore); err != nil {
h.log.Warn().Err(err).Msg("failed add new agent in db")
return domain.RegisterAgentResponse{}, err return domain.RegisterAgentResponse{}, err
} }
data.AgentId = AgentID
agentStore := toCreateAgentModel(data)
if err := h.store.NewAgent(ctx, agentStore); err != nil {
h.log.Error().Err(err).Str("agentId", AgentID).Str("agentName", data.AgentName).Msg("failed to create new agent in database")
return domain.RegisterAgentResponse{}, err
}
h.log.Info().Str("agentId", AgentID).Str("agentName", data.AgentName).Str("hostname", data.Host.Hostname).Msg("agent registered successfully")
return domain.RegisterAgentResponse{AgentID: AgentID, Heartbeat: 5}, nil return domain.RegisterAgentResponse{AgentID: AgentID, Heartbeat: 5}, nil
} }
@@ -0,0 +1,18 @@
package hub_service
import (
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
domainHub "github.com/lorsanstand/HomeOps-Hub/internal/hub/domain"
)
func toCreateAgentModel(agent domain.RegisterAgentRequest) domainHub.CreateAgentModel {
return domainHub.CreateAgentModel{
AgentID: agent.AgentId,
AgentName: agent.AgentName,
Architecture: agent.Host.Arch,
System: agent.Host.System,
Hostname: agent.Host.Hostname,
Version: agent.AgentVersion,
Capabilities: agent.Capabilities,
}
}
+69
View File
@@ -0,0 +1,69 @@
package store
import (
"encoding/json"
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
domainHub "github.com/lorsanstand/HomeOps-Hub/internal/hub/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen"
)
func toDBAgent(agent domainHub.CreateAgentModel) gen.CreateAgentParams {
return gen.CreateAgentParams{
AgentID: agent.AgentID,
AgentName: &agent.AgentName,
Architecture: agent.Architecture,
System: agent.System,
Hostname: agent.Hostname,
Version: agent.Version,
Capabilities: toJsonCapabilities(agent.Capabilities),
}
}
func toUpdateDBAgent(agent domainHub.CreateAgentModel) gen.UpdateAgentByIDParams {
return gen.UpdateAgentByIDParams{
AgentID: agent.AgentID,
AgentName: &agent.AgentName,
Architecture: agent.Architecture,
System: agent.System,
Hostname: agent.Hostname,
Version: agent.Version,
Capabilities: toJsonCapabilities(agent.Capabilities),
}
}
func toJsonCapabilities(caps []domain.Capability) []byte {
data, err := json.Marshal(caps)
if err != nil {
// Note: Error is silently handled - consider logging in production
return []byte{}
}
return data
}
func toAgentModel(dbAgent gen.Agent) domainHub.AgentModel {
var dbAgentName string
if dbAgent.AgentName != nil {
dbAgentName = *dbAgent.AgentName
}
return domainHub.AgentModel{
ID: int(dbAgent.ID),
AgentID: dbAgent.AgentID,
AgentName: dbAgentName,
Architecture: dbAgent.Architecture,
System: dbAgent.System,
Hostname: dbAgent.Hostname,
Capabilities: toDomainCapabilities(dbAgent.Capabilities),
}
}
func toDomainCapabilities(caps []byte) []domain.Capability {
var capabilities []domain.Capability
err := json.Unmarshal(caps, &capabilities)
if err != nil {
// Note: Error is silently handled - consider logging in production
return []domain.Capability{}
}
return capabilities
}
+75
View File
@@ -36,3 +36,78 @@ func (q *Queries) CreateAgent(ctx context.Context, arg CreateAgentParams) error
) )
return err return err
} }
const getAgentByAgentID = `-- name: GetAgentByAgentID :one
SELECT id, agent_id, agent_name, architecture, system, hostname, version, capabilities, registered_at from agents
WHERE agent_id=$1
`
func (q *Queries) GetAgentByAgentID(ctx context.Context, agentID string) (Agent, error) {
row := q.db.QueryRow(ctx, getAgentByAgentID, agentID)
var i Agent
err := row.Scan(
&i.ID,
&i.AgentID,
&i.AgentName,
&i.Architecture,
&i.System,
&i.Hostname,
&i.Version,
&i.Capabilities,
&i.RegisteredAt,
)
return i, err
}
const getAgentByID = `-- name: GetAgentByID :one
SELECT id, agent_id, agent_name, architecture, system, hostname, version, capabilities, registered_at from agents
WHERE id=$1
`
func (q *Queries) GetAgentByID(ctx context.Context, id int32) (Agent, error) {
row := q.db.QueryRow(ctx, getAgentByID, id)
var i Agent
err := row.Scan(
&i.ID,
&i.AgentID,
&i.AgentName,
&i.Architecture,
&i.System,
&i.Hostname,
&i.Version,
&i.Capabilities,
&i.RegisteredAt,
)
return i, err
}
const updateAgentByID = `-- name: UpdateAgentByID :exec
UPDATE agents
SET agent_id=$1, agent_name=$2, architecture=$3, system=$4, hostname=$5, version=$6, capabilities=$7
WHERE id=$8
`
type UpdateAgentByIDParams struct {
AgentID string
AgentName *string
Architecture string
System string
Hostname string
Version string
Capabilities []byte
ID int32
}
func (q *Queries) UpdateAgentByID(ctx context.Context, arg UpdateAgentByIDParams) error {
_, err := q.db.Exec(ctx, updateAgentByID,
arg.AgentID,
arg.AgentName,
arg.Architecture,
arg.System,
arg.Hostname,
arg.Version,
arg.Capabilities,
arg.ID,
)
return err
}
+1 -1
View File
@@ -9,7 +9,7 @@ import (
) )
type Agent struct { type Agent struct {
ID int64 ID int32
AgentID string AgentID string
AgentName *string AgentName *string
Architecture string Architecture string
+14 -1
View File
@@ -1,3 +1,16 @@
-- name: CreateAgent :exec -- name: CreateAgent :exec
INSERT INTO agents (agent_id, agent_name, architecture, system, hostname, version, capabilities) INSERT INTO agents (agent_id, agent_name, architecture, system, hostname, version, capabilities)
VALUES ($1, $2, $3, $4, $5, $6, $7); VALUES ($1, $2, $3, $4, $5, $6, $7);
-- name: GetAgentByID :one
SELECT * from agents
WHERE id=$1;
-- name: GetAgentByAgentID :one
SELECT * from agents
WHERE agent_id=$1;
-- name: UpdateAgentByID :exec
UPDATE agents
SET agent_id=$1, agent_name=$2, architecture=$3, system=$4, hostname=$5, version=$6, capabilities=$7
WHERE id=$8;
+16 -1
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
domainHub "github.com/lorsanstand/HomeOps-Hub/internal/hub/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen" "github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen"
) )
@@ -16,6 +17,20 @@ func NewHubStore(db *pgxpool.Pool) *HubStore {
return &HubStore{queries} return &HubStore{queries}
} }
func (h *HubStore) NewAgent(ctx context.Context, agent Agent) error { func (h *HubStore) NewAgent(ctx context.Context, agent domainHub.CreateAgentModel) error {
return h.queries.CreateAgent(ctx, toDBAgent(agent)) return h.queries.CreateAgent(ctx, toDBAgent(agent))
} }
func (h *HubStore) GetAgentByAgentID(ctx context.Context, AgentID string) (domainHub.AgentModel, error) {
data, err := h.queries.GetAgentByAgentID(ctx, AgentID)
if err != nil {
return domainHub.AgentModel{}, err
}
return toAgentModel(data), nil
}
func (h *HubStore) UpdateAgentByID(ctx context.Context, ID int, updateAgent domainHub.CreateAgentModel) error {
data := toUpdateDBAgent(updateAgent)
data.ID = int32(ID)
return h.queries.UpdateAgentByID(ctx, data)
}
-37
View File
@@ -1,37 +0,0 @@
package store
import (
"encoding/json"
"github.com/lorsanstand/HomeOps-Hub/internal/domain"
"github.com/lorsanstand/HomeOps-Hub/internal/hub/store/sqlc/gen"
)
type Agent struct {
AgentID string
AgentName string
Architecture string
System string
Hostname string
Version string
Capabilities []domain.Capability
}
func toDBAgent(agent Agent) gen.CreateAgentParams {
return gen.CreateAgentParams{
AgentID: agent.AgentID,
AgentName: &agent.AgentName,
Architecture: agent.Architecture,
System: agent.System,
Version: agent.Version,
Capabilities: toJsonCapabilities(agent.Capabilities),
}
}
func toJsonCapabilities(caps []domain.Capability) []byte {
data, err := json.Marshal(caps)
if err != nil {
return []byte{}
}
return data
}