1- package connector
1+ package main
22
33import (
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.
2525type 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}
0 commit comments