Skip to content

Commit 19153e8

Browse files
henrybarretogustavosbarreto
authored andcommitted
chore(agent): move agent's code from pkg to it own package
1 parent 5cc0a45 commit 19153e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+165
-146
lines changed

pkg/agent/agent.go renamed to agent/agent.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// }
3737
//
3838
// [ShellHub Agent]: https://github.com/shellhub-io/shellhub/tree/master/agent
39-
package agent
39+
package main
4040

4141
import (
4242
"context"
@@ -59,32 +59,17 @@ import (
5959
dockerclient "github.com/docker/docker/client"
6060
"github.com/labstack/echo/v4"
6161
"github.com/pkg/errors"
62-
"github.com/shellhub-io/shellhub/pkg/agent/pkg/keygen"
63-
"github.com/shellhub-io/shellhub/pkg/agent/pkg/sysinfo"
64-
"github.com/shellhub-io/shellhub/pkg/agent/pkg/tunnel"
65-
"github.com/shellhub-io/shellhub/pkg/agent/server"
62+
"github.com/shellhub-io/shellhub/agent/pkg/keygen"
63+
"github.com/shellhub-io/shellhub/agent/pkg/sysinfo"
64+
"github.com/shellhub-io/shellhub/agent/pkg/tunnel"
65+
"github.com/shellhub-io/shellhub/agent/server"
6666
"github.com/shellhub-io/shellhub/pkg/api/client"
6767
"github.com/shellhub-io/shellhub/pkg/envs"
6868
"github.com/shellhub-io/shellhub/pkg/models"
6969
"github.com/shellhub-io/shellhub/pkg/validator"
7070
log "github.com/sirupsen/logrus"
7171
)
7272

73-
// AgentVersion store the version to be embed inside the binary. This is
74-
// injected using `-ldflags` build option.
75-
//
76-
// go build -ldflags "-X main.AgentVersion=1.2.3"
77-
//
78-
// If set to `latest`, the auto-updating mechanism is disabled. This is intended
79-
// to be used during development only.
80-
var AgentVersion string
81-
82-
// AgentPlatform stores what platform the agent is running on. This is injected in build time in the [ShellHub Agent]
83-
// implementation.
84-
//
85-
// [ShellHub Agent]: https://github.com/shellhub-io/shellhub/tree/master/agent
86-
var AgentPlatform string
87-
8873
// Config provides the configuration for the agent service.
8974
type Config struct {
9075
// Set the ShellHub Cloud server address the agent will use to connect.

pkg/agent/agent_test.go renamed to agent/agent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package agent
1+
package main
22

33
import (
44
"testing"

pkg/agent/connector/docker.go renamed to agent/connector.go

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package connector
1+
package main
22

33
import (
44
"context"
@@ -12,14 +12,14 @@ import (
1212
"github.com/docker/docker/api/types/events"
1313
"github.com/docker/docker/api/types/filters"
1414
dockerclient "github.com/docker/docker/client"
15-
"github.com/shellhub-io/shellhub/pkg/agent"
15+
"github.com/shellhub-io/shellhub/agent/pkg/connector"
1616
"github.com/shellhub-io/shellhub/pkg/api/client"
1717
"github.com/shellhub-io/shellhub/pkg/envs"
1818
"github.com/shellhub-io/shellhub/pkg/validator"
1919
log "github.com/sirupsen/logrus"
2020
)
2121

22-
var _ Connector = new(DockerConnector)
22+
var _ connector.Connector = new(DockerConnector)
2323

2424
// DockerConnector is a struct that represents a connector that uses Docker as the container runtime.
2525
type DockerConnector struct {
@@ -39,8 +39,8 @@ type DockerConnector struct {
3939
cancels map[string]context.CancelFunc
4040
}
4141

42-
// Config provides the configuration for the agent connector service.
43-
type Config struct {
42+
// ConfigConnector provides the configuration for the agent connector service.
43+
type ConfigConnector struct {
4444
// Set the ShellHub server address the agent will use to connect.
4545
// This is required.
4646
ServerAddress string `env:"SERVER_ADDRESS,required"`
@@ -64,8 +64,8 @@ type Config struct {
6464
Label string `env:"CONNECTOR_LABEL,default="`
6565
}
6666

67-
func LoadConfigFromEnv() (*Config, map[string]interface{}, error) {
68-
cfg, err := envs.ParseWithPrefix[Config]("SHELLHUB_")
67+
func LoadConfigConnectorFromEnv() (*ConfigConnector, map[string]interface{}, error) {
68+
cfg, err := envs.ParseWithPrefix[ConfigConnector]("SHELLHUB_")
6969
if err != nil {
7070
log.Fatal(err)
7171
}
@@ -80,7 +80,7 @@ func LoadConfigFromEnv() (*Config, map[string]interface{}, error) {
8080
return cfg, nil, nil
8181
}
8282

83-
func NewDockerConnectorWithClient(cli *dockerclient.Client, config *Config) Connector {
83+
func NewDockerConnectorWithClient(cli *dockerclient.Client, config *ConfigConnector) connector.Connector {
8484
return &DockerConnector{
8585
cli: cli,
8686
server: config.ServerAddress,
@@ -92,7 +92,7 @@ func NewDockerConnectorWithClient(cli *dockerclient.Client, config *Config) Conn
9292
}
9393

9494
// NewDockerConnector creates a new [Connector] that uses Docker as the container runtime.
95-
func NewDockerConnector(config *Config) (Connector, error) {
95+
func NewDockerConnector(config *ConfigConnector) (connector.Connector, error) {
9696
cli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithAPIVersionNegotiation())
9797
if err != nil {
9898
return nil, err
@@ -120,7 +120,7 @@ func (d *DockerConnector) events(ctx context.Context) (<-chan events.Message, <-
120120
})
121121
}
122122

123-
func (d *DockerConnector) List(ctx context.Context) ([]Container, error) {
123+
func (d *DockerConnector) List(ctx context.Context) ([]connector.Container, error) {
124124
filters := filters.NewArgs()
125125
if d.Label != "" {
126126
filters.Add("label", d.Label)
@@ -133,7 +133,7 @@ func (d *DockerConnector) List(ctx context.Context) ([]Container, error) {
133133
return nil, err
134134
}
135135

136-
list := make([]Container, len(containers))
136+
list := make([]connector.Container, len(containers))
137137
for i, container := range containers {
138138
list[i].ID = container.ID
139139

@@ -157,7 +157,7 @@ func (d *DockerConnector) Start(ctx context.Context, id string, name string) {
157157
d.mu.Unlock()
158158

159159
privateKey := fmt.Sprintf("%s/%s.key", d.privateKeys, id)
160-
go initContainerAgent(ctx, d.cli, Container{
160+
go initContainerAgent(ctx, d.cli, connector.Container{
161161
ID: id,
162162
Name: name,
163163
ServerAddress: d.server,
@@ -244,16 +244,16 @@ func (d *DockerConnector) CheckUpdate() (*semver.Version, error) {
244244
api, err := client.NewClient(d.server)
245245
if err != nil {
246246
log.WithError(err).WithFields(log.Fields{
247-
"version": ConnectorVersion,
247+
"version": connector.ConnectorVersion,
248248
}).Error("Failed to create HTTP client to check agent version")
249249

250250
return nil, err
251251
}
252252

253-
info, err := api.GetInfo(ConnectorVersion)
253+
info, err := api.GetInfo(connector.ConnectorVersion)
254254
if err != nil {
255255
log.WithError(err).WithFields(log.Fields{
256-
"version": ConnectorVersion,
256+
"version": connector.ConnectorVersion,
257257
}).Error("Failed to get info from ShellHub's server")
258258

259259
return nil, err
@@ -263,12 +263,12 @@ func (d *DockerConnector) CheckUpdate() (*semver.Version, error) {
263263
}
264264

265265
// initContainerAgent initializes the agent for a container.
266-
func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container Container) {
267-
agent.AgentPlatform = "connector"
268-
agent.AgentVersion = ConnectorVersion
266+
func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container connector.Container) {
267+
AgentPlatform = "connector"
268+
AgentVersion = connector.ConnectorVersion
269269

270-
// TODO: Let this configuration build next to the Agent [agent.LoadConfigFromEnv] function.
271-
cfg := &agent.Config{
270+
// TODO: Let this configuration build next to the Agent [agent.LoadConfigConnectorFromEnv] function.
271+
cfg := &Config{
272272
ServerAddress: container.ServerAddress,
273273
TenantID: container.Tenant,
274274
PrivateKey: container.PrivateKey,
@@ -285,10 +285,10 @@ func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container
285285
"tenant_id": cfg.TenantID,
286286
"server_address": cfg.ServerAddress,
287287
"timestamp": time.Now(),
288-
"version": agent.AgentVersion,
288+
"version": AgentVersion,
289289
}).Info("Connector container started")
290290

291-
mode, err := agent.NewConnectorMode(cli, container.ID)
291+
mode, err := NewConnectorMode(cli, container.ID)
292292
if err != nil {
293293
log.WithError(err).WithFields(log.Fields{
294294
"id": container.ID,
@@ -297,24 +297,24 @@ func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container
297297
"tenant_id": cfg.TenantID,
298298
"server_address": cfg.ServerAddress,
299299
"timestamp": time.Now(),
300-
"version": agent.AgentVersion,
300+
"version": AgentVersion,
301301
}).Fatal("Failed to create connector mode")
302302
}
303303

304-
ag, err := agent.NewAgentWithConfig(cfg, mode)
304+
ag, err := NewAgentWithConfig(cfg, mode)
305305
if err != nil {
306306
log.WithError(err).WithFields(log.Fields{
307307
"id": container.ID,
308308
"configuration": cfg,
309-
"version": agent.AgentVersion,
309+
"version": AgentVersion,
310310
}).Fatal("Failed to create agent")
311311
}
312312

313313
if err := ag.Initialize(); err != nil {
314314
log.WithError(err).WithFields(log.Fields{
315315
"id": container.ID,
316316
"configuration": cfg,
317-
"version": agent.AgentVersion,
317+
"version": AgentVersion,
318318
}).Fatal("Failed to initialize agent")
319319
}
320320

@@ -325,7 +325,7 @@ func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container
325325
"tenant_id": cfg.TenantID,
326326
"server_address": cfg.ServerAddress,
327327
"timestamp": time.Now(),
328-
"version": agent.AgentVersion,
328+
"version": AgentVersion,
329329
}).Info("Listening for connections")
330330

331331
// NOTICE(r): listing for connection and wait for a channel message to close the agent. It will receives
@@ -339,7 +339,7 @@ func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container
339339
"tenant_id": cfg.TenantID,
340340
"server_address": cfg.ServerAddress,
341341
"timestamp": time.Now(),
342-
"version": agent.AgentVersion,
342+
"version": AgentVersion,
343343
}).Fatal("Failed to listen for connections")
344344
}
345345

@@ -349,6 +349,6 @@ func initContainerAgent(ctx context.Context, cli *dockerclient.Client, container
349349
"hostname": cfg.PreferredHostname,
350350
"tenant_id": cfg.TenantID,
351351
"server_address": cfg.ServerAddress,
352-
"version": agent.AgentVersion,
352+
"version": AgentVersion,
353353
}).Info("Connector container done")
354354
}

agent/go.mod

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@ go 1.23.0
55
toolchain go1.24.6
66

77
require (
8+
github.com/GehirnInc/crypt v0.0.0-20230320061759-8cc1b52080c5
89
github.com/Masterminds/semver v1.5.0
10+
github.com/creack/pty v1.1.18
11+
github.com/docker/docker v28.0.0+incompatible
12+
github.com/gliderlabs/ssh v0.3.5
13+
github.com/go-playground/assert/v2 v2.2.0
14+
github.com/labstack/echo/v4 v4.10.2
15+
github.com/mattn/go-shellwords v1.0.12
16+
github.com/openwall/yescrypt-go v1.0.0
17+
github.com/pkg/errors v0.9.1
18+
github.com/pkg/sftp v1.13.5
919
github.com/shellhub-io/shellhub v0.13.4
1020
github.com/sirupsen/logrus v1.9.3
1121
github.com/spf13/cobra v1.10.1
22+
github.com/stretchr/testify v1.10.0
23+
golang.org/x/crypto v0.36.0
24+
golang.org/x/sys v0.31.0
1225
)
1326

1427
require (
@@ -21,16 +34,13 @@ require (
2134

2235
require (
2336
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
24-
github.com/GehirnInc/crypt v0.0.0-20230320061759-8cc1b52080c5 // indirect
2537
github.com/Microsoft/go-winio v0.6.2 // indirect
2638
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
27-
github.com/creack/pty v1.1.18 // indirect
39+
github.com/davecgh/go-spew v1.1.1 // indirect
2840
github.com/distribution/reference v0.6.0 // indirect
29-
github.com/docker/docker v28.0.0+incompatible // indirect
3041
github.com/docker/go-connections v0.5.0 // indirect
3142
github.com/docker/go-units v0.5.0 // indirect
3243
github.com/felixge/httpsnoop v1.0.4 // indirect
33-
github.com/gliderlabs/ssh v0.3.5 // indirect
3444
github.com/go-logr/logr v1.4.3 // indirect
3545
github.com/go-logr/stdr v1.2.2 // indirect
3646
github.com/go-playground/locales v0.14.1 // indirect
@@ -42,28 +52,24 @@ require (
4252
github.com/gorilla/websocket v1.5.0 // indirect
4353
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4454
github.com/kr/fs v0.1.0 // indirect
45-
github.com/labstack/echo/v4 v4.10.2 // indirect
4655
github.com/leodido/go-urn v1.2.2 // indirect
47-
github.com/mattn/go-shellwords v1.0.12 // indirect
4856
github.com/moby/docker-image-spec v1.3.1 // indirect
4957
github.com/opencontainers/go-digest v1.0.0 // indirect
5058
github.com/opencontainers/image-spec v1.1.0 // indirect
51-
github.com/openwall/yescrypt-go v1.0.0 // indirect
52-
github.com/pkg/errors v0.9.1 // indirect
53-
github.com/pkg/sftp v1.13.5 // indirect
59+
github.com/pmezard/go-difflib v1.0.0 // indirect
5460
github.com/sethvargo/go-envconfig v0.9.0 // indirect
5561
github.com/spf13/pflag v1.0.9 // indirect
62+
github.com/stretchr/objx v0.5.2 // indirect
5663
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
5764
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
5865
go.opentelemetry.io/otel v1.37.0 // indirect
5966
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect
6067
go.opentelemetry.io/otel/metric v1.37.0 // indirect
6168
go.opentelemetry.io/otel/trace v1.37.0 // indirect
6269
go.opentelemetry.io/proto/otlp v1.2.0 // indirect
63-
golang.org/x/crypto v0.36.0 // indirect
6470
golang.org/x/net v0.38.0 // indirect
65-
golang.org/x/sys v0.31.0 // indirect
6671
golang.org/x/text v0.23.0 // indirect
72+
gopkg.in/yaml.v3 v3.0.1 // indirect
6773
gotest.tools/v3 v3.5.1 // indirect
6874
)
6975

agent/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
6161
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
6262
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
6363
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
64+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
65+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
66+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
67+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
6468
github.com/labstack/echo/v4 v4.10.2 h1:n1jAhnq/elIFTHr1EYpiYtyKgx4RW9ccVgkqByZaN2M=
6569
github.com/labstack/echo/v4 v4.10.2/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k=
6670
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
@@ -94,6 +98,8 @@ github.com/pkg/sftp v1.13.5 h1:a3RLUqkyjYRtBTZJZ1VRrKbN3zhuPLlUc3sphVz81go=
9498
github.com/pkg/sftp v1.13.5/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
9599
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
96100
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
101+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
102+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
97103
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
98104
github.com/rwtodd/Go.Sed v0.0.0-20210816025313-55464686f9ef/go.mod h1:8AEUvGVi2uQ5b24BIhcr0GCcpd/RNAFWaN2CJFrWIIQ=
99105
github.com/sethvargo/go-envconfig v0.9.0 h1:Q6FQ6hVEeTECULvkJZakq3dZMeBQ3JUpcKMfPQbKMDE=
@@ -211,6 +217,8 @@ google.golang.org/grpc v1.63.0/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDom
211217
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
212218
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
213219
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
220+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
221+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
214222
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
215223
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
216224
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

agent/init_docker.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ package main
66
import (
77
"os"
88

9-
"github.com/shellhub-io/shellhub/pkg/agent/pkg/sysinfo"
9+
"github.com/shellhub-io/shellhub/agent/pkg/sysinfo"
1010
)
1111

12+
// AgentVersion store the version to be embed inside the binary. This is
13+
// injected using `-ldflags` build option.
14+
//
15+
// go build -ldflags "-X main.AgentVersion=1.2.3"
16+
//
17+
// If set to `latest`, the auto-updating mechanism is disabled. This is intended
18+
// to be used during development only.
19+
var AgentVersion string
20+
21+
// AgentPlatform stores what platform the agent is running on. This is injected in build time in the [ShellHub Agent]
22+
// implementation.
23+
//
24+
// [ShellHub Agent]: https://github.com/shellhub-io/shellhub/tree/master/agent
1225
var AgentPlatform string
1326

1427
func init() {

agent/init_native.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,21 @@
33

44
package main
55

6-
var AgentPlatform = "native"
6+
// AgentVersion store the version to be embed inside the binary. This is
7+
// injected using `-ldflags` build option.
8+
//
9+
// go build -ldflags "-X main.AgentVersion=1.2.3"
10+
//
11+
// If set to `latest`, the auto-updating mechanism is disabled. This is intended
12+
// to be used during development only.
13+
var AgentVersion string
14+
15+
// AgentPlatform stores what platform the agent is running on. This is injected in build time in the [ShellHub Agent]
16+
// implementation.
17+
//
18+
// [ShellHub Agent]: https://github.com/shellhub-io/shellhub/tree/master/agent
19+
var AgentPlatform string
20+
21+
func init() {
22+
AgentPlatform = "native"
23+
}

0 commit comments

Comments
 (0)