Skip to content
Snippets Groups Projects
Commit 176d98c2 authored by Lars Seipel's avatar Lars Seipel
Browse files

runner/libvirt: on the mgmt server, include devices in /etc/hosts

parent 16ee2c1c
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"net"
"os/exec" "os/exec"
"strings" "strings"
...@@ -172,14 +173,14 @@ func writeExtraMgmtServerCommands(w io.Writer, d *device) { ...@@ -172,14 +173,14 @@ func writeExtraMgmtServerCommands(w io.Writer, d *device) {
io.WriteString(w, "run-command systemctl enable dnsmasq.service\n") io.WriteString(w, "run-command systemctl enable dnsmasq.service\n")
} }
func generateHostsFile(ctx context.Context, r *Runner, t *topology.T) (file []byte, err error) { type etherHost struct {
defer func() { name string
if err != nil { ip *net.IPAddr
err = fmt.Errorf("generateHostsFile: %w", err) mac net.HardwareAddr
} }
}()
var buf bytes.Buffer func gatherHosts(ctx context.Context, r *Runner, t *topology.T) []etherHost {
var hosts []etherHost
for name, d := range r.devices { for name, d := range r.devices {
if name == "oob-mgmt-server" || name == "oob-mgmt-switch" { if name == "oob-mgmt-server" || name == "oob-mgmt-switch" {
continue continue
...@@ -193,9 +194,20 @@ func generateHostsFile(ctx context.Context, r *Runner, t *topology.T) (file []by ...@@ -193,9 +194,20 @@ func generateHostsFile(ctx context.Context, r *Runner, t *topology.T) (file []by
if mgmtIP == nil { if mgmtIP == nil {
continue continue
} }
fmt.Fprintf(&buf, "%s,%s,%s\n", eth0.mac, mgmtIP, name) hosts = append(hosts, etherHost{
name: name,
ip: mgmtIP,
mac: eth0.mac,
})
} }
return buf.Bytes(), nil return hosts
}
func generateDnsmasqHostsFile(hosts []etherHost) []byte {
var buf bytes.Buffer
for _, h := range hosts {
fmt.Fprintf(&buf, "%s,%s,%s\n", h.mac, h.ip, h.name)
}
return buf.Bytes()
} }
...@@ -52,7 +52,7 @@ func TestDnsmasqHostsFile(t *testing.T) { ...@@ -52,7 +52,7 @@ func TestDnsmasqHostsFile(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
ctx := context.Background() ctx := context.Background()
content, err := generateHostsFile(ctx, r, topo) content := generateDnsmasqHostsFile(gatherHosts(ctx, r, topo))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
......
...@@ -525,13 +525,14 @@ func (r *Runner) customizeDomains(ctx context.Context, t *topology.T) (err error ...@@ -525,13 +525,14 @@ func (r *Runner) customizeDomains(ctx context.Context, t *topology.T) (err error
fmt.Fprintf(&buf, "ssh-inject %s:string:%s\n", user, k) fmt.Fprintf(&buf, "ssh-inject %s:string:%s\n", user, k)
} }
if d.topoDev.Function() == topology.FunctionOOBServer { if d.topoDev.Function() == topology.FunctionOOBServer {
hostsBytes, err1 := generateHostsFile(ctx, r, t) hosts := gatherHosts(ctx, r, t)
if err != nil { for _, h := range hosts {
err = err1 fmt.Fprintf(&buf, "append-line /etc/hosts:%s %s\n",
break h.ip, h.name)
} }
dnsmasqHosts := generateDnsmasqHostsFile(hosts)
fmt.Fprintf(&buf, "write /etc/dnsmasq.hostsfile:%s\n", fmt.Fprintf(&buf, "write /etc/dnsmasq.hostsfile:%s\n",
bytes.Replace(hostsBytes, []byte("\n"), bytes.Replace(dnsmasqHosts, []byte("\n"),
[]byte("\\\n"), -1)) []byte("\\\n"), -1))
} }
extra := strings.NewReader(buf.String()) extra := strings.NewReader(buf.String())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment