f-55: lab: l'outil devient labctl, noms et chemins neutres #55

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-12 16:18:50 +02:00
commit 070faec37a
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
9 changed files with 88 additions and 88 deletions

View file

@ -46,7 +46,7 @@ func flags(name string, stderr io.Writer) (*flag.FlagSet, *string) {
func lab(runDir string, plan string, stdout, stderr io.Writer) (machine.Lab, bool) {
dir, err := filepath.Abs(runDir)
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return machine.Lab{}, false
}
p, ok := load(plan, stderr)
@ -67,7 +67,7 @@ func lab(runDir string, plan string, stdout, stderr io.Writer) (machine.Lab, boo
func current(runDir string, stdout, stderr io.Writer) (machine.Lab, bool) {
saved := filepath.Join(runDir, topologyFile)
if _, err := os.Stat(saved); err != nil {
fmt.Fprintf(stderr, "lab: no lab in %s: %v\n", runDir, err)
fmt.Fprintf(stderr, "labctl: no lab in %s: %v\n", runDir, err)
return machine.Lab{}, false
}
return lab(runDir, saved, stdout, stderr)
@ -75,7 +75,7 @@ func current(runDir string, stdout, stderr io.Writer) (machine.Lab, bool) {
func upCmd(args []string, stdout, stderr io.Writer) int {
fs, runDir := flags("up", stderr)
cacheDir := fs.String("cache", defaultDir(".cache", "two-lab"), "image cache directory")
cacheDir := fs.String("cache", defaultDir(".cache", "labctl"), "image cache directory")
timeout := fs.Duration("timeout", 20*time.Minute, "how long to wait for the nodes to be ready")
if err := fs.Parse(args); err != nil || fs.NArg() != 1 {
fs.Usage()
@ -83,7 +83,7 @@ func upCmd(args []string, stdout, stderr io.Writer) int {
}
cache, err := filepath.Abs(*cacheDir)
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
@ -94,11 +94,11 @@ func upCmd(args []string, stdout, stderr io.Writer) int {
}
running, err := previous.Running()
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
if len(running) > 0 {
fmt.Fprintf(stderr, "lab: lab %s is still running in %s: 'lab down' first\n", previous.Plan.Name, previous.RunDir)
fmt.Fprintf(stderr, "labctl: lab %s is still running in %s: 'lab down' first\n", previous.Plan.Name, previous.RunDir)
return 1
}
}
@ -109,15 +109,15 @@ func upCmd(args []string, stdout, stderr io.Writer) int {
}
source, err := os.ReadFile(fs.Arg(0))
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
if err := os.MkdirAll(l.RunDir, 0o700); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
if err := os.WriteFile(filepath.Join(l.RunDir, topologyFile), source, 0o600); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
@ -125,7 +125,7 @@ func upCmd(args []string, stdout, stderr io.Writer) int {
defer stop()
fetcher := provision.Fetcher{Client: &http.Client{}, CacheDir: cache}
if err := l.Up(ctx, fetcher, *timeout); err != nil {
fmt.Fprintf(stderr, "lab: %v\nlab: started nodes keep running: 'lab status', 'lab down'\n", err)
fmt.Fprintf(stderr, "labctl: %v\nlab: started nodes keep running: 'lab status', 'lab down'\n", err)
return 1
}
return 0
@ -142,7 +142,7 @@ func statusCmd(args []string, stdout, stderr io.Writer) int {
return 1
}
if err := l.Status(stdout); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
return 0
@ -159,7 +159,7 @@ func downCmd(args []string, stdout, stderr io.Writer) int {
return 1
}
if err := l.Down(context.Background()); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
return 0
@ -177,16 +177,16 @@ func sshCmd(args []string, stdout, stderr io.Writer) int {
}
argv, err := l.SSH(fs.Arg(0), isTerminal(os.Stdin), fs.Args()[1:])
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
path, err := exec.LookPath(argv[0])
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
if err := execve(path, argv, os.Environ()); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
return 0

View file

@ -13,7 +13,7 @@ import (
"git.g3e.fr/syonad/two/internal/lab/topology"
)
const usage = `usage: lab <command> [options] <topology.yml> [dir]
const usage = `usage: labctl <command> [options] <topology.yml> [dir]
plan <topology.yml>
validate the topology and print the deterministic plan: addresses, cables, ports
@ -36,7 +36,7 @@ const usage = `usage: lab <command> [options] <topology.yml> [dir]
ssh [-run dir] <node> [command...]
open a shell on a node, or run a command on it, with the key generated by up
defaults: -run $HOME/lab-run, -cache $HOME/.cache/two-lab, -timeout 20m
defaults: -run $HOME/lab-run, -cache $HOME/.cache/labctl, -timeout 20m
`
type keyFiles []string
@ -79,12 +79,12 @@ func run(args []string, stdout, stderr io.Writer) int {
func load(path string, stderr io.Writer) (*topology.Plan, bool) {
t, err := topology.Load(path)
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return nil, false
}
p, err := topology.Compute(t)
if err != nil {
fmt.Fprintf(stderr, "lab: %s:\n%v\n", path, err)
fmt.Fprintf(stderr, "labctl: %s:\n%v\n", path, err)
return nil, false
}
return p, true
@ -96,7 +96,7 @@ func plan(path string, stdout, stderr io.Writer) int {
return 1
}
if err := p.Write(stdout); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
return 0
@ -122,27 +122,27 @@ func renderCmd(args []string, stdout, stderr io.Writer) int {
}
dir, err := filepath.Abs(fs.Arg(1))
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
authorized, err := readKeys(keys)
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
opts := render.Options{RunDir: dir, AuthorizedKeys: authorized}
if err := provision.ReadInputs(p, &opts); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
nodes, err := render.Render(p, opts)
if err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
for _, n := range nodes {
if err := provision.WriteFiles(n); err != nil {
fmt.Fprintf(stderr, "lab: %v\n", err)
fmt.Fprintf(stderr, "labctl: %v\n", err)
return 1
}
fmt.Fprintf(stdout, "%s\n", n.Dir)

View file

@ -78,7 +78,7 @@ func TestRun_MissingFile(t *testing.T) {
func TestRun_RenderWritesEveryNodeFiles(t *testing.T) {
dir := t.TempDir()
key := filepath.Join(dir, "lab.pub")
if err := os.WriteFile(key, []byte("# lab key\n\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O two-lab-automation\n"), 0o600); err != nil {
if err := os.WriteFile(key, []byte("# lab key\n\nssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O lab-automation\n"), 0o600); err != nil {
t.Fatal(err)
}
out := filepath.Join(dir, "run")
@ -112,7 +112,7 @@ func TestRun_RenderWritesEveryNodeFiles(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(userData), "two-lab-automation") || strings.Contains(string(userData), "# lab key") {
if !strings.Contains(string(userData), "lab-automation") || strings.Contains(string(userData), "# lab key") {
t.Errorf("keys not read as an authorized_keys file:\n%s", userData)
}
}
@ -157,7 +157,7 @@ func TestRun_CommandsWithoutALabInTheRunDir(t *testing.T) {
args = append(args, "hv1")
}
code, _, stderr := runLab(args...)
if code != 1 || !strings.Contains(stderr, "lab: no lab in "+run) {
if code != 1 || !strings.Contains(stderr, "labctl: no lab in "+run) {
t.Errorf("%s: code %d, stderr %q", cmd, code, stderr)
}
}
@ -304,7 +304,7 @@ func TestRun_UpRefusesToReplaceARunningLab(t *testing.T) {
func TestRun_RenderShipsTheExampleFRRConfigs(t *testing.T) {
dir := t.TempDir()
key := filepath.Join(dir, "lab.pub")
if err := os.WriteFile(key, []byte("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O two-lab-automation\n"), 0o600); err != nil {
if err := os.WriteFile(key, []byte("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFG/JMmjfko96WkJV8DiL6rip/H/q/R++y8s27Z+Cj6O lab-automation\n"), 0o600); err != nil {
t.Fatal(err)
}
out := filepath.Join(dir, "run")