mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
ovsutils: add NormalizeDbHost
This commit is contained in:
@@ -15,10 +15,13 @@
|
||||
package ovsutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
"yunion.io/x/onecloud/pkg/util/procutils"
|
||||
@@ -103,3 +106,23 @@ func CleanAllHiddenPorts() {
|
||||
CleanHiddenPorts(br)
|
||||
}
|
||||
}
|
||||
|
||||
func NormalizeDbHost(db string) (string, error) {
|
||||
if strings.HasPrefix(db, "tcp:") {
|
||||
host, port, err := net.SplitHostPort(db[4:])
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "split host port: %s", db)
|
||||
}
|
||||
if ip := net.ParseIP(host); len(ip) == 0 {
|
||||
addrs, err := net.LookupHost(host)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "dns lookup (%s) failed", host)
|
||||
}
|
||||
if len(addrs) == 0 {
|
||||
return "", fmt.Errorf("dns lookup (%s) returned empty result", host)
|
||||
}
|
||||
return "tcp:" + addrs[0] + ":" + port, nil
|
||||
}
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user