f-55: lab: fichiers des nœuds sur un ISO dédié, envoi des binaires compilés en local #55

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-12 15:50:54 +02:00
commit aceffe7b58
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
16 changed files with 181 additions and 58 deletions

View file

@ -26,6 +26,9 @@ func qemuArgs(p *topology.Plan, n topology.NodePlan, index int, dir string) []st
"-drive", "file=" + filepath.Join(dir, DiskFile) + ",if=virtio,format=qcow2",
"-drive", "file=" + filepath.Join(dir, SeedFile) + ",media=cdrom,readonly=on",
}
if len(n.Files) > 0 {
args = append(args, "-drive", "file="+filepath.Join(dir, FilesISO)+",media=cdrom,readonly=on")
}
restrict := "on"
if n.Role == topology.RoleSwitch {

View file

@ -20,6 +20,8 @@ const (
DiskFile = "disk.qcow2"
SeedFile = "seed.iso"
FilesISO = "files.iso"
FilesLabel = "LABFILES"
ConsoleFile = "console.log"
QMPFile = "qmp.sock"
PIDFile = "qemu.pid"
@ -33,12 +35,12 @@ type Options struct {
FRR map[string]string
Provision map[string]string
PostProvision map[string]string
Files map[string]map[string][]byte
}
type Node struct {
Name string
Dir string
Files map[string]string
QEMU []string
MetaData []byte
UserData []byte
@ -59,11 +61,6 @@ func Render(p *topology.Plan, o Options) ([]Node, error) {
if _, ok := o.PostProvision[n.Name]; n.PostProvision != "" && !ok {
return nil, fmt.Errorf("node %s: post_provision script %s was not read", n.Name, n.PostProvision)
}
for name, src := range n.Files {
if _, ok := o.Files[n.Name][name]; !ok {
return nil, fmt.Errorf("node %s: file %s (%s) was not read", n.Name, name, src)
}
}
}
var nodes []Node
for index, n := range p.Nodes {
@ -83,6 +80,7 @@ func Render(p *topology.Plan, o Options) ([]Node, error) {
nodes = append(nodes, Node{
Name: n.Name,
Dir: dir,
Files: n.Files,
QEMU: qemuArgs(p, n, index, dir),
MetaData: meta,
UserData: user,

View file

@ -36,7 +36,6 @@ func roleOptions() Options {
FRR: frrConfigs,
Provision: map[string]string{"hv1": "#!/bin/sh\necho provision\n"},
PostProvision: map[string]string{"hv1": "#!/bin/sh\necho post\n"},
Files: map[string]map[string][]byte{"hv1": {"agent.yml": []byte("dhcp:\n backend: two\n")}},
}
}
@ -155,9 +154,10 @@ func TestRoles_FRRKeyIsThePinnedRepositoryKey(t *testing.T) {
func TestRoles_ProvisioningIsOneScriptThatStopsAtTheFirstFailure(t *testing.T) {
nodes := renderRoles(t)
env := "LAB_NODE=hv1 LAB_UPLINK=underlay LAB_FILES=/etc/lab/files "
files := "mkdir -p /etc/lab/files /run/lab-files\nmount -o ro LABEL=LABFILES /run/lab-files\ncp -R /run/lab-files/. /etc/lab/files/\numount /run/lab-files\n"
for name, steps := range map[string]string{
"sw1": "systemctl daemon-reload\nsystemctl enable --now lab-switch.service\n/usr/local/sbin/lab-frr\n",
"hv1": env + "/usr/local/sbin/lab-node-provision\n/usr/local/sbin/lab-frr\n" + env + "/usr/local/sbin/lab-node-post-provision\n",
"hv1": files + env + "/usr/local/sbin/lab-node-provision\n/usr/local/sbin/lab-frr\n" + env + "/usr/local/sbin/lab-node-post-provision\n",
} {
cfg := user(t, nodeNamed(t, nodes, name))
if !reflect.DeepEqual(cfg.Runcmd, [][]string{{"/usr/local/sbin/lab-provision"}}) {
@ -173,7 +173,29 @@ func TestRoles_ProvisioningIsOneScriptThatStopsAtTheFirstFailure(t *testing.T) {
}
}
func TestRoles_HostReceivesItsScriptsAndFiles(t *testing.T) {
func TestRoles_FilesTravelOnASecondCDROM(t *testing.T) {
nodes := renderRoles(t)
hv1 := nodeNamed(t, nodes, "hv1")
if !reflect.DeepEqual(hv1.Files, map[string]string{"agent.yml": "two.yml"}) {
t.Errorf("hv1 files = %v", hv1.Files)
}
drive := "file=/srv/lab/hv1/files.iso,media=cdrom,readonly=on"
if !strings.Contains(strings.Join(hv1.QEMU, " "), "-drive "+drive) {
t.Errorf("hv1 qemu lacks %s:\n%s", drive, strings.Join(hv1.QEMU, "\n"))
}
for _, f := range user(t, hv1).WriteFiles {
if strings.HasPrefix(f.Path, "/etc/lab/files/") {
t.Errorf("hv1 user-data carries %s, files belong to the ISO", f.Path)
}
}
for _, name := range []string{"sw1", "rr1", "hv2"} {
if n := nodeNamed(t, nodes, name); strings.Contains(strings.Join(n.QEMU, " "), "files.iso") || n.Files != nil {
t.Errorf("%s gets a files ISO", name)
}
}
}
func TestRoles_HostReceivesItsScripts(t *testing.T) {
cfg := user(t, nodeNamed(t, renderRoles(t), "hv1"))
for path, want := range map[string]struct {
content string
@ -181,7 +203,6 @@ func TestRoles_HostReceivesItsScriptsAndFiles(t *testing.T) {
}{
"/usr/local/sbin/lab-node-provision": {"#!/bin/sh\necho provision\n", "0755"},
"/usr/local/sbin/lab-node-post-provision": {"#!/bin/sh\necho post\n", "0755"},
"/etc/lab/files/agent.yml": {"dhcp:\n backend: two\n", "0644"},
} {
f := fileAt(t, cfg, path)
got, err := base64.StdEncoding.DecodeString(f.Content)
@ -280,7 +301,6 @@ func TestRoles_RefusesUnreadProvisionInputs(t *testing.T) {
}{
"provision": {func(o *Options) { o.Provision = nil }, "node hv1: provision script hv.sh was not read"},
"post": {func(o *Options) { o.PostProvision = nil }, "node hv1: post_provision script hv-post.sh was not read"},
"file": {func(o *Options) { o.Files = nil }, "node hv1: file agent.yml (two.yml) was not read"},
}
for name, c := range cases {
o := roleOptions()

View file

@ -4,7 +4,6 @@ import (
_ "embed"
"encoding/base64"
"fmt"
"sort"
"strings"
"go.yaml.in/yaml/v3"
@ -31,6 +30,7 @@ const (
NodeProvision = "/usr/local/sbin/lab-node-provision"
NodePostProvision = "/usr/local/sbin/lab-node-post-provision"
FilesDir = "/etc/lab/files"
FilesMount = "/run/lab-files"
)
//go:embed frrouting.gpg
@ -114,13 +114,13 @@ func userData(p *topology.Plan, n topology.NodePlan, o Options) ([]byte, error)
}
steps = append(steps, "systemctl daemon-reload", "systemctl enable --now lab-node.service")
}
names := make([]string, 0, len(n.Files))
for name := range n.Files {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
cfg.WriteFiles = append(cfg.WriteFiles, writeFile{Path: FilesDir + "/" + name, Permissions: "0644", Encoding: "b64", Content: base64.StdEncoding.EncodeToString(o.Files[n.Name][name])})
if len(n.Files) > 0 {
steps = append(steps,
"mkdir -p "+FilesDir+" "+FilesMount,
"mount -o ro LABEL="+FilesLabel+" "+FilesMount,
"cp -R "+FilesMount+"/. "+FilesDir+"/",
"umount "+FilesMount,
)
}
if n.Provision != "" {
cfg.WriteFiles = append(cfg.WriteFiles, writeFile{Path: NodeProvision, Permissions: "0755", Encoding: "b64", Content: base64.StdEncoding.EncodeToString([]byte(o.Provision[n.Name]))})