Skip to content

Commit

Permalink
fix(connector): use ShellHub version as connector's one
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto authored and gustavosbarreto committed Oct 31, 2023
1 parent 0a6f2f7 commit 073bc3a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion connector/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ RUN go mod download

WORKDIR $GOPATH/src/github.com/shellhub-io/shellhub/connector

RUN go build -tags docker
RUN go build -tags docker -ldflags "-X main.ConnectorVersion=${SHELLHUB_VERSION}"

# development stage
FROM base AS development
Expand Down
11 changes: 11 additions & 0 deletions connector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/spf13/cobra"
)

// ConnectorVersion store the version to be embed inside the binary. This is
// injected using `-ldflags` build option (e.g: `go build -ldflags "-X
// main.ConnectorVersion=1.2.3"`).
var ConnectorVersion string

func main() {
rootCmd := &cobra.Command{ // nolint: exhaustruct
Use: "docker",
Expand All @@ -29,29 +34,35 @@ func main() {
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
"private_keys": cfg.PrivateKeys,
"version": ConnectorVersion,
}).Info("Starting ShellHub Docker Connector")

connector.ConnectorVersion = ConnectorVersion
connector, err := connector.NewDockerConnector(cfg.ServerAddress, cfg.TenantID, cfg.PrivateKeys)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
"version": ConnectorVersion,
}).Fatal("Failed to create ShellHub Docker Connector")
}

if err := connector.Listen(cmd.Context()); err != nil {
log.WithError(err).WithFields(log.Fields{
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
"version": ConnectorVersion,
}).Fatal("Failed to listen for connections")
}

log.WithFields(log.Fields{
"address": cfg.ServerAddress,
"tenant_id": cfg.TenantID,
"version": ConnectorVersion,
}).Info("ShellHub Docker Connector stopped")
},
}

rootCmd.Version = ConnectorVersion
rootCmd.Execute() // nolint: errcheck
}
2 changes: 1 addition & 1 deletion connector/refresh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ included_extensions:
- .go
build_target_path: ""
build_path: /go/src/github.com/shellhub-io/shellhub/connector
build_flags: []
build_flags: ["-tags", "docker", "-ldflags", "-X main.ConnectorVersion=latest"]
build_delay: 200ns
binary_name: connector
command_flags: ["docker"]
Expand Down
4 changes: 4 additions & 0 deletions pkg/agent/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"context"
)

// ConnectorVersion stores the version of the ShellHub Instane that is running the connector.
// It is used in the ShellHub Agents initialized by the connector when a container is started.
var ConnectorVersion string

// Container is a struct that represents a container that will be managed by the connector.
type Container struct {
// ID is the container ID.
Expand Down
15 changes: 10 additions & 5 deletions pkg/agent/connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package connector
import (
"context"
"fmt"
"os"
"sync"
"time"

Expand All @@ -14,8 +13,6 @@ import (
log "github.com/sirupsen/logrus"
)

const AgentPlatformConnector = "connector"

var _ Connector = new(DockerConnector)

// DockerConnector is a struct that represents a connector that uses Docker as the container runtime.
Expand Down Expand Up @@ -159,8 +156,8 @@ func (d *DockerConnector) Listen(ctx context.Context) error {

// initContainerAgent initializes the agent for a container.
func initContainerAgent(ctx context.Context, container Container) {
agent.AgentVersion = os.Getenv("SHELLHUB_VERSION")
agent.AgentPlatform = AgentPlatformConnector
agent.AgentPlatform = "connector"
agent.AgentVersion = ConnectorVersion

cfg := &agent.Config{
ServerAddress: container.ServerAddress,
Expand All @@ -179,20 +176,23 @@ func initContainerAgent(ctx context.Context, container Container) {
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Info("Connector container started")

ag, err := agent.NewAgentWithConfig(cfg)
if err != nil {
log.WithError(err).WithFields(log.Fields{
"id": container.ID,
"configuration": cfg,
"version": agent.AgentVersion,
}).Fatal("Failed to create agent")
}

if err := ag.Initialize(); err != nil {
log.WithError(err).WithFields(log.Fields{
"id": container.ID,
"configuration": cfg,
"version": agent.AgentVersion,
}).Fatal("Failed to initialize agent")
}

Expand All @@ -205,6 +205,7 @@ func initContainerAgent(ctx context.Context, container Container) {
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Fatal("Failed to ping server")
}

Expand All @@ -215,6 +216,7 @@ func initContainerAgent(ctx context.Context, container Container) {
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Info("Stopped pinging server")
}()

Expand All @@ -225,6 +227,7 @@ func initContainerAgent(ctx context.Context, container Container) {
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Info("Listening for connections")

// NOTICE(r): listing for connection and wait for a channel message to close the agent. It will receives
Expand All @@ -238,6 +241,7 @@ func initContainerAgent(ctx context.Context, container Container) {
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"timestamp": time.Now(),
"version": agent.AgentVersion,
}).Fatal("Failed to listen for connections")
}

Expand All @@ -247,5 +251,6 @@ func initContainerAgent(ctx context.Context, container Container) {
"hostname": cfg.PreferredHostname,
"tenant_id": cfg.TenantID,
"server_address": cfg.ServerAddress,
"version": agent.AgentVersion,
}).Info("Connector container done")
}

0 comments on commit 073bc3a

Please sign in to comment.