fix: refactor retrieval of network and docker inspect information
This commit is contained in:
1 parent
25759bcbb4
commit
31333452c8
2 files changed
+22
-26
No files matched your search
@@ -158,7 +158,7 @@ func VerifyImage(image string) bool {
|
|||||||
|
|
||||||
// ContainerIsRunning checks to see if a container is running
|
// ContainerIsRunning checks to see if a container is running
|
||||||
func ContainerIsRunning(containerId string) bool {
|
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 {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -175,6 +175,20 @@ func DirectoryExists(filePath string) bool {
|
|||||||
return fi.IsDir()
|
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
|
// DokkuApps returns a list of all local apps
|
||||||
func DokkuApps() ([]string, error) {
|
func DokkuApps() ([]string, error) {
|
||||||
var apps []string
|
var apps []string
|
||||||
|
|||||||
@@ -23,25 +23,10 @@ func GetContainerIpaddress(appName string, procType string, isHerokuishContainer
|
|||||||
return ipAddress
|
return ipAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
imageCmd := common.NewShellCmd(strings.Join([]string{
|
b, err := common.DockerInspect(containerId, "'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'")
|
||||||
"docker",
|
|
||||||
"inspect",
|
|
||||||
"--format",
|
|
||||||
"'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'",
|
|
||||||
containerId,
|
|
||||||
}, " "))
|
|
||||||
|
|
||||||
b, err := imageCmd.Output()
|
|
||||||
if err != nil || len(b) == 0 {
|
if err != nil || len(b) == 0 {
|
||||||
// docker < 1.9 compatibility
|
// docker < 1.9 compatibility
|
||||||
imageCmd = common.NewShellCmd(strings.Join([]string{
|
b, err = common.DockerInspect(containerId, "'{{ .NetworkSettings.IPAddress }}'")
|
||||||
"docker",
|
|
||||||
"inspect",
|
|
||||||
"--format",
|
|
||||||
"'{{ .NetworkSettings.IPAddress }}'",
|
|
||||||
containerId,
|
|
||||||
}, " "))
|
|
||||||
b, err = imageCmd.Output()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -61,7 +46,10 @@ func GetContainerPort(appName string, procType string, isHerokuishContainer bool
|
|||||||
port := ""
|
port := ""
|
||||||
|
|
||||||
if isHerokuishContainer {
|
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 {
|
if len(dockerfilePorts) > 0 {
|
||||||
@@ -79,13 +67,7 @@ func GetContainerPort(appName string, procType string, isHerokuishContainer bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !proxy.IsAppProxyEnabled(appName) {
|
if !proxy.IsAppProxyEnabled(appName) {
|
||||||
portCmd := common.NewShellCmd(strings.Join([]string{
|
b, err := sh.Command("docker", "port", containerId, port).Output()
|
||||||
"docker",
|
|
||||||
"port",
|
|
||||||
containerId,
|
|
||||||
port,
|
|
||||||
}, " "))
|
|
||||||
b, err := portCmd.Output()
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
port = strings.Split(string(b[:]), ":")[1]
|
port = strings.Split(string(b[:]), ":")[1]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user