isle/go/cmd/entrypoint/host.go

172 lines
3.8 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"errors"
"fmt"
"isle/daemon/network"
"os"
)
2024-07-22 13:52:51 +00:00
var subCmdHostCreate = subCmd{
name: "create",
descr: "Creates a new host in the network, writing its new bootstrap.json to stdout",
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
var (
2024-07-22 08:42:25 +00:00
hostName hostNameFlag
ip ipFlag
)
hostNameF := ctx.flags.VarPF(
2024-07-22 08:42:25 +00:00
&hostName,
"hostname", "n",
"Name of the host to generate bootstrap.json for",
)
ctx.flags.VarP(&ip, "ip", "i", "IP of the new host. An available IP will be chosen if none is given.")
canCreateHosts := ctx.flags.Bool(
"can-create-hosts",
false,
"The new host should have the ability to create hosts too",
)
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
2024-07-21 15:03:59 +00:00
if !hostNameF.Changed {
return errors.New("--hostname is required")
}
daemonRPC, err := ctx.newDaemonRPC()
if err != nil {
return fmt.Errorf("creating daemon RPC client: %w", err)
}
defer daemonRPC.Close()
res, err := daemonRPC.CreateHost(
ctx, hostName.V, network.CreateHostOpts{
2024-09-04 20:35:29 +00:00
IP: ip.V,
CanCreateHosts: *canCreateHosts,
},
)
if err != nil {
return fmt.Errorf("calling CreateHost: %w", err)
}
return json.NewEncoder(os.Stdout).Encode(res)
},
}
2024-07-22 13:52:51 +00:00
var subCmdHostList = subCmd{
name: "list",
descr: "Lists all hosts in the network, and their IPs",
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return nil, fmt.Errorf("parsing flags: %w", err)
}
daemonRPC, err := ctx.newDaemonRPC()
if err != nil {
return nil, fmt.Errorf("creating daemon RPC client: %w", err)
}
defer daemonRPC.Close()
currBoostrap, err := daemonRPC.GetBootstrap(ctx)
2022-10-26 22:23:39 +00:00
if err != nil {
return nil, fmt.Errorf("calling GetBootstrap: %w", err)
2022-10-26 22:23:39 +00:00
}
hosts := currBoostrap.HostsOrdered()
type storageAllocationView struct {
ID string `yaml:"id"`
RPCPort int `yaml:"rpc_port"`
S3APIPort int `yaml:"s3_api_port"`
}
type hostView struct {
Name string `yaml:"name"`
VPN struct {
IP string `yaml:"ip"`
PublicAddr string `yaml:"public_addr,omitempty"`
}
Storage struct {
Allocations []storageAllocationView `yaml:"allocations"`
} `yaml:",omitempty"`
2022-11-05 15:55:17 +00:00
}
hostViews := make([]hostView, len(hosts))
for i, host := range hosts {
storageAllocViews := make([]storageAllocationView, len(host.Garage.Instances))
for i := range host.Garage.Instances {
storageAllocViews[i] = storageAllocationView(host.Garage.Instances[i])
2022-11-05 15:55:17 +00:00
}
hostView := hostView{
Name: string(host.Name),
}
2022-11-05 15:55:17 +00:00
hostView.VPN.IP = host.IP().String()
hostView.VPN.PublicAddr = host.Nebula.PublicAddr
hostView.Storage.Allocations = storageAllocViews
hostViews[i] = hostView
}
return hostViews, nil
}),
}
2024-07-22 13:52:51 +00:00
var subCmdHostRemove = subCmd{
2024-07-12 15:05:39 +00:00
name: "remove",
descr: "Removes a host from the network",
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
2024-07-12 15:05:39 +00:00
var (
2024-07-22 08:42:25 +00:00
hostName hostNameFlag
2024-07-12 15:05:39 +00:00
)
hostNameF := ctx.flags.VarPF(
2024-07-22 08:42:25 +00:00
&hostName,
"hostname", "n",
2024-07-12 15:05:39 +00:00
"Name of the host to remove",
)
ctx, err := ctx.withParsedFlags(nil)
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
2024-07-12 15:05:39 +00:00
if !hostNameF.Changed {
2022-11-05 11:34:49 +00:00
return errors.New("--hostname is required")
}
daemonRPC, err := ctx.newDaemonRPC()
if err != nil {
return fmt.Errorf("creating daemon RPC client: %w", err)
}
defer daemonRPC.Close()
if err := daemonRPC.RemoveHost(ctx, hostName.V); err != nil {
2024-07-12 15:05:39 +00:00
return fmt.Errorf("calling RemoveHost: %w", err)
2022-10-26 22:23:39 +00:00
}
2024-07-12 15:05:39 +00:00
return nil
},
}
2024-07-22 13:52:51 +00:00
var subCmdHost = subCmd{
name: "host",
plural: "s",
descr: "Sub-commands having to do with configuration of hosts in the network",
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
return ctx.doSubCmd(
2024-07-22 13:52:51 +00:00
subCmdHostCreate,
subCmdHostRemove,
subCmdHostList,
)
},
}