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:
1
plugins/network/.gitignore
vendored
1
plugins/network/.gitignore
vendored
@@ -1,6 +1,7 @@
|
||||
/commands
|
||||
/subcommands/*
|
||||
/triggers/*
|
||||
/triggers
|
||||
/network-*
|
||||
/install
|
||||
/post-app-clone-setup
|
||||
|
||||
@@ -1,37 +1,6 @@
|
||||
include ../../common.mk
|
||||
|
||||
GO_ARGS ?= -a
|
||||
|
||||
SUBCOMMANDS = subcommands/rebuild subcommands/rebuildall subcommands/report subcommands/set
|
||||
TRIGGERS = triggers/install triggers/network-build-config triggers/network-compute-ports triggers/network-config-exists triggers/network-get-ipaddr triggers/network-get-listeners triggers/network-get-port triggers/network-get-property triggers/network-write-ipaddr triggers/network-write-port triggers/post-app-clone-setup triggers/post-create triggers/post-delete triggers/report
|
||||
build-in-docker: clean
|
||||
docker run --rm \
|
||||
-v $$PWD/../..:$(GO_REPO_ROOT) \
|
||||
-w $(GO_REPO_ROOT)/plugins/network \
|
||||
$(BUILD_IMAGE) \
|
||||
bash -c "GO_ARGS='$(GO_ARGS)' make -j4 build" || exit $$?
|
||||
BUILD = commands subcommands triggers
|
||||
PLUGIN_NAME = network
|
||||
|
||||
build: commands subcommands triggers
|
||||
$(MAKE) triggers-copy
|
||||
|
||||
commands: **/**/commands.go
|
||||
go build $(GO_ARGS) -o commands src/commands/commands.go
|
||||
|
||||
subcommands: $(SUBCOMMANDS)
|
||||
|
||||
subcommands/%: src/subcommands/*/%.go
|
||||
go build $(GO_ARGS) -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -rf commands subcommands triggers network-* install post-app-clone-setup post-create post-delete report
|
||||
|
||||
src-clean:
|
||||
rm -rf .gitignore src triggers vendor Makefile *.go glide.*
|
||||
|
||||
triggers: $(TRIGGERS)
|
||||
|
||||
triggers/%: src/triggers/*/%.go
|
||||
go build $(GO_ARGS) -o $@ $<
|
||||
|
||||
triggers-copy:
|
||||
cp triggers/* .
|
||||
include ../../common.mk
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
37
plugins/network/src/subcommands/subcommands.go
Normal file
37
plugins/network/src/subcommands/subcommands.go
Normal 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))
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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, " "))
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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, " "))
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
}
|
||||
@@ -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, "")
|
||||
}
|
||||
78
plugins/network/src/triggers/triggers.go
Normal file
78
plugins/network/src/triggers/triggers.go
Normal 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))
|
||||
}
|
||||
}
|
||||
48
plugins/network/subcommands.go
Normal file
48
plugins/network/subcommands.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
// rebuilds network settings for all apps
|
||||
func CommandRebuildall() {
|
||||
apps, err := common.DokkuApps()
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
for _, appName := range apps {
|
||||
BuildConfig(appName)
|
||||
}
|
||||
}
|
||||
|
||||
// displays a network report for one or more apps
|
||||
func CommandReport(appName string, infoFlag string) {
|
||||
if strings.HasPrefix(appName, "--") {
|
||||
infoFlag = appName
|
||||
appName = ""
|
||||
}
|
||||
|
||||
if len(appName) == 0 {
|
||||
apps, err := common.DokkuApps()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, appName := range apps {
|
||||
ReportSingleApp(appName, infoFlag)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ReportSingleApp(appName, infoFlag)
|
||||
}
|
||||
|
||||
// set or clear a network property for an app
|
||||
func CommandSet(appName string, property string, value string) {
|
||||
if property == "bind-all-interfaces" && value == "" {
|
||||
value = "false"
|
||||
}
|
||||
|
||||
common.CommandPropertySet("network", appName, property, value, DefaultProperties)
|
||||
}
|
||||
178
plugins/network/triggers.go
Normal file
178
plugins/network/triggers.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
"github.com/dokku/dokku/plugins/config"
|
||||
"github.com/dokku/dokku/plugins/proxy"
|
||||
)
|
||||
|
||||
// runs the install step for the network plugin
|
||||
func TriggerInstall() {
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// computes the ports for a given app container
|
||||
func TriggerNetworkComputePorts(appName string, processType string, isHerokuishContainer bool) {
|
||||
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, " "))
|
||||
}
|
||||
|
||||
// writes true or false to stdout whether a given app has network config
|
||||
func TriggerNetworkConfigExists(appName string) {
|
||||
if HasNetworkConfig(appName) {
|
||||
fmt.Fprintln(os.Stdout, "true")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stdout, "false")
|
||||
}
|
||||
|
||||
// write the ipaddress to stdout for a given app container
|
||||
func TriggerNetworkGetIppaddr(appName string, processType string, containerID string) {
|
||||
ipAddress := GetContainerIpaddress(appName, processType, containerID)
|
||||
fmt.Fprintln(os.Stdout, ipAddress)
|
||||
}
|
||||
|
||||
// returns the listeners (host:port combinations) for a given app container
|
||||
func TriggerNetworkGetListeners(appName string) {
|
||||
listeners := GetListeners(appName)
|
||||
fmt.Fprint(os.Stdout, strings.Join(listeners, " "))
|
||||
}
|
||||
|
||||
// write the port to stdout for a given app container
|
||||
func TriggerNetworkGetPort(appName string, processType string, isHerokuishContainer bool, containerID string) {
|
||||
port := GetContainerPort(appName, processType, isHerokuishContainer, containerID)
|
||||
fmt.Fprintln(os.Stdout, port)
|
||||
}
|
||||
|
||||
// writes the network property to stdout for a given app container
|
||||
func TriggerNetworkGetProperty(appName string, property string) {
|
||||
defaultValue := GetDefaultValue(property)
|
||||
value := common.PropertyGetDefault("network", appName, property, defaultValue)
|
||||
fmt.Fprintln(os.Stdout, value)
|
||||
}
|
||||
|
||||
// writes the ip to disk
|
||||
func TriggerNetworkWriteIpaddr(appName string, processType string, containerIndex string, ip string) {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
// writes the port to disk
|
||||
func TriggerNetworkWritePort(appName string, processType string, containerIndex string, port string) {
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
// cleanup network files for a new app clone
|
||||
func TriggerPostAppCloneSetup(appName string) {
|
||||
success := PostAppCloneSetup(appName)
|
||||
if !success {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// set bind-all-interfaces to false by default
|
||||
func TriggerPostCreate(appName string) {
|
||||
err := common.PropertyWrite("network", appName, "bind-all-interfaces", "false")
|
||||
if err != nil {
|
||||
common.LogWarn(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// destroys the network property for a given app container
|
||||
func TriggerPostDelete(appName string) {
|
||||
err := common.PropertyDestroy("network", appName)
|
||||
if err != nil {
|
||||
common.LogFail(err.Error())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user