feat: implement container network attaching
Containers can be attached: - after they are created, but before they are started - after a successful deploy, but before the proxy reloads This allows folks to have flexibility around when they would like a container to be made available to a network.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -19,9 +21,55 @@ var (
|
||||
// DefaultProperties is a map of all valid network properties with corresponding default property values
|
||||
DefaultProperties = map[string]string{
|
||||
"bind-all-interfaces": "false",
|
||||
"attach-post-create": "",
|
||||
"attach-post-deploy": "",
|
||||
"tld": "",
|
||||
}
|
||||
)
|
||||
|
||||
// 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 {
|
||||
@@ -186,6 +234,21 @@ 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")
|
||||
@@ -217,6 +280,8 @@ func ReportSingleApp(appName, infoFlag string) {
|
||||
|
||||
infoFlags := map[string]string{
|
||||
"--network-bind-all-interfaces": common.PropertyGet("network", appName, "bind-all-interfaces"),
|
||||
"--network-attach-post-create": common.PropertyGet("network", appName, "attach-post-create"),
|
||||
"--network-attach-post-deploy": common.PropertyGet("network", appName, "attach-post-deploy"),
|
||||
"--network-listeners": strings.Join(GetListeners(appName), " "),
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user