refactor: simplify ports-configure codebase
This makes it slightly easier to reason about, at the cost of being slower. Also ignore failure to remove image as this was flaky.
This commit is contained in:
1 parent
48d5820b8c
commit
977b4567f8
3 files changed
+133
-101
No files matched your search
+126
-4
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -142,6 +143,113 @@ func getProxySSLPort(appName string) int {
|
|||||||
return port
|
return port
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initializeProxyPort(appName string, rawTCPPorts []int) error {
|
||||||
|
port := getProxyPort(appName)
|
||||||
|
if port != 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rawTCPPorts) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if isAppVhostEnabled(appName) {
|
||||||
|
port = getGlobalProxyPort()
|
||||||
|
} else {
|
||||||
|
common.LogInfo1("No port set, setting to random open high port")
|
||||||
|
port = getAvailablePort()
|
||||||
|
}
|
||||||
|
|
||||||
|
if port == 0 {
|
||||||
|
port = 80
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := setProxyPort(appName, port); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initializeProxySSLPort(appName string, rawTCPPorts []int) error {
|
||||||
|
port := getProxySSLPort(appName)
|
||||||
|
if port != 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !doesCertExist(appName) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
port = getGlobalProxySSLPort()
|
||||||
|
if port == 0 {
|
||||||
|
port = 443
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rawTCPPorts) == 0 && !isAppVhostEnabled(appName) {
|
||||||
|
common.LogInfo1("No ssl port set, setting to random open high port")
|
||||||
|
port = getAvailablePort()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := setProxySSLPort(appName, port); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initializePortMap(appName string, rawTCPPorts []int) error {
|
||||||
|
portMaps := getPortMaps(appName)
|
||||||
|
if len(portMaps) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rawTCPPorts) > 0 {
|
||||||
|
for _, rawTcpPort := range rawTCPPorts {
|
||||||
|
portMaps = append(portMaps, PortMap{
|
||||||
|
ContainerPort: rawTcpPort,
|
||||||
|
HostPort: rawTcpPort,
|
||||||
|
Scheme: "http",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
upstreamPort := 5000
|
||||||
|
portFile := filepath.Join(common.AppRoot(appName), "PORT.web.1")
|
||||||
|
if common.FileExists(portFile) {
|
||||||
|
if port, err := strconv.Atoi(common.ReadFirstLine(portFile)); err == nil {
|
||||||
|
upstreamPort = port
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dokkuProxyPort := getProxyPort(appName)
|
||||||
|
dokkuProxySSLPort := getProxySSLPort(appName)
|
||||||
|
|
||||||
|
if dokkuProxyPort != 0 {
|
||||||
|
portMaps = append(portMaps, PortMap{
|
||||||
|
ContainerPort: upstreamPort,
|
||||||
|
HostPort: dokkuProxyPort,
|
||||||
|
Scheme: "http",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if dokkuProxySSLPort != 0 {
|
||||||
|
portMaps = append(portMaps, PortMap{
|
||||||
|
ContainerPort: upstreamPort,
|
||||||
|
HostPort: dokkuProxySSLPort,
|
||||||
|
Scheme: "https",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(portMaps) > 0 {
|
||||||
|
return setPortMaps(appName, portMaps)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func inRange(value int, min int, max int) bool {
|
||||||
|
return min < value && value < max
|
||||||
|
}
|
||||||
|
|
||||||
func isAppVhostEnabled(appName string) bool {
|
func isAppVhostEnabled(appName string) bool {
|
||||||
if err := common.PlugnTrigger("domains-vhost-enabled", []string{appName}...); err != nil {
|
if err := common.PlugnTrigger("domains-vhost-enabled", []string{appName}...); err != nil {
|
||||||
return false
|
return false
|
||||||
@@ -149,10 +257,6 @@ func isAppVhostEnabled(appName string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func inRange(value int, min int, max int) bool {
|
|
||||||
return min < value && value < max
|
|
||||||
}
|
|
||||||
|
|
||||||
func listAppPortMaps(appName string) error {
|
func listAppPortMaps(appName string) error {
|
||||||
portMaps := getPortMaps(appName)
|
portMaps := getPortMaps(appName)
|
||||||
|
|
||||||
@@ -285,6 +389,24 @@ func setPortMaps(appName string, portMaps []PortMap) error {
|
|||||||
}, map[string]string{"DOKKU_QUIET_OUTPUT": "1"})
|
}, map[string]string{"DOKKU_QUIET_OUTPUT": "1"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setProxyPort(appName string, port int) error {
|
||||||
|
return common.EnvWrap(func() error {
|
||||||
|
entries := map[string]string{
|
||||||
|
"DOKKU_PROXY_PORT": fmt.Sprint(port),
|
||||||
|
}
|
||||||
|
return config.SetMany(appName, entries, false)
|
||||||
|
}, map[string]string{"DOKKU_QUIET_OUTPUT": "1"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func setProxySSLPort(appName string, port int) error {
|
||||||
|
return common.EnvWrap(func() error {
|
||||||
|
entries := map[string]string{
|
||||||
|
"DOKKU_PROXY_SSL_PORT": fmt.Sprint(port),
|
||||||
|
}
|
||||||
|
return config.SetMany(appName, entries, false)
|
||||||
|
}, map[string]string{"DOKKU_QUIET_OUTPUT": "1"})
|
||||||
|
}
|
||||||
|
|
||||||
func uniquePortMaps(portMaps []PortMap) []PortMap {
|
func uniquePortMaps(portMaps []PortMap) []PortMap {
|
||||||
var unique []PortMap
|
var unique []PortMap
|
||||||
existingPortMaps := map[string]bool{}
|
existingPortMaps := map[string]bool{}
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package ports
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/dokku/dokku/plugins/common"
|
"github.com/dokku/dokku/plugins/common"
|
||||||
"github.com/dokku/dokku/plugins/config"
|
"github.com/dokku/dokku/plugins/config"
|
||||||
@@ -18,101 +16,16 @@ func TriggerPortsClear(appName string) error {
|
|||||||
func TriggerPortsConfigure(appName string) error {
|
func TriggerPortsConfigure(appName string) error {
|
||||||
rawTCPPorts := getDockerfileRawTCPPorts(appName)
|
rawTCPPorts := getDockerfileRawTCPPorts(appName)
|
||||||
|
|
||||||
dokkuProxyPort := getProxyPort(appName)
|
if err := initializeProxyPort(appName, rawTCPPorts); err != nil {
|
||||||
dokkuProxySSLPort := getProxySSLPort(appName)
|
return err
|
||||||
portMaps := getPortMaps(appName)
|
|
||||||
|
|
||||||
vhostEnabled := isAppVhostEnabled(appName)
|
|
||||||
|
|
||||||
if dokkuProxyPort == 0 && len(rawTCPPorts) == 0 {
|
|
||||||
proxyPort := 80
|
|
||||||
if !vhostEnabled {
|
|
||||||
common.LogInfo1("No port set, setting to random open high port")
|
|
||||||
proxyPort = getAvailablePort()
|
|
||||||
} else {
|
|
||||||
proxyPort = getGlobalProxyPort()
|
|
||||||
}
|
|
||||||
|
|
||||||
if proxyPort == 0 {
|
|
||||||
proxyPort = 80
|
|
||||||
}
|
|
||||||
|
|
||||||
dokkuProxyPort = proxyPort
|
|
||||||
err := common.EnvWrap(func() error {
|
|
||||||
entries := map[string]string{
|
|
||||||
"DOKKU_PROXY_PORT": fmt.Sprint(proxyPort),
|
|
||||||
}
|
|
||||||
return config.SetMany(appName, entries, false)
|
|
||||||
}, map[string]string{"DOKKU_QUIET_OUTPUT": "1"})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if dokkuProxySSLPort == 0 {
|
if err := initializeProxySSLPort(appName, rawTCPPorts); err != nil {
|
||||||
if doesCertExist(appName) {
|
return err
|
||||||
proxySSLPort := getGlobalProxySSLPort()
|
|
||||||
if proxySSLPort == 0 {
|
|
||||||
proxySSLPort = 443
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(rawTCPPorts) == 0 && !vhostEnabled {
|
|
||||||
common.LogInfo1("No ssl port set, setting to random open high port")
|
|
||||||
proxySSLPort = getAvailablePort()
|
|
||||||
}
|
|
||||||
|
|
||||||
dokkuProxySSLPort = proxySSLPort
|
|
||||||
err := common.EnvWrap(func() error {
|
|
||||||
entries := map[string]string{
|
|
||||||
"DOKKU_PROXY_SSL_PORT": fmt.Sprint(proxySSLPort),
|
|
||||||
}
|
|
||||||
return config.SetMany(appName, entries, false)
|
|
||||||
}, map[string]string{"DOKKU_QUIET_OUTPUT": "1"})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(portMaps) == 0 {
|
if err := initializePortMap(appName, rawTCPPorts); err != nil {
|
||||||
if len(rawTCPPorts) > 0 {
|
return err
|
||||||
for _, rawTcpPort := range rawTCPPorts {
|
|
||||||
portMaps = append(portMaps, PortMap{
|
|
||||||
ContainerPort: rawTcpPort,
|
|
||||||
HostPort: rawTcpPort,
|
|
||||||
Scheme: "http",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
upstreamPort := 5000
|
|
||||||
portFile := filepath.Join(common.AppRoot(appName), "PORT.web.1")
|
|
||||||
if common.FileExists(portFile) {
|
|
||||||
if port, err := strconv.Atoi(common.ReadFirstLine(portFile)); err == nil {
|
|
||||||
upstreamPort = port
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if dokkuProxyPort != 0 {
|
|
||||||
portMaps = append(portMaps, PortMap{
|
|
||||||
ContainerPort: upstreamPort,
|
|
||||||
HostPort: dokkuProxyPort,
|
|
||||||
Scheme: "http",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if dokkuProxySSLPort != 0 {
|
|
||||||
portMaps = append(portMaps, PortMap{
|
|
||||||
ContainerPort: upstreamPort,
|
|
||||||
HostPort: dokkuProxySSLPort,
|
|
||||||
Scheme: "https",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(portMaps) > 0 {
|
|
||||||
return setPortMaps(appName, portMaps)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -127,8 +127,5 @@ teardown() {
|
|||||||
assert_success
|
assert_success
|
||||||
assert_output_contains "Overriding default nginx.conf with detected nginx.conf.sigil" 2
|
assert_output_contains "Overriding default nginx.conf with detected nginx.conf.sigil" 2
|
||||||
|
|
||||||
run /bin/bash -c "docker image rm dokku-test/$TEST_APP:latest dokku-test/$TEST_APP:v2"
|
docker image rm dokku-test/$TEST_APP:latest dokku-test/$TEST_APP:v2 || true
|
||||||
echo "output: $output"
|
|
||||||
echo "status: $status"
|
|
||||||
assert_success
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user