Skip to content

Commit

Permalink
feat(connector): set agent hostname as container's name
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto authored and gustavosbarreto committed Oct 26, 2023
1 parent 9acfb59 commit 70beda4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions connector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ require (
golang.org/x/tools v0.14.0 // indirect
gotest.tools/v3 v3.5.1 // indirect
)

replace github.com/shellhub-io/shellhub => ../
2 changes: 0 additions & 2 deletions connector/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Q
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shellhub-io/shellhub v0.13.0-rc.6.0.20231026135513-f00f02afa3d1 h1:guDFUaQrRMYdhQs6Q1Z97VKG8EitEpScotZNwatdhXc=
github.com/shellhub-io/shellhub v0.13.0-rc.6.0.20231026135513-f00f02afa3d1/go.mod h1:nvY8PAWYiGT+iYYBNc3q8hnBO7KtyzYqJEFsBqUuWcM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
Expand Down
6 changes: 4 additions & 2 deletions pkg/agent/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
type Container struct {
// ID is the container ID.
ID string
// Name is the container name.
Name string
// ServerAddress is the ShellHub address of the server that the agent will connect to.
ServerAddress string
// Tenant is the tenant ID of the namespace that the agent belongs to.
Expand All @@ -22,9 +24,9 @@ type Container struct {
// Connector is an interface that defines the methods that a connector must implement.
type Connector interface {
// List lists all containers running on the host.
List(ctx context.Context) ([]string, error)
List(ctx context.Context) ([]Container, error)
// Start starts the agent for the container with the given ID.
Start(ctx context.Context, id string)
Start(ctx context.Context, id string, name string)
// Stop stops the agent for the container with the given ID.
Stop(ctx context.Context, id string)
// Listen listens for events and starts or stops the agent for the container that was created or removed.
Expand Down
37 changes: 30 additions & 7 deletions pkg/agent/connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,29 @@ func (d *DockerConnector) events(ctx context.Context) (<-chan events.Message, <-
return d.cli.Events(ctx, types.EventsOptions{})
}

func (d *DockerConnector) List(ctx context.Context) ([]string, error) {
func (d *DockerConnector) List(ctx context.Context) ([]Container, error) {
containers, err := d.cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
return nil, err
}

list := make([]string, len(containers))
list := make([]Container, len(containers))
for i, container := range containers {
list[i] = container.ID
list[i].ID = container.ID

name, err := d.getContainerNameFromID(ctx, container.ID)
if err != nil {
return nil, err
}

list[i].Name = name
}

return list, nil
}

// Start starts the agent for the container with the given ID.
func (d *DockerConnector) Start(ctx context.Context, id string) {
func (d *DockerConnector) Start(ctx context.Context, id string, name string) {
id = id[:12]

d.mu.Lock()
Expand All @@ -80,6 +87,7 @@ func (d *DockerConnector) Start(ctx context.Context, id string) {
privateKey := fmt.Sprintf("%s/%s.key", d.privateKeys, id)
go initContainerAgent(ctx, Container{
ID: id,
Name: name,
ServerAddress: d.server,
Tenant: d.tenant,
PrivateKey: privateKey,
Expand All @@ -101,6 +109,16 @@ func (d *DockerConnector) Stop(_ context.Context, id string) {
}
}

func (d *DockerConnector) getContainerNameFromID(ctx context.Context, id string) (string, error) {
container, err := d.cli.ContainerInspect(ctx, id)
if err != nil {
return "", err
}

// NOTICE: It removes the first character on container's name that is a `/`.
return container.Name[1:], nil
}

// Listen listens for events and starts or stops the agent for the containers.
func (d *DockerConnector) Listen(ctx context.Context) error {
containers, err := d.List(ctx)
Expand All @@ -109,7 +127,7 @@ func (d *DockerConnector) Listen(ctx context.Context) error {
}

for _, container := range containers {
d.Start(ctx, container)
d.Start(ctx, container.ID, container.Name)
}

events, errs := d.events(ctx)
Expand All @@ -126,7 +144,12 @@ func (d *DockerConnector) Listen(ctx context.Context) error {
// the "start" event will be called too. The same happens with the "die" event.
switch container.Action {
case "start":
d.Start(ctx, container.ID)
name, err := d.getContainerNameFromID(ctx, container.ID)
if err != nil {
return err
}

d.Start(ctx, container.ID, name)
case "die":
d.Stop(ctx, container.ID)
}
Expand All @@ -143,8 +166,8 @@ func initContainerAgent(ctx context.Context, container Container) {
ServerAddress: container.ServerAddress,
TenantID: container.Tenant,
PrivateKey: container.PrivateKey,
PreferredHostname: container.ID,
PreferredIdentity: container.ID,
PreferredHostname: container.Name,
Mode: agent.ModeConnector,
KeepAliveInterval: 30,
}
Expand Down

0 comments on commit 70beda4

Please sign in to comment.