From aadfa5234dc77d686e0919743a461b6582d60cfa Mon Sep 17 00:00:00 2001 From: noobcoder Date: Mon, 4 Dec 2023 20:02:07 +0800 Subject: [PATCH] change host to name in hosts moudle Signed-off-by: noobcoder --- cli/cli/cli.go | 12 ++++++------ cli/command/hosts/list_test.go | 2 +- cli/command/hosts/playbook.go | 2 +- internal/configure/hosts/hc_get.go | 10 ++++++++++ internal/configure/hosts/hc_item.go | 6 ++++++ internal/configure/hosts/hosts.go | 12 ++++++------ internal/configure/monitor.go | 2 +- internal/errno/errno.go | 2 +- internal/task/task/common/client_status.go | 2 +- internal/task/task/common/collect_client.go | 2 +- internal/tui/hosts.go | 6 +++--- 11 files changed, 37 insertions(+), 21 deletions(-) diff --git a/cli/cli/cli.go b/cli/cli/cli.go index 48139b4a8..5882350cd 100644 --- a/cli/cli/cli.go +++ b/cli/cli/cli.go @@ -278,10 +278,10 @@ func (curveadm *CurveAdm) ClusterTopologyData() string { return curveadm.c func (curveadm *CurveAdm) ClusterPoolData() string { return curveadm.clusterPoolData } func (curveadm *CurveAdm) Monitor() storage.Monitor { return curveadm.monitor } -func (curveadm *CurveAdm) GetHost(host string) (*hosts.HostConfig, error) { +func (curveadm *CurveAdm) GetHost(name string) (*hosts.HostConfig, error) { if len(curveadm.Hosts()) == 0 { return nil, errno.ERR_HOST_NOT_FOUND. - F("host: %s", host) + F("host: %s", name) } hcs, err := hosts.ParseHosts(curveadm.Hosts()) if err != nil { @@ -289,12 +289,12 @@ func (curveadm *CurveAdm) GetHost(host string) (*hosts.HostConfig, error) { } for _, hc := range hcs { - if hc.GetHost() == host { + if hc.GetName() == name { return hc, nil } } return nil, errno.ERR_HOST_NOT_FOUND. - F("host: %s", host) + F("host: %s", name) } func (curveadm *CurveAdm) ParseTopologyData(data string) ([]*topology.DeployConfig, error) { @@ -304,7 +304,7 @@ func (curveadm *CurveAdm) ParseTopologyData(data string) ([]*topology.DeployConf return nil, err } for _, hc := range hcs { - ctx.Add(hc.GetHost(), hc.GetHostname()) + ctx.Add(hc.GetName(), hc.GetHostname()) } dcs, err := topology.ParseTopology(data, ctx) @@ -464,7 +464,7 @@ func (curveadm *CurveAdm) DiffTopology(data1, data2 string) ([]topology.Topology return nil, err } for _, hc := range hcs { - ctx.Add(hc.GetHost(), hc.GetHostname()) + ctx.Add(hc.GetName(), hc.GetHostname()) } if len(data1) == 0 { diff --git a/cli/command/hosts/list_test.go b/cli/command/hosts/list_test.go index 2f52ba707..a02041e7b 100644 --- a/cli/command/hosts/list_test.go +++ b/cli/command/hosts/list_test.go @@ -37,7 +37,7 @@ func run(t *testing.T, data string, labels []string, out []string) { assert.Nil(err) assert.Equal(len(hcs), len(out)) for i, hc := range hcs { - assert.Equal(hc.GetHost(), out[i]) + assert.Equal(hc.GetName(), out[i]) } } diff --git a/cli/command/hosts/playbook.go b/cli/command/hosts/playbook.go index 8bb1f0336..7feab82ee 100644 --- a/cli/command/hosts/playbook.go +++ b/cli/command/hosts/playbook.go @@ -91,7 +91,7 @@ func NewPlaybookCommand(curveadm *cli.CurveAdm) *cobra.Command { func execute(curveadm *cli.CurveAdm, options playbookOptions, idx int, hc *hosts.HostConfig) { defer func() { wg.Done() }() - name := hc.GetHost() + name := hc.GetName() target := path.Join("/tmp", utils.RandString(8)) err := tools.Scp(curveadm, name, options.filepath, target) if err != nil { diff --git a/internal/configure/hosts/hc_get.go b/internal/configure/hosts/hc_get.go index c7c0c6a07..e9fce008f 100644 --- a/internal/configure/hosts/hc_get.go +++ b/internal/configure/hosts/hc_get.go @@ -67,7 +67,17 @@ func (hc *HostConfig) getBool(i *comm.Item) bool { return v.(bool) } +func (hc *HostConfig) getName() string { + res := hc.GetHost() + if res != "" { + return res + } + + return hc.getString(CONFIG_NAME) +} + func (hc *HostConfig) GetHost() string { return hc.getString(CONFIG_HOST) } +func (hc *HostConfig) GetName() string { return hc.getName() } func (hc *HostConfig) GetHostname() string { return hc.getString(CONFIG_HOSTNAME) } func (hc *HostConfig) GetSSHHostname() string { return hc.getString(CONFIG_SSH_HOSTNAME) } func (hc *HostConfig) GetSSHPort() int { return hc.getInt(CONFIG_SSH_PORT) } diff --git a/internal/configure/hosts/hc_item.go b/internal/configure/hosts/hc_item.go index 6900cbd63..fcd0714d2 100644 --- a/internal/configure/hosts/hc_item.go +++ b/internal/configure/hosts/hc_item.go @@ -45,6 +45,12 @@ var ( nil, ) + CONFIG_NAME = itemset.Insert( + "name", + comm.REQUIRE_STRING, + false, + nil, + ) CONFIG_HOSTNAME = itemset.Insert( "hostname", comm.REQUIRE_STRING, diff --git a/internal/configure/hosts/hosts.go b/internal/configure/hosts/hosts.go index c8fa86011..7efbf3cd9 100644 --- a/internal/configure/hosts/hosts.go +++ b/internal/configure/hosts/hosts.go @@ -141,9 +141,9 @@ func (hc *HostConfig) Build() error { } privateKeyFile := hc.GetPrivateKeyFile() - if len(hc.GetHost()) == 0 { + if len(hc.GetName()) == 0 { return errno.ERR_HOST_FIELD_MISSING. - F("hosts[%d].host = nil", hc.sequence) + F("hosts[%d].host/name = nil", hc.sequence) } else if len(hc.GetHostname()) == 0 { return errno.ERR_HOSTNAME_FIELD_MISSING. F("hosts[%d].hostname = nil", hc.sequence) @@ -206,12 +206,12 @@ func ParseHosts(data string) ([]*HostConfig, error) { return nil, err } - if _, ok := exist[hc.GetHost()]; ok { - return nil, errno.ERR_DUPLICATE_HOST. - F("duplicate host: %s", hc.GetHost()) + if _, ok := exist[hc.GetName()]; ok { + return nil, errno.ERR_DUPLICATE_NAME. + F("duplicate host: %s", hc.GetName()) } hcs = append(hcs, hc) - exist[hc.GetHost()] = true + exist[hc.GetName()] = true } build.DEBUG(build.DEBUG_HOSTS, hosts) return hcs, nil diff --git a/internal/configure/monitor.go b/internal/configure/monitor.go index 6d7f34a94..2a4339b21 100644 --- a/internal/configure/monitor.go +++ b/internal/configure/monitor.go @@ -265,7 +265,7 @@ func ParseMonitorConfig(curveadm *cli.CurveAdm, filename string, data string, hs return nil, err } for _, hc := range hcs { - ctx.Add(hc.GetHost(), hc.GetHostname()) + ctx.Add(hc.GetName(), hc.GetHostname()) } mkind := dcs[0].GetKind() diff --git a/internal/errno/errno.go b/internal/errno/errno.go index 46b8228c5..79c5bbc6d 100644 --- a/internal/errno/errno.go +++ b/internal/errno/errno.go @@ -310,7 +310,7 @@ var ( ERR_PRIVATE_KEY_FILE_REQUIRE_ABSOLUTE_PATH = EC(321004, "SSH private key file needs to be an absolute path") ERR_PRIVATE_KEY_FILE_NOT_EXIST = EC(321005, "SSH private key file not exist") ERR_PRIVATE_KEY_FILE_REQUIRE_600_PERMISSIONS = EC(321006, "SSH private key file require 600 permissions") - ERR_DUPLICATE_HOST = EC(321007, "host is duplicate") + ERR_DUPLICATE_NAME = EC(321007, "name is duplicate") ERR_HOSTNAME_REQUIRES_VALID_IP_ADDRESS = EC(321008, "hostname requires valid IP address") // 322: configure (monitor.yaml: parse failed) diff --git a/internal/task/task/common/client_status.go b/internal/task/task/common/client_status.go index 87628814a..845fa3c6e 100644 --- a/internal/task/task/common/client_status.go +++ b/internal/task/task/common/client_status.go @@ -163,7 +163,7 @@ func NewGetClientStatusTask(curveadm *cli.CurveAdm, v interface{}) (*task.Task, containerId := client.ContainerId subname := fmt.Sprintf("host=%s kind=%s containerId=%s", - hc.GetHost(), client.Kind, tui.TrimContainerId(containerId)) + hc.GetName(), client.Kind, tui.TrimContainerId(containerId)) t := task.NewTask("Get Client Status", subname, hc.GetSSHConfig()) // add step diff --git a/internal/task/task/common/collect_client.go b/internal/task/task/common/collect_client.go index f9085cf20..527e452ee 100644 --- a/internal/task/task/common/collect_client.go +++ b/internal/task/task/common/collect_client.go @@ -46,7 +46,7 @@ func NewCollectClientTask(curveadm *cli.CurveAdm, v interface{}) (*task.Task, er // new task containerId := client.ContainerId subname := fmt.Sprintf("host=%s kind=%s containerId=%s", - hc.GetHost(), client.Kind, tui.TrimContainerId(containerId)) + hc.GetName(), client.Kind, tui.TrimContainerId(containerId)) t := task.NewTask("Collect Client", subname, hc.GetSSHConfig()) // add step to task diff --git a/internal/tui/hosts.go b/internal/tui/hosts.go index 9a19ba983..938ce80db 100644 --- a/internal/tui/hosts.go +++ b/internal/tui/hosts.go @@ -41,7 +41,7 @@ const ( func FormatHosts(hcs []*configure.HostConfig, verbose bool) string { lines := [][]interface{}{} title := []string{ - "Host", + "Name", "Hostname", "User", "Port", @@ -58,7 +58,7 @@ func FormatHosts(hcs []*configure.HostConfig, verbose bool) string { for i := 0; i < len(hcs); i++ { hc := hcs[i] - host := hc.GetHost() + name := hc.GetName() hostname := hc.GetHostname() user := hc.GetUser() port := strconv.Itoa(hc.GetSSHPort()) @@ -74,7 +74,7 @@ func FormatHosts(hcs []*configure.HostConfig, verbose bool) string { } lines = append(lines, []interface{}{ - host, + name, hostname, user, port,