refactor: move code for fetching an available port to the ports plugin

Also translate the code to golang.
This commit is contained in:
Jose Diaz-Gonzalez
2023-07-09 02:45:36 -04:00
parent 121e4737a8
commit 192f7cf768
9 changed files with 59 additions and 16 deletions

View File

@@ -3,6 +3,7 @@ package ports
import (
"errors"
"fmt"
"net"
"os"
"sort"
"strconv"
@@ -38,6 +39,26 @@ func filterAppPortMaps(appName string, scheme string, hostPort int) []PortMap {
return filteredPortMaps
}
func getAvailablePort() int {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0
}
for {
l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0
}
defer l.Close()
port := l.Addr().(*net.TCPAddr).Port
if port >= 1025 && port <= 65535 {
return port
}
}
}
func getDockerfileRawTCPPorts(appName string) []int {
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_DOCKERFILE_PORTS"}...)
dockerfilePorts := strings.TrimSpace(string(b[:]))