2022-10-20 19:59:46 +00:00
|
|
|
package main
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
2024-07-14 09:58:39 +00:00
|
|
|
"encoding/json"
|
2021-04-20 21:31:37 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-09-09 14:34:00 +00:00
|
|
|
"isle/daemon/network"
|
2021-04-20 21:31:37 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-07-22 13:52:51 +00:00
|
|
|
var subCmdHostCreate = subCmd{
|
2024-07-13 14:31:52 +00:00
|
|
|
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 {
|
2024-07-13 14:31:52 +00:00
|
|
|
var (
|
2024-07-22 08:42:25 +00:00
|
|
|
hostName hostNameFlag
|
|
|
|
ip ipFlag
|
2024-07-13 14:31:52 +00:00
|
|
|
)
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
hostNameF := ctx.flags.VarPF(
|
2024-07-22 08:42:25 +00:00
|
|
|
&hostName,
|
2024-07-22 14:37:22 +00:00
|
|
|
"hostname", "n",
|
2024-07-13 14:31:52 +00:00
|
|
|
"Name of the host to generate bootstrap.json for",
|
|
|
|
)
|
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
ctx.flags.VarP(&ip, "ip", "i", "IP of the new host. An available IP will be chosen if none is given.")
|
2024-07-13 14:31:52 +00:00
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
canCreateHosts := ctx.flags.Bool(
|
2024-07-14 11:33:29 +00:00
|
|
|
"can-create-hosts",
|
|
|
|
false,
|
|
|
|
"The new host should have the ability to create hosts too",
|
|
|
|
)
|
|
|
|
|
2024-12-10 21:07:25 +00:00
|
|
|
ctx, err := ctx.withParsedFlags(nil)
|
2024-09-23 18:50:45 +00:00
|
|
|
if err != nil {
|
2024-07-13 14:31:52 +00:00
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-21 15:03:59 +00:00
|
|
|
if !hostNameF.Changed {
|
|
|
|
return errors.New("--hostname is required")
|
2024-07-13 14:31:52 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 21:00:55 +00:00
|
|
|
res, err := ctx.daemonRPC.CreateHost(
|
2024-09-09 14:34:00 +00:00
|
|
|
ctx, hostName.V, network.CreateHostOpts{
|
2024-09-04 20:35:29 +00:00
|
|
|
IP: ip.V,
|
|
|
|
CanCreateHosts: *canCreateHosts,
|
2024-07-13 14:31:52 +00:00
|
|
|
},
|
2024-09-04 20:46:38 +00:00
|
|
|
)
|
2024-07-13 14:31:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("calling CreateHost: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-09-07 11:52:32 +00:00
|
|
|
return json.NewEncoder(os.Stdout).Encode(res)
|
2024-07-13 14:31:52 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-07-22 13:52:51 +00:00
|
|
|
var subCmdHostList = subCmd{
|
2024-06-23 12:37:10 +00:00
|
|
|
name: "list",
|
|
|
|
descr: "Lists all hosts in the network, and their IPs",
|
2024-11-09 16:39:49 +00:00
|
|
|
do: doWithOutput(func(ctx subCmdCtx) (any, error) {
|
2024-12-10 21:07:25 +00:00
|
|
|
ctx, err := ctx.withParsedFlags(nil)
|
2024-09-23 18:50:45 +00:00
|
|
|
if err != nil {
|
2024-11-09 16:39:49 +00:00
|
|
|
return nil, fmt.Errorf("parsing flags: %w", err)
|
2024-09-23 18:50:45 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 21:00:55 +00:00
|
|
|
currBoostrap, err := ctx.daemonRPC.GetBootstrap(ctx)
|
2022-10-26 22:23:39 +00:00
|
|
|
if err != nil {
|
2024-12-07 19:39:13 +00:00
|
|
|
return nil, fmt.Errorf("calling GetBootstrap: %w", err)
|
2022-10-26 22:23:39 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
hosts := currBoostrap.HostsOrdered()
|
|
|
|
|
2024-12-07 21:23:49 +00:00
|
|
|
type storageAllocationView struct {
|
2024-12-07 19:39:13 +00:00
|
|
|
ID string `yaml:"id"`
|
|
|
|
RPCPort int `yaml:"rpc_port"`
|
|
|
|
S3APIPort int `yaml:"s3_api_port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type hostView struct {
|
|
|
|
Name string `yaml:"name"`
|
2024-06-10 16:56:36 +00:00
|
|
|
VPN struct {
|
2024-12-07 19:39:13 +00:00
|
|
|
IP string `yaml:"ip"`
|
|
|
|
PublicAddr string `yaml:"public_addr,omitempty"`
|
2024-06-10 16:56:36 +00:00
|
|
|
}
|
2024-12-07 19:39:13 +00:00
|
|
|
Storage struct {
|
2024-12-07 21:23:49 +00:00
|
|
|
Allocations []storageAllocationView `yaml:"allocations"`
|
2024-12-07 19:39:13 +00:00
|
|
|
} `yaml:",omitempty"`
|
2022-11-05 15:55:17 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
hostViews := make([]hostView, len(hosts))
|
|
|
|
for i, host := range hosts {
|
2024-12-07 21:23:49 +00:00
|
|
|
storageAllocViews := make([]storageAllocationView, len(host.Garage.Instances))
|
2024-12-07 19:39:13 +00:00
|
|
|
for i := range host.Garage.Instances {
|
2024-12-07 21:23:49 +00:00
|
|
|
storageAllocViews[i] = storageAllocationView(host.Garage.Instances[i])
|
2022-11-05 15:55:17 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
hostView := hostView{
|
|
|
|
Name: string(host.Name),
|
|
|
|
}
|
2022-11-05 15:55:17 +00:00
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
hostView.VPN.IP = host.IP().String()
|
|
|
|
hostView.VPN.PublicAddr = host.Nebula.PublicAddr
|
2024-12-07 21:23:49 +00:00
|
|
|
hostView.Storage.Allocations = storageAllocViews
|
2024-12-07 19:39:13 +00:00
|
|
|
hostViews[i] = hostView
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-12-07 19:39:13 +00:00
|
|
|
return hostViews, nil
|
2024-11-09 16:39:49 +00:00
|
|
|
}),
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
)
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-09-23 18:50:45 +00:00
|
|
|
hostNameF := ctx.flags.VarPF(
|
2024-07-22 08:42:25 +00:00
|
|
|
&hostName,
|
2024-07-22 14:37:22 +00:00
|
|
|
"hostname", "n",
|
2024-07-12 15:05:39 +00:00
|
|
|
"Name of the host to remove",
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
2024-12-10 21:07:25 +00:00
|
|
|
ctx, err := ctx.withParsedFlags(nil)
|
2024-09-23 18:50:45 +00:00
|
|
|
if err != nil {
|
2021-04-20 21:31:37 +00:00
|
|
|
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")
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2024-12-10 21:00:55 +00:00
|
|
|
if err := ctx.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
|
2021-04-20 21:31:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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,
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|