fix: refactor retrieval of network and docker inspect information
This commit is contained in:
committed by
Michael Hobbs
parent
25759bcbb4
commit
31333452c8
@@ -158,7 +158,7 @@ func VerifyImage(image string) bool {
|
||||
|
||||
// ContainerIsRunning checks to see if a container is running
|
||||
func ContainerIsRunning(containerId string) bool {
|
||||
b, err := sh.Command("docker", "inspect", "--format", "'{{.State.Running}}'", containerId).Output()
|
||||
b, err := DockerInspect(containerId, "'{{.State.Running}}'")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -175,6 +175,20 @@ func DirectoryExists(filePath string) bool {
|
||||
return fi.IsDir()
|
||||
}
|
||||
|
||||
// DockerInspect runs an inspect command with a given format against a container id
|
||||
func DockerInspect(containerId, format string) (string, error) {
|
||||
b, err := sh.Command("docker", "inspect", "--format", format, containerId).Output()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
output := strings.TrimSpace(string(b[:]))
|
||||
if strings.HasPrefix(output, "'") && strings.HasSuffix(output, "'") {
|
||||
output = strings.TrimSuffix(strings.TrimPrefix(output, "'"), "'")
|
||||
}
|
||||
return output, err
|
||||
|
||||
}
|
||||
|
||||
// DokkuApps returns a list of all local apps
|
||||
func DokkuApps() ([]string, error) {
|
||||
var apps []string
|
||||
|
||||
@@ -23,25 +23,10 @@ func GetContainerIpaddress(appName string, procType string, isHerokuishContainer
|
||||
return ipAddress
|
||||
}
|
||||
|
||||
imageCmd := common.NewShellCmd(strings.Join([]string{
|
||||
"docker",
|
||||
"inspect",
|
||||
"--format",
|
||||
"'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'",
|
||||
containerId,
|
||||
}, " "))
|
||||
|
||||
b, err := imageCmd.Output()
|
||||
b, err := common.DockerInspect(containerId, "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'")
|
||||
if err != nil || len(b) == 0 {
|
||||
// docker < 1.9 compatibility
|
||||
imageCmd = common.NewShellCmd(strings.Join([]string{
|
||||
"docker",
|
||||
"inspect",
|
||||
"--format",
|
||||
"'{{ .NetworkSettings.IPAddress }}'",
|
||||
containerId,
|
||||
}, " "))
|
||||
b, err = imageCmd.Output()
|
||||
b, err = common.DockerInspect(containerId, "'{{ .NetworkSettings.IPAddress }}'")
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
@@ -61,7 +46,10 @@ func GetContainerPort(appName string, procType string, isHerokuishContainer bool
|
||||
port := ""
|
||||
|
||||
if isHerokuishContainer {
|
||||
dockerfilePorts = strings.Split(config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", ""), " ")
|
||||
configValue := config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", "")
|
||||
if configValue != "" {
|
||||
dockerfilePorts = strings.Split(configValue, " ")
|
||||
}
|
||||
}
|
||||
|
||||
if len(dockerfilePorts) > 0 {
|
||||
@@ -79,13 +67,7 @@ func GetContainerPort(appName string, procType string, isHerokuishContainer bool
|
||||
}
|
||||
|
||||
if !proxy.IsAppProxyEnabled(appName) {
|
||||
portCmd := common.NewShellCmd(strings.Join([]string{
|
||||
"docker",
|
||||
"port",
|
||||
containerId,
|
||||
port,
|
||||
}, " "))
|
||||
b, err := portCmd.Output()
|
||||
b, err := sh.Command("docker", "port", containerId, port).Output()
|
||||
if err == nil {
|
||||
port = strings.Split(string(b[:]), ":")[1]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user