Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve(hosts): use more meaningful name for host name #330

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cli/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,23 @@ 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 {
return nil, err
}

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the host printed here need to be replaced?

}

func (curveadm *CurveAdm) ParseTopologyData(data string) ([]*topology.DeployConfig, error) {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/hosts/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/command/hosts/playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions internal/configure/hosts/hc_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,17 @@ func (hc *HostConfig) getBool(i *comm.Item) bool {
return v.(bool)
}

func (hc *HostConfig) getName() string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数可以去掉,每个配置项都提供了一个默认值的钩子,你可以在上面做事情,可以参见 CONFIG_USER.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the guidance. Following your advice, I have re-implemented this functionality in the hc_item. If there are further areas that need modification, I would appreciate your guidance once again.

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) }
Expand Down
6 changes: 6 additions & 0 deletions internal/configure/hosts/hc_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ var (
nil,
)

CONFIG_NAME = itemset.Insert(
"name",
comm.REQUIRE_STRING,
false,
nil,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注意换行

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my mistake, i will fix it

CONFIG_HOSTNAME = itemset.Insert(
"hostname",
comm.REQUIRE_STRING,
Expand Down
12 changes: 6 additions & 6 deletions internal/configure/hosts/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

}
hcs = append(hcs, hc)
exist[hc.GetHost()] = true
exist[hc.GetName()] = true
}
build.DEBUG(build.DEBUG_HOSTS, hosts)
return hcs, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/configure/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion internal/errno/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/task/task/common/client_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/task/task/common/collect_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions internal/tui/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
func FormatHosts(hcs []*configure.HostConfig, verbose bool) string {
lines := [][]interface{}{}
title := []string{
"Host",
"Name",
"Hostname",
"User",
"Port",
Expand All @@ -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())
Expand All @@ -74,7 +74,7 @@ func FormatHosts(hcs []*configure.HostConfig, verbose bool) string {
}

lines = append(lines, []interface{}{
host,
name,
hostname,
user,
port,
Expand Down