From 406a20eaf9e683c27eea9a0903516392fe64fc41 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 | 43 +++++++++++++++++++++++++++++++++------ data/ignition.template | 16 +++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/cmd/planner-agent/main.go b/cmd/planner-agent/main.go index b899d56..b440599 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,42 @@ 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) { + getContent := func(filepath string) (string, error) { + content, err := os.ReadFile(filepath) + if err != nil { + return "", err + } + return string(bytes.TrimSpace(content)), nil + } + + // look for the file in $HOME + homedir, err := os.UserHomeDir() + if err == nil { + homedirFilename := path.Join(homedir, agentFilename) + _, err := os.Stat(homedirFilename) + if err == nil { + return getContent(homedirFilename) + } + } + + // look for it in config dir + configDirPath := path.Join(a.config.ConfigDir, agentFilename) + if _, err := os.Stat(configDirPath); err == nil { + return getContent(configDirPath) + } + + return "", errors.New("agent_id not found") +} diff --git a/data/ignition.template b/data/ignition.template index a209613..cb73ad3 100644 --- a/data/ignition.template +++ b/data/ignition.template @@ -99,6 +99,7 @@ storage: [Service] Restart=on-failure RestartSec=5 + ExecStartPre=/bin/bash -c 'until [ -z /var/home/core/agent_id ]; do sleep 1; done' [Install] WantedBy=multi-user.target default.target @@ -124,3 +125,18 @@ storage: [Install] WantedBy=multi-user.target default.target + - path: /etc/systemd/system/agent_id.service + mode: 0644 + contents: + inline: | + [Unit] + Description=Service to retrieve system uuid + + [Service] + Type=oneshot + ExecStart=/bin/bash -c 'dmidecode -s system-uuid > /var/home/core/agent_id' + ExecStartPost=/bin/bash -c 'chown core:core /var/home/core/agent_id' + RemainAfterExit=true + + [Install] + WantedBy=multi-user.target