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:
Jose Diaz-Gonzalez
2023-07-09 18:31:18 -04:00
parent 48d5820b8c
commit 977b4567f8
3 changed files with 133 additions and 101 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
@@ -142,6 +143,113 @@ func getProxySSLPort(appName string) int {
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 {
if err := common.PlugnTrigger("domains-vhost-enabled", []string{appName}...); err != nil {
return false
@@ -149,10 +257,6 @@ func isAppVhostEnabled(appName string) bool {
return true
}
func inRange(value int, min int, max int) bool {
return min < value && value < max
}
func listAppPortMaps(appName string) error {
portMaps := getPortMaps(appName)
@@ -285,6 +389,24 @@ func setPortMaps(appName string, portMaps []PortMap) error {
}, 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 {
var unique []PortMap
existingPortMaps := map[string]bool{}

View File

@@ -2,8 +2,6 @@ package ports
import (
"fmt"
"path/filepath"
"strconv"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
@@ -18,101 +16,16 @@ func TriggerPortsClear(appName string) error {
func TriggerPortsConfigure(appName string) error {
rawTCPPorts := getDockerfileRawTCPPorts(appName)
dokkuProxyPort := getProxyPort(appName)
dokkuProxySSLPort := getProxySSLPort(appName)
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 err := initializeProxyPort(appName, rawTCPPorts); err != nil {
return err
}
if dokkuProxySSLPort == 0 {
if doesCertExist(appName) {
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 err := initializeProxySSLPort(appName, rawTCPPorts); err != nil {
return err
}
if len(portMaps) == 0 {
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
}
}
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
if err := initializePortMap(appName, rawTCPPorts); err != nil {
return err
}
return nil

View File

@@ -127,8 +127,5 @@ teardown() {
assert_success
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"
echo "output: $output"
echo "status: $status"
assert_success
docker image rm dokku-test/$TEST_APP:latest dokku-test/$TEST_APP:v2 || true
}