isle/go/cmd/entrypoint/network.go

135 lines
3.1 KiB
Go
Raw Normal View History

package main
import (
"errors"
"fmt"
"isle/daemon/network"
"isle/jsonutil"
)
var subCmdNetworkCreate = subCmd{
name: "create",
descr: "Create's a new network, with this host being the first host in that network.",
noNetwork: true,
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
var (
2024-07-22 08:42:25 +00:00
ipNet ipNetFlag
hostName hostNameFlag
)
name := ctx.flags.StringP(
"name", "N", "",
"Human-readable name to identify the network as.",
)
domain := ctx.flags.StringP(
2024-07-22 08:42:25 +00:00
"domain", "d", "",
"Domain name that should be used as the root domain in the network.",
)
ipNetF := ctx.flags.VarPF(
2024-07-22 08:42:25 +00:00
&ipNet, "ip-net", "i",
`An IP subnet, in CIDR form, which will be the overall range of`+
` possible IPs in the network. The first IP in this network`+
` range will become this first host's IP.`,
)
hostNameF := ctx.flags.VarPF(
2024-07-22 08:42:25 +00:00
&hostName,
"hostname", "n",
"Name of this host, which will be the first host in the network",
)
ctx, err := ctx.withParsedFlags()
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
2024-07-22 08:42:25 +00:00
if *name == "" ||
*domain == "" ||
!ipNetF.Changed ||
!hostNameF.Changed {
return errors.New("--name, --domain, --ip-net, and --hostname are required")
}
2024-11-14 20:49:35 +00:00
err = ctx.getDaemonRPC().CreateNetwork(
2024-09-04 20:35:29 +00:00
ctx, *name, *domain, ipNet.V, hostName.V,
2024-09-04 19:24:45 +00:00
)
if err != nil {
return fmt.Errorf("creating network: %w", err)
}
return nil
},
}
var subCmdNetworkJoin = subCmd{
name: "join",
descr: "Joins this host to an existing network",
noNetwork: true,
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
bootstrapPath := ctx.flags.StringP(
"bootstrap-path", "b", "", "Path to a bootstrap.json file.",
)
ctx, err := ctx.withParsedFlags()
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
if *bootstrapPath == "" {
return errors.New("--bootstrap-path is required")
}
var newBootstrap network.JoiningBootstrap
if err := jsonutil.LoadFile(&newBootstrap, *bootstrapPath); err != nil {
return fmt.Errorf(
"loading bootstrap from %q: %w", *bootstrapPath, err,
)
}
2024-11-14 20:49:35 +00:00
return ctx.getDaemonRPC().JoinNetwork(ctx, newBootstrap)
},
}
2024-09-24 09:03:18 +00:00
var subCmdNetworkList = subCmd{
name: "list",
descr: "Lists all networks which have been joined",
noNetwork: true,
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
2024-09-24 09:03:18 +00:00
ctx, err := ctx.withParsedFlags()
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
2024-09-24 09:03:18 +00:00
}
2024-11-14 20:49:35 +00:00
return ctx.getDaemonRPC().GetNetworks(ctx)
}),
2024-09-24 09:03:18 +00:00
}
2024-11-12 17:18:47 +00:00
var subCmdNetworkGetConfig = subCmd{
name: "get-config",
descr: "Displays the currently active configuration for a joined network",
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
ctx, err := ctx.withParsedFlags()
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
}
2024-11-14 20:49:35 +00:00
return ctx.getDaemonRPC().GetConfig(ctx)
2024-11-12 17:18:47 +00:00
}),
}
var subCmdNetwork = subCmd{
2024-09-24 09:03:18 +00:00
name: "network",
descr: "Sub-commands related to network membership",
plural: "s",
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
return ctx.doSubCmd(
subCmdNetworkCreate,
subCmdNetworkJoin,
2024-09-24 09:03:18 +00:00
subCmdNetworkList,
2024-11-12 17:18:47 +00:00
subCmdNetworkGetConfig,
)
},
}