2024-07-07 10:44:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-07-09 09:43:17 +00:00
|
|
|
"isle/daemon"
|
2024-07-14 09:58:39 +00:00
|
|
|
"isle/jsonutil"
|
2024-07-07 10:44:49 +00:00
|
|
|
)
|
|
|
|
|
2024-07-09 09:43:17 +00:00
|
|
|
var subCmdNetworkCreate = subCmd{
|
|
|
|
name: "create",
|
2024-07-14 11:11:18 +00:00
|
|
|
descr: "Create's a new network, with this host being the first host in that network.",
|
2024-09-04 20:35:29 +00:00
|
|
|
do: func(ctx subCmdCtx) error {
|
2024-07-09 09:43:17 +00:00
|
|
|
var (
|
2024-09-04 20:35:29 +00:00
|
|
|
flags = ctx.flagSet(false)
|
2024-07-22 08:42:25 +00:00
|
|
|
ipNet ipNetFlag
|
|
|
|
hostName hostNameFlag
|
2024-07-09 09:43:17 +00:00
|
|
|
)
|
|
|
|
|
2024-07-22 08:42:25 +00:00
|
|
|
name := flags.StringP(
|
2024-07-22 14:37:22 +00:00
|
|
|
"name", "N", "",
|
2024-07-09 09:43:17 +00:00
|
|
|
"Human-readable name to identify the network as.",
|
|
|
|
)
|
|
|
|
|
2024-07-22 08:42:25 +00:00
|
|
|
domain := flags.StringP(
|
|
|
|
"domain", "d", "",
|
2024-07-09 09:43:17 +00:00
|
|
|
"Domain name that should be used as the root domain in the network.",
|
|
|
|
)
|
|
|
|
|
2024-07-12 13:30:21 +00:00
|
|
|
ipNetF := flags.VarPF(
|
2024-07-22 08:42:25 +00:00
|
|
|
&ipNet, "ip-net", "i",
|
2024-07-12 13:30:21 +00:00
|
|
|
`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.`,
|
2024-07-09 09:43:17 +00:00
|
|
|
)
|
|
|
|
|
2024-07-12 13:30:21 +00:00
|
|
|
hostNameF := flags.VarPF(
|
2024-07-22 08:42:25 +00:00
|
|
|
&hostName,
|
2024-07-22 14:37:22 +00:00
|
|
|
"hostname", "n",
|
2024-07-09 09:43:17 +00:00
|
|
|
"Name of this host, which will be the first host in the network",
|
|
|
|
)
|
|
|
|
|
2024-09-04 20:35:29 +00:00
|
|
|
if err := flags.Parse(ctx.args); err != nil {
|
2024-07-09 09:43:17 +00:00
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-22 08:42:25 +00:00
|
|
|
if *name == "" ||
|
|
|
|
*domain == "" ||
|
2024-07-12 13:30:21 +00:00
|
|
|
!ipNetF.Changed ||
|
|
|
|
!hostNameF.Changed {
|
2024-07-09 09:43:17 +00:00
|
|
|
return errors.New("--name, --domain, --ip-net, and --hostname are required")
|
|
|
|
}
|
|
|
|
|
2024-09-05 15:28:10 +00:00
|
|
|
err := ctx.daemonRPC.CreateNetwork(
|
2024-09-04 20:35:29 +00:00
|
|
|
ctx, *name, *domain, ipNet.V, hostName.V,
|
2024-09-04 19:24:45 +00:00
|
|
|
)
|
2024-07-09 09:43:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating network: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-07 10:44:49 +00:00
|
|
|
var subCmdNetworkJoin = subCmd{
|
|
|
|
name: "join",
|
|
|
|
descr: "Joins this host to an existing network",
|
2024-09-04 20:35:29 +00:00
|
|
|
do: func(ctx subCmdCtx) error {
|
2024-07-07 10:44:49 +00:00
|
|
|
var (
|
2024-09-04 20:35:29 +00:00
|
|
|
flags = ctx.flagSet(false)
|
2024-09-04 19:24:45 +00:00
|
|
|
bootstrapPath = flags.StringP(
|
|
|
|
"bootstrap-path", "b", "", "Path to a bootstrap.json file.",
|
|
|
|
)
|
2024-07-07 10:44:49 +00:00
|
|
|
)
|
|
|
|
|
2024-09-04 20:35:29 +00:00
|
|
|
if err := flags.Parse(ctx.args); err != nil {
|
2024-07-07 10:44:49 +00:00
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *bootstrapPath == "" {
|
|
|
|
return errors.New("--bootstrap-path is required")
|
|
|
|
}
|
|
|
|
|
2024-07-14 09:58:39 +00:00
|
|
|
var newBootstrap daemon.JoiningBootstrap
|
|
|
|
if err := jsonutil.LoadFile(&newBootstrap, *bootstrapPath); err != nil {
|
2024-07-07 10:44:49 +00:00
|
|
|
return fmt.Errorf(
|
|
|
|
"loading bootstrap from %q: %w", *bootstrapPath, err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-09-05 15:28:10 +00:00
|
|
|
return ctx.daemonRPC.JoinNetwork(ctx, newBootstrap)
|
2024-07-07 10:44:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var subCmdNetwork = subCmd{
|
|
|
|
name: "network",
|
|
|
|
descr: "Sub-commands related to network membership",
|
2024-09-04 20:35:29 +00:00
|
|
|
do: func(ctx subCmdCtx) error {
|
|
|
|
return ctx.doSubCmd(
|
2024-07-09 09:43:17 +00:00
|
|
|
subCmdNetworkCreate,
|
2024-07-07 10:44:49 +00:00
|
|
|
subCmdNetworkJoin,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|