From 7cebd751abe7258f0b83216fe66ddf418580f8fd Mon Sep 17 00:00:00 2001 From: Cosmin Tupangiu Date: Thu, 28 Nov 2024 17:16:27 +0100 Subject: [PATCH] agent: Retrieve system-uuid This commit adds a service to agent image that is retrieving the system-uuid and write it into `/var/home/core/agent_id`. Agent reads this file at start up. Signed-off-by: Cosmin Tupangiu --- cmd/planner-agent/main.go | 29 +++++++++++++++++++++++------ data/ignition.template | 16 ++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/cmd/planner-agent/main.go b/cmd/planner-agent/main.go index b899d56..c692a8e 100644 --- a/cmd/planner-agent/main.go +++ b/cmd/planner-agent/main.go @@ -1,18 +1,21 @@ package main import ( + "bytes" "context" + "errors" "flag" "fmt" "os" + "path" "github.com/google/uuid" "github.com/kubev2v/migration-planner/internal/agent" "github.com/kubev2v/migration-planner/pkg/log" ) -var ( - agentID string +const ( + agentFilename = "agent_id" ) func main() { @@ -35,7 +38,6 @@ func NewAgentCommand() *agentCmd { } flag.StringVar(&a.configFile, "config", agent.DefaultConfigFile, "Path to the agent's configuration file.") - flag.StringVar(&agentID, "id", os.Getenv("AGENT_ID"), "ID of the agent") flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) @@ -58,13 +60,28 @@ func NewAgentCommand() *agentCmd { } func (a *agentCmd) Execute() error { - // FIXME: !!! - if agentID == "" { - agentID = uuid.New().String() + agentID, err := a.getAgentID() + if err != nil { + a.log.Fatalf("failed to retreive agent_id: %v", err) } + agentInstance := agent.New(uuid.MustParse(agentID), a.log, a.config) if err := agentInstance.Run(context.Background()); err != nil { a.log.Fatalf("running device agent: %v", err) } return nil } + +func (a *agentCmd) getAgentID() (string, error) { + // look for it in data dir + dataDirPath := path.Join(a.config.DataDir, agentFilename) + if _, err := os.Stat(dataDirPath); err == nil { + content, err := os.ReadFile(dataDirPath) + if err != nil { + return "", err + } + return string(bytes.TrimSpace(content)), nil + } + + return "", errors.New("agent_id not found") +} diff --git a/data/ignition.template b/data/ignition.template index a209613..2d9c5bf 100644 --- a/data/ignition.template +++ b/data/ignition.template @@ -9,6 +9,21 @@ passwd: - {{.SshKey}} {{end}} +systemd: + units: + - name: planner-agent-id.service + enabled: true + contents: | + [Unit] + Description=Service to retrieve system uuid + [Service] + Type=oneshot + ExecStart=/bin/bash -c 'cat /sys/class/dmi/id/product_uuid > /var/home/core/.migration-planner/data/agent_id' + ExecStartPost=/bin/bash -c 'chown core:core /var/home/core/.migration-planner/data/agent_id' + RemainAfterExit=true + [Install] + WantedBy=multi-user.target + storage: links: - path: /home/core/.config/systemd/user/timers.target.wants/podman-auto-update.timer @@ -99,6 +114,7 @@ storage: [Service] Restart=on-failure RestartSec=5 + ExecStartPre=/bin/bash -c 'until [ -f /var/home/core/.migration-planner/data/agent_id ]; do sleep 1; done' [Install] WantedBy=multi-user.target default.target