refactor: bubble up error handling so that standard exit codes can be set at the top-level

LogFail shouldn't be called internally if at all possible.
This commit is contained in:
Jose Diaz-Gonzalez committed 2021-02-28 15:20:46 -05:00
1 parent ccdbaa84f9
commit d09a7506df
26 files changed
+113 -80

No files matched your search

+55 -32
View File
@@ -11,15 +11,16 @@ import (
)
// TriggerInstall runs the install step for the network plugin
func TriggerInstall() {
func TriggerInstall() error {
if err := common.PropertySetup("network"); err != nil {
common.LogFail(fmt.Sprintf("Unable to install the network plugin: %s", err.Error()))
return fmt.Errorf("Unable to install the network plugin: %s", err.Error())
}
apps, err := common.DokkuApps()
if err != nil {
return
return nil
}
for _, appName := range apps {
if common.PropertyExists("network", appName, "bind-all-interfaces") {
continue
@@ -36,10 +37,12 @@ func TriggerInstall() {
}
}
}
return nil
}
// TriggerNetworkComputePorts computes the ports for a given app container
func TriggerNetworkComputePorts(appName string, processType string, isHerokuishContainer bool) {
func TriggerNetworkComputePorts(appName string, processType string, isHerokuishContainer bool) error {
var dockerfilePorts []string
if !isHerokuishContainer {
dokkuDockerfilePorts := strings.Trim(config.GetWithDefault(appName, "DOKKU_DOCKERFILE_PORTS", ""), " ")
@@ -60,80 +63,91 @@ func TriggerNetworkComputePorts(appName string, processType string, isHerokuishC
ports = append(ports, port)
}
}
fmt.Println(strings.Join(ports, " "))
return nil
}
// TriggerNetworkConfigExists writes true or false to stdout whether a given app has network config
func TriggerNetworkConfigExists(appName string) {
func TriggerNetworkConfigExists(appName string) error {
if HasNetworkConfig(appName) {
fmt.Println("true")
return
return nil
}
fmt.Println("false")
return nil
}
// TriggerNetworkGetIppaddr writes the ipaddress to stdout for a given app container
func TriggerNetworkGetIppaddr(appName string, processType string, containerID string) {
func TriggerNetworkGetIppaddr(appName string, processType string, containerID string) error {
ipAddress := GetContainerIpaddress(appName, processType, containerID)
fmt.Println(ipAddress)
return nil
}
// TriggerNetworkGetListeners returns the listeners (host:port combinations) for a given app container
func TriggerNetworkGetListeners(appName string, processType string) {
func TriggerNetworkGetListeners(appName string, processType string) error {
if processType == "" {
common.LogWarn("Deprecated: Please specify a processType argument for the network-get-listeners plugin trigger")
processType = "web"
}
listeners := GetListeners(appName, processType)
fmt.Println(strings.Join(listeners, " "))
return nil
}
// TriggerNetworkGetPort writes the port to stdout for a given app container
func TriggerNetworkGetPort(appName string, processType string, containerID string, isHerokuishContainer bool) {
func TriggerNetworkGetPort(appName string, processType string, containerID string, isHerokuishContainer bool) error {
port := GetContainerPort(appName, processType, containerID, isHerokuishContainer)
fmt.Println(port)
return nil
}
// TriggerNetworkGetProperty writes the network property to stdout for a given app container
func TriggerNetworkGetProperty(appName string, property string) {
func TriggerNetworkGetProperty(appName string, property string) error {
defaultValue := GetDefaultValue(property)
value := common.PropertyGetDefault("network", appName, property, defaultValue)
fmt.Println(value)
return nil
}
// TriggerNetworkWriteIpaddr writes the ip to disk
func TriggerNetworkWriteIpaddr(appName string, processType string, containerIndex string, ip string) {
func TriggerNetworkWriteIpaddr(appName string, processType string, containerIndex string, ip string) error {
appRoot := common.AppRoot(appName)
filename := fmt.Sprintf("%v/IP.%v.%v", appRoot, processType, containerIndex)
f, err := os.Create(filename)
if err != nil {
common.LogFail(err.Error())
return err
}
defer f.Close()
ipBytes := []byte(ip)
_, err = f.Write(ipBytes)
if err != nil {
common.LogFail(err.Error())
return err
}
return nil
}
// TriggerNetworkWritePort writes the port to disk
func TriggerNetworkWritePort(appName string, processType string, containerIndex string, port string) {
func TriggerNetworkWritePort(appName string, processType string, containerIndex string, port string) error {
appRoot := common.AppRoot(appName)
filename := fmt.Sprintf("%v/PORT.%v.%v", appRoot, processType, containerIndex)
f, err := os.Create(filename)
if err != nil {
common.LogFail(err.Error())
return err
}
defer f.Close()
portBytes := []byte(port)
_, err = f.Write(portBytes)
if err != nil {
common.LogFail(err.Error())
return err
}
return nil
}
// TriggerPostAppCloneSetup creates new network files
@@ -170,75 +184,84 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
}
// TriggerPostContainerCreate associates the container with a specified network
func TriggerPostContainerCreate(containerType string, containerID string, appName string, phase string, processType string) {
func TriggerPostContainerCreate(containerType string, containerID string, appName string, phase string, processType string) error {
if containerType != "app" {
return
return nil
}
property := "attach-post-create"
defaultValue := GetDefaultValue(property)
networkName := common.PropertyGetDefault("network", appName, property, defaultValue)
if networkName == "" {
return
return nil
}
exists, err := networkExists(networkName)
if err != nil {
common.LogFail(err.Error())
return err
}
if !exists {
common.LogFail(fmt.Sprintf("Network %v does not exist", networkName))
return fmt.Errorf("Network %v does not exist", networkName)
}
attachAppToNetwork(containerID, networkName, appName, phase, processType)
return attachAppToNetwork(containerID, networkName, appName, phase, processType)
}
// TriggerPostCreate sets bind-all-interfaces to false by default
func TriggerPostCreate(appName string) {
func TriggerPostCreate(appName string) error {
err := common.PropertyWrite("network", appName, "bind-all-interfaces", "false")
if err != nil {
common.LogWarn(err.Error())
}
return nil
}
// TriggerPostDelete destroys the network property for a given app container
func TriggerPostDelete(appName string) {
func TriggerPostDelete(appName string) error {
err := common.PropertyDestroy("network", appName)
if err != nil {
common.LogFail(err.Error())
return err
}
return nil
}
// TriggerCorePostDeploy associates the container with a specified network
func TriggerCorePostDeploy(appName string) {
func TriggerCorePostDeploy(appName string) error {
property := "attach-post-deploy"
defaultValue := GetDefaultValue(property)
networkName := common.PropertyGetDefault("network", appName, property, defaultValue)
if networkName == "" {
return
return nil
}
common.LogInfo1Quiet(fmt.Sprintf("Associating app with network %s", networkName))
containerIDs, err := common.GetAppRunningContainerIDs(appName, "")
if err != nil {
common.LogFail(err.Error())
return err
}
exists, err := networkExists(networkName)
if err != nil {
common.LogFail(err.Error())
return err
}
if !exists {
common.LogFail(fmt.Sprintf("Network %v does not exist", networkName))
return fmt.Errorf("Network %v does not exist", networkName)
}
for _, containerID := range containerIDs {
processType, err := common.DockerInspect(containerID, "{{ index .Config.Labels \"com.dokku.process-type\"}}")
if err != nil {
common.LogFail(err.Error())
return err
}
if err := attachAppToNetwork(containerID, networkName, appName, "deploy", processType); err != nil {
return err
}
attachAppToNetwork(containerID, networkName, appName, "deploy", processType)
}
return nil
}