Skip to content

Commit

Permalink
change host to name in hosts moudle
Browse files Browse the repository at this point in the history
Signed-off-by: noobcoder <[email protected]>
  • Loading branch information
tiansuo114 committed Dec 5, 2023
1 parent f243ad6 commit aadfa52
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 21 deletions.
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)
}

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 {
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,
)
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())
}
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

0 comments on commit aadfa52

Please sign in to comment.