Skip to content

Commit

Permalink
agent: Retrieve system-uuid
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tupyy committed Nov 28, 2024
1 parent 3427a9c commit 406a20e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
43 changes: 37 additions & 6 deletions cmd/planner-agent/main.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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])
Expand All @@ -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")
}
16 changes: 16 additions & 0 deletions data/ignition.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 406a20e

Please sign in to comment.