Skip to content

Commit

Permalink
Refactor elemental-agent to use subcommands
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Mazzotti <[email protected]>
  • Loading branch information
anmazzotti committed Aug 28, 2024
1 parent 6cbed87 commit b9537ca
Show file tree
Hide file tree
Showing 41 changed files with 639 additions and 1,433 deletions.
5 changes: 3 additions & 2 deletions Dockerfile.agent
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ WORKDIR /workspace
# Copy the go source
COPY go.mod go.mod
COPY go.sum go.sum
COPY cmd/agent/main.go cmd/agent/main.go
COPY main.go main.go
COPY cmd/agent/ cmd/agent/
COPY api/ api/
COPY internal/ internal/
COPY pkg/ pkg/
Expand All @@ -24,7 +25,7 @@ RUN CGO_ENABLED=1 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build \
-X github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version.Version=$TAG \
-X github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version.Commit=$COMMIT \
-X github.com/rancher-sandbox/cluster-api-provider-elemental/internal/version.CommitDate=$COMMITDATE" \
-a -o elemental-agent cmd/agent/main.go
-a -o elemental-agent main.go

# Build elemental-toolkit plugin
RUN CGO_ENABLED=1 go build \
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ test: manifests generate fmt vet envtest generate-mocks $(GINKGO) ## Run tests.
##@ Build
.PHONY: build-agent
build-agent: fmt vet ## Build agent binary for local architecture.
CGO_ENABLED=1 go build -ldflags '$(LDFLAGS)' -o bin/elemental-agent cmd/agent/main.go
CGO_ENABLED=1 go build -ldflags '$(LDFLAGS)' -o bin/elemental-agent main.go

# This does depend on cross compilation library, for example: cross-aarch64-gcc13
.PHONY: build-agent-all
build-agent-all: fmt vet ## Build agent binary for all architectures.
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o bin/elemental_agent_linux_amd64 cmd/agent/main.go
CC=$(CROSS_COMPILER) CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -ldflags '$(LDFLAGS)' -o bin/elemental_agent_linux_arm64 cmd/agent/main.go
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags '$(LDFLAGS)' -o bin/elemental_agent_linux_amd64 main.go
CC=$(CROSS_COMPILER) CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build -ldflags '$(LDFLAGS)' -o bin/elemental_agent_linux_arm64 main.go

.PHONY: build-manager
build-manager: manifests generate fmt vet ## Build manager binary.
Expand Down
29 changes: 29 additions & 0 deletions cmd/agent/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package agent

import (
"github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/log"
"github.com/rancher-sandbox/cluster-api-provider-elemental/internal/agent/phase"
"github.com/spf13/cobra"
)

// installCmd represents the install command.
var installCmd = &cobra.Command{
Use: "install",
Short: "Installs the OS on this Elemental host",
Long: "Installs the OS on this Elemental host",
Run: func(cmd *cobra.Command, args []string) {
log.Info("Initializing agent")
agentContext, err := InitAgent()
if err != nil {
log.Fatal(err, "Could not initialize agent")
}

log.Info("Installing host")
installationHandler := phase.NewInstallHandler(*agentContext)
installationHandler.Install()
},
}

func init() {
rootCmd.AddCommand(installCmd)
}
273 changes: 0 additions & 273 deletions cmd/agent/main.go

This file was deleted.

Loading

0 comments on commit b9537ca

Please sign in to comment.