refactor: move all internal functions to functions.go

This commit is contained in:
Jose Diaz-Gonzalez
2020-02-09 01:20:39 -05:00
parent 231078125a
commit 52ef7e7cdc
4 changed files with 114 additions and 88 deletions

View File

@@ -0,0 +1,110 @@
package network
import (
"bytes"
"errors"
"fmt"
"strings"
"github.com/dokku/dokku/plugins/common"
sh "github.com/codeskyblue/go-sh"
)
// attachAppToNetwork attaches a container to a network
func attachAppToNetwork(containerID string, networkName string, appName string, phase string, processType string) {
cmdParts := []string{
common.DockerBin(),
"network",
"connect",
}
if phase == "deploy" {
property := "tld"
defaultValue := GetDefaultValue(property)
tld := common.PropertyGetDefault("network", appName, property, defaultValue)
networkAlias := fmt.Sprintf("%v.%v", appName, processType)
if tld != "" {
networkAlias = fmt.Sprintf("%v.%v", networkAlias, tld)
}
cmdParts = append(cmdParts, "--alias")
cmdParts = append(cmdParts, networkAlias)
hostname, err := common.DockerInspect(containerID, "{{ .Config.Hostname }}")
if err != nil {
common.LogWarn(err.Error())
} else {
cmdParts = append(cmdParts, "--alias")
cmdParts = append(cmdParts, fmt.Sprintf("%v.%v", hostname, networkAlias))
}
}
cmdParts = append(cmdParts, networkName)
cmdParts = append(cmdParts, containerID)
attachCmd := common.NewShellCmd(strings.Join(cmdParts, " "))
var stderr bytes.Buffer
attachCmd.ShowOutput = false
attachCmd.Command.Stderr = &stderr
_, err := attachCmd.Output()
if err != nil {
err = errors.New(strings.TrimSpace(stderr.String()))
common.LogFail(fmt.Sprintf("Unable to attach container to network: %v", err.Error()))
}
}
// isConflictingPropertyValue returns true if the other attach property has a conflicting value
func isConflictingPropertyValue(appName string, property string, value string) bool {
if value == "" {
return false
}
otherProperty := "attach-post-create"
if property == otherProperty {
otherProperty = "attach-post-deploy"
}
defaultValue := GetDefaultValue(otherProperty)
otherValue := common.PropertyGetDefault("network", appName, otherProperty, defaultValue)
return value == otherValue
}
// networkExists checks to see if a network exists
func networkExists(networkName string) (bool, error) {
if networkName == "" {
return false, errors.New("No network name specified")
}
exists := false
networks, err := listNetworks()
if err != nil {
return false, err
}
for _, n := range networks {
if networkName == n {
exists = true
break
}
}
return exists, nil
}
// listNetworks returns a list of docker networks
func listNetworks() ([]string, error) {
b, err := sh.Command(common.DockerBin(), "network", "list", "--format", "{{ .Name }}").Output()
output := strings.TrimSpace(string(b[:]))
networks := []string{}
if err != nil {
common.LogVerboseQuiet(output)
return networks, err
}
networks = strings.Split(output, "\n")
return networks, nil
}

View File

@@ -1,8 +1,6 @@
package network
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
@@ -27,49 +25,6 @@ var (
}
)
// AttachAppToNetwork attaches a container to a network
func AttachAppToNetwork(containerID string, networkName string, appName string, phase string, processType string) {
cmdParts := []string{
common.DockerBin(),
"network",
"connect",
}
if phase == "deploy" {
property := "tld"
defaultValue := GetDefaultValue(property)
tld := common.PropertyGetDefault("network", appName, property, defaultValue)
networkAlias := fmt.Sprintf("%v.%v", appName, processType)
if tld != "" {
networkAlias = fmt.Sprintf("%v.%v", networkAlias, tld)
}
cmdParts = append(cmdParts, "--alias")
cmdParts = append(cmdParts, networkAlias)
hostname, err := common.DockerInspect(containerID, "{{ .Config.Hostname }}")
if err != nil {
common.LogWarn(err.Error())
} else {
cmdParts = append(cmdParts, "--alias")
cmdParts = append(cmdParts, fmt.Sprintf("%v.%v", hostname, networkAlias))
}
}
cmdParts = append(cmdParts, networkName)
cmdParts = append(cmdParts, containerID)
attachCmd := common.NewShellCmd(strings.Join(cmdParts, " "))
var stderr bytes.Buffer
attachCmd.ShowOutput = false
attachCmd.Command.Stderr = &stderr
_, err := attachCmd.Output()
if err != nil {
err = errors.New(strings.TrimSpace(stderr.String()))
common.LogFail(fmt.Sprintf("Unable to attach container to network: %v", err.Error()))
}
}
// BuildConfig builds network config files
func BuildConfig(appName string) {
if err := common.VerifyAppName(appName); err != nil {
@@ -234,21 +189,6 @@ func HasNetworkConfig(appName string) bool {
return common.FileExists(ipfile) && common.FileExists(portfile)
}
// ListNetworks returns a list of docker networks
func ListNetworks() ([]string, error) {
b, err := sh.Command(common.DockerBin(), "network", "list", "--format", "{{ .Name }}").Output()
output := strings.TrimSpace(string(b[:]))
networks := []string{}
if err != nil {
common.LogVerboseQuiet(output)
return networks, err
}
networks = strings.Split(output, "\n")
return networks, nil
}
// PostAppCloneSetup removes old IP and PORT files for a newly cloned app
func PostAppCloneSetup(appName string) bool {
dokkuRoot := common.MustGetEnv("DOKKU_ROOT")

View File

@@ -81,23 +81,15 @@ func CommandDestroy(networkName string, forceDestroy bool) error {
// CommandExists checks if a network exists
func CommandExists(networkName string) error {
exists := false
if networkName == "" {
return errors.New("No network name specified")
}
networks, err := ListNetworks()
exists, err := networkExists(networkName)
if err != nil {
return err
}
for _, n := range networks {
if networkName == n {
exists = true
break
}
}
if exists {
common.LogInfo1Quiet("Network exists")
} else {
@@ -114,7 +106,7 @@ func CommandInfo() error {
// CommandList is an alias for "docker network ls"
func CommandList() error {
networks, err := ListNetworks()
networks, err := listNetworks()
if err != nil {
return err
}
@@ -186,19 +178,3 @@ func CommandSet(appName string, property string, value string) {
common.CommandPropertySet("network", appName, property, value, DefaultProperties)
}
func isConflictingPropertyValue(appName string, property string, value string) bool {
if value == "" {
return false
}
otherProperty := "attach-post-create"
if property == otherProperty {
otherProperty = "attach-post-deploy"
}
defaultValue := GetDefaultValue(otherProperty)
otherValue := common.PropertyGetDefault("network", appName, otherProperty, defaultValue)
return value == otherValue
}

View File

@@ -174,7 +174,7 @@ func TriggerPostContainerCreate(containerType string, containerID string, appNam
return
}
AttachAppToNetwork(containerID, networkName, appName, phase, processType)
attachAppToNetwork(containerID, networkName, appName, phase, processType)
}
// TriggerPostCreate sets bind-all-interfaces to false by default
@@ -213,6 +213,6 @@ func TriggerCorePostDeploy(appName string) {
if err != nil {
common.LogFail(err.Error())
}
AttachAppToNetwork(containerID, networkName, appName, "deploy", processType)
attachAppToNetwork(containerID, networkName, appName, "deploy", processType)
}
}