mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 14:25:16 +03:00
Merge pull request #3 from lorsanstand/feat/docker-system-client
Feat/docker system client
This commit is contained in:
+32
-32
@@ -70,10 +70,7 @@ type RegisterAgentRequest struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
AgentId string `protobuf:"bytes,1,opt,name=agent_id,json=agentId,proto3" json:"agent_id,omitempty"`
|
||||
AgentName string `protobuf:"bytes,2,opt,name=agent_name,json=agentName,proto3" json:"agent_name,omitempty"`
|
||||
Hostname string `protobuf:"bytes,3,opt,name=hostname,proto3" json:"hostname,omitempty"`
|
||||
Version string `protobuf:"bytes,4,opt,name=version,proto3" json:"version,omitempty"`
|
||||
Arch string `protobuf:"bytes,5,opt,name=arch,proto3" json:"arch,omitempty"`
|
||||
Config *AgentConfig `protobuf:"bytes,6,opt,name=config,proto3" json:"config,omitempty"`
|
||||
Config *AgentConfig `protobuf:"bytes,3,opt,name=config,proto3" json:"config,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -122,27 +119,6 @@ func (x *RegisterAgentRequest) GetAgentName() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterAgentRequest) GetHostname() string {
|
||||
if x != nil {
|
||||
return x.Hostname
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterAgentRequest) GetVersion() string {
|
||||
if x != nil {
|
||||
return x.Version
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterAgentRequest) GetArch() string {
|
||||
if x != nil {
|
||||
return x.Arch
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RegisterAgentRequest) GetConfig() *AgentConfig {
|
||||
if x != nil {
|
||||
return x.Config
|
||||
@@ -154,6 +130,9 @@ type AgentConfig struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
System string `protobuf:"bytes,1,opt,name=system,proto3" json:"system,omitempty"`
|
||||
Docker bool `protobuf:"varint,2,opt,name=docker,proto3" json:"docker,omitempty"`
|
||||
Hostname string `protobuf:"bytes,3,opt,name=hostname,proto3" json:"hostname,omitempty"`
|
||||
Version string `protobuf:"bytes,4,opt,name=version,proto3" json:"version,omitempty"`
|
||||
Arch string `protobuf:"bytes,5,opt,name=arch,proto3" json:"arch,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -202,6 +181,27 @@ func (x *AgentConfig) GetDocker() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (x *AgentConfig) GetHostname() string {
|
||||
if x != nil {
|
||||
return x.Hostname
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *AgentConfig) GetVersion() string {
|
||||
if x != nil {
|
||||
return x.Version
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *AgentConfig) GetArch() string {
|
||||
if x != nil {
|
||||
return x.Arch
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type RegisterAgentResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
HeartbeatIntervalSecond int64 `protobuf:"varint,1,opt,name=heartbeat_interval_second,json=heartbeatIntervalSecond,proto3" json:"heartbeat_interval_second,omitempty"`
|
||||
@@ -252,18 +252,18 @@ const file_homeops_hub_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x11homeops/hub.proto\x1a\x1bgoogle/protobuf/empty.proto\"\"\n" +
|
||||
"\fPongResponse\x12\x12\n" +
|
||||
"\x04pong\x18\x01 \x01(\tR\x04pong\"\xc0\x01\n" +
|
||||
"\x04pong\x18\x01 \x01(\tR\x04pong\"v\n" +
|
||||
"\x14RegisterAgentRequest\x12\x19\n" +
|
||||
"\bagent_id\x18\x01 \x01(\tR\aagentId\x12\x1d\n" +
|
||||
"\n" +
|
||||
"agent_name\x18\x02 \x01(\tR\tagentName\x12\x1a\n" +
|
||||
"\bhostname\x18\x03 \x01(\tR\bhostname\x12\x18\n" +
|
||||
"\aversion\x18\x04 \x01(\tR\aversion\x12\x12\n" +
|
||||
"\x04arch\x18\x05 \x01(\tR\x04arch\x12$\n" +
|
||||
"\x06config\x18\x06 \x01(\v2\f.AgentConfigR\x06config\"=\n" +
|
||||
"agent_name\x18\x02 \x01(\tR\tagentName\x12$\n" +
|
||||
"\x06config\x18\x03 \x01(\v2\f.AgentConfigR\x06config\"\x87\x01\n" +
|
||||
"\vAgentConfig\x12\x16\n" +
|
||||
"\x06system\x18\x01 \x01(\tR\x06system\x12\x16\n" +
|
||||
"\x06docker\x18\x02 \x01(\bR\x06docker\"S\n" +
|
||||
"\x06docker\x18\x02 \x01(\bR\x06docker\x12\x1a\n" +
|
||||
"\bhostname\x18\x03 \x01(\tR\bhostname\x12\x18\n" +
|
||||
"\aversion\x18\x04 \x01(\tR\aversion\x12\x12\n" +
|
||||
"\x04arch\x18\x05 \x01(\tR\x04arch\"S\n" +
|
||||
"\x15RegisterAgentResponse\x12:\n" +
|
||||
"\x19heartbeat_interval_second\x18\x01 \x01(\x03R\x17heartbeatIntervalSecond2x\n" +
|
||||
"\x03Hub\x12/\n" +
|
||||
|
||||
@@ -16,15 +16,15 @@ message PongResponse {
|
||||
message RegisterAgentRequest {
|
||||
string agent_id = 1;
|
||||
string agent_name = 2;
|
||||
string hostname = 3;
|
||||
string version = 4;
|
||||
string arch = 5;
|
||||
AgentConfig config = 6;
|
||||
AgentConfig config = 3;
|
||||
}
|
||||
|
||||
message AgentConfig {
|
||||
string system = 1;
|
||||
bool docker = 2;
|
||||
string hostname = 3;
|
||||
string version = 4;
|
||||
string arch = 5;
|
||||
}
|
||||
|
||||
message RegisterAgentResponse {
|
||||
|
||||
@@ -4,20 +4,28 @@ go 1.26.1
|
||||
|
||||
require (
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0
|
||||
github.com/moby/moby v28.5.2+incompatible
|
||||
github.com/rs/zerolog v1.35.0
|
||||
google.golang.org/grpc v1.80.0
|
||||
google.golang.org/protobuf v1.36.11
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.2.1 // indirect
|
||||
github.com/docker/docker v28.5.2+incompatible // indirect
|
||||
github.com/docker/go-connections v0.6.0 // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.1.0 // indirect
|
||||
golang.org/x/net v0.49.0 // indirect
|
||||
golang.org/x/sys v0.40.0 // indirect
|
||||
golang.org/x/text v0.33.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260120221211-b8f7ae30c516 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gotest.tools/v3 v3.5.2 // indirect
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
|
||||
)
|
||||
|
||||
@@ -2,6 +2,12 @@ github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak
|
||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/docker/docker v28.5.2+incompatible h1:DBX0Y0zAjZbSrm1uzOkdr1onVghKaftjlSWt4AFexzM=
|
||||
github.com/docker/docker v28.5.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
|
||||
github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE=
|
||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
@@ -20,6 +26,14 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
|
||||
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/moby v28.5.2+incompatible h1:hIn6qcenb3JY1E3STwqEbBvJ8bha+u1LpqjX4CBvNCk=
|
||||
github.com/moby/moby v28.5.2+incompatible/go.mod h1:fDXVQ6+S340veQPv35CzDahGBmHsiclFwfEygB/TWMc=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
|
||||
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
|
||||
github.com/rs/zerolog v1.35.0 h1:VD0ykx7HMiMJytqINBsKcbLS+BJ4WYjz+05us+LRTdI=
|
||||
github.com/rs/zerolog v1.35.0/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
@@ -53,5 +67,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
||||
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package docker_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/moby/moby/api/types"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
)
|
||||
|
||||
type dockerAPI interface {
|
||||
Ping(ctx context.Context) (types.Ping, error)
|
||||
ContainerList(ctx context.Context, opts container.ListOptions) ([]container.Summary, error)
|
||||
}
|
||||
|
||||
type DockerService struct {
|
||||
dockerClient dockerAPI
|
||||
}
|
||||
|
||||
func NewDockerService(api dockerAPI) *DockerService {
|
||||
return &DockerService{dockerClient: api}
|
||||
}
|
||||
|
||||
func (d *DockerService) CheckDockerDaemon(ctx context.Context) error {
|
||||
_, err := d.dockerClient.Ping(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DockerService) ContainersList(ctx context.Context) ([]container.Summary, error) {
|
||||
ContainersList, err := d.dockerClient.ContainerList(ctx, container.ListOptions{})
|
||||
return ContainersList, err
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package docker_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/api/types"
|
||||
"github.com/moby/moby/api/types/container"
|
||||
)
|
||||
|
||||
var testError error = errors.New("test")
|
||||
|
||||
type DockerMock struct {
|
||||
pingErr error
|
||||
containers []container.Summary
|
||||
containerErr error
|
||||
}
|
||||
|
||||
func (d DockerMock) Ping(ctx context.Context) (types.Ping, error) {
|
||||
return types.Ping{}, d.pingErr
|
||||
}
|
||||
|
||||
func (d DockerMock) ContainerList(ctx context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return d.containers, d.containerErr
|
||||
}
|
||||
|
||||
func TestCheckDockerDaemon(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mock DockerMock
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: nil,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "docker error",
|
||||
mock: DockerMock{
|
||||
pingErr: testError,
|
||||
containers: nil,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantErr: testError,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := NewDockerService(tt.mock)
|
||||
|
||||
err := svc.CheckDockerDaemon(context.Background())
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("expected error %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainersList(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
containers := []container.Summary{
|
||||
{ID: "123", Image: "postgres:latest"},
|
||||
{ID: "456", Image: "nginx:latest"},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
mock DockerMock
|
||||
wantLen int
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: containers,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantLen: len(containers),
|
||||
wantErr: nil,
|
||||
},
|
||||
{
|
||||
name: "docker error",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: nil,
|
||||
containerErr: testError,
|
||||
},
|
||||
wantLen: 0,
|
||||
wantErr: testError,
|
||||
},
|
||||
{
|
||||
name: "docker empty container",
|
||||
mock: DockerMock{
|
||||
pingErr: nil,
|
||||
containers: nil,
|
||||
containerErr: nil,
|
||||
},
|
||||
wantLen: 0,
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc := NewDockerService(tt.mock)
|
||||
|
||||
got, err := svc.ContainersList(context.Background())
|
||||
if !errors.Is(err, tt.wantErr) {
|
||||
t.Fatalf("expected error %v, got: %v", tt.wantErr, err)
|
||||
}
|
||||
|
||||
if tt.wantLen != len(got) {
|
||||
t.Fatalf("expected %d containers, got: %d", tt.wantLen, len(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1 +1,21 @@
|
||||
package hub_service
|
||||
|
||||
import (
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/rpc"
|
||||
"github.com/lorsanstand/HomeOps-Hub/internal/agent/service/docker_service"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type HubService struct {
|
||||
docker *docker_service.DockerService
|
||||
log zerolog.Logger
|
||||
hubConn *rpc.Connection
|
||||
}
|
||||
|
||||
func NewHubService(docker *docker_service.DockerService, log zerolog.Logger) *HubService {
|
||||
return &HubService{docker: docker, log: log}
|
||||
}
|
||||
|
||||
func (h *HubService) GatherInfoSystem() {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package hub_service
|
||||
|
||||
type AgentRegistrationData struct {
|
||||
AgentID string
|
||||
AgentName string
|
||||
Config AgentConfig
|
||||
}
|
||||
|
||||
type AgentConfig struct {
|
||||
System string
|
||||
Docker bool
|
||||
Hostname string
|
||||
Version string
|
||||
Arch string
|
||||
}
|
||||
Reference in New Issue
Block a user