isle/go/cmd/entrypoint/host.go

144 lines
2.8 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"errors"
"fmt"
"isle/bootstrap"
"isle/daemon/network"
"isle/jsonutil"
"os"
"sort"
)
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()
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")
}
res, err := newDaemonRPCClient().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",
2024-09-04 20:35:29 +00:00
do: func(ctx subCmdCtx) error {
ctx, err := ctx.withParsedFlags()
if err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
2024-09-04 20:35:29 +00:00
hostsRes, err := ctx.getHosts()
2022-10-26 22:23:39 +00:00
if err != nil {
return fmt.Errorf("calling GetHosts: %w", err)
2022-10-26 22:23:39 +00:00
}
2022-11-05 15:55:17 +00:00
type host struct {
Name string
VPN struct {
IP string
}
Storage bootstrap.GarageHost `json:",omitempty"`
2022-11-05 15:55:17 +00:00
}
hosts := make([]host, 0, len(hostsRes))
for _, h := range hostsRes {
2022-11-05 15:55:17 +00:00
host := host{
Name: string(h.Name),
Storage: h.Garage,
2022-11-05 15:55:17 +00:00
}
host.VPN.IP = h.IP().String()
2022-11-05 15:55:17 +00:00
hosts = append(hosts, host)
}
sort.Slice(hosts, func(i, j int) bool { return hosts[i].Name < hosts[j].Name })
return jsonutil.WriteIndented(os.Stdout, hosts)
},
}
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()
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")
}
if err := newDaemonRPCClient().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,
)
},
}