feat: add network:report command

This commit is contained in:
Jose Diaz-Gonzalez
2017-10-04 00:48:02 -04:00
parent fdbab2c291
commit ad732bd42b
8 changed files with 141 additions and 17 deletions

View File

@@ -8,6 +8,7 @@ import (
"os/exec"
"regexp"
"strings"
"unicode"
sh "github.com/codeskyblue/go-sh"
)
@@ -311,6 +312,14 @@ func ToBool(s string) bool {
return s == "true"
}
// UcFirst uppercases the first character in a string
func UcFirst(str string) string {
for i, v := range str {
return string(unicode.ToUpper(v)) + str[i+1:]
}
return ""
}
// VerifyAppName verifies app name format and app existence"
func VerifyAppName(appName string) (err error) {
if appName == "" {

View File

@@ -31,7 +31,7 @@ func LogInfo2(text string) {
// LogInfo2Quiet is the info2 header formatter (with quiet option)
func LogInfo2Quiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") != "" {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
LogInfo2(text)
}
}

View File

@@ -8,7 +8,7 @@ build-in-docker: clean
bash -c "make build" || exit $$?
build: commands subcommands triggers
subcommands: subcommands/rebuild subcommands/rebuildall subcommands/set
subcommands: subcommands/rebuild subcommands/rebuildall subcommands/report subcommands/set
triggers: install network-build-config network-compute-ports network-config-exists network-get-ipaddr network-get-listeners network-get-port network-get-property network-write-ipaddr network-write-port post-create post-delete
commands: **/**/commands.go
go build -a -o commands src/commands/commands.go
@@ -19,6 +19,9 @@ subcommands/rebuild: **/**/**/rebuild.go
subcommands/rebuildall: **/**/**/rebuildall.go
go build -a -o subcommands/rebuildall src/subcommands/rebuildall/rebuildall.go
subcommands/report: **/**/**/report.go
go build -a -o subcommands/report src/subcommands/report/report.go
subcommands/set: **/**/**/set.go
go build -a -o subcommands/set src/subcommands/set/set.go

View File

@@ -2,6 +2,7 @@ package network
import (
"fmt"
"path/filepath"
"strconv"
"strings"
@@ -148,6 +149,23 @@ func GetDefaultValue(property string) (value string) {
return
}
// GetListeners returns a string array of app listeners
func GetListeners(appName string) []string {
dokkuRoot := common.MustGetEnv("DOKKU_ROOT")
appRoot := strings.Join([]string{dokkuRoot, appName}, "/")
files, _ := filepath.Glob(appRoot + "/IP.web.*")
var listeners []string
for _, ipfile := range files {
portfile := strings.Replace(ipfile, "/IP.web.", "/PORT.web.", 1)
ipAddress := common.ReadFirstLine(ipfile)
port := common.ReadFirstLine(portfile)
listeners = append(listeners, fmt.Sprintf("%s:%s", ipAddress, port))
}
return listeners
}
// HasNetworkConfig returns whether the network configuration for a given app exists
func HasNetworkConfig(appName string) bool {
appRoot := strings.Join([]string{common.MustGetEnv("DOKKU_ROOT"), appName}, "/")

View File

@@ -18,6 +18,7 @@ Manages network settings for an app
Additional commands:`
helpContent = `
network:report [<app>] [<flag>], Displays a network report for one or more apps
network:rebuild <app>, Rebuilds network settings for an app
network:rebuildall, Rebuild network settings for all apps
network:set <app> <property> (<value>), Set or clear a network property for an app

View File

@@ -0,0 +1,67 @@
package main
import (
"flag"
"fmt"
"os"
"reflect"
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/network"
)
func reportSingleApp(appName, infoFlag string) {
infoFlags := map[string]string{
"--network-bind-all-interfaces": common.PropertyGet("network", appName, "bind-all-interfaces"),
"--network-listeners": strings.Join(network.GetListeners(appName), " "),
}
if len(infoFlag) == 0 {
common.LogInfo2Quiet(fmt.Sprintf("%s network information", appName))
for k, v := range infoFlags {
key := common.UcFirst(strings.Replace(strings.TrimPrefix(k, "--"), "-", " ", -1))
common.LogVerbose(fmt.Sprintf("%s: %s", key, v))
}
return
}
for k, v := range infoFlags {
if infoFlag == k {
fmt.Fprintln(os.Stdout, v)
return
}
}
keys := reflect.ValueOf(infoFlags).MapKeys()
strkeys := make([]string, len(keys))
for i := 0; i < len(keys); i++ {
strkeys[i] = keys[i].String()
}
common.LogFail(fmt.Sprintf("Invalid flag passed, valid flags: %s", strings.Join(strkeys, ", ")))
}
// set or clear a network property for an app
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 {
reportSingleApp(appName, infoFlag)
}
return
}
reportSingleApp(appName, infoFlag)
}

View File

@@ -4,10 +4,9 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/network"
)
// returns the listeners (host:port combinations) for a given app container
@@ -15,18 +14,6 @@ func main() {
flag.Parse()
appName := flag.Arg(0)
dokkuRoot := common.MustGetEnv("DOKKU_ROOT")
appRoot := strings.Join([]string{dokkuRoot, appName}, "/")
files, _ := filepath.Glob(appRoot + "/IP.web.*")
var listeners []string
for _, ipfile := range files {
portfile := strings.Replace(ipfile, "/IP.web.", "/PORT.web.", 1)
ipAddress := common.ReadFirstLine(ipfile)
port := common.ReadFirstLine(portfile)
listeners = append(listeners, fmt.Sprintf("%s:%s", ipAddress, port))
}
listeners := network.GetListeners(appName)
fmt.Fprint(os.Stdout, strings.Join(listeners, " "))
}