f-55: lab: provision générique des nœuds, rôle host, two installé par test/e2e #55
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
4438bb3916
commit
f713f482fa
22 changed files with 481 additions and 369 deletions
|
|
@ -130,17 +130,12 @@ func renderCmd(args []string, stdout, stderr io.Writer) int {
|
|||
fmt.Fprintf(stderr, "lab: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
frr, err := provision.ReadFRR(p)
|
||||
if err != nil {
|
||||
opts := render.Options{RunDir: dir, AuthorizedKeys: authorized}
|
||||
if err := provision.ReadInputs(p, &opts); err != nil {
|
||||
fmt.Fprintf(stderr, "lab: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
agent, err := provision.ReadAgent(p)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "lab: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
nodes, err := render.Render(p, render.Options{RunDir: dir, AuthorizedKeys: authorized, FRR: frr, Agent: agent})
|
||||
nodes, err := render.Render(p, opts)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "lab: %v\n", err)
|
||||
return 1
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
|
@ -9,6 +10,8 @@ import (
|
|||
"strings"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
"go.yaml.in/yaml/v3"
|
||||
)
|
||||
|
||||
func runLab(args ...string) (int, string, string) {
|
||||
|
|
@ -165,11 +168,11 @@ func TestRun_CommandsWithoutALabInTheRunDir(t *testing.T) {
|
|||
|
||||
func TestRun_StatusOfAStoppedLab(t *testing.T) {
|
||||
code, stdout, stderr := runLab("status", "-run", savedLab(t))
|
||||
want := `node role state pid ssh
|
||||
sw1 switch stopped - 127.0.0.1:2200
|
||||
rr1 rr stopped - 127.0.0.1:2201
|
||||
hv1 hypervisor stopped - 127.0.0.1:2202
|
||||
hv2 hypervisor stopped - 127.0.0.1:2203
|
||||
want := `node role state pid ssh
|
||||
sw1 switch stopped - 127.0.0.1:2200
|
||||
rr1 rr stopped - 127.0.0.1:2201
|
||||
hv1 host stopped - 127.0.0.1:2202
|
||||
hv2 host stopped - 127.0.0.1:2203
|
||||
`
|
||||
if code != 0 || stdout != want {
|
||||
t.Errorf("code %d, stderr %q, stdout:\n%s", code, stderr, stdout)
|
||||
|
|
@ -315,8 +318,8 @@ func TestRun_RenderShipsTheExampleFRRConfigs(t *testing.T) {
|
|||
for node, wants := range map[string][]string{
|
||||
"sw1": {"router bgp 65100"},
|
||||
"rr1": {"bgp listen range 192.168.14.0/24 peer-group fabric"},
|
||||
"hv1": {"hostname hv1", "route_reflectors: [10.255.255.1]"},
|
||||
"hv2": {"hostname hv2", "route_reflectors: [10.255.255.1]"},
|
||||
"hv1": {"hostname hv1"},
|
||||
"hv2": {"hostname hv2"},
|
||||
} {
|
||||
data, err := os.ReadFile(filepath.Join(out, node, "user-data"))
|
||||
if err != nil {
|
||||
|
|
@ -328,6 +331,32 @@ func TestRun_RenderShipsTheExampleFRRConfigs(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
for _, node := range []string{"hv1", "hv2"} {
|
||||
data, err := os.ReadFile(filepath.Join(out, node, "user-data"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var cfg struct {
|
||||
Files []struct {
|
||||
Path string `yaml:"path"`
|
||||
Content string `yaml:"content"`
|
||||
} `yaml:"write_files"`
|
||||
}
|
||||
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var agent []byte
|
||||
for _, f := range cfg.Files {
|
||||
if f.Path == "/etc/lab/files/agent.yml" {
|
||||
if agent, err = base64.StdEncoding.DecodeString(f.Content); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !strings.Contains(string(agent), "route_reflectors: [10.255.255.1]") {
|
||||
t.Errorf("%s agent.yml = %q", node, agent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun_RenderReportsAMissingFRRConfig(t *testing.T) {
|
||||
|
|
@ -344,7 +373,7 @@ segments:
|
|||
underlay: { switch: sw1, cidr: 10.1.0.0/24 }
|
||||
nodes:
|
||||
sw1: { role: switch, image: deb, cpus: 1, memory: 512, frr: frr/absent.conf }
|
||||
hv1: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv1: { role: host, image: deb, cpus: 1, memory: 512, segments: [underlay] }
|
||||
`
|
||||
if err := os.WriteFile(topo, []byte(doc), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
|
|
@ -231,7 +231,7 @@ Ce que le fichier déclare :
|
|||
bridge (15 caractères au plus sous Linux).
|
||||
|
||||
``nodes``
|
||||
``role`` (``switch``, ``rr`` ou ``hypervisor``), ``image``, ``cpus``, ``memory`` en Mio
|
||||
``role`` (``switch``, ``rr`` ou ``host``), ``image``, ``cpus``, ``memory`` en Mio
|
||||
(256 au moins), ``segments`` auxquels le nœud est relié, et ``addresses`` pour fixer
|
||||
l'adresse d'un nœud sur un segment (``addresses: {underlay: 192.168.14.50}``). Un switch ne
|
||||
déclare ni ``segments`` ni ``addresses`` : il porte ceux dont il est le ``switch``.
|
||||
|
|
@ -247,15 +247,17 @@ Ce que le fichier déclare :
|
|||
(``loopback: 10.255.255.1/32``) ;
|
||||
* ``frr`` — le chemin d'un ``frr.conf``, relatif au fichier de topologie : FRR est installé
|
||||
au démarrage et la configuration déposée **telle quelle** (voir `Rôles`_).
|
||||
* ``release`` — **obligatoire pour un hyperviseur**, refusé ailleurs : le tag de la release de
|
||||
two que ``deploy.sh`` installe (``release: 0.2.0rc008``). Sans lui, ``deploy.sh`` prendrait la
|
||||
dernière release, et le lab ne serait plus reproductible.
|
||||
* ``agent`` — pour un hyperviseur seulement : le chemin d'un ``agent.yml``, relatif au fichier
|
||||
de topologie, déposé dans ``/etc/two/agent.yml`` avant ``deploy.sh``. Sans lui, l'agent tourne
|
||||
avec sa configuration par défaut. Les deux fichiers fournis donnent à l'agent la section
|
||||
``bgp`` (AS 64600, route reflector ``10.255.255.1``) et un watchdog toutes les 10 s ;
|
||||
``test/e2e/topologies/agent/two.yml`` met en plus hv1 sur le serveur DHCP intégré
|
||||
(``dhcp.backend: two``), ``agent/bgp.yml`` laisse les autres sur dnsmasq.
|
||||
* ``provision`` — le chemin d'un script, relatif au fichier de topologie, exécuté au premier
|
||||
démarrage **avant** FRR ;
|
||||
* ``post_provision`` — un second script, exécuté **après** FRR ;
|
||||
* ``files`` — des fichiers déposés dans ``/etc/lab/files/<nom>`` avant les scripts, sous la
|
||||
forme ``{<nom>: <chemin>}`` (``files: {agent.yml: agent/two.yml}``) ;
|
||||
* ``services`` — les unités systemd que ``lab up`` vérifie actives sur le nœud.
|
||||
|
||||
Les scripts reçoivent ``LAB_NODE`` (nom du nœud), ``LAB_UPLINK`` (interface de son premier
|
||||
segment, qui porte la route par défaut) et ``LAB_FILES`` (``/etc/lab/files``). L'outil ne sait
|
||||
rien de ce qu'ils installent : l'installation de two est une provision de
|
||||
``test/e2e/topologies/`` (voir `Rôles`_).
|
||||
|
||||
``mgmt0`` et ``lo1`` sont réservés : aucun segment ne peut porter ces noms.
|
||||
|
||||
|
|
@ -295,18 +297,18 @@ Limites : 1000 nœuds, 256 segments, et autant de câbles que la plage UDP le pe
|
|||
lab evpn-2hv: nodes 4, segments 1, cables 3
|
||||
|
||||
nodes
|
||||
name role image cpus memory ssh
|
||||
sw1 switch debian12 2 1024 MiB 127.0.0.1:2200
|
||||
rr1 rr debian12 1 1024 MiB 127.0.0.1:2201
|
||||
hv1 hypervisor debian12 4 16384 MiB 127.0.0.1:2202
|
||||
hv2 hypervisor debian12 4 16384 MiB 127.0.0.1:2203
|
||||
name role image cpus memory ssh
|
||||
sw1 switch debian12 2 1024 MiB 127.0.0.1:2200
|
||||
rr1 rr debian12 1 1024 MiB 127.0.0.1:2201
|
||||
hv1 host debian12 4 16384 MiB 127.0.0.1:2202
|
||||
hv2 host debian12 4 16384 MiB 127.0.0.1:2203
|
||||
|
||||
roles
|
||||
name loopback secondary frr release agent
|
||||
sw1 - underlay 169.254.0.1/28 sw1.conf - -
|
||||
rr1 lo1 10.255.255.1/32 underlay 169.254.0.3/28 rr1.conf - -
|
||||
hv1 - - hv1.conf 0.2.0rc008 two.yml
|
||||
hv2 - - hv2.conf 0.2.0rc008 bgp.yml
|
||||
name loopback secondary frr provision post_provision files services
|
||||
sw1 - underlay 169.254.0.1/28 sw1.conf - - - -
|
||||
rr1 lo1 10.255.255.1/32 underlay 169.254.0.3/28 rr1.conf - - - -
|
||||
hv1 - - hv1.conf hypervisor.sh hypervisor-post.sh agent.yml agent.service
|
||||
hv2 - - hv2.conf hypervisor.sh hypervisor-post.sh agent.yml agent.service
|
||||
|
||||
segment underlay: 192.168.14.0/24, mtu 9000, switch sw1, bridge br-underlay, gateway 192.168.14.1
|
||||
node interface address mac udp switch port mac udp
|
||||
|
|
@ -365,34 +367,42 @@ même valeur sur ``keys.openpgp.org`` et ``keyserver.ubuntu.com`` :
|
|||
Une clé renouvelée par FRR fera échouer l'installation (signature inconnue) : remplacer le fichier
|
||||
après avoir vérifié les nouvelles empreintes.
|
||||
|
||||
**Hyperviseurs.** Ils se déploient comme en production, par ``deploy.sh`` — celui **du dépôt**,
|
||||
embarqué dans ``lab`` avec ``bootstrap_kvm.sh`` (paquet ``scripts``) et déposé dans
|
||||
``/opt/two/scripts/``, où ``deploy.sh`` cherche d'abord ``bootstrap_kvm.sh`` :
|
||||
**Hyperviseurs.** Ce sont des nœuds ``host`` dont la provision installe two comme en
|
||||
production. ``test/e2e/topologies/provision/hypervisor.sh`` dépose ``agent.yml`` (fichier
|
||||
``agent.yml`` du nœud) dans ``/etc/two/agent.yml``, télécharge ``deploy.sh`` et
|
||||
``bootstrap_kvm.sh`` **au tag de la release testée** dans ``/opt/two/scripts/``, où ``deploy.sh``
|
||||
cherche d'abord ``bootstrap_kvm.sh``, puis lance :
|
||||
|
||||
.. code-block:: text
|
||||
|
||||
deploy.sh --noup_script -i -u <segment> -t <release>
|
||||
deploy.sh --noup_script -i -u "$LAB_UPLINK" -t <release>
|
||||
|
||||
``--noup_script`` empêche l'auto-mise à jour de remplacer le script par celui de ``main`` : le lab
|
||||
teste les scripts de sa branche. L'uplink ``-u`` est l'interface qui porte la route par défaut —
|
||||
celle du premier segment de l'hyperviseur dans l'ordre de déclaration des segments — parce que
|
||||
``deploy.sh`` y lit l'adresse et la passerelle qu'il déplace sur ``br-000000``. ``deploy.sh``
|
||||
télécharge la release sur ``git.g3e.fr`` sans réessayer : le lancement attend d'abord que le serveur
|
||||
réponde, à travers le switch. FRR est installé **après** : il démarre sur le réseau final. Démarré
|
||||
La release est fixée dans le script (``RELEASE=``) : sans elle, ``deploy.sh`` prendrait la dernière,
|
||||
et le lab ne serait plus reproductible. ``--noup_script`` empêche l'auto-mise à jour de remplacer le
|
||||
script par celui de ``main``. L'uplink ``-u`` est l'interface qui porte la route par défaut, parce
|
||||
que ``deploy.sh`` y lit l'adresse et la passerelle qu'il déplace sur ``br-000000``. ``deploy.sh``
|
||||
télécharge la release sur ``git.g3e.fr`` sans réessayer : le script attend d'abord que le serveur
|
||||
réponde, à travers le switch. Les deux fichiers ``agent/`` donnent à l'agent la section ``bgp``
|
||||
(AS 64600, route reflector ``10.255.255.1``) et un watchdog toutes les 10 s ;
|
||||
``agent/two.yml`` met en plus hv1 sur le serveur DHCP intégré (``dhcp.backend: two``),
|
||||
``agent/bgp.yml`` laisse les autres sur dnsmasq.
|
||||
|
||||
FRR est installé **après** la provision : il démarre sur le réseau final. Démarré
|
||||
avant la migration, zebra peut perdre la route par défaut quand elle passe de l'uplink à
|
||||
``br-000000`` — constaté le 2026-10-11 sur un hyperviseur sur deux : route reflector
|
||||
``unresolved``, session BGP jamais établie. L'agent, configuré avec une section ``bgp``, refuse de
|
||||
démarrer tant que FRR n'est pas là : son premier démarrage par ``deploy.sh`` échoue, et le
|
||||
provisionnement le **redémarre** une fois FRR installé.
|
||||
démarrer tant que FRR n'est pas là : son premier démarrage par ``deploy.sh`` échoue, et
|
||||
``provision/hypervisor-post.sh`` (``post_provision``) le **redémarre** une fois FRR installé.
|
||||
|
||||
**Un seul script de provisionnement par nœud.** cloud-init exécute ``runcmd`` comme un script
|
||||
``sh`` sans ``set -e`` : seule la dernière commande compte, et un ``deploy.sh`` en échec suivi d'un
|
||||
FRR installé avec succès passerait pour un démarrage réussi. Chaque nœud reçoit donc
|
||||
``/usr/local/sbin/lab-provision``, en ``set -eu``, qui enchaîne ses étapes ; ``runcmd`` n'appelle
|
||||
``/usr/local/sbin/lab-provision``, en ``set -eu``, qui enchaîne ses étapes — script du switch ou
|
||||
de la loopback, ``provision``, FRR, ``post_provision`` ; ``runcmd`` n'appelle
|
||||
que lui, et la première étape en échec met cloud-init en erreur.
|
||||
|
||||
**Ce que vérifie** ``lab up``, une fois cloud-init terminé sans erreur : ``agent.service`` actif
|
||||
sur chaque hyperviseur, ``frr`` actif sur chaque nœud qui en a un. Ce contrôle couvre ce que
|
||||
**Ce que vérifie** ``lab up``, une fois cloud-init terminé sans erreur : les ``services`` déclarés
|
||||
par chaque nœud (``agent.service`` sur les hyperviseurs), et ``frr`` sur chaque nœud qui en a un. Ce contrôle couvre ce que
|
||||
cloud-init ne voit pas — si la migration réseau échoue, ``deploy.sh`` arme un redémarrage de
|
||||
secours, et la VM redémarrée ne rejoue pas ``runcmd``.
|
||||
|
||||
|
|
|
|||
|
|
@ -168,10 +168,7 @@ func (l Lab) waitReady(ctx context.Context, n topology.NodePlan) error {
|
|||
}
|
||||
|
||||
func services(n topology.NodePlan) []string {
|
||||
var units []string
|
||||
if n.Role == topology.RoleHypervisor {
|
||||
units = append(units, "agent.service")
|
||||
}
|
||||
units := append([]string(nil), n.Services...)
|
||||
if n.FRR != "" {
|
||||
units = append(units, "frr.service")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -127,9 +127,9 @@ nodes:
|
|||
return p
|
||||
}
|
||||
|
||||
const switchLast = ` rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay] }
|
||||
hv1: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
sw1: { role: switch, image: deb, cpus: 2, memory: 1024 }
|
||||
const switchLast = ` rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay] }
|
||||
hv1: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay], services: [agent.service] }
|
||||
sw1: { role: switch, image: deb, cpus: 2, memory: 1024 }
|
||||
`
|
||||
|
||||
type fixture struct {
|
||||
|
|
@ -278,10 +278,10 @@ func TestStatus(t *testing.T) {
|
|||
if w < 3 {
|
||||
w = 3
|
||||
}
|
||||
want := fmt.Sprintf("node role state %-*s ssh\n", w, "pid") +
|
||||
fmt.Sprintf("rr1 rr stopped %-*s 127.0.0.1:2200\n", w, "-") +
|
||||
fmt.Sprintf("hv1 hypervisor running %-*s 127.0.0.1:2201\n", w, pid) +
|
||||
fmt.Sprintf("sw1 switch stopped %-*s 127.0.0.1:2202\n", w, "-")
|
||||
want := fmt.Sprintf("node role state %-*s ssh\n", w, "pid") +
|
||||
fmt.Sprintf("rr1 rr stopped %-*s 127.0.0.1:2200\n", w, "-") +
|
||||
fmt.Sprintf("hv1 host running %-*s 127.0.0.1:2201\n", w, pid) +
|
||||
fmt.Sprintf("sw1 switch stopped %-*s 127.0.0.1:2202\n", w, "-")
|
||||
if out.String() != want {
|
||||
t.Errorf("status:\n%s\nwant:\n%s", out.String(), want)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,15 +72,11 @@ func Prepare(ctx context.Context, p *topology.Plan, o Options) ([]render.Node, e
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
frr, err := ReadFRR(p)
|
||||
if err != nil {
|
||||
opts := render.Options{RunDir: o.RunDir, AuthorizedKeys: []string{key}}
|
||||
if err := ReadInputs(p, &opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
agent, err := ReadAgent(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes, err := render.Render(p, render.Options{RunDir: o.RunDir, AuthorizedKeys: []string{key}, FRR: frr, Agent: agent})
|
||||
nodes, err := render.Render(p, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -92,12 +88,31 @@ func Prepare(ctx context.Context, p *topology.Plan, o Options) ([]render.Node, e
|
|||
return nodes, nil
|
||||
}
|
||||
|
||||
func ReadFRR(p *topology.Plan) (map[string]string, error) {
|
||||
return readNodeFiles(p, func(n topology.NodePlan) string { return n.FRR })
|
||||
}
|
||||
|
||||
func ReadAgent(p *topology.Plan) (map[string]string, error) {
|
||||
return readNodeFiles(p, func(n topology.NodePlan) string { return n.Agent })
|
||||
func ReadInputs(p *topology.Plan, o *render.Options) error {
|
||||
var err error
|
||||
if o.FRR, err = readNodeFiles(p, func(n topology.NodePlan) string { return n.FRR }); err != nil {
|
||||
return err
|
||||
}
|
||||
if o.Provision, err = readNodeFiles(p, func(n topology.NodePlan) string { return n.Provision }); err != nil {
|
||||
return err
|
||||
}
|
||||
if o.PostProvision, err = readNodeFiles(p, func(n topology.NodePlan) string { return n.PostProvision }); err != nil {
|
||||
return err
|
||||
}
|
||||
o.Files = map[string]map[string][]byte{}
|
||||
for _, n := range p.Nodes {
|
||||
for name, src := range n.Files {
|
||||
data, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
return fmt.Errorf("node %s: file %s: %w", n.Name, name, err)
|
||||
}
|
||||
if o.Files[n.Name] == nil {
|
||||
o.Files[n.Name] = map[string][]byte{}
|
||||
}
|
||||
o.Files[n.Name][name] = data
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readNodeFiles(p *topology.Plan, path func(topology.NodePlan) string) (map[string]string, error) {
|
||||
|
|
|
|||
|
|
@ -60,8 +60,8 @@ segments:
|
|||
nodes:
|
||||
sw1: { role: switch, image: debian12, cpus: 2, memory: 1024 }
|
||||
rr1: { role: rr, image: debian12, cpus: 1, memory: 1024, segments: [underlay] }
|
||||
hv1: { role: hypervisor, image: debian12, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv2: { role: hypervisor, image: debian12, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv1: { role: host, image: debian12, cpus: 4, memory: 16384, segments: [underlay] }
|
||||
hv2: { role: host, image: debian12, cpus: 4, memory: 16384, segments: [underlay] }
|
||||
`
|
||||
topo, err := topology.Parse([]byte(doc))
|
||||
if err != nil {
|
||||
|
|
@ -316,7 +316,7 @@ func TestEnsureKey_WithTheRealSSHKeygen(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestReadFRR_ReadsOnlyTheNodesThatDeclareOne(t *testing.T) {
|
||||
func TestReadInputs_ReadsOnlyTheFRRConfigsDeclared(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
conf := filepath.Join(dir, "rr1.conf")
|
||||
if err := os.WriteFile(conf, []byte("hostname rr1\n"), 0o600); err != nil {
|
||||
|
|
@ -326,19 +326,20 @@ func TestReadFRR_ReadsOnlyTheNodesThatDeclareOne(t *testing.T) {
|
|||
p := labPlan(t, m)
|
||||
p.Nodes[1].FRR = conf
|
||||
|
||||
got, err := ReadFRR(p)
|
||||
var o render.Options
|
||||
err := ReadInputs(p, &o)
|
||||
|
||||
if err != nil || !reflect.DeepEqual(got, map[string]string{"rr1": "hostname rr1\n"}) {
|
||||
t.Errorf("ReadFRR = %q, %v", got, err)
|
||||
if err != nil || !reflect.DeepEqual(o.FRR, map[string]string{"rr1": "hostname rr1\n"}) {
|
||||
t.Errorf("FRR = %q, %v", o.FRR, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFRR_NamesTheNodeOfAMissingFile(t *testing.T) {
|
||||
func TestReadInputs_NamesTheNodeOfAMissingFRRConfig(t *testing.T) {
|
||||
m := newMirror(t)
|
||||
p := labPlan(t, m)
|
||||
p.Nodes[2].FRR = filepath.Join(t.TempDir(), "absent.conf")
|
||||
|
||||
if _, err := ReadFRR(p); err == nil || !strings.Contains(err.Error(), "node hv1: ") || !strings.Contains(err.Error(), "absent.conf") {
|
||||
if err := ReadInputs(p, &render.Options{}); err == nil || !strings.Contains(err.Error(), "node hv1: ") || !strings.Contains(err.Error(), "absent.conf") {
|
||||
t.Errorf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -386,22 +387,37 @@ func TestPrepare_PutsTheFRRConfigIntoTheSeed(t *testing.T) {
|
|||
t.Errorf("hv1 user-data has no /etc/lab/frr.conf:\n%s", data)
|
||||
}
|
||||
|
||||
func TestReadAgent_ReadsOnlyTheNodesThatDeclareOne(t *testing.T) {
|
||||
conf := filepath.Join(t.TempDir(), "two.yml")
|
||||
if err := os.WriteFile(conf, []byte("dhcp:\n backend: two\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
func TestReadInputs_ReadsTheProvisionScriptsAndFiles(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
write := func(name, content string) string {
|
||||
path := filepath.Join(dir, name)
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
m := newMirror(t)
|
||||
p := labPlan(t, m)
|
||||
p.Nodes[2].Agent = conf
|
||||
p.Nodes[2].Provision = write("hv.sh", "#!/bin/sh\necho provision\n")
|
||||
p.Nodes[2].PostProvision = write("hv-post.sh", "#!/bin/sh\necho post\n")
|
||||
p.Nodes[2].Files = map[string]string{"agent.yml": write("two.yml", "dhcp:\n backend: two\n")}
|
||||
|
||||
got, err := ReadAgent(p)
|
||||
|
||||
if err != nil || !reflect.DeepEqual(got, map[string]string{"hv1": "dhcp:\n backend: two\n"}) {
|
||||
t.Errorf("ReadAgent = %q, %v", got, err)
|
||||
var o render.Options
|
||||
if err := ReadInputs(p, &o); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p.Nodes[3].Agent = filepath.Join(t.TempDir(), "absent.yml")
|
||||
if _, err := ReadAgent(p); err == nil || !strings.Contains(err.Error(), "node hv2: ") {
|
||||
if !reflect.DeepEqual(o.Provision, map[string]string{"hv1": "#!/bin/sh\necho provision\n"}) {
|
||||
t.Errorf("provision = %q", o.Provision)
|
||||
}
|
||||
if !reflect.DeepEqual(o.PostProvision, map[string]string{"hv1": "#!/bin/sh\necho post\n"}) {
|
||||
t.Errorf("post_provision = %q", o.PostProvision)
|
||||
}
|
||||
if !reflect.DeepEqual(o.Files, map[string]map[string][]byte{"hv1": {"agent.yml": []byte("dhcp:\n backend: two\n")}}) {
|
||||
t.Errorf("files = %q", o.Files)
|
||||
}
|
||||
|
||||
p.Nodes[3].Files = map[string]string{"agent.yml": filepath.Join(dir, "absent.yml")}
|
||||
if err := ReadInputs(p, &render.Options{}); err == nil || !strings.Contains(err.Error(), "node hv2: file agent.yml: ") {
|
||||
t.Errorf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,9 @@ type Options struct {
|
|||
RunDir string
|
||||
AuthorizedKeys []string
|
||||
FRR map[string]string
|
||||
Agent map[string]string
|
||||
Provision map[string]string
|
||||
PostProvision map[string]string
|
||||
Files map[string]map[string][]byte
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
|
|
@ -51,8 +53,16 @@ func Render(p *topology.Plan, o Options) ([]Node, error) {
|
|||
if _, ok := o.FRR[n.Name]; n.FRR != "" && !ok {
|
||||
return nil, fmt.Errorf("node %s: frr configuration %s was not read", n.Name, n.FRR)
|
||||
}
|
||||
if _, ok := o.Agent[n.Name]; n.Agent != "" && !ok {
|
||||
return nil, fmt.Errorf("node %s: agent configuration %s was not read", n.Name, n.Agent)
|
||||
if _, ok := o.Provision[n.Name]; n.Provision != "" && !ok {
|
||||
return nil, fmt.Errorf("node %s: provision script %s was not read", n.Name, n.Provision)
|
||||
}
|
||||
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
|
||||
|
|
|
|||
|
|
@ -22,8 +22,8 @@ segments:
|
|||
nodes:
|
||||
sw1: { role: switch, image: deb, cpus: 2, memory: 1024 }
|
||||
rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay] }
|
||||
hv1: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv2: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv1: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay] }
|
||||
hv2: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay] }
|
||||
`
|
||||
|
||||
const twoSegments = `name: two-seg
|
||||
|
|
@ -36,7 +36,7 @@ segments:
|
|||
blue: { switch: sw, cidr: 10.2.0.0/24, mtu: 1500 }
|
||||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [blue, red], release: 0.2.0rc002 }
|
||||
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [blue, red] }
|
||||
`
|
||||
|
||||
func plan(t *testing.T, doc string) *topology.Plan {
|
||||
|
|
@ -315,9 +315,9 @@ func TestNetworkConfig_DefaultRouteOnlyOnFirstSegment(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestUserData_Hypervisor(t *testing.T) {
|
||||
func TestUserData_HostWithoutProvision(t *testing.T) {
|
||||
cfg := user(t, nodeNamed(t, renderAll(t, twoHypervisors), "hv1"))
|
||||
if cfg.Hostname != "hv1" || !cfg.DisableRoot || len(cfg.Packages) != 0 || !reflect.DeepEqual(cfg.Runcmd, [][]string{{"/usr/local/sbin/lab-provision"}}) {
|
||||
if cfg.Hostname != "hv1" || !cfg.DisableRoot || len(cfg.Packages) != 0 || cfg.Runcmd != nil || len(cfg.WriteFiles) != 0 {
|
||||
t.Errorf("hv1 user-data = %+v", cfg)
|
||||
}
|
||||
if !reflect.DeepEqual(cfg.SSHAuthorizedKeys, []string{labKey}) {
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
|
@ -21,8 +19,8 @@ segments:
|
|||
nodes:
|
||||
sw1: { role: switch, image: deb, cpus: 2, memory: 1024, secondary: { underlay: [169.254.0.1/28] }, frr: sw1.conf }
|
||||
rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay], secondary: { underlay: [169.254.0.3/28] }, loopback: 10.255.255.1/32, frr: rr1.conf }
|
||||
hv1: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], frr: hv1.conf, release: 0.2.0rc002 }
|
||||
hv2: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv1: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay], frr: hv1.conf, provision: hv.sh, post_provision: hv-post.sh, files: { agent.yml: two.yml }, services: [agent.service] }
|
||||
hv2: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay] }
|
||||
`
|
||||
|
||||
var frrConfigs = map[string]string{
|
||||
|
|
@ -31,9 +29,20 @@ var frrConfigs = map[string]string{
|
|||
"hv1": "hostname hv1\nrouter bgp 64600\n",
|
||||
}
|
||||
|
||||
func roleOptions() Options {
|
||||
return Options{
|
||||
RunDir: "/srv/lab",
|
||||
AuthorizedKeys: []string{labKey},
|
||||
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")}},
|
||||
}
|
||||
}
|
||||
|
||||
func renderRoles(t *testing.T) map[string]Node {
|
||||
t.Helper()
|
||||
nodes, err := Render(plan(t, withRoles), Options{RunDir: "/srv/lab", AuthorizedKeys: []string{labKey}, FRR: frrConfigs})
|
||||
nodes, err := Render(plan(t, withRoles), roleOptions())
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
|
|
@ -145,10 +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 "
|
||||
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": "/usr/local/sbin/lab-deploy\n/usr/local/sbin/lab-frr\nsystemctl restart agent.service\n",
|
||||
"hv2": "/usr/local/sbin/lab-deploy\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",
|
||||
} {
|
||||
cfg := user(t, nodeNamed(t, nodes, name))
|
||||
if !reflect.DeepEqual(cfg.Runcmd, [][]string{{"/usr/local/sbin/lab-provision"}}) {
|
||||
|
|
@ -159,47 +168,34 @@ func TestRoles_ProvisioningIsOneScriptThatStopsAtTheFirstFailure(t *testing.T) {
|
|||
t.Errorf("%s lab-provision (%s):\n%s", name, f.Permissions, f.Content)
|
||||
}
|
||||
}
|
||||
if cfg := user(t, nodeNamed(t, nodes, "hv2")); cfg.Runcmd != nil {
|
||||
t.Errorf("hv2 has nothing to provision, runcmd = %q", cfg.Runcmd)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoles_HypervisorDeploysTheReleaseThroughTheRepositoryScripts(t *testing.T) {
|
||||
func TestRoles_HostReceivesItsScriptsAndFiles(t *testing.T) {
|
||||
cfg := user(t, nodeNamed(t, renderRoles(t), "hv1"))
|
||||
|
||||
f := fileAt(t, cfg, "/usr/local/sbin/lab-deploy")
|
||||
want := `#!/bin/sh
|
||||
set -eu
|
||||
n=0
|
||||
until curl -fsS -o /dev/null https://git.g3e.fr/; do
|
||||
n=$((n + 1))
|
||||
[ "$n" -lt 30 ] || exit 1
|
||||
sleep 10
|
||||
done
|
||||
cd /opt/two/scripts
|
||||
bash ./deploy.sh --noup_script -i -u underlay -t 0.2.0rc002
|
||||
`
|
||||
if f.Content != want || f.Permissions != "0755" {
|
||||
t.Errorf("lab-deploy (%s):\n%s\nwant:\n%s", f.Permissions, f.Content, want)
|
||||
}
|
||||
|
||||
for path, source := range map[string]string{
|
||||
"/opt/two/scripts/deploy.sh": "../../../scripts/deploy.sh",
|
||||
"/opt/two/scripts/bootstrap_kvm.sh": "../../../scripts/bootstrap_kvm.sh",
|
||||
for path, want := range map[string]struct {
|
||||
content string
|
||||
permissions string
|
||||
}{
|
||||
"/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)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo, err := os.ReadFile(source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.Encoding != "b64" || f.Permissions != "0755" || !bytes.Equal(got, repo) {
|
||||
t.Errorf("%s: encoding %q, permissions %q, identical to %s: %v", path, f.Encoding, f.Permissions, source, bytes.Equal(got, repo))
|
||||
if f.Encoding != "b64" || f.Permissions != want.permissions || string(got) != want.content {
|
||||
t.Errorf("%s: encoding %q, permissions %q, content %q", path, f.Encoding, f.Permissions, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoles_UplinkIsTheInterfaceCarryingTheDefaultRoute(t *testing.T) {
|
||||
p := plan(t, twoSegments)
|
||||
hv := nodeNamed(t, renderAll(t, twoSegments), "hv")
|
||||
var withDefault []string
|
||||
for name, e := range network(t, hv).Ethernets {
|
||||
|
|
@ -212,22 +208,36 @@ func TestRoles_UplinkIsTheInterfaceCarryingTheDefaultRoute(t *testing.T) {
|
|||
if !reflect.DeepEqual(withDefault, []string{"red"}) {
|
||||
t.Fatalf("default route on %v, want [red]", withDefault)
|
||||
}
|
||||
if got := fileAt(t, user(t, hv), "/usr/local/sbin/lab-deploy").Content; !strings.HasSuffix(got, "bash ./deploy.sh --noup_script -i -u red -t 0.2.0rc002\n") {
|
||||
t.Errorf("lab-deploy:\n%s", got)
|
||||
for _, n := range p.Nodes {
|
||||
if n.Name == "hv" {
|
||||
if got := provisionEnv(p, n); got != "LAB_NODE=hv LAB_UPLINK=red LAB_FILES=/etc/lab/files " {
|
||||
t.Errorf("provision environment = %q", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoles_OnlyHypervisorsDeployTwo(t *testing.T) {
|
||||
func TestRoles_NodesWithoutProvisionReceiveNoProvisionFiles(t *testing.T) {
|
||||
nodes := renderRoles(t)
|
||||
for _, name := range []string{"sw1", "rr1"} {
|
||||
for _, name := range []string{"sw1", "rr1", "hv2"} {
|
||||
for _, f := range user(t, nodeNamed(t, nodes, name)).WriteFiles {
|
||||
if strings.HasPrefix(f.Path, "/opt/two/") || f.Path == "/usr/local/sbin/lab-deploy" {
|
||||
if strings.HasPrefix(f.Path, "/etc/lab/files/") || strings.HasPrefix(f.Path, "/usr/local/sbin/lab-node-") {
|
||||
t.Errorf("%s receives %s", name, f.Path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoles_FRRDaemons(t *testing.T) {
|
||||
p := plan(t, withRoles)
|
||||
want := map[string][]string{"sw1": {"bgpd", "bfdd"}, "rr1": {"bgpd", "bfdd"}, "hv1": {"bgpd"}}
|
||||
for _, n := range p.Nodes {
|
||||
if w, ok := want[n.Name]; ok && !reflect.DeepEqual(frrDaemons(n), w) {
|
||||
t.Errorf("%s daemons = %v, want %v", n.Name, frrDaemons(n), w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoles_NodeWithoutRoleFieldsGetsNoFRR(t *testing.T) {
|
||||
for _, f := range user(t, nodeNamed(t, renderRoles(t), "hv2")).WriteFiles {
|
||||
if strings.Contains(f.Path, "frr") {
|
||||
|
|
@ -246,7 +256,7 @@ func TestRoles_RefusesAnUnreadFRRConfig(t *testing.T) {
|
|||
|
||||
func TestRoles_SwitchLoopbackIsCreatedByTheSwitchScript(t *testing.T) {
|
||||
doc := strings.Replace(withRoles, "secondary: { underlay: [169.254.0.1/28] }, frr: sw1.conf", "secondary: { underlay: [169.254.0.1/28] }, loopback: 10.255.254.1/32, frr: sw1.conf", 1)
|
||||
nodes, err := Render(plan(t, doc), Options{RunDir: "/srv/lab", AuthorizedKeys: []string{labKey}, FRR: frrConfigs})
|
||||
nodes, err := Render(plan(t, doc), roleOptions())
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
|
|
@ -263,36 +273,20 @@ func TestRoles_SwitchLoopbackIsCreatedByTheSwitchScript(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRoles_HypervisorAgentConfigIsWrittenBeforeTheDeployment(t *testing.T) {
|
||||
doc := strings.Replace(withRoles, "frr: hv1.conf, release: 0.2.0rc002 }", "frr: hv1.conf, release: 0.2.0rc002, agent: two.yml }", 1)
|
||||
nodes, err := Render(plan(t, doc), Options{RunDir: "/srv/lab", AuthorizedKeys: []string{labKey}, FRR: frrConfigs, Agent: map[string]string{"hv1": "dhcp:\n backend: two\n"}})
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
func TestRoles_RefusesUnreadProvisionInputs(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
mutate func(*Options)
|
||||
want string
|
||||
}{
|
||||
"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"},
|
||||
}
|
||||
var hv1, hv2 Node
|
||||
for _, n := range nodes {
|
||||
switch n.Name {
|
||||
case "hv1":
|
||||
hv1 = n
|
||||
case "hv2":
|
||||
hv2 = n
|
||||
}
|
||||
}
|
||||
f := fileAt(t, user(t, hv1), "/etc/two/agent.yml")
|
||||
if f.Content != "dhcp:\n backend: two\n" || f.Permissions != "0640" {
|
||||
t.Errorf("agent.yml (%s) = %q", f.Permissions, f.Content)
|
||||
}
|
||||
for _, w := range user(t, hv2).WriteFiles {
|
||||
if w.Path == "/etc/two/agent.yml" {
|
||||
t.Error("hv2 receives an agent.yml it does not declare")
|
||||
for name, c := range cases {
|
||||
o := roleOptions()
|
||||
c.mutate(&o)
|
||||
if _, err := Render(plan(t, withRoles), o); err == nil || err.Error() != c.want {
|
||||
t.Errorf("%s: error = %v, want %q", name, err, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoles_RefusesAnUnreadAgentConfig(t *testing.T) {
|
||||
doc := strings.Replace(withRoles, "frr: hv1.conf, release: 0.2.0rc002 }", "frr: hv1.conf, release: 0.2.0rc002, agent: two.yml }", 1)
|
||||
_, err := Render(plan(t, doc), Options{RunDir: "/srv/lab", AuthorizedKeys: []string{labKey}, FRR: frrConfigs})
|
||||
if err == nil || err.Error() != "node hv1: agent configuration two.yml was not read" {
|
||||
t.Errorf("error = %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,12 +4,12 @@ import (
|
|||
_ "embed"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"go.yaml.in/yaml/v3"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/lab/topology"
|
||||
"git.g3e.fr/syonad/two/scripts"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -27,11 +27,10 @@ const (
|
|||
FRRRepo = "https://deb.frrouting.org/frr"
|
||||
FRRPackages = "frr frr-pythontools"
|
||||
|
||||
ProvisionScript = "/usr/local/sbin/lab-provision"
|
||||
DeployScript = "/usr/local/sbin/lab-deploy"
|
||||
TwoScriptsDir = "/opt/two/scripts"
|
||||
TwoGitServer = "https://git.g3e.fr/"
|
||||
AgentConfig = "/etc/two/agent.yml"
|
||||
ProvisionScript = "/usr/local/sbin/lab-provision"
|
||||
NodeProvision = "/usr/local/sbin/lab-node-provision"
|
||||
NodePostProvision = "/usr/local/sbin/lab-node-post-provision"
|
||||
FilesDir = "/etc/lab/files"
|
||||
)
|
||||
|
||||
//go:embed frrouting.gpg
|
||||
|
|
@ -115,16 +114,17 @@ func userData(p *topology.Plan, n topology.NodePlan, o Options) ([]byte, error)
|
|||
}
|
||||
steps = append(steps, "systemctl daemon-reload", "systemctl enable --now lab-node.service")
|
||||
}
|
||||
if n.Agent != "" {
|
||||
cfg.WriteFiles = append(cfg.WriteFiles, writeFile{Path: AgentConfig, Permissions: "0640", Content: o.Agent[n.Name]})
|
||||
names := make([]string, 0, len(n.Files))
|
||||
for name := range n.Files {
|
||||
names = append(names, name)
|
||||
}
|
||||
if n.Role == topology.RoleHypervisor {
|
||||
cfg.WriteFiles = append(cfg.WriteFiles,
|
||||
writeFile{Path: TwoScriptsDir + "/deploy.sh", Permissions: "0755", Encoding: "b64", Content: base64.StdEncoding.EncodeToString(scripts.Deploy)},
|
||||
writeFile{Path: TwoScriptsDir + "/bootstrap_kvm.sh", Permissions: "0755", Encoding: "b64", Content: base64.StdEncoding.EncodeToString(scripts.BootstrapKVM)},
|
||||
writeFile{Path: DeployScript, Permissions: "0755", Content: deployScript(p, n)},
|
||||
)
|
||||
steps = append(steps, DeployScript)
|
||||
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 n.Provision != "" {
|
||||
cfg.WriteFiles = append(cfg.WriteFiles, writeFile{Path: NodeProvision, Permissions: "0755", Encoding: "b64", Content: base64.StdEncoding.EncodeToString([]byte(o.Provision[n.Name]))})
|
||||
steps = append(steps, provisionEnv(p, n)+NodeProvision)
|
||||
}
|
||||
if n.FRR != "" {
|
||||
cfg.WriteFiles = append(cfg.WriteFiles,
|
||||
|
|
@ -133,9 +133,10 @@ func userData(p *topology.Plan, n topology.NodePlan, o Options) ([]byte, error)
|
|||
writeFile{Path: FRRScript, Permissions: "0755", Content: frrScript(n)},
|
||||
)
|
||||
steps = append(steps, FRRScript)
|
||||
if n.Role == topology.RoleHypervisor {
|
||||
steps = append(steps, "systemctl restart agent.service")
|
||||
}
|
||||
}
|
||||
if n.PostProvision != "" {
|
||||
cfg.WriteFiles = append(cfg.WriteFiles, writeFile{Path: NodePostProvision, Permissions: "0755", Encoding: "b64", Content: base64.StdEncoding.EncodeToString([]byte(o.PostProvision[n.Name]))})
|
||||
steps = append(steps, provisionEnv(p, n)+NodePostProvision)
|
||||
}
|
||||
if len(steps) > 0 {
|
||||
cfg.WriteFiles = append(cfg.WriteFiles,
|
||||
|
|
@ -154,16 +155,16 @@ func retry(command string) string {
|
|||
return "n=0\nuntil " + command + "; do\n n=$((n + 1))\n [ \"$n\" -lt 30 ] || exit 1\n sleep 10\ndone\n"
|
||||
}
|
||||
|
||||
func deployScript(p *topology.Plan, n topology.NodePlan) string {
|
||||
uplink := nodeCables(p, n.Name)[0].NodeInterface
|
||||
return "#!/bin/sh\nset -eu\n" +
|
||||
retry("curl -fsS -o /dev/null "+TwoGitServer) +
|
||||
"cd " + TwoScriptsDir + "\n" +
|
||||
fmt.Sprintf("bash ./deploy.sh --noup_script -i -u %s -t %s\n", uplink, n.Release)
|
||||
func provisionEnv(p *topology.Plan, n topology.NodePlan) string {
|
||||
uplink := ""
|
||||
if cables := nodeCables(p, n.Name); len(cables) > 0 {
|
||||
uplink = cables[0].NodeInterface
|
||||
}
|
||||
return fmt.Sprintf("LAB_NODE=%s LAB_UPLINK=%s LAB_FILES=%s ", n.Name, uplink, FilesDir)
|
||||
}
|
||||
|
||||
func frrDaemons(n topology.NodePlan) []string {
|
||||
if n.Role == topology.RoleHypervisor {
|
||||
if n.Role == topology.RoleHost {
|
||||
return []string{"bgpd"}
|
||||
}
|
||||
return []string{"bgpd", "bfdd"}
|
||||
|
|
|
|||
|
|
@ -22,15 +22,17 @@ func (p *Plan) Write(w io.Writer) error {
|
|||
|
||||
var extras []NodePlan
|
||||
for _, n := range p.Nodes {
|
||||
if len(n.Secondary) > 0 || n.Loopback.IsValid() || n.FRR != "" || n.Release != "" || n.Agent != "" {
|
||||
if len(n.Secondary) > 0 || n.Loopback.IsValid() || n.FRR != "" || n.Provision != "" || n.PostProvision != "" || len(n.Files) > 0 || len(n.Services) > 0 {
|
||||
extras = append(extras, n)
|
||||
}
|
||||
}
|
||||
if len(extras) > 0 {
|
||||
fmt.Fprintf(tw, "\nroles\n")
|
||||
fmt.Fprintf(tw, " name\tloopback\tsecondary\tfrr\trelease\tagent\n")
|
||||
fmt.Fprintf(tw, " name\tloopback\tsecondary\tfrr\tprovision\tpost_provision\tfiles\tservices\n")
|
||||
for _, n := range extras {
|
||||
fmt.Fprintf(tw, " %s\t%s\t%s\t%s\t%s\t%s\n", n.Name, orDash(loopback(n)), orDash(secondary(n)), orDash(filepath.Base(n.FRR)), orDash(n.Release), orDash(filepath.Base(n.Agent)))
|
||||
fmt.Fprintf(tw, " %s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", n.Name, orDash(loopback(n)), orDash(secondary(n)),
|
||||
orDash(filepath.Base(n.FRR)), orDash(filepath.Base(n.Provision)), orDash(filepath.Base(n.PostProvision)),
|
||||
orDash(strings.Join(sortedKeys(n.Files), ",")), orDash(strings.Join(n.Services, ",")))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,17 +34,19 @@ type SegmentPlan struct {
|
|||
}
|
||||
|
||||
type NodePlan struct {
|
||||
Name string
|
||||
Role string
|
||||
Image string
|
||||
CPUs int
|
||||
Memory int
|
||||
SSHPort int
|
||||
Secondary map[string][]netip.Prefix
|
||||
Loopback netip.Prefix
|
||||
FRR string
|
||||
Release string
|
||||
Agent string
|
||||
Name string
|
||||
Role string
|
||||
Image string
|
||||
CPUs int
|
||||
Memory int
|
||||
SSHPort int
|
||||
Secondary map[string][]netip.Prefix
|
||||
Loopback netip.Prefix
|
||||
FRR string
|
||||
Provision string
|
||||
PostProvision string
|
||||
Files map[string]string
|
||||
Services []string
|
||||
}
|
||||
|
||||
type Cable struct {
|
||||
|
|
@ -71,15 +73,17 @@ func Compute(t *Topology) (*Plan, error) {
|
|||
p := &Plan{Name: t.Name, Images: append([]Image(nil), t.Images...)}
|
||||
for i, n := range t.Nodes {
|
||||
node := NodePlan{
|
||||
Name: n.Name,
|
||||
Role: n.Role,
|
||||
Image: n.Image,
|
||||
CPUs: n.CPUs,
|
||||
Memory: n.Memory,
|
||||
SSHPort: SSHBasePort + i,
|
||||
FRR: n.FRR,
|
||||
Release: n.Release,
|
||||
Agent: n.Agent,
|
||||
Name: n.Name,
|
||||
Role: n.Role,
|
||||
Image: n.Image,
|
||||
CPUs: n.CPUs,
|
||||
Memory: n.Memory,
|
||||
SSHPort: SSHBasePort + i,
|
||||
FRR: n.FRR,
|
||||
Provision: n.Provision,
|
||||
PostProvision: n.PostProvision,
|
||||
Files: n.Files,
|
||||
Services: n.Services,
|
||||
}
|
||||
for segment, raws := range n.Secondary {
|
||||
for _, raw := range raws {
|
||||
|
|
|
|||
|
|
@ -55,8 +55,8 @@ segments:
|
|||
nodes:
|
||||
sw1: { role: switch, image: deb, cpus: 2, memory: 1024 }
|
||||
rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay] }
|
||||
hv1: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv2: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], release: 0.2.0rc002 }
|
||||
hv1: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay] }
|
||||
hv2: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay] }
|
||||
`
|
||||
|
||||
func TestCompute_TwoHypervisorsPlan(t *testing.T) {
|
||||
|
|
@ -102,7 +102,7 @@ func TestCompute_TwoHypervisorsPlan(t *testing.T) {
|
|||
t.Errorf("%s ssh port = %d, want %d", name, got, port)
|
||||
}
|
||||
}
|
||||
if hv := nodeOf(t, p, "hv1"); hv.Role != "hypervisor" || hv.CPUs != 4 || hv.Memory != 16384 || hv.Image != "deb" {
|
||||
if hv := nodeOf(t, p, "hv1"); hv.Role != "host" || hv.CPUs != 4 || hv.Memory != 16384 || hv.Image != "deb" {
|
||||
t.Errorf("hv1 = %+v", hv)
|
||||
}
|
||||
}
|
||||
|
|
@ -165,7 +165,7 @@ segments:
|
|||
blue: { switch: sw, cidr: 10.2.0.0/24, mtu: 1500 }
|
||||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [blue, red], release: 0.2.0rc002 }
|
||||
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [blue, red] }
|
||||
rr: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red] }
|
||||
`)
|
||||
got := make([]string, 0, len(p.Cables))
|
||||
|
|
@ -193,7 +193,7 @@ nodes:
|
|||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
n1: { role: rr, image: deb, cpus: 1, memory: 512, segments: [a, b] }
|
||||
n2: { role: rr, image: deb, cpus: 1, memory: 512, segments: [a, b] }
|
||||
n3: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [b, a], release: 0.2.0rc002 }
|
||||
n3: { role: host, image: deb, cpus: 1, memory: 512, segments: [b, a] }
|
||||
`)
|
||||
macs := map[string]bool{}
|
||||
ports := map[int]bool{}
|
||||
|
|
@ -308,16 +308,11 @@ func TestWrite_TwoHypervisorsPlan(t *testing.T) {
|
|||
want := `lab lab-test: nodes 4, segments 1, cables 3
|
||||
|
||||
nodes
|
||||
name role image cpus memory ssh
|
||||
sw1 switch deb 2 1024 MiB 127.0.0.1:2200
|
||||
rr1 rr deb 1 1024 MiB 127.0.0.1:2201
|
||||
hv1 hypervisor deb 4 16384 MiB 127.0.0.1:2202
|
||||
hv2 hypervisor deb 4 16384 MiB 127.0.0.1:2203
|
||||
|
||||
roles
|
||||
name loopback secondary frr release agent
|
||||
hv1 - - - 0.2.0rc002 -
|
||||
hv2 - - - 0.2.0rc002 -
|
||||
name role image cpus memory ssh
|
||||
sw1 switch deb 2 1024 MiB 127.0.0.1:2200
|
||||
rr1 rr deb 1 1024 MiB 127.0.0.1:2201
|
||||
hv1 host deb 4 16384 MiB 127.0.0.1:2202
|
||||
hv2 host deb 4 16384 MiB 127.0.0.1:2203
|
||||
|
||||
segment underlay: 10.250.0.0/24, mtu 9000, switch sw1, bridge br-underlay, gateway 10.250.0.1
|
||||
node interface address mac udp switch port mac udp
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ segments:
|
|||
nodes:
|
||||
sw1: { role: switch, image: deb, cpus: 2, memory: 1024, secondary: { underlay: [169.254.0.1/28] }, frr: frr/sw1.conf }
|
||||
rr1: { role: rr, image: deb, cpus: 1, memory: 1024, segments: [underlay], secondary: { underlay: [169.254.0.3/28] }, loopback: 10.255.255.1/32, frr: frr/rr1.conf }
|
||||
hv1: { role: hypervisor, image: deb, cpus: 4, memory: 16384, segments: [underlay], frr: /etc/lab/hv1.conf, release: 0.2.0rc002 }
|
||||
hv1: { role: host, image: deb, cpus: 4, memory: 16384, segments: [underlay], frr: /etc/lab/hv1.conf, provision: provision/hv.sh, post_provision: /opt/lab/post.sh, files: { agent.yml: agent/two.yml, deploy.sh: /opt/deploy.sh }, services: [agent.service] }
|
||||
`
|
||||
|
||||
func TestCompute_CarriesTheRoleFields(t *testing.T) {
|
||||
|
|
@ -99,7 +99,7 @@ segments:
|
|||
blue: { switch: sw, cidr: 10.2.0.0/24 }
|
||||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red, blue], release: 0.2.0rc002 }
|
||||
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red, blue] }
|
||||
` + c.node + `
|
||||
`
|
||||
requireContains(t, validationError(t, doc), c.want)
|
||||
|
|
@ -115,7 +115,7 @@ segments:
|
|||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512, secondary: { red: [169.254.0.1/28], blue: [169.254.1.1/28] } }
|
||||
other: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red, blue], release: 0.2.0rc002 }
|
||||
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red, blue] }
|
||||
`
|
||||
msg := validationError(t, doc)
|
||||
requireContains(t, msg, "node sw: secondary address given for segment blue it is not attached to")
|
||||
|
|
@ -130,7 +130,7 @@ segments:
|
|||
lo1: { switch: sw, cidr: 10.1.0.0/24 }
|
||||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [lo1], release: 0.2.0rc002 }
|
||||
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [lo1] }
|
||||
`
|
||||
requireContains(t, validationError(t, doc), "segment lo1: name is reserved for the loopback interface")
|
||||
}
|
||||
|
|
@ -142,10 +142,10 @@ func TestWrite_ShowsTheRoles(t *testing.T) {
|
|||
}
|
||||
want := `
|
||||
roles
|
||||
name loopback secondary frr release agent
|
||||
sw1 - underlay 169.254.0.1/28 sw1.conf - -
|
||||
rr1 lo1 10.255.255.1/32 underlay 169.254.0.3/28 rr1.conf - -
|
||||
hv1 - - hv1.conf 0.2.0rc002 -
|
||||
name loopback secondary frr provision post_provision files services
|
||||
sw1 - underlay 169.254.0.1/28 sw1.conf - - - -
|
||||
rr1 lo1 10.255.255.1/32 underlay 169.254.0.3/28 rr1.conf - - - -
|
||||
hv1 - - hv1.conf hv.sh post.sh agent.yml,deploy.sh agent.service
|
||||
`
|
||||
if !bytes.Contains(buf.Bytes(), []byte(want)) {
|
||||
t.Errorf("plan:\n%s\ndoes not contain:\n%s", buf.String(), want)
|
||||
|
|
@ -169,27 +169,30 @@ nodes:
|
|||
}
|
||||
}
|
||||
|
||||
func TestValidate_ReleaseRejections(t *testing.T) {
|
||||
func TestValidate_ProvisionRejections(t *testing.T) {
|
||||
cases := map[string]struct {
|
||||
node string
|
||||
want string
|
||||
}{
|
||||
"hypervisor without release": {
|
||||
"the hypervisor role is gone": {
|
||||
`hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red] }`,
|
||||
"node hv: a hypervisor needs the release of two to deploy (release: <tag>)",
|
||||
`node hv: role "hypervisor" must be one of switch, rr, host`,
|
||||
},
|
||||
"release with shell characters": {
|
||||
`hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: "0.2.0; reboot" }`,
|
||||
`node hv: release "0.2.0; reboot" must match`,
|
||||
"file name leaving the files directory": {
|
||||
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], files: { ../etc/passwd: a.yml } }`,
|
||||
`node hv: file name "../etc/passwd" must match`,
|
||||
},
|
||||
"release on a route reflector": {
|
||||
`hv: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc002 }`,
|
||||
"node hv: release is only for hypervisors",
|
||||
"file without source": {
|
||||
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], files: { agent.yml: "" } }`,
|
||||
"node hv: file agent.yml has no source",
|
||||
},
|
||||
"release on a switch": {
|
||||
`hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc002 }
|
||||
sw2: { role: switch, image: deb, cpus: 1, memory: 512, release: 0.2.0rc002 }`,
|
||||
"node sw2: release is only for hypervisors",
|
||||
"service with shell characters": {
|
||||
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], services: ["agent.service; reboot"] }`,
|
||||
`node hv: service "agent.service; reboot" must match`,
|
||||
},
|
||||
"service listed twice": {
|
||||
`hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], services: [agent.service, agent.service] }`,
|
||||
"node hv: service agent.service listed twice",
|
||||
},
|
||||
}
|
||||
for name, c := range cases {
|
||||
|
|
@ -206,60 +209,61 @@ nodes:
|
|||
}
|
||||
}
|
||||
|
||||
func TestCompute_CarriesTheRelease(t *testing.T) {
|
||||
if got := nodeOf(t, compute(t, withRoles), "hv1").Release; got != "0.2.0rc002" {
|
||||
t.Errorf("hv1 release = %q", got)
|
||||
}
|
||||
if got := nodeOf(t, compute(t, withRoles), "rr1").Release; got != "" {
|
||||
t.Errorf("rr1 release = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_AgentOnlyForHypervisors(t *testing.T) {
|
||||
func TestValidate_ProvisionIsAllowedOnEveryRole(t *testing.T) {
|
||||
doc := header + `
|
||||
segments:
|
||||
red: { switch: sw, cidr: 10.1.0.0/24 }
|
||||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
rr: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red], agent: agent.yml }
|
||||
hv: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc003, agent: agent.yml }
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512, provision: sw.sh, files: { a.conf: a.conf }, services: [lab-switch.service] }
|
||||
rr: { role: rr, image: deb, cpus: 1, memory: 512, segments: [red], provision: rr.sh, post_provision: rr-post.sh }
|
||||
hv: { role: host, image: deb, cpus: 1, memory: 512, segments: [red], files: { agent.yml: agent.yml }, services: [agent.service] }
|
||||
`
|
||||
msg := validationError(t, doc)
|
||||
requireContains(t, msg, "node rr: agent is only for hypervisors")
|
||||
if bytes.Contains([]byte(msg), []byte("node hv: agent")) {
|
||||
t.Errorf("a hypervisor agent was refused:\n%s", msg)
|
||||
compute(t, doc)
|
||||
}
|
||||
|
||||
func TestCompute_CarriesTheProvision(t *testing.T) {
|
||||
hv1 := nodeOf(t, compute(t, withRoles), "hv1")
|
||||
if hv1.Provision != "provision/hv.sh" || hv1.PostProvision != "/opt/lab/post.sh" {
|
||||
t.Errorf("hv1 provision = %q, post_provision = %q", hv1.Provision, hv1.PostProvision)
|
||||
}
|
||||
if !reflect.DeepEqual(hv1.Files, map[string]string{"agent.yml": "agent/two.yml", "deploy.sh": "/opt/deploy.sh"}) {
|
||||
t.Errorf("hv1 files = %v", hv1.Files)
|
||||
}
|
||||
if !reflect.DeepEqual(hv1.Services, []string{"agent.service"}) {
|
||||
t.Errorf("hv1 services = %v", hv1.Services)
|
||||
}
|
||||
rr1 := nodeOf(t, compute(t, withRoles), "rr1")
|
||||
if rr1.Provision != "" || rr1.PostProvision != "" || rr1.Files != nil || rr1.Services != nil {
|
||||
t.Errorf("rr1 = %+v, want no provision", rr1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_ResolvesTheAgentPathAgainstTheTopologyFile(t *testing.T) {
|
||||
func TestLoad_ResolvesProvisionPathsAgainstTheTopologyFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "lab.yml")
|
||||
doc := header + `
|
||||
segments:
|
||||
red: { switch: sw, cidr: 10.1.0.0/24 }
|
||||
nodes:
|
||||
sw: { role: switch, image: deb, cpus: 1, memory: 512 }
|
||||
hv1: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc003, agent: agent/two.yml }
|
||||
hv2: { role: hypervisor, image: deb, cpus: 1, memory: 512, segments: [red], release: 0.2.0rc003, agent: /etc/lab/agent.yml }
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(doc), 0o600); err != nil {
|
||||
if err := os.WriteFile(path, []byte(withRoles), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
topo, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
want := map[string]string{"sw": "", "hv1": filepath.Join(dir, "agent", "two.yml"), "hv2": "/etc/lab/agent.yml"}
|
||||
for _, n := range topo.Nodes {
|
||||
if n.Agent != want[n.Name] {
|
||||
t.Errorf("%s agent = %q, want %q", n.Name, n.Agent, want[n.Name])
|
||||
}
|
||||
}
|
||||
p, err := Compute(topo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := nodeOf(t, p, "hv1").Agent; got != filepath.Join(dir, "agent", "two.yml") {
|
||||
t.Errorf("plan hv1 agent = %q", got)
|
||||
hv1 := nodeOf(t, p, "hv1")
|
||||
if hv1.Provision != filepath.Join(dir, "provision", "hv.sh") {
|
||||
t.Errorf("provision = %q", hv1.Provision)
|
||||
}
|
||||
if hv1.PostProvision != "/opt/lab/post.sh" {
|
||||
t.Errorf("post_provision = %q, an absolute path must be kept", hv1.PostProvision)
|
||||
}
|
||||
want := map[string]string{"agent.yml": filepath.Join(dir, "agent", "two.yml"), "deploy.sh": "/opt/deploy.sh"}
|
||||
if !reflect.DeepEqual(hv1.Files, want) {
|
||||
t.Errorf("files = %v, want %v", hv1.Files, want)
|
||||
}
|
||||
if rr1 := nodeOf(t, p, "rr1"); rr1.Provision != "" || rr1.Files != nil {
|
||||
t.Errorf("rr1 = %+v", rr1)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
RoleSwitch = "switch"
|
||||
RoleRR = "rr"
|
||||
RoleHypervisor = "hypervisor"
|
||||
RoleSwitch = "switch"
|
||||
RoleRR = "rr"
|
||||
RoleHost = "host"
|
||||
|
||||
DefaultMTU = 9000
|
||||
)
|
||||
|
|
@ -39,18 +39,20 @@ type Segment struct {
|
|||
}
|
||||
|
||||
type Node struct {
|
||||
Name string
|
||||
Role string
|
||||
Image string
|
||||
CPUs int
|
||||
Memory int
|
||||
Segments []string
|
||||
Addresses map[string]string
|
||||
Secondary map[string][]string
|
||||
Loopback string
|
||||
FRR string
|
||||
Release string
|
||||
Agent string
|
||||
Name string
|
||||
Role string
|
||||
Image string
|
||||
CPUs int
|
||||
Memory int
|
||||
Segments []string
|
||||
Addresses map[string]string
|
||||
Secondary map[string][]string
|
||||
Loopback string
|
||||
FRR string
|
||||
Provision string
|
||||
PostProvision string
|
||||
Files map[string]string
|
||||
Services []string
|
||||
}
|
||||
|
||||
type fileImage struct {
|
||||
|
|
@ -65,17 +67,19 @@ type fileSegment struct {
|
|||
}
|
||||
|
||||
type fileNode struct {
|
||||
Role string `yaml:"role"`
|
||||
Image string `yaml:"image"`
|
||||
CPUs int `yaml:"cpus"`
|
||||
Memory int `yaml:"memory"`
|
||||
Segments []string `yaml:"segments"`
|
||||
Addresses map[string]string `yaml:"addresses"`
|
||||
Secondary map[string][]string `yaml:"secondary"`
|
||||
Loopback string `yaml:"loopback"`
|
||||
FRR string `yaml:"frr"`
|
||||
Release string `yaml:"release"`
|
||||
Agent string `yaml:"agent"`
|
||||
Role string `yaml:"role"`
|
||||
Image string `yaml:"image"`
|
||||
CPUs int `yaml:"cpus"`
|
||||
Memory int `yaml:"memory"`
|
||||
Segments []string `yaml:"segments"`
|
||||
Addresses map[string]string `yaml:"addresses"`
|
||||
Secondary map[string][]string `yaml:"secondary"`
|
||||
Loopback string `yaml:"loopback"`
|
||||
FRR string `yaml:"frr"`
|
||||
Provision string `yaml:"provision"`
|
||||
PostProvision string `yaml:"post_provision"`
|
||||
Files map[string]string `yaml:"files"`
|
||||
Services []string `yaml:"services"`
|
||||
}
|
||||
|
||||
type file struct {
|
||||
|
|
@ -94,17 +98,25 @@ func Load(path string) (*Topology, error) {
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
dir := filepath.Dir(path)
|
||||
for i, n := range t.Nodes {
|
||||
if n.FRR != "" && !filepath.IsAbs(n.FRR) {
|
||||
t.Nodes[i].FRR = filepath.Join(filepath.Dir(path), n.FRR)
|
||||
}
|
||||
if n.Agent != "" && !filepath.IsAbs(n.Agent) {
|
||||
t.Nodes[i].Agent = filepath.Join(filepath.Dir(path), n.Agent)
|
||||
t.Nodes[i].FRR = relativeTo(dir, n.FRR)
|
||||
t.Nodes[i].Provision = relativeTo(dir, n.Provision)
|
||||
t.Nodes[i].PostProvision = relativeTo(dir, n.PostProvision)
|
||||
for name, src := range n.Files {
|
||||
t.Nodes[i].Files[name] = relativeTo(dir, src)
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func relativeTo(dir, path string) string {
|
||||
if path == "" || filepath.IsAbs(path) {
|
||||
return path
|
||||
}
|
||||
return filepath.Join(dir, path)
|
||||
}
|
||||
|
||||
func Parse(data []byte) (*Topology, error) {
|
||||
var f file
|
||||
dec := yaml.NewDecoder(bytes.NewReader(data))
|
||||
|
|
@ -138,18 +150,20 @@ func Parse(data []byte) (*Topology, error) {
|
|||
for _, name := range order["nodes"] {
|
||||
n := f.Nodes[name]
|
||||
t.Nodes = append(t.Nodes, Node{
|
||||
Name: name,
|
||||
Role: n.Role,
|
||||
Image: n.Image,
|
||||
CPUs: n.CPUs,
|
||||
Memory: n.Memory,
|
||||
Segments: n.Segments,
|
||||
Addresses: n.Addresses,
|
||||
Secondary: n.Secondary,
|
||||
Loopback: n.Loopback,
|
||||
FRR: n.FRR,
|
||||
Release: n.Release,
|
||||
Agent: n.Agent,
|
||||
Name: name,
|
||||
Role: n.Role,
|
||||
Image: n.Image,
|
||||
CPUs: n.CPUs,
|
||||
Memory: n.Memory,
|
||||
Segments: n.Segments,
|
||||
Addresses: n.Addresses,
|
||||
Secondary: n.Secondary,
|
||||
Loopback: n.Loopback,
|
||||
FRR: n.FRR,
|
||||
Provision: n.Provision,
|
||||
PostProvision: n.PostProvision,
|
||||
Files: n.Files,
|
||||
Services: n.Services,
|
||||
})
|
||||
}
|
||||
return t, nil
|
||||
|
|
|
|||
|
|
@ -23,7 +23,8 @@ const (
|
|||
var (
|
||||
namePattern = regexp.MustCompile(`^[a-z][a-z0-9-]{0,14}$`)
|
||||
segmentPattern = regexp.MustCompile(`^[a-z][a-z0-9]{0,11}$`)
|
||||
releasePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`)
|
||||
filePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$`)
|
||||
unitPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9@._:-]{0,127}$`)
|
||||
)
|
||||
|
||||
func (t *Topology) Validate() error {
|
||||
|
|
@ -108,9 +109,9 @@ func (t *Topology) Validate() error {
|
|||
add("node %s: name must match %s", n.Name, namePattern)
|
||||
}
|
||||
switch n.Role {
|
||||
case RoleSwitch, RoleRR, RoleHypervisor:
|
||||
case RoleSwitch, RoleRR, RoleHost:
|
||||
default:
|
||||
add("node %s: role %q must be one of %s, %s, %s", n.Name, n.Role, RoleSwitch, RoleRR, RoleHypervisor)
|
||||
add("node %s: role %q must be one of %s, %s, %s", n.Name, n.Role, RoleSwitch, RoleRR, RoleHost)
|
||||
}
|
||||
if !images[n.Image] {
|
||||
add("node %s: image %q is not declared", n.Name, n.Image)
|
||||
|
|
@ -207,16 +208,23 @@ func validateExtras(n Node, segments map[string]Segment, add func(string, ...any
|
|||
}
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case n.Role == RoleHypervisor && n.Release == "":
|
||||
add("node %s: a hypervisor needs the release of two to deploy (release: <tag>)", n.Name)
|
||||
case n.Role == RoleHypervisor && !releasePattern.MatchString(n.Release):
|
||||
add("node %s: release %q must match %s", n.Name, n.Release, releasePattern)
|
||||
case n.Role != RoleHypervisor && n.Release != "":
|
||||
add("node %s: release is only for hypervisors", n.Name)
|
||||
for _, name := range sortedKeys(n.Files) {
|
||||
switch {
|
||||
case !filePattern.MatchString(name):
|
||||
add("node %s: file name %q must match %s", n.Name, name, filePattern)
|
||||
case n.Files[name] == "":
|
||||
add("node %s: file %s has no source", n.Name, name)
|
||||
}
|
||||
}
|
||||
if n.Role != RoleHypervisor && n.Agent != "" {
|
||||
add("node %s: agent is only for hypervisors", n.Name)
|
||||
seenUnits := map[string]bool{}
|
||||
for _, unit := range n.Services {
|
||||
switch {
|
||||
case !unitPattern.MatchString(unit):
|
||||
add("node %s: service %q must match %s", n.Name, unit, unitPattern)
|
||||
case seenUnits[unit]:
|
||||
add("node %s: service %s listed twice", n.Name, unit)
|
||||
}
|
||||
seenUnits[unit] = true
|
||||
}
|
||||
if n.Loopback != "" {
|
||||
prefix, err := netip.ParsePrefix(n.Loopback)
|
||||
|
|
|
|||
|
|
@ -1,9 +0,0 @@
|
|||
package scripts
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed deploy.sh
|
||||
var Deploy []byte
|
||||
|
||||
//go:embed bootstrap_kvm.sh
|
||||
var BootstrapKVM []byte
|
||||
|
|
@ -20,10 +20,12 @@ nodes:
|
|||
addresses: { underlay: 192.168.14.2 }, secondary: { underlay: [169.254.0.3/28] },
|
||||
loopback: 10.255.255.1/32, frr: frr/rr1.conf }
|
||||
hv1:
|
||||
{ role: hypervisor, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.11 }, frr: frr/hv1.conf, release: 0.2.0rc008,
|
||||
agent: agent/two.yml }
|
||||
{ role: host, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.11 }, frr: frr/hv1.conf, provision: provision/hypervisor.sh,
|
||||
post_provision: provision/hypervisor-post.sh, files: { agent.yml: agent/two.yml },
|
||||
services: [agent.service] }
|
||||
hv2:
|
||||
{ role: hypervisor, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.12 }, frr: frr/hv2.conf, release: 0.2.0rc008,
|
||||
agent: agent/bgp.yml }
|
||||
{ role: host, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.12 }, frr: frr/hv2.conf, provision: provision/hypervisor.sh,
|
||||
post_provision: provision/hypervisor-post.sh, files: { agent.yml: agent/bgp.yml },
|
||||
services: [agent.service] }
|
||||
|
|
|
|||
|
|
@ -20,14 +20,17 @@ nodes:
|
|||
addresses: { underlay: 192.168.14.2 }, secondary: { underlay: [169.254.0.3/28] },
|
||||
loopback: 10.255.255.1/32, frr: frr/rr1.conf }
|
||||
hv1:
|
||||
{ role: hypervisor, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.11 }, frr: frr/hv1.conf, release: 0.2.0rc008,
|
||||
agent: agent/two.yml }
|
||||
{ role: host, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.11 }, frr: frr/hv1.conf, provision: provision/hypervisor.sh,
|
||||
post_provision: provision/hypervisor-post.sh, files: { agent.yml: agent/two.yml },
|
||||
services: [agent.service] }
|
||||
hv2:
|
||||
{ role: hypervisor, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.12 }, frr: frr/hv2.conf, release: 0.2.0rc008,
|
||||
agent: agent/bgp.yml }
|
||||
{ role: host, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.12 }, frr: frr/hv2.conf, provision: provision/hypervisor.sh,
|
||||
post_provision: provision/hypervisor-post.sh, files: { agent.yml: agent/bgp.yml },
|
||||
services: [agent.service] }
|
||||
hv3:
|
||||
{ role: hypervisor, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.13 }, frr: frr/hv3.conf, release: 0.2.0rc008,
|
||||
agent: agent/bgp.yml }
|
||||
{ role: host, image: debian12, cpus: 4, memory: 16384, segments: [underlay],
|
||||
addresses: { underlay: 192.168.14.13 }, frr: frr/hv3.conf, provision: provision/hypervisor.sh,
|
||||
post_provision: provision/hypervisor-post.sh, files: { agent.yml: agent/bgp.yml },
|
||||
services: [agent.service] }
|
||||
|
|
|
|||
3
test/e2e/topologies/provision/hypervisor-post.sh
Executable file
3
test/e2e/topologies/provision/hypervisor-post.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
systemctl restart agent.service
|
||||
19
test/e2e/topologies/provision/hypervisor.sh
Executable file
19
test/e2e/topologies/provision/hypervisor.sh
Executable file
|
|
@ -0,0 +1,19 @@
|
|||
#!/bin/sh
|
||||
set -eu
|
||||
RELEASE=0.2.0rc008
|
||||
GIT_SERVER=https://git.g3e.fr/
|
||||
REPO=syonad/two
|
||||
n=0
|
||||
until curl -fsS -o /dev/null "${GIT_SERVER}"; do
|
||||
n=$((n + 1))
|
||||
[ "$n" -lt 30 ] || exit 1
|
||||
sleep 10
|
||||
done
|
||||
install -D -m 0640 "${LAB_FILES}/agent.yml" /etc/two/agent.yml
|
||||
mkdir -p /opt/two/scripts
|
||||
cd /opt/two/scripts
|
||||
for script in deploy.sh bootstrap_kvm.sh; do
|
||||
curl -fsS -o "${script}" "${GIT_SERVER}${REPO}/raw/tag/${RELEASE}/scripts/${script}"
|
||||
done
|
||||
chmod 0755 deploy.sh bootstrap_kvm.sh
|
||||
bash ./deploy.sh --noup_script -i -u "${LAB_UPLINK}" -t "${RELEASE}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue