2022-10-20 19:59:46 +00:00
|
|
|
package main
|
2021-04-20 21:31:37 +00:00
|
|
|
|
|
|
|
import (
|
2023-08-05 21:53:17 +00:00
|
|
|
"isle/bootstrap"
|
2021-04-20 21:31:37 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2022-10-15 16:41:07 +00:00
|
|
|
"sort"
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/v2/mlog"
|
2021-04-20 21:31:37 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var hostNameRegexp = regexp.MustCompile(`^[a-z][a-z0-9\-]*$`)
|
|
|
|
|
|
|
|
func validateHostName(name string) error {
|
|
|
|
|
|
|
|
if !hostNameRegexp.MatchString(name) {
|
|
|
|
return errors.New("a host's name must start with a letter and only contain letters, numbers, and dashes")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var subCmdHostsList = subCmd{
|
|
|
|
name: "list",
|
|
|
|
descr: "Lists all hosts in the network, and their IPs",
|
|
|
|
checkLock: true,
|
|
|
|
do: func(subCmdCtx subCmdCtx) error {
|
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
flags := subCmdCtx.flagSet(false)
|
|
|
|
|
|
|
|
logLevelStr := flags.StringP(
|
|
|
|
"log-level", "l", "info",
|
|
|
|
`Maximum log level which should be output. Values can be "debug", "info", "warn", "error", "fatal". Does not apply to sub-processes`,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := flags.Parse(subCmdCtx.args); err != nil {
|
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := subCmdCtx.ctx
|
|
|
|
|
|
|
|
logLevel := mlog.LevelFromString(*logLevelStr)
|
|
|
|
if logLevel == nil {
|
|
|
|
return fmt.Errorf("couldn't parse log level %q", *logLevelStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := subCmdCtx.logger.WithMaxLevel(logLevel.Int())
|
|
|
|
|
2022-10-26 22:37:03 +00:00
|
|
|
hostBootstrap, err := loadHostBootstrap()
|
2022-10-26 22:23:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading host bootstrap: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:45:42 +00:00
|
|
|
hostsMap, err := hostBootstrap.GetGarageBootstrapHosts(ctx, logger)
|
2022-10-15 16:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("retrieving hosts from garage: %w", err)
|
|
|
|
}
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2022-11-05 15:55:17 +00:00
|
|
|
type host struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Nebula struct {
|
|
|
|
IP string `yaml:"ip"`
|
|
|
|
} `yaml:"nebula"`
|
|
|
|
Garage bootstrap.GarageHost `yaml:"garage,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts := make([]host, 0, len(hostsMap))
|
|
|
|
for _, h := range hostsMap {
|
|
|
|
|
|
|
|
host := host{
|
|
|
|
Name: h.Name,
|
|
|
|
Garage: h.Garage,
|
|
|
|
}
|
|
|
|
|
|
|
|
host.Nebula.IP = h.IP().String()
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
hosts = append(hosts, host)
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 16:41:07 +00:00
|
|
|
sort.Slice(hosts, func(i, j int) bool { return hosts[i].Name < hosts[j].Name })
|
|
|
|
|
|
|
|
return yaml.NewEncoder(os.Stdout).Encode(hosts)
|
2021-04-20 21:31:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var subCmdHostsDelete = subCmd{
|
|
|
|
name: "delete",
|
|
|
|
descr: "Deletes a host from the network",
|
|
|
|
checkLock: true,
|
|
|
|
do: func(subCmdCtx subCmdCtx) error {
|
|
|
|
|
|
|
|
flags := subCmdCtx.flagSet(false)
|
|
|
|
|
2022-11-05 11:34:49 +00:00
|
|
|
hostName := flags.StringP(
|
|
|
|
"hostname", "h", "",
|
2022-10-15 16:41:07 +00:00
|
|
|
"Name of the host to delete",
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := flags.Parse(subCmdCtx.args); err != nil {
|
|
|
|
return fmt.Errorf("parsing flags: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-05 11:34:49 +00:00
|
|
|
if *hostName == "" {
|
|
|
|
return errors.New("--hostname is required")
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 22:37:03 +00:00
|
|
|
hostBootstrap, err := loadHostBootstrap()
|
2022-10-26 22:23:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("loading host bootstrap: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := hostBootstrap.GlobalBucketS3APIClient()
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2022-11-05 11:34:49 +00:00
|
|
|
return bootstrap.RemoveGarageBootstrapHost(subCmdCtx.ctx, client, *hostName)
|
2021-04-20 21:31:37 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
|
|
|
subCmdHostsDelete,
|
2022-10-16 15:18:50 +00:00
|
|
|
subCmdHostsList,
|
2021-04-20 21:31:37 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|