fix: implement missing network:info command

Also add json format output to the network:list command.

Closes #7093
This commit is contained in:
Jose Diaz-Gonzalez
2024-09-23 20:27:13 -04:00
parent 0f8395365e
commit 61b7752cce
5 changed files with 181 additions and 33 deletions

View File

@@ -1,13 +1,27 @@
package network
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/dokku/dokku/plugins/common"
)
type DockerNetwork struct {
CreatedAt time.Time
Driver string
ID string
Internal bool
IPv6 bool
Labels map[string]string
Name string
Scope string
}
// attachAppToNetwork attaches a container to a network
func attachAppToNetwork(containerID string, networkName string, appName string, phase string, processType string) error {
if isContainerInNetwork(containerID, networkName) {
@@ -110,13 +124,13 @@ func networkExists(networkName string) (bool, error) {
exists := false
networks, err := listNetworks()
networks, err := getNetworks()
if err != nil {
return false, err
}
for _, n := range networks {
if networkName == n {
for _, network := range networks {
if networkName == network.Name {
exists = true
break
}
@@ -125,21 +139,75 @@ func networkExists(networkName string) (bool, error) {
return exists, nil
}
// listNetworks returns a list of docker networks
func listNetworks() ([]string, error) {
// getNetworks returns a list of docker networks
func getNetworks() (map[string]DockerNetwork, error) {
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: common.DockerBin(),
Args: []string{"network", "ls", "--format", "{{ .Name }}"},
Args: []string{"network", "ls", "--format", "json"},
})
if err != nil {
common.LogVerboseQuiet(result.StderrContents())
return []string{}, err
return map[string]DockerNetwork{}, err
}
if result.ExitCode != 0 {
common.LogVerboseQuiet(result.StderrContents())
return []string{}, fmt.Errorf("Unable to list networks")
return map[string]DockerNetwork{}, fmt.Errorf("Unable to list networks")
}
networkLines := strings.Split(result.StdoutContents(), "\n")
networks := map[string]DockerNetwork{}
for _, line := range networkLines {
if line == "" {
continue
}
result := make(map[string]interface{})
err := json.Unmarshal([]byte(line), &result)
if err != nil {
return map[string]DockerNetwork{}, err
}
network := DockerNetwork{
Driver: result["Driver"].(string),
ID: result["ID"].(string),
Name: result["Name"].(string),
Scope: result["Scope"].(string),
Labels: map[string]string{},
}
if createdAtVal := result["CreatedAt"].(string); createdAtVal != "" {
createdAt, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", "2024-02-25 01:55:24.275184461 +0000 UTC")
if err == nil {
network.CreatedAt = createdAt
}
}
if ipv6Val := result["IPv6"].(string); ipv6Val != "" {
val, err := strconv.ParseBool(ipv6Val)
if err == nil {
network.IPv6 = val
}
}
if internalVal := result["Internal"].(string); internalVal != "" {
val, err := strconv.ParseBool(internalVal)
if err == nil {
network.Internal = val
}
}
labels := strings.Split(result["Labels"].(string), ",")
for _, v := range labels {
parts := strings.SplitN(v, "=", 2)
if len(parts) != 2 {
continue
}
key := parts[0]
value := parts[1]
network.Labels[key] = value
}
networks[network.Name] = network
}
networks := strings.Split(result.StdoutContents(), "\n")
return networks, nil
}

View File

@@ -21,8 +21,8 @@ Additional commands:`
network:create <network>, Creates an attachable docker network
network:destroy <network>, Destroys a docker network
network:exists <network>, Checks if a docker network exists
network:info <network>, Outputs information about a docker network
network:list, Lists all docker networks
network:info <network> [--format text|json], Outputs information about a docker network
network:list [--format text|json], Lists all docker networks
network:rebuild <app>, Rebuilds network settings for an app
network:rebuildall, Rebuild network settings for all apps
network:report [<app>] [<flag>], Displays a network report for one or more apps

View File

@@ -36,12 +36,15 @@ func main() {
err = network.CommandExists(networkName)
case "info":
args := flag.NewFlagSet("network:info", flag.ExitOnError)
format := args.String("format", "text", "format: [ text | json ]")
args.Parse(os.Args[2:])
err = network.CommandInfo()
networkName := args.Arg(0)
err = network.CommandInfo(networkName, *format)
case "list":
args := flag.NewFlagSet("network:list", flag.ExitOnError)
format := args.String("format", "text", "format: [ text | json ]")
args.Parse(os.Args[2:])
err = network.CommandList()
err = network.CommandList(*format)
case "rebuild":
args := flag.NewFlagSet("network:rebuild", flag.ExitOnError)
args.Parse(os.Args[2:])

View File

@@ -1,6 +1,7 @@
package network
import (
"encoding/json"
"errors"
"fmt"
"os"
@@ -74,20 +75,67 @@ func CommandExists(networkName string) error {
}
// CommandInfo is an alias for "docker network inspect"
func CommandInfo() error {
return nil
}
func CommandInfo(networkName string, format string) error {
if networkName == "" {
return errors.New("No network name specified")
}
// CommandList is an alias for "docker network ls"
func CommandList() error {
networks, err := listNetworks()
if format != "json" && format != "text" {
return errors.New("Invalid format specified, use either text or json")
}
networks, err := getNetworks()
if err != nil {
return err
}
network, ok := networks[networkName]
if !ok {
return errors.New("Network does not exist")
}
if format == "json" {
out, err := json.Marshal(network)
if err != nil {
return err
}
common.Log(string(out))
return nil
}
length := 10
common.LogInfo2Quiet(fmt.Sprintf("%s network information", networkName))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("ID:", length, " "), network.ID))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("Name:", length, " "), network.Name))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("Driver:", length, " "), network.Driver))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("Scope:", length, " "), network.Scope))
return nil
}
// CommandList is an alias for "docker network ls"
func CommandList(format string) error {
networks, err := getNetworks()
if err != nil {
return err
}
if format == "json" {
networkList := []DockerNetwork{}
for _, network := range networks {
networkList = append(networkList, network)
}
out, err := json.Marshal(networkList)
if err != nil {
return err
}
common.Log(string(out))
return nil
}
common.LogInfo2Quiet("Networks")
for _, networkName := range networks {
fmt.Println(networkName)
for _, network := range networks {
fmt.Println(network.Name)
}
return nil