refactor: decrease compile time for golang plugins

This change refactors the compiled golang plugins into 3 distinct binaries:

- commands: already existing
- subcommands/subcommands: entrypoint into all subcommands
- triggers: entrypoint into all triggers

It then further symlinks triggers and subcommands to the built binaries. This results in both a much faster build process as well as smaller package size.
This commit is contained in:
Jose Diaz-Gonzalez
2019-09-07 04:47:18 -04:00
parent f83a03a3dc
commit 6a7d2e5252
73 changed files with 865 additions and 1179 deletions

View File

@@ -1,15 +0,0 @@
package main
import (
"flag"
"github.com/dokku/dokku/plugins/network"
)
// rebuilds network settings for an app
func main() {
flag.Parse()
appName := flag.Arg(1)
network.BuildConfig(appName)
}

View File

@@ -1,17 +0,0 @@
package main
import (
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/network"
)
// rebuilds network settings for all apps
func main() {
apps, err := common.DokkuApps()
if err != nil {
common.LogFail(err.Error())
}
for _, appName := range apps {
network.BuildConfig(appName)
}
}

View File

@@ -1,34 +0,0 @@
package main
import (
"flag"
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/network"
)
// displays a network report for one or more apps
func main() {
flag.Parse()
appName := flag.Arg(1)
infoFlag := flag.Arg(2)
if strings.HasPrefix(appName, "--") {
infoFlag = appName
appName = ""
}
if len(appName) == 0 {
apps, err := common.DokkuApps()
if err != nil {
return
}
for _, appName := range apps {
network.ReportSingleApp(appName, infoFlag)
}
return
}
network.ReportSingleApp(appName, infoFlag)
}

View File

@@ -1,22 +0,0 @@
package main
import (
"flag"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/network"
)
// set or clear a network property for an app
func main() {
flag.Parse()
appName := flag.Arg(1)
property := flag.Arg(2)
value := flag.Arg(3)
if property == "bind-all-interfaces" && value == "" {
value = "false"
}
common.CommandPropertySet("network", appName, property, value, network.DefaultProperties)
}

View File

@@ -0,0 +1,37 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/network"
)
// main entrypoint to all subcommands
func main() {
parts := strings.Split(os.Args[0], "/")
subcommand := parts[len(parts)-1]
flag.Parse()
switch subcommand {
case "rebuild":
appName := flag.Arg(1)
network.BuildConfig(appName)
case "rebuildall":
network.CommandRebuildall()
case "report":
appName := flag.Arg(1)
infoFlag := flag.Arg(2)
network.CommandReport(appName, infoFlag)
case "set":
appName := flag.Arg(1)
property := flag.Arg(2)
value := flag.Arg(3)
network.CommandSet(appName, property, value)
default:
common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand))
}
}

View File

@@ -1,36 +0,0 @@
package main
import (
"fmt"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/proxy"
)
// runs the install step for the network plugin
func main() {
if err := common.PropertySetup("network"); err != nil {
common.LogFail(fmt.Sprintf("Unable to install the network plugin: %s", err.Error()))
}
apps, err := common.DokkuApps()
if err != nil {
return
}
for _, appName := range apps {
if common.PropertyExists("network", appName, "bind-all-interfaces") {
continue
}
if proxy.IsAppProxyEnabled(appName) {
common.LogVerboseQuiet("Setting network property 'bind-all-interfaces' to false")
if err := common.PropertyWrite("network", appName, "bind-all-interfaces", "false"); err != nil {
common.LogWarn(err.Error())
}
} else {
common.LogVerboseQuiet("Setting network property 'bind-all-interfaces' to true")
if err := common.PropertyWrite("network", appName, "bind-all-interfaces", "true"); err != nil {
common.LogWarn(err.Error())
}
}
}
}

View File

@@ -1,15 +0,0 @@
package main
import (
"flag"
"github.com/dokku/dokku/plugins/network"
)
// rebuilds network settings for an app
func main() {
flag.Parse()
appName := flag.Arg(0)
network.BuildConfig(appName)
}

View File

@@ -1,46 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"unicode/utf8"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/config"
)
// computes the ports for a given app container
func main() {
flag.Parse()
appName := flag.Arg(0)
processType := flag.Arg(1)
isHerokuishContainer := common.ToBool(flag.Arg(2))
if processType != "web" {
return
}
var dockerfilePorts []string
if !isHerokuishContainer {
dokkuDockerfilePorts := strings.Trim(config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", ""), " ")
if utf8.RuneCountInString(dokkuDockerfilePorts) > 0 {
dockerfilePorts = strings.Split(dokkuDockerfilePorts, " ")
}
}
var ports []string
if len(dockerfilePorts) == 0 {
ports = append(ports, "5000")
} else {
for _, port := range dockerfilePorts {
port = strings.TrimSuffix(strings.TrimSpace(port), "/tcp")
if port == "" || strings.HasSuffix(port, "/udp") {
continue
}
ports = append(ports, port)
}
}
fmt.Fprint(os.Stdout, strings.Join(ports, " "))
}

View File

@@ -1,22 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"github.com/dokku/dokku/plugins/network"
)
// writes true or false to stdout whether a given app has network config
func main() {
flag.Parse()
appName := flag.Arg(0)
if network.HasNetworkConfig(appName) {
fmt.Fprintln(os.Stdout, "true")
return
}
fmt.Fprintln(os.Stdout, "false")
}

View File

@@ -1,20 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"github.com/dokku/dokku/plugins/network"
)
// write the ipaddress to stdout for a given app container
func main() {
flag.Parse()
appName := flag.Arg(0)
processType := flag.Arg(1)
containerID := flag.Arg(2)
ipAddress := network.GetContainerIpaddress(appName, processType, containerID)
fmt.Fprintln(os.Stdout, ipAddress)
}

View File

@@ -1,19 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/network"
)
// returns the listeners (host:port combinations) for a given app container
func main() {
flag.Parse()
appName := flag.Arg(0)
listeners := network.GetListeners(appName)
fmt.Fprint(os.Stdout, strings.Join(listeners, " "))
}

View File

@@ -1,22 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"github.com/dokku/dokku/plugins/common"
network "github.com/dokku/dokku/plugins/network"
)
// write the port to stdout for a given app container
func main() {
flag.Parse()
appName := flag.Arg(0)
processType := flag.Arg(1)
isHerokuishContainer := common.ToBool(flag.Arg(2))
containerID := flag.Arg(3)
port := network.GetContainerPort(appName, processType, isHerokuishContainer, containerID)
fmt.Fprintln(os.Stdout, port)
}

View File

@@ -1,21 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"github.com/dokku/dokku/plugins/common"
network "github.com/dokku/dokku/plugins/network"
)
// writes the network property to stdout for a given app container
func main() {
flag.Parse()
appName := flag.Arg(0)
property := flag.Arg(1)
defaultValue := network.GetDefaultValue(property)
value := common.PropertyGetDefault("network", appName, property, defaultValue)
fmt.Fprintln(os.Stdout, value)
}

View File

@@ -1,41 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
)
// writes the ip to disk
func main() {
flag.Parse()
appName := flag.Arg(0)
processType := flag.Arg(1)
containerIndex := flag.Arg(2)
ip := flag.Arg(3)
if appName == "" {
common.LogFail("Please specify an app to run the command on")
}
err := common.VerifyAppName(appName)
if err != nil {
common.LogFail(err.Error())
}
appRoot := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName}, "/")
filename := fmt.Sprintf("%v/IP.%v.%v", appRoot, processType, containerIndex)
f, err := os.Create(filename)
if err != nil {
common.LogFail(err.Error())
}
defer f.Close()
ipBytes := []byte(ip)
_, err = f.Write(ipBytes)
if err != nil {
common.LogFail(err.Error())
}
}

View File

@@ -1,41 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
)
// writes the port to disk
func main() {
flag.Parse()
appName := flag.Arg(0)
processType := flag.Arg(1)
containerIndex := flag.Arg(2)
port := flag.Arg(3)
if appName == "" {
common.LogFail("Please specify an app to run the command on")
}
err := common.VerifyAppName(appName)
if err != nil {
common.LogFail(err.Error())
}
appRoot := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName}, "/")
filename := fmt.Sprintf("%v/PORT.%v.%v", appRoot, processType, containerIndex)
f, err := os.Create(filename)
if err != nil {
common.LogFail(err.Error())
}
defer f.Close()
portBytes := []byte(port)
_, err = f.Write(portBytes)
if err != nil {
common.LogFail(err.Error())
}
}

View File

@@ -1,19 +0,0 @@
package main
import (
"flag"
"os"
"github.com/dokku/dokku/plugins/network"
)
// cleanup network files for a new app clone
func main() {
flag.Parse()
appName := flag.Arg(1)
success := network.PostAppCloneSetup(appName)
if !success {
os.Exit(1)
}
}

View File

@@ -1,18 +0,0 @@
package main
import (
"flag"
"github.com/dokku/dokku/plugins/common"
)
// set bind-all-interfaces to false by default
func main() {
flag.Parse()
appName := flag.Arg(0)
err := common.PropertyWrite("network", appName, "bind-all-interfaces", "false")
if err != nil {
common.LogWarn(err.Error())
}
}

View File

@@ -1,18 +0,0 @@
package main
import (
"flag"
"github.com/dokku/dokku/plugins/common"
)
// destroys the network property for a given app container
func main() {
flag.Parse()
appName := flag.Arg(0)
err := common.PropertyDestroy("network", appName)
if err != nil {
common.LogFail(err.Error())
}
}

View File

@@ -1,15 +0,0 @@
package main
import (
"flag"
"github.com/dokku/dokku/plugins/network"
)
// displays a network report for one or more apps
func main() {
flag.Parse()
appName := flag.Arg(0)
network.ReportSingleApp(appName, "")
}

View File

@@ -0,0 +1,78 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/network"
)
// main entrypoint to all triggers
func main() {
parts := strings.Split(os.Args[0], "/")
trigger := parts[len(parts)-1]
flag.Parse()
switch trigger {
case "install":
network.TriggerInstall()
case "network-build-config":
appName := flag.Arg(0)
network.BuildConfig(appName)
case "network-compute-ports":
appName := flag.Arg(0)
processType := flag.Arg(1)
isHerokuishContainer := common.ToBool(flag.Arg(2))
network.TriggerNetworkComputePorts(appName, processType, isHerokuishContainer)
case "network-config-exists":
appName := flag.Arg(0)
network.TriggerNetworkConfigExists(appName)
case "network-get-ipaddr":
appName := flag.Arg(0)
processType := flag.Arg(1)
containerID := flag.Arg(2)
network.TriggerNetworkGetIppaddr(appName, processType, containerID)
case "network-get-listeners":
appName := flag.Arg(0)
network.TriggerNetworkGetListeners(appName)
case "network-get-port":
appName := flag.Arg(0)
processType := flag.Arg(1)
isHerokuishContainer := common.ToBool(flag.Arg(2))
containerID := flag.Arg(3)
network.TriggerNetworkGetPort(appName, processType, isHerokuishContainer, containerID)
case "network-get-property":
appName := flag.Arg(0)
property := flag.Arg(1)
network.TriggerNetworkGetProperty(appName, property)
case "network-write-ipaddr":
appName := flag.Arg(0)
processType := flag.Arg(1)
containerIndex := flag.Arg(2)
ip := flag.Arg(3)
network.TriggerNetworkWriteIpaddr(appName, processType, containerIndex, ip)
case "network-write-port":
appName := flag.Arg(0)
processType := flag.Arg(1)
containerIndex := flag.Arg(2)
port := flag.Arg(3)
network.TriggerNetworkWritePort(appName, processType, containerIndex, port)
case "post-app-clone-setup":
appName := flag.Arg(1)
network.TriggerPostAppCloneSetup(appName)
case "post-create":
appName := flag.Arg(0)
network.TriggerPostCreate(appName)
case "post-delete":
appName := flag.Arg(0)
network.TriggerPostDelete(appName)
case "report":
appName := flag.Arg(0)
network.ReportSingleApp(appName, "")
default:
common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger))
}
}