isle/go/cmd/entrypoint/hosts.go

154 lines
3.1 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"errors"
"fmt"
"isle/bootstrap"
2024-07-12 15:05:39 +00:00
"isle/daemon"
"isle/jsonutil"
2024-07-12 15:05:39 +00:00
"isle/nebula"
"net/netip"
"os"
"sort"
)
var subCmdHostsCreate = subCmd{
name: "create",
descr: "Creates a new host in the network, writing its new bootstrap.json to stdout",
do: func(subCmdCtx subCmdCtx) error {
var (
flags = subCmdCtx.flagSet(false)
hostName nebula.HostName
ip netip.Addr
)
hostNameF := flags.VarPF(
textUnmarshalerFlag{&hostName},
"hostname", "h",
"Name of the host to generate bootstrap.json for",
)
ipF := flags.VarPF(
textUnmarshalerFlag{&ip}, "ip", "i", "IP of the new host",
)
canCreateHosts := flags.Bool(
"can-create-hosts",
false,
"The new host should have the ability to create hosts too",
)
if err := flags.Parse(subCmdCtx.args); err != nil {
return fmt.Errorf("parsing flags: %w", err)
}
if !hostNameF.Changed || !ipF.Changed {
return errors.New("--hostname and --ip are required")
}
var res daemon.CreateHostResult
err := subCmdCtx.daemonRCPClient.Call(
subCmdCtx.ctx,
&res,
"CreateHost",
daemon.CreateHostRequest{
HostName: hostName,
IP: ip,
Opts: daemon.CreateHostOpts{
CanCreateHosts: *canCreateHosts,
},
},
)
if err != nil {
return fmt.Errorf("calling CreateHost: %w", err)
}
return json.NewEncoder(os.Stdout).Encode(res.JoiningBootstrap)
},
}
var subCmdHostsList = subCmd{
name: "list",
descr: "Lists all hosts in the network, and their IPs",
do: func(subCmdCtx subCmdCtx) error {
2024-07-09 13:14:29 +00:00
hostsRes, err := subCmdCtx.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
}
2024-07-09 13:14:29 +00:00
hosts := make([]host, 0, len(hostsRes.Hosts))
for _, h := range hostsRes.Hosts {
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-12 15:05:39 +00:00
var subCmdHostsRemove = subCmd{
name: "remove",
descr: "Removes a host from the network",
do: func(subCmdCtx subCmdCtx) error {
2024-07-12 15:05:39 +00:00
var (
flags = subCmdCtx.flagSet(false)
hostName nebula.HostName
)
2024-07-12 15:05:39 +00:00
hostNameF := flags.VarPF(
textUnmarshalerFlag{&hostName},
"hostname", "h",
"Name of the host to remove",
)
if err := flags.Parse(subCmdCtx.args); 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")
}
2024-07-12 14:03:37 +00:00
err := subCmdCtx.daemonRCPClient.Call(
2024-07-12 15:05:39 +00:00
subCmdCtx.ctx, nil, "RemoveHost", daemon.RemoveHostRequest{
HostName: hostName,
},
2024-07-12 14:03:37 +00:00
)
2022-10-26 22:23:39 +00:00
if 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
},
}
var subCmdHosts = subCmd{
name: "hosts",
descr: "Sub-commands having to do with configuration of hosts in the network",
do: func(subCmdCtx subCmdCtx) error {
return subCmdCtx.doSubCmd(
subCmdHostsCreate,
2024-07-12 15:05:39 +00:00
subCmdHostsRemove,
subCmdHostsList,
)
},
}