From ef7392f09b9afe17060f639881c7d6a261413918 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Mon, 12 Oct 2026 16:33:02 +0200 Subject: [PATCH] =?UTF-8?q?labctl:=20d=C3=A9p=C3=B4t=20autonome=20?= =?UTF-8?q?=E2=80=94=20module=20git.g3e.fr/syonad/labctl,=20exemple=20evpn?= =?UTF-8?q?-2hv,=20LABCTL=5FBIN,=20licence,=20README,=20CI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: GnomeZworc --- .forgejo/workflows/build.yml | 43 ++++++++ .forgejo/workflows/release-pipeline.yml | 119 +++++++++++++++++++++ .forgejo/workflows/release.yml | 112 ++++++++++++++++++++ .forgejo/workflows/test.yml | 25 +++++ .gitignore | 2 + LICENSE | 131 ++++++++++++++++++++++++ README.md | 106 +++++++++++++++++++ cmd/labctl/lifecycle.go | 4 +- cmd/labctl/main.go | 6 +- cmd/labctl/main_test.go | 25 +++-- examples/evpn-2hv/frr/hv1.conf | 18 ++++ examples/evpn-2hv/frr/hv2.conf | 18 ++++ examples/evpn-2hv/frr/rr1.conf | 43 ++++++++ examples/evpn-2hv/frr/sw1.conf | 28 +++++ examples/evpn-2hv/motd | 1 + examples/evpn-2hv/provision/host.sh | 4 + examples/evpn-2hv/topology.yml | 29 ++++++ go.mod | 10 ++ go.sum | 6 ++ internal/machine/machine.go | 6 +- internal/machine/machine_test.go | 4 +- internal/provision/image.go | 2 +- internal/provision/image_test.go | 2 +- internal/provision/provision.go | 4 +- internal/provision/provision_test.go | 4 +- internal/render/qemu.go | 2 +- internal/render/render.go | 2 +- internal/render/render_test.go | 2 +- internal/render/seed.go | 2 +- scripts/lab-host.sh | 15 ++- scripts/lab-host_test.sh | 27 ++++- 31 files changed, 766 insertions(+), 36 deletions(-) create mode 100644 .forgejo/workflows/build.yml create mode 100644 .forgejo/workflows/release-pipeline.yml create mode 100644 .forgejo/workflows/release.yml create mode 100644 .forgejo/workflows/test.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 examples/evpn-2hv/frr/hv1.conf create mode 100644 examples/evpn-2hv/frr/hv2.conf create mode 100644 examples/evpn-2hv/frr/rr1.conf create mode 100644 examples/evpn-2hv/frr/sw1.conf create mode 100644 examples/evpn-2hv/motd create mode 100755 examples/evpn-2hv/provision/host.sh create mode 100644 examples/evpn-2hv/topology.yml create mode 100644 go.mod create mode 100644 go.sum diff --git a/.forgejo/workflows/build.yml b/.forgejo/workflows/build.yml new file mode 100644 index 0000000..d80e151 --- /dev/null +++ b/.forgejo/workflows/build.yml @@ -0,0 +1,43 @@ +on: + workflow_call: + inputs: + tag: + required: true + type: string + goos: + required: true + type: string + goarch: + required: true + type: string + binari: + required: true + type: string + +jobs: + build: + runs-on: docker + env: + RELEASE_CIBLE: ${{ inputs.tag }} + GOOS: ${{ inputs.goos }} + GOARCH: ${{ inputs.goarch }} + BINARI: ${{ inputs.binari }} + CGO_ENABLED: 0 + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v5 + with: + go-version: "1.25.14" + - name: Build du projet + run: | + echo "Building for ${BINARI}/${GOOS}/${GOARCH} (release: ${RELEASE_CIBLE})" + go env GOOS GOARCH + mkdir -p dist/ + go build -o dist/${BINARI}_${GOOS}_${GOARCH} ./cmd/${BINARI} + echo "artifact pour ${RELEASE_CIBLE} ${BINARI} ${GOOS} ${GOARCH}" > dist/${BINARI}-${GOOS}-${GOARCH}.txt + ls -l ./dist + - name: Upload artifact + uses: actions/upload-artifact@v3 + with: + name: ${{ env.BINARI }}-${{ env.RELEASE_CIBLE }}-${{ env.GOOS }}-${{ env.GOARCH }} + path: dist/${{ env.BINARI }}_${{ env.GOOS }}_${{ env.GOARCH }} \ No newline at end of file diff --git a/.forgejo/workflows/release-pipeline.yml b/.forgejo/workflows/release-pipeline.yml new file mode 100644 index 0000000..0167ad9 --- /dev/null +++ b/.forgejo/workflows/release-pipeline.yml @@ -0,0 +1,119 @@ +name: Release Pipeline + +on: + push: + tags: + - '[0-9]*.[0-9]*.[0-9]*' + +jobs: + set-release-target: + runs-on: docker + outputs: + release_cible: ${{ steps.setvar.outputs.release_cible }} + prerelease: ${{ steps.setvar.outputs.prerelease }} + steps: + - name: Déterminer la release cible + id: setvar + run: | + TAG="${GITHUB_REF#refs/tags/}" + + # Formes acceptées, et rien d'autre : + # X.Y.Z release finale → prerelease=false, notes obligatoires + # X.Y.ZrcNNN préversion → prerelease=true, notes optionnelles + # + # Le nombre de chiffres de rc est fixé à 3 (rc016, pas rc16) pour que + # le tri lexicographique des tags suive l'ordre chronologique. + if [[ "${TAG}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] + then + PRERELEASE=false + elif [[ "${TAG}" =~ ^[0-9]+\.[0-9]+\.[0-9]+rc[0-9]{3}$ ]] + then + PRERELEASE=true + else + echo "tag hors convention : ${TAG}" >&2 + echo "formes acceptées : X.Y.Z (release) ou X.Y.ZrcNNN (préversion, rc sur 3 chiffres)" >&2 + exit 1 + fi + + echo "release_cible=${TAG}" >> $GITHUB_OUTPUT + echo "prerelease=${PRERELEASE}" >> $GITHUB_OUTPUT + + - name: Afficher la variable + run: | + echo "Release cible = ${{ steps.setvar.outputs.release_cible }}" + echo "Prerelease = ${{ steps.setvar.outputs.prerelease }}" + build: + needs: [set-release-target] + strategy: + matrix: + include: + - goos: linux + goarch: amd64 + - goos: darwin + goarch: arm64 + uses: ./.forgejo/workflows/build.yml + with: + tag: ${{ needs.set-release-target.outputs.release_cible }} + goos: ${{ matrix.goos }} + goarch: ${{ matrix.goarch }} + binari: labctl + secrets: inherit + # Scripts publiés comme assets de release, à côté des binaires. + upload-assets: + runs-on: docker + needs: [set-release-target] + strategy: + matrix: + include: + - path: scripts/lab-host.sh + name: lab-host.sh + steps: + - uses: actions/checkout@v3 + - name: Move asset + run: | + mkdir -p "dist" + cp "${{ matrix.path }}" dist/ + - name: Upload asset + uses: actions/upload-artifact@v3 + with: + name: ${{ matrix.name }}-${{ needs.set-release-target.outputs.release_cible }} + path: dist/${{ matrix.name }} + # Manifeste des sommes de contrôle de tous les artefacts, au format sha256sum. + # Dépend de tous les jobs qui produisent des artefacts : ajouter un producteur + # sans l'ajouter ici donnerait un manifeste incomplet, donc une vérification qui + # refuse des assets légitimes. + checksums: + runs-on: docker + needs: [set-release-target, build, upload-assets] + steps: + - name: Download all artifacts + uses: actions/download-artifact@v3 + with: + path: artifacts/ + - name: Générer SHA256SUMS + run: | + mkdir -p dist + # download-artifact place chaque artefact dans son propre + # sous-répertoire : on aplatit pour que le manifeste porte les noms + # d'assets, tels qu'on les télécharge. + find artifacts/ -type f -exec cp {} dist/ \; + # Généré depuis dist/ pour que les noms soient nus, et hors de dist/ + # pour que le manifeste ne se liste pas lui-même. + ( cd dist && sha256sum * ) > SHA256SUMS + cat SHA256SUMS + - name: Upload SHA256SUMS + uses: actions/upload-artifact@v3 + with: + name: SHA256SUMS-${{ needs.set-release-target.outputs.release_cible }} + path: SHA256SUMS + publish: + # Tous les producteurs d'artefacts sont dans les needs : sans ça, le job + # release peut appeler download-artifact avant la fin des uploads et publier + # une release incomplète (assets ou manifeste manquants de façon non + # déterministe). + needs: [set-release-target, build, upload-assets, checksums] + uses: ./.forgejo/workflows/release.yml + with: + tag: ${{ needs.set-release-target.outputs.release_cible }} + prerelease: ${{ needs.set-release-target.outputs.prerelease }} + secrets: inherit diff --git a/.forgejo/workflows/release.yml b/.forgejo/workflows/release.yml new file mode 100644 index 0000000..a6a10a5 --- /dev/null +++ b/.forgejo/workflows/release.yml @@ -0,0 +1,112 @@ +on: + workflow_call: + inputs: + tag: + required: true + type: string + prerelease: + required: true + type: string + +jobs: + release: + runs-on: docker + env: + TOKEN: ${{ secrets.RELEASE }} + TAG: ${{ inputs.tag }} + PRERELEASE: ${{ inputs.prerelease }} + steps: + - uses: actions/checkout@v3 + - name: Download all build artifacts + uses: actions/download-artifact@v3 + with: + path: dist/ + - name: Publier tous les binaires + run: ls -lR dist/ + - name: Install jq + run: | + apt-get update + apt-get install -y jq + - name: Préparer les notes de version + run: | + NOTES="release_notes/${TAG}.md" + # Titre par défaut : le tag seul. Un H1 en première ligne des notes le + # remplace par « — ». + RELEASE_NAME="${TAG}" + + if [[ -f "${NOTES}" ]] + then + echo "notes trouvées : ${NOTES}" + FIRST_LINE=$(head -1 "${NOTES}") + if [[ "${FIRST_LINE}" =~ ^\#[[:space:]]+(.+)$ ]] + then + RELEASE_NAME="${TAG} — ${BASH_REMATCH[1]}" + # H1 retiré du corps : Forgejo affiche déjà le titre au-dessus, le + # garder l'afficherait deux fois. `/./,$!d` mange les lignes vides + # laissées en tête. + tail -n +2 "${NOTES}" | sed '/./,$!d' > body.md + else + cp "${NOTES}" body.md + fi + elif [[ "${PRERELEASE}" == "true" ]] + then + echo "aucune note pour ${TAG}, texte générique (préversion)" + echo "Prerelease automatique générée par la CI" > body.md + else + echo "release finale ${TAG} sans notes de version" >&2 + echo "créer ${NOTES} avant de pousser le tag" >&2 + exit 1 + fi + + echo "RELEASE_NAME=${RELEASE_NAME}" >> "${GITHUB_ENV}" + echo "--- titre --- ${RELEASE_NAME}" + echo "--- body ---"; cat body.md + - name: Create release + run: | + jq -n \ + --arg tag "${TAG}" \ + --arg name "${RELEASE_NAME}" \ + --rawfile body body.md \ + --argjson prerelease "${PRERELEASE}" \ + '{ + tag_name: $tag, + name: $name, + body: $body, + draft: false, + prerelease: $prerelease, + hide_archive_links: true + }' > payload.json + cat payload.json | jq -c '.body |= (.[0:60] + "…")' + + # Code HTTP vérifié explicitement plutôt qu'avec --fail-with-body, qui + # exige curl >= 7.76 : sans ça un échec (409 tag déjà publié, 401 token + # invalide) passerait inaperçu et la release resterait sans assets. + HTTP=$(curl -sS -o resp.json -w '%{http_code}' -X POST \ + -H "Authorization: token ${TOKEN}" \ + -H "Content-Type: application/json" \ + "https://git.g3e.fr/api/v1/repos/${{ github.repository }}/releases" \ + -d @payload.json) + echo "HTTP ${HTTP}" + cat resp.json; echo + [[ "${HTTP}" == 2* ]] || { echo "création de la release échouée" >&2; exit 1; } + - name: Upload asset + run: | + RELEASE_ID=$(curl -s \ + -H "Authorization: token ${TOKEN}" \ + https://git.g3e.fr/api/v1/repos/${{ github.repository }}/releases/tags/${TAG} \ + | jq -r .id) + echo ${RELEASE_ID} + # Sans ce garde-fou, un id absent (null) enverrait chaque asset sur + # .../releases/null/assets : une cascade de 404 silencieux et une + # release publiée sans aucun binaire. + [[ "${RELEASE_ID}" =~ ^[0-9]+$ ]] || { echo "id de release introuvable pour ${TAG}" >&2; exit 1; } + ls dist | while read tmp + do + FILE=$(ls "./dist/${tmp}") + echo ${FILE} + curl -X POST \ + -H "Authorization: token ${TOKEN}" \ + -H "Content-Type: application/octet-stream" \ + --data-binary @dist/${tmp}/${FILE} \ + "https://git.g3e.fr/api/v1/repos/${{ github.repository }}/releases/${RELEASE_ID}/assets?name=${FILE}" + done diff --git a/.forgejo/workflows/test.yml b/.forgejo/workflows/test.yml new file mode 100644 index 0000000..f0258c4 --- /dev/null +++ b/.forgejo/workflows/test.yml @@ -0,0 +1,25 @@ +name: Tests + +on: + push: + branches: ['**'] + pull_request: + +jobs: + test: + runs-on: docker + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v5 + with: + go-version: "1.25.14" + - name: Outils des tests de lab-host.sh + run: | + apt-get update + apt-get install -y jq python3 + - name: go vet + run: go vet ./... + - name: go test + run: go test ./... + - name: lab-host.sh + run: bash scripts/lab-host_test.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25b86e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/dist/ +/labctl diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1a71cb6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,131 @@ +# PolyForm Noncommercial License 1.0.0 + + + +## Acceptance + +In order to get any license under these terms, you must agree +to them as both strict obligations and conditions to all +your licenses. + +## Copyright License + +The licensor grants you a copyright license for the +software to do everything you might do with the software +that would otherwise infringe the licensor's copyright +in it for any permitted purpose. However, you may +only distribute the software according to [Distribution +License](#distribution-license) and make changes or new works +based on the software according to [Changes and New Works +License](#changes-and-new-works-license). + +## Distribution License + +The licensor grants you an additional copyright license +to distribute copies of the software. Your license +to distribute covers distributing the software with +changes and new works permitted by [Changes and New Works +License](#changes-and-new-works-license). + +## Notices + +You must ensure that anyone who gets a copy of any part of +the software from you also gets a copy of these terms or the +URL for them above, as well as copies of any plain-text lines +beginning with `Required Notice:` that the licensor provided +with the software. For example: + +> Required Notice: Copyright Yoyodyne, Inc. (http://example.com) + +## Changes and New Works License + +The licensor grants you an additional copyright license to +make changes and new works based on the software for any +permitted purpose. + +## Patent License + +The licensor grants you a patent license for the software that +covers patent claims the licensor can license, or becomes able +to license, that you would infringe by using the software. + +## Noncommercial Purposes + +Any noncommercial purpose is a permitted purpose. + +## Personal Uses + +Personal use for research, experiment, and testing for +the benefit of public knowledge, personal study, private +entertainment, hobby projects, amateur pursuits, or religious +observance, without any anticipated commercial application, +is use for a permitted purpose. + +## Noncommercial Organizations + +Use by any charitable organization, educational institution, +public research organization, public safety or health +organization, environmental protection organization, +or government institution is use for a permitted purpose +regardless of the source of funding or obligations resulting +from the funding. + +## Fair Use + +You may have "fair use" rights for the software under the +law. These terms do not limit them. + +## No Other Rights + +These terms do not allow you to sublicense or transfer any of +your licenses to anyone else, or prevent the licensor from +granting licenses to anyone else. These terms do not imply +any other licenses. + +## Patent Defense + +If you make any written claim that the software infringes or +contributes to infringement of any patent, your patent license +for the software granted under these terms ends immediately. If +your company makes such a claim, your patent license ends +immediately for work on behalf of your company. + +## Violations + +The first time you are notified in writing that you have +violated any of these terms, or done anything with the software +not covered by your licenses, your licenses can nonetheless +continue if you come into full compliance with these terms, +and take practical steps to correct past violations, within +32 days of receiving notice. Otherwise, all your licenses +end immediately. + +## No Liability + +***As far as the law allows, the software comes as is, without +any warranty or condition, and the licensor will not be liable +to you for any damages arising out of these terms or the use +or nature of the software, under any kind of legal claim.*** + +## Definitions + +The **licensor** is the individual or entity offering these +terms, and the **software** is the software the licensor makes +available under these terms. + +**You** refers to the individual or entity agreeing to these +terms. + +**Your company** is any legal entity, sole proprietorship, +or other kind of organization that you work for, plus all +organizations that have control over, are under the control of, +or are under common control with that organization. **Control** +means ownership of substantially all the assets of an entity, +or the power to direct its management and policies by vote, +contract, or otherwise. Control can be direct or indirect. + +**Your licenses** are all the licenses granted to you for the +software under these terms. + +**Use** means anything you do with the software requiring one +of your licenses. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1efab1f --- /dev/null +++ b/README.md @@ -0,0 +1,106 @@ +# labctl + +Monte un **lab réseau virtuel** à partir d'une topologie YAML : des VM QEMU/KVM reliées par des +segments L2, des switchs qui font passerelle et NAT, FRR installé et configuré sur les nœuds qui +le demandent, et une provision libre (scripts, fichiers, services à vérifier) pour installer ce +que l'on veut tester. Le plan est **déterministe** : même fichier, mêmes adresses, MAC et ports. + +`labctl` tourne sur tout hôte Linux avec KVM (virtualisation imbriquée si l'hôte est lui-même une +VM). `scripts/lab-host.sh` loue et prépare un tel hôte à l'heure chez Scaleway (Elastic Metal) ; +c'est un exemple de fournisseur, pas une dépendance. + +## Installer + +Depuis une release : télécharger `labctl_linux_amd64` (et `lab-host.sh` si besoin) **et vérifier** +leur somme dans `SHA256SUMS`. Ou compiler : + +``` +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o labctl ./cmd/labctl +``` + +Sur l'hôte : `qemu-system-x86_64`, `qemu-img`, `genisoimage`, `ssh-keygen`, et `/dev/kvm`. + +## Commandes + +``` +labctl plan valide et affiche le plan, sans rien lancer +labctl render -key écrit les arguments QEMU et les seeds cloud-init +labctl up [-run rép] [-cache rép] [-timeout durée] +labctl status | down | ssh [commande] [-run rép] +``` + +Par défaut : `-run ~/lab-run`, `-cache ~/.cache/labctl`, `-timeout 20m`. `up` télécharge et vérifie +les images (`SHA512SUMS`), crée des disques neufs, démarre les nœuds (switchs d'abord), attend la +fin de cloud-init puis vérifie les services de chaque nœud. Un fichier invalide est refusé avec +toutes ses erreurs et un code de sortie 1. + +## Topologie + +Voir `examples/evpn-2hv/` : un switch, un route reflector et deux hôtes en EVPN. + +- `images` : `url` et `sums` (HTTPS). +- `segments` : `switch`, `cidr` IPv4 (/8 à /30), `mtu` (9000 par défaut). Nom ≤ 12 caractères. +- `nodes` : `role` (`switch`, `rr` ou `host`), `image`, `cpus`, `memory` (Mio), `segments`, + `addresses` (adresse fixée par segment). Champs facultatifs : + - `secondary` — adresses supplémentaires hors du CIDR du segment ; + - `loopback` — adresse sur une interface `dummy` `lo1` ; + - `frr` — un `frr.conf` déposé tel quel ; FRR est installé au premier démarrage (`bgpd`, et + `bfdd` sur `switch` et `rr`) ; + - `provision` / `post_provision` — scripts exécutés **avant** / **après** FRR ; + - `files` — `{: }`, fichiers **ou répertoires** gravés dans un ISO propre au nœud et + recopiés dans `/etc/lab/files/` avant les scripts (aucune limite de taille cloud-init) ; + - `services` — unités systemd que `up` vérifie actives. + +Les chemins sont relatifs au fichier de topologie. Les scripts reçoivent `LAB_NODE`, +`LAB_UPLINK` (interface du premier segment, qui porte la route par défaut) et `LAB_FILES`. +Ordre au premier démarrage : script du switch ou de la loopback, `provision`, FRR, +`post_provision` — dans un seul script en `set -eu`, la première étape en échec met cloud-init en +erreur. + +Déduit de façon déterministe : passerelle d'un segment = première adresse (portée par le switch) ; +adresses suivantes dans l'ordre de déclaration des nœuds ; MAC `02:4c::::` ; +SSH d'administration sur `127.0.0.1:<2200 + rang>`. **Réordonner les nœuds change les adresses.** + +## Hôte loué chez Scaleway : `scripts/lab-host.sh` + +``` +lab-host.sh plan | up | status | ssh [cmd] | prepare | push | down | session [cmd] +``` + +`plan` et `status` sont gratuits ; `up` et `session` **créent un serveur facturé à l'heure** +(chaque heure entamée est due). Toujours commencer par `plan`. `push` dépose `labctl` (compilé +depuis ce dépôt, ou celui de `LABCTL_BIN`) et le répertoire de la topologie ; ensuite +`lab-host.sh ssh './labctl up topology/'`. + +Prérequis : un projet Scaleway **dédié** (`down` supprime tous les serveurs portant le tag +`labctl`), une clé d'API limitée à ce projet, des clés SSH publiques enregistrées dans le projet. +Fichiers locaux : `~/.config/labctl/scaleway.env` (0600, `SCW_SECRET_KEY`, +`SCW_DEFAULT_PROJECT_ID`), `~/.config/labctl/ssh/lab_ed25519` (clé dédiée facultative), état sous +`~/.cache/labctl/`. Tout se surcharge par variable d'environnement (`lab-host.sh` sans argument). + +Garanties : jamais d'offre mensuelle ni de frais de mise en service, pas de second serveur, +suppression réessayée et vérifiée jusqu'au 404, échec signalé par `SERVEUR(S) DE LAB TOUJOURS +FACTURÉ(S)`. Pas de garantie face à un `SIGKILL` ou une coupure du poste : en cas de doute, +`status` puis `down`. + +## Sécurité + +- La clé secrète Scaleway n'apparaît ni dans les arguments de processus, ni dans les journaux, ni + dans l'environnement de `ssh`. **Ne jamais lancer `lab-host.sh` sous `bash -x`.** +- La clé SSH des VM est générée sur l'hôte et ne le quitte pas ; les VM n'écoutent qu'en boucle + locale. Leurs clés d'hôte ne sont pas vérifiées (`known_hosts` jetable). +- Les images sont vérifiées par `SHA512SUMS`, servi par la même origine : cela protège de la + corruption, pas d'une origine compromise. + +## Tests + +``` +go test ./... +bash scripts/lab-host_test.sh +``` + +Sans réseau : `curl` et `ssh` sont remplacés par de faux clients (API Scaleway dans le pire cas). + +## Licence + +PolyForm Noncommercial 1.0.0 — voir `LICENSE`. diff --git a/cmd/labctl/lifecycle.go b/cmd/labctl/lifecycle.go index 6bc6547..8389f09 100644 --- a/cmd/labctl/lifecycle.go +++ b/cmd/labctl/lifecycle.go @@ -13,8 +13,8 @@ import ( "syscall" "time" - "git.g3e.fr/syonad/two/internal/lab/machine" - "git.g3e.fr/syonad/two/internal/lab/provision" + "git.g3e.fr/syonad/labctl/internal/machine" + "git.g3e.fr/syonad/labctl/internal/provision" ) const ( diff --git a/cmd/labctl/main.go b/cmd/labctl/main.go index b4bd2c9..71d44b2 100644 --- a/cmd/labctl/main.go +++ b/cmd/labctl/main.go @@ -8,9 +8,9 @@ import ( "path/filepath" "strings" - "git.g3e.fr/syonad/two/internal/lab/provision" - "git.g3e.fr/syonad/two/internal/lab/render" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/provision" + "git.g3e.fr/syonad/labctl/internal/render" + "git.g3e.fr/syonad/labctl/internal/topology" ) const usage = `usage: labctl [options] [dir] diff --git a/cmd/labctl/main_test.go b/cmd/labctl/main_test.go index 4f87a51..02da308 100644 --- a/cmd/labctl/main_test.go +++ b/cmd/labctl/main_test.go @@ -27,7 +27,7 @@ func TestRun_UsageOnMissingArguments(t *testing.T) { } func TestRun_PlanOfTheShippedExampleTopology(t *testing.T) { - code, stdout, stderr := runLab("plan", filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml")) + code, stdout, stderr := runLab("plan", filepath.Join("..", "..", "examples", "evpn-2hv", "topology.yml")) if code != 0 { t.Fatalf("code %d, stderr %s", code, stderr) } @@ -82,7 +82,7 @@ func TestRun_RenderWritesEveryNodeFiles(t *testing.T) { t.Fatal(err) } out := filepath.Join(dir, "run") - code, stdout, stderr := runLab("render", "-key", key, filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"), out) + code, stdout, stderr := runLab("render", "-key", key, filepath.Join("..", "..", "examples", "evpn-2hv", "topology.yml"), out) if code != 0 { t.Fatalf("code %d, stderr %s", code, stderr) } @@ -118,7 +118,7 @@ func TestRun_RenderWritesEveryNodeFiles(t *testing.T) { } func TestRun_RenderRefusesMissingKeyFile(t *testing.T) { - code, _, stderr := runLab("render", "-key", filepath.Join(t.TempDir(), "absent.pub"), filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"), t.TempDir()) + code, _, stderr := runLab("render", "-key", filepath.Join(t.TempDir(), "absent.pub"), filepath.Join("..", "..", "examples", "evpn-2hv", "topology.yml"), t.TempDir()) if code != 1 || !strings.Contains(stderr, "absent.pub") { t.Errorf("code %d, stderr %q", code, stderr) } @@ -130,7 +130,7 @@ func savedLab(t *testing.T) string { if err := os.MkdirAll(run, 0o700); err != nil { t.Fatal(err) } - example, err := os.ReadFile(filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml")) + example, err := os.ReadFile(filepath.Join("..", "..", "examples", "evpn-2hv", "topology.yml")) if err != nil { t.Fatal(err) } @@ -308,15 +308,15 @@ func TestRun_RenderShipsTheExampleFRRConfigs(t *testing.T) { t.Fatal(err) } out := filepath.Join(dir, "run") - code, _, stderr := runLab("render", "-key", key, filepath.Join("..", "..", "test", "e2e", "topologies", "evpn-2hv.yml"), out) + code, _, stderr := runLab("render", "-key", key, filepath.Join("..", "..", "examples", "evpn-2hv", "topology.yml"), out) if code != 0 { t.Fatalf("code %d, stderr %s", code, stderr) } 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"}, - "hv2": {"hostname hv2"}, + "hv1": {"hostname hv1", "neighbor 10.255.255.1 peer-group fabric"}, + "hv2": {"hostname hv2", "neighbor 10.255.255.1 peer-group fabric"}, } { data, err := os.ReadFile(filepath.Join(out, node, "user-data")) if err != nil { @@ -337,10 +337,13 @@ func TestRun_RenderShipsTheExampleFRRConfigs(t *testing.T) { t.Errorf("%s user-data does not mount its files", node) } } - for _, agent := range []string{"two.yml", "bgp.yml"} { - data, err := os.ReadFile(filepath.Join("..", "..", "test", "e2e", "topologies", "agent", agent)) - if err != nil || !strings.Contains(string(data), "route_reflectors: [10.255.255.1]") { - t.Errorf("agent/%s = %q, %v", agent, data, err) + for _, node := range []string{"hv1", "hv2"} { + data, err := os.ReadFile(filepath.Join(out, node, "user-data")) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(data), "LAB_NODE="+node+" LAB_UPLINK=underlay LAB_FILES=/etc/lab/files /usr/local/sbin/lab-node-provision") { + t.Errorf("%s user-data does not run the example provision", node) } } } diff --git a/examples/evpn-2hv/frr/hv1.conf b/examples/evpn-2hv/frr/hv1.conf new file mode 100644 index 0000000..bd8916c --- /dev/null +++ b/examples/evpn-2hv/frr/hv1.conf @@ -0,0 +1,18 @@ +frr defaults traditional +hostname hv1 +log syslog informational +! +router bgp 64600 + bgp router-id 192.168.14.11 + no bgp default ipv4-unicast + neighbor fabric peer-group + neighbor fabric remote-as 64600 + neighbor fabric update-source 192.168.14.11 + neighbor 10.255.255.1 peer-group fabric + ! + address-family l2vpn evpn + neighbor fabric activate + advertise-all-vni + exit-address-family +exit +! diff --git a/examples/evpn-2hv/frr/hv2.conf b/examples/evpn-2hv/frr/hv2.conf new file mode 100644 index 0000000..8a8eda9 --- /dev/null +++ b/examples/evpn-2hv/frr/hv2.conf @@ -0,0 +1,18 @@ +frr defaults traditional +hostname hv2 +log syslog informational +! +router bgp 64600 + bgp router-id 192.168.14.12 + no bgp default ipv4-unicast + neighbor fabric peer-group + neighbor fabric remote-as 64600 + neighbor fabric update-source 192.168.14.12 + neighbor 10.255.255.1 peer-group fabric + ! + address-family l2vpn evpn + neighbor fabric activate + advertise-all-vni + exit-address-family +exit +! diff --git a/examples/evpn-2hv/frr/rr1.conf b/examples/evpn-2hv/frr/rr1.conf new file mode 100644 index 0000000..44081b1 --- /dev/null +++ b/examples/evpn-2hv/frr/rr1.conf @@ -0,0 +1,43 @@ +frr defaults traditional +hostname rr1 +log syslog informational +! +ip prefix-list RR-LOOPBACK-OUT seq 10 permit 10.255.255.1/32 +! +route-map NO-IN deny 999 + description deny +exit +! +router bgp 65000 + no bgp default ipv4-unicast + bgp router-id 10.255.255.1 + bgp cluster-id 10.255.255.1 + neighbor CLUSTER peer-group + neighbor CLUSTER remote-as 65100 + neighbor CLUSTER bfd + neighbor 169.254.0.1 peer-group CLUSTER + neighbor 169.254.0.1 description router-1 + neighbor 169.254.0.2 peer-group CLUSTER + neighbor 169.254.0.2 description router-2 + neighbor fabric peer-group + neighbor fabric remote-as 64600 + neighbor fabric local-as 64600 no-prepend replace-as + neighbor fabric capability extended-nexthop + neighbor fabric update-source 10.255.255.1 + bgp listen range 192.168.14.0/24 peer-group fabric + bgp listen limit 200 + ! + address-family ipv4 unicast + network 10.255.255.1/32 + neighbor CLUSTER activate + neighbor CLUSTER prefix-list RR-LOOPBACK-OUT out + neighbor CLUSTER route-map NO-IN in + exit-address-family + ! + address-family l2vpn evpn + neighbor fabric activate + neighbor fabric route-reflector-client + exit-address-family + ! +exit +! diff --git a/examples/evpn-2hv/frr/sw1.conf b/examples/evpn-2hv/frr/sw1.conf new file mode 100644 index 0000000..55df925 --- /dev/null +++ b/examples/evpn-2hv/frr/sw1.conf @@ -0,0 +1,28 @@ +frr defaults traditional +hostname sw1 +log syslog informational +! +ip prefix-list RR-LOOPBACK-IN seq 10 permit 10.255.255.1/32 +! +route-map RR-IN permit 10 + match ip address prefix-list RR-LOOPBACK-IN +exit +! +route-map NO-OUT deny 999 +exit +! +router bgp 65100 + no bgp default ipv4-unicast + bgp router-id 169.254.0.1 + neighbor 169.254.0.3 remote-as 65000 + neighbor 169.254.0.3 description rr1 + neighbor 169.254.0.3 bfd + ! + address-family ipv4 unicast + neighbor 169.254.0.3 activate + neighbor 169.254.0.3 route-map RR-IN in + neighbor 169.254.0.3 route-map NO-OUT out + exit-address-family + ! +exit +! diff --git a/examples/evpn-2hv/motd b/examples/evpn-2hv/motd new file mode 100644 index 0000000..856688a --- /dev/null +++ b/examples/evpn-2hv/motd @@ -0,0 +1 @@ +labctl example host diff --git a/examples/evpn-2hv/provision/host.sh b/examples/evpn-2hv/provision/host.sh new file mode 100755 index 0000000..776ab62 --- /dev/null +++ b/examples/evpn-2hv/provision/host.sh @@ -0,0 +1,4 @@ +#!/bin/sh +set -eu +install -m 0644 "${LAB_FILES}/motd" /etc/motd +echo "${LAB_NODE}: uplink ${LAB_UPLINK}" >> /etc/motd diff --git a/examples/evpn-2hv/topology.yml b/examples/evpn-2hv/topology.yml new file mode 100644 index 0000000..3510e35 --- /dev/null +++ b/examples/evpn-2hv/topology.yml @@ -0,0 +1,29 @@ +name: evpn-2hv + +images: + debian12: + url: https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 + sums: https://cloud.debian.org/images/cloud/bookworm/latest/SHA512SUMS + +segments: + underlay: + switch: sw1 + cidr: 192.168.14.0/24 + mtu: 9000 + +nodes: + sw1: + { role: switch, image: debian12, cpus: 2, memory: 1024, + secondary: { underlay: [169.254.0.1/28] }, frr: frr/sw1.conf } + rr1: + { role: rr, image: debian12, cpus: 1, memory: 1024, segments: [underlay], + 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: host, image: debian12, cpus: 2, memory: 2048, segments: [underlay], + addresses: { underlay: 192.168.14.11 }, frr: frr/hv1.conf, provision: provision/host.sh, + files: { motd: motd } } + hv2: + { role: host, image: debian12, cpus: 2, memory: 2048, segments: [underlay], + addresses: { underlay: 192.168.14.12 }, frr: frr/hv2.conf, provision: provision/host.sh, + files: { motd: motd } } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b1339f4 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module git.g3e.fr/syonad/labctl + +go 1.25.0 + +toolchain go1.25.14 + +require ( + go.yaml.in/yaml/v3 v3.0.4 + golang.org/x/sys v0.45.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3600d5b --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY= +golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/internal/machine/machine.go b/internal/machine/machine.go index a88c386..b48bc66 100644 --- a/internal/machine/machine.go +++ b/internal/machine/machine.go @@ -14,9 +14,9 @@ import ( "text/tabwriter" "time" - "git.g3e.fr/syonad/two/internal/lab/provision" - "git.g3e.fr/syonad/two/internal/lab/render" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/provision" + "git.g3e.fr/syonad/labctl/internal/render" + "git.g3e.fr/syonad/labctl/internal/topology" ) const ( diff --git a/internal/machine/machine_test.go b/internal/machine/machine_test.go index 651b319..06d485b 100644 --- a/internal/machine/machine_test.go +++ b/internal/machine/machine_test.go @@ -20,8 +20,8 @@ import ( "testing" "time" - "git.g3e.fr/syonad/two/internal/lab/provision" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/provision" + "git.g3e.fr/syonad/labctl/internal/topology" ) const labKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O labctl" diff --git a/internal/provision/image.go b/internal/provision/image.go index c4e8347..2a581ec 100644 --- a/internal/provision/image.go +++ b/internal/provision/image.go @@ -17,7 +17,7 @@ import ( "regexp" "strings" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/topology" ) const maxSumsSize = 1 << 20 diff --git a/internal/provision/image_test.go b/internal/provision/image_test.go index cd1ab20..fdfdacc 100644 --- a/internal/provision/image_test.go +++ b/internal/provision/image_test.go @@ -12,7 +12,7 @@ import ( "sync" "testing" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/topology" ) const imageName = "debian-12-generic-amd64.qcow2" diff --git a/internal/provision/provision.go b/internal/provision/provision.go index 4613fb1..e609d9e 100644 --- a/internal/provision/provision.go +++ b/internal/provision/provision.go @@ -10,8 +10,8 @@ import ( "sort" "strings" - "git.g3e.fr/syonad/two/internal/lab/render" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/render" + "git.g3e.fr/syonad/labctl/internal/topology" ) const ( diff --git a/internal/provision/provision_test.go b/internal/provision/provision_test.go index 057f529..2b17abc 100644 --- a/internal/provision/provision_test.go +++ b/internal/provision/provision_test.go @@ -11,8 +11,8 @@ import ( "go.yaml.in/yaml/v3" - "git.g3e.fr/syonad/two/internal/lab/render" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/render" + "git.g3e.fr/syonad/labctl/internal/topology" ) const generatedKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O labctl" diff --git a/internal/render/qemu.go b/internal/render/qemu.go index 4c380c8..dce3bb7 100644 --- a/internal/render/qemu.go +++ b/internal/render/qemu.go @@ -5,7 +5,7 @@ import ( "path/filepath" "strconv" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/topology" ) const loopback = "127.0.0.1" diff --git a/internal/render/render.go b/internal/render/render.go index 7fbe3b7..661d254 100644 --- a/internal/render/render.go +++ b/internal/render/render.go @@ -7,7 +7,7 @@ import ( "path/filepath" "strings" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/topology" ) const ( diff --git a/internal/render/render_test.go b/internal/render/render_test.go index 3f38a41..3a9cd51 100644 --- a/internal/render/render_test.go +++ b/internal/render/render_test.go @@ -7,7 +7,7 @@ import ( "go.yaml.in/yaml/v3" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/topology" ) const labKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O lab-automation" diff --git a/internal/render/seed.go b/internal/render/seed.go index 948fe5d..09a7a8f 100644 --- a/internal/render/seed.go +++ b/internal/render/seed.go @@ -8,7 +8,7 @@ import ( "go.yaml.in/yaml/v3" - "git.g3e.fr/syonad/two/internal/lab/topology" + "git.g3e.fr/syonad/labctl/internal/topology" ) const ( diff --git a/scripts/lab-host.sh b/scripts/lab-host.sh index db1e985..f3f8547 100755 --- a/scripts/lab-host.sh +++ b/scripts/lab-host.sh @@ -46,7 +46,8 @@ usage: ${0##*/} [arguments] environnement : SCW_SECRET_KEY et SCW_DEFAULT_PROJECT_ID (requis, à défaut lus dans ${CREDENTIALS_FILE}, fichier en 0600, lignes CLÉ=valeur), LAB_ZONES (zones candidates, dans l'ordre : ${ZONES} ; SCW_DEFAULT_ZONE passe en tête), LAB_OFFER (${OFFER_NAME}), LAB_OS_NAME (${OS_NAME}), LAB_OS_VERSION (${OS_VERSION}), - LAB_SSH_KEY (${SSH_KEY} ; à défaut, l'agent SSH) + LAB_SSH_KEY (${SSH_KEY} ; à défaut, l'agent SSH), LABCTL_BIN (binaire labctl linux/amd64 + envoyé par push au lieu de le compiler depuis le dépôt) ne jamais lancer sous 'bash -x' : la trace afficherait SCW_SECRET_KEY. EOF @@ -388,9 +389,15 @@ cmd_push () { [[ -f "${TOPOLOGY}" ]] || die "topologie introuvable : ${TOPOLOGY}" [[ "${NAME}" =~ ^[A-Za-z0-9._-]+$ ]] || die "nom de topologie refusé : ${NAME} (lettres, chiffres, '.', '_', '-')" require_known_server - info "compilation de labctl (linux/amd64)" - (cd "${REPO_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "${BINARY}" ./cmd/labctl) \ - || die "compilation de labctl échouée" + if [[ -n "${LABCTL_BIN:-}" ]]; then + [[ -f "${LABCTL_BIN}" && -x "${LABCTL_BIN}" ]] || die "LABCTL_BIN n'est pas un exécutable : ${LABCTL_BIN}" + BINARY="${LABCTL_BIN}" + info "labctl fourni : ${BINARY}" + else + info "compilation de labctl (linux/amd64)" + (cd "${REPO_DIR}" && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o "${BINARY}" ./cmd/labctl) \ + || die "compilation de labctl échouée" + fi push_file "${BINARY}" labctl 755 || die "envoi de labctl échoué" push_dir "$(dirname "${TOPOLOGY}")" || die "envoi du répertoire de ${NAME} échoué" info "déposés sur le serveur : ~/labctl, ~/topology/ — ensuite : ${0##*/} ssh './labctl up topology/${NAME}'" diff --git a/scripts/lab-host_test.sh b/scripts/lab-host_test.sh index ea9ec85..9fd598a 100755 --- a/scripts/lab-host_test.sh +++ b/scripts/lab-host_test.sh @@ -227,7 +227,7 @@ lab_env () { SCW_DEFAULT_PROJECT_ID="${PROJECT}" LAB_POLL_INTERVAL="${LAB_POLL_INTERVAL:-0}" \ LAB_INSTALL_TIMEOUT="${LAB_INSTALL_TIMEOUT:-60}" LAB_DELETE_TIMEOUT="${LAB_DELETE_TIMEOUT:-60}" \ LAB_SSH_TIMEOUT="${LAB_SSH_TIMEOUT:-60}" LAB_ZONES="${LAB_ZONES:-fr-par-1}" \ - LAB_OFFER="${LAB_OFFER:-EM-B212X-SSD}" ${SCW_DEFAULT_ZONE:+SCW_DEFAULT_ZONE="${SCW_DEFAULT_ZONE}"} \ + LAB_OFFER="${LAB_OFFER:-EM-B212X-SSD}" ${SCW_DEFAULT_ZONE:+SCW_DEFAULT_ZONE="${SCW_DEFAULT_ZONE}"} ${LABCTL_BIN:+LABCTL_BIN="${LABCTL_BIN}"} \ "$@" } @@ -725,6 +725,31 @@ test_push_stops_when_the_build_fails () { teardown } +test_push_sends_a_provided_binary_without_building () { + setup "push : LABCTL_BIN est envoyé tel quel, sans compilation" + known_server + printf 'name: x\n' > "${WORK}/x.yml" + printf 'binaire-de-release' > "${WORK}/labctl-release" + chmod 755 "${WORK}/labctl-release" + LABCTL_BIN="${WORK}/labctl-release" run_lab push "${WORK}/x.yml" || fail "code de sortie $?" + [[ -s "${WORK}/go.log" ]] && fail "compilation lancée : $(cat "${WORK}/go.log")" + [[ $(cat "${WORK}/pushed.1") == "binaire-de-release" ]] || fail "contenu envoyé : $(cat "${WORK}/pushed.1")" + teardown +} + +test_push_refuses_a_provided_binary_that_is_not_executable () { + setup "push : LABCTL_BIN absent ou non exécutable refusé avant tout envoi" + known_server + printf 'name: x\n' > "${WORK}/x.yml" + printf 'x' > "${WORK}/labctl-release" + for BIN in "${WORK}/absent" "${WORK}/labctl-release"; do + LABCTL_BIN="${BIN}" run_lab push "${WORK}/x.yml" && fail "push a réussi avec ${BIN}" + grep -q "LABCTL_BIN n'est pas un exécutable" "${WORK}/out.log" || fail "message absent pour ${BIN}" + done + [[ -s "${WORK}/ssh.log" ]] && fail "ssh lancé" + teardown +} + test_push_fails_when_a_transfer_fails () { setup "push : un envoi échoué fait échouer push" known_server