diff --git a/plugins/app-json/functions.go b/plugins/app-json/functions.go index a41071fdf8b..e70c5f28160 100644 --- a/plugins/app-json/functions.go +++ b/plugins/app-json/functions.go @@ -245,14 +245,14 @@ func executeScript(appName string, image string, imageTag string, phase string) containerID, err := createdContainerID(appName, dockerArgs, image, script, phase) if err != nil { - common.LogFail(fmt.Sprintf("Failed to create %s execution container: %s", phase, err.Error())) + return fmt.Errorf("Failed to create %s execution container: %s", phase, err.Error()) } if !waitForExecution(containerID) { common.LogInfo2Quiet(fmt.Sprintf("Start of %s %s task (%s) output", appName, phaseName, containerID[0:9])) common.LogVerboseQuietContainerLogs(containerID) common.LogInfo2Quiet(fmt.Sprintf("End of %s %s task (%s) output", appName, phaseName, containerID[0:9])) - common.LogFail(fmt.Sprintf("Execution of %s task failed: %s", phase, phaseName)) + return fmt.Errorf("Execution of %s task failed: %s", phaseName, command) } common.LogInfo2Quiet(fmt.Sprintf("Start of %s %s task (%s) output", appName, phaseName, containerID[0:9])) @@ -292,7 +292,7 @@ func executeScript(appName string, image string, imageTag string, phase string) containerCommitCmd.ShowOutput = false containerCommitCmd.Command.Stderr = os.Stderr if !containerCommitCmd.Execute() { - common.LogFail(fmt.Sprintf("Commiting of '%s' to image failed: %s", phase, command)) + return fmt.Errorf("Commiting of '%s' to image failed: %s", phase, command) } return common.PlugnTrigger("scheduler-register-retired", []string{appName, containerID}...) diff --git a/plugins/app-json/src/triggers/triggers.go b/plugins/app-json/src/triggers/triggers.go index 5ef4cb5f5ee..7b1a67b1ebd 100644 --- a/plugins/app-json/src/triggers/triggers.go +++ b/plugins/app-json/src/triggers/triggers.go @@ -32,10 +32,10 @@ func main() { imageTag := flag.Arg(1) err = appjson.TriggerPreDeploy(appName, imageTag) default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/apps/src/subcommands/subcommands.go b/plugins/apps/src/subcommands/subcommands.go index 5b455660ea9..0874f70848d 100644 --- a/plugins/apps/src/subcommands/subcommands.go +++ b/plugins/apps/src/subcommands/subcommands.go @@ -78,10 +78,10 @@ func main() { appName := args.Arg(0) err = apps.CommandUnlock(appName) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/apps/src/triggers/triggers.go b/plugins/apps/src/triggers/triggers.go index 7cb643f523c..0fc2dbebea6 100644 --- a/plugins/apps/src/triggers/triggers.go +++ b/plugins/apps/src/triggers/triggers.go @@ -37,10 +37,10 @@ func main() { appName := flag.Arg(0) err = apps.ReportSingleApp(appName, "", "") default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/buildpacks/src/subcommands/subcommands.go b/plugins/buildpacks/src/subcommands/subcommands.go index f86003c2e24..f245238f708 100644 --- a/plugins/buildpacks/src/subcommands/subcommands.go +++ b/plugins/buildpacks/src/subcommands/subcommands.go @@ -72,10 +72,10 @@ func main() { } err = buildpacks.CommandSetProperty(appName, property, value) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/buildpacks/src/triggers/triggers.go b/plugins/buildpacks/src/triggers/triggers.go index c77498d6ee8..cf33fd006d0 100644 --- a/plugins/buildpacks/src/triggers/triggers.go +++ b/plugins/buildpacks/src/triggers/triggers.go @@ -42,10 +42,10 @@ func main() { appName := flag.Arg(0) err = buildpacks.ReportSingleApp(appName, "", "") default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/common/common.go b/plugins/common/common.go index 59c009e423b..eab1018c0c8 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -515,6 +515,24 @@ func isValidAppNameOld(appName string) error { return errors.New("App name must begin with lowercase alphanumeric character, and cannot include uppercase characters, or colons") } +// AppDoesNotExist wraps error to include the app name +// and is used to distinguish between a normal error and an error +// where the app is missing +type AppDoesNotExist struct { + appName string +} + +// ExitCode returns an exit code to use in case this error bubbles +// up into an os.Exit() call +func (err *AppDoesNotExist) ExitCode() int { + return 20 +} + +// Error returns a standard non-existent app error +func (err *AppDoesNotExist) Error() string { + return fmt.Sprintf("App %s does not exist", err.appName) +} + // VerifyAppName checks if an app conforming to either the old or new // naming conventions exists func VerifyAppName(appName string) error { @@ -526,7 +544,7 @@ func VerifyAppName(appName string) error { appRoot := AppRoot(appName) if !DirectoryExists(appRoot) { - return fmt.Errorf("App %s does not exist", appName) + return &AppDoesNotExist{appName} } return nil diff --git a/plugins/common/functions b/plugins/common/functions index 97aeef6757d..a5b5a371114 100755 --- a/plugins/common/functions +++ b/plugins/common/functions @@ -150,13 +150,13 @@ dokku_log_fail_quiet() { if [[ -z "$DOKKU_QUIET_OUTPUT" ]]; then echo " ! $*" 1>&2 fi - exit 1 + exit ${DOKKU_FAIL_EXIT_CODE:=1} } dokku_log_fail() { declare desc="log fail formatter" echo " ! $*" 1>&2 - exit 1 + exit ${DOKKU_FAIL_EXIT_CODE:=1} } dokku_log_stderr() { @@ -256,7 +256,7 @@ verify_app_name() { dokku_log_fail "App name must begin with lowercase alphanumeric character, and cannot include uppercase characters, colons, or underscores" fi - [[ ! -d "$DOKKU_ROOT/$APP" ]] && dokku_log_fail "App $APP does not exist" + [[ ! -d "$DOKKU_ROOT/$APP" ]] && DOKKU_FAIL_EXIT_CODE=20 dokku_log_fail "App $APP does not exist" return 0 } diff --git a/plugins/common/log.go b/plugins/common/log.go index 6b9e053e2c4..7cfc87b62ad 100644 --- a/plugins/common/log.go +++ b/plugins/common/log.go @@ -8,6 +8,11 @@ import ( "strings" ) +// ErrWithExitCode wraps error and exposes an ExitCode method +type ErrWithExitCode interface { + ExitCode() int +} + // LogFail is the failure log formatter // prints text to stderr and exits with status 1 func LogFail(text string) { @@ -15,6 +20,16 @@ func LogFail(text string) { os.Exit(1) } +// LogFailWithError is the failure log formatter +// prints text to stderr and exits with the specified exit code +func LogFailWithError(err error) { + fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", err.Error())) + if errExit, ok := err.(ErrWithExitCode); ok { + os.Exit(errExit.ExitCode()) + } + os.Exit(1) +} + // LogFailQuiet is the failure log formatter (with quiet option) // prints text to stderr and exits with status 1 func LogFailQuiet(text string) { diff --git a/plugins/common/properties.go b/plugins/common/properties.go index 0fc20a38ac2..9b9f9fec7a8 100644 --- a/plugins/common/properties.go +++ b/plugins/common/properties.go @@ -16,7 +16,7 @@ import ( func CommandPropertySet(pluginName, appName, property, value string, properties map[string]string, globalProperties map[string]bool) { if appName != "--global" { if err := VerifyAppName(appName); err != nil { - LogFail(err.Error()) + LogFailWithError(err) } } if appName == "--global" && !globalProperties[property] { @@ -42,9 +42,8 @@ func CommandPropertySet(pluginName, appName, property, value string, properties PropertyWrite(pluginName, appName, property, value) } else { LogInfo2Quiet(fmt.Sprintf("Unsetting %s", property)) - err := PropertyDelete(pluginName, appName, property) - if err != nil { - LogFail(err.Error()) + if err := PropertyDelete(pluginName, appName, property); err != nil { + LogFailWithError(err) } } } diff --git a/plugins/config/config.go b/plugins/config/config.go index 71feb4788c7..45fb9fb2095 100644 --- a/plugins/config/config.go +++ b/plugins/config/config.go @@ -137,9 +137,11 @@ func getEnvironment(appName string, merged bool) (env *Env) { } else { env, err = loadAppOrGlobalEnv(appName) } + if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } + return env } diff --git a/plugins/config/environment.go b/plugins/config/environment.go index 2b4602f0b60..0798c3200d1 100644 --- a/plugins/config/environment.go +++ b/plugins/config/environment.go @@ -77,7 +77,7 @@ func LoadMergedAppEnv(appName string) (env *Env, err error) { } global, err := LoadGlobalEnv() if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } global.Merge(env) global.filename = "" diff --git a/plugins/config/src/commands/commands.go b/plugins/config/src/commands/commands.go index fef47579c80..df63fabaf8f 100644 --- a/plugins/config/src/commands/commands.go +++ b/plugins/config/src/commands/commands.go @@ -48,7 +48,7 @@ func main() { appName := args.Arg(0) err := config.CommandShow(appName, *global, *merged, *shell, *export) if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } case "config:help": usage() diff --git a/plugins/config/src/subcommands/subcommands.go b/plugins/config/src/subcommands/subcommands.go index ae543e1ab0a..f7f1f0d0960 100644 --- a/plugins/config/src/subcommands/subcommands.go +++ b/plugins/config/src/subcommands/subcommands.go @@ -105,10 +105,10 @@ func main() { keys := getKeys(args.Args(), *global) err = config.CommandUnset(appName, keys, *global, *noRestart) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/config/src/triggers/triggers.go b/plugins/config/src/triggers/triggers.go index f55d8078696..814c2e83f1a 100644 --- a/plugins/config/src/triggers/triggers.go +++ b/plugins/config/src/triggers/triggers.go @@ -27,15 +27,15 @@ func main() { case "config-get": appName := flag.Arg(0) key := flag.Arg(1) - config.TriggerConfigGet(appName, key) + err = config.TriggerConfigGet(appName, key) case "config-get-global": key := flag.Arg(0) - config.TriggerConfigGetGlobal(key) + err = config.TriggerConfigGetGlobal(key) default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/cron/src/subcommands/subcommands.go b/plugins/cron/src/subcommands/subcommands.go index 81a47e8adc1..12daa862f87 100644 --- a/plugins/cron/src/subcommands/subcommands.go +++ b/plugins/cron/src/subcommands/subcommands.go @@ -33,10 +33,10 @@ func main() { err = cron.CommandReport(appName, *format, infoFlag) } default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/cron/src/triggers/triggers.go b/plugins/cron/src/triggers/triggers.go index f9a4d34ee76..f48bdc58d59 100644 --- a/plugins/cron/src/triggers/triggers.go +++ b/plugins/cron/src/triggers/triggers.go @@ -28,10 +28,10 @@ func main() { appName := flag.Arg(0) err = cron.ReportSingleApp(appName, "", "") default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/logs/src/commands/commands.go b/plugins/logs/src/commands/commands.go index c4fcc3a9312..42d348e07ef 100644 --- a/plugins/logs/src/commands/commands.go +++ b/plugins/logs/src/commands/commands.go @@ -54,7 +54,7 @@ func main() { appName := args.Arg(0) err := logs.CommandDefault(appName, *num, *ps, *tail, *quiet) if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } case "logs:help": usage() diff --git a/plugins/logs/src/subcommands/subcommands.go b/plugins/logs/src/subcommands/subcommands.go index c14b9503977..0ed04c2775b 100644 --- a/plugins/logs/src/subcommands/subcommands.go +++ b/plugins/logs/src/subcommands/subcommands.go @@ -62,10 +62,10 @@ func main() { args.Parse(os.Args[2:]) err = logs.CommandVectorStop() default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/logs/src/triggers/triggers.go b/plugins/logs/src/triggers/triggers.go index 7c9765e4528..a4d35dc010d 100644 --- a/plugins/logs/src/triggers/triggers.go +++ b/plugins/logs/src/triggers/triggers.go @@ -34,10 +34,10 @@ func main() { appName := flag.Arg(0) err = logs.ReportSingleApp(appName, "", "") default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/network/functions.go b/plugins/network/functions.go index e07e02756a3..17d0ee8e8ed 100644 --- a/plugins/network/functions.go +++ b/plugins/network/functions.go @@ -12,7 +12,7 @@ import ( ) // attachAppToNetwork attaches a container to a network -func attachAppToNetwork(containerID string, networkName string, appName string, phase string, processType string) { +func attachAppToNetwork(containerID string, networkName string, appName string, phase string, processType string) error { cmdParts := []string{ common.DockerBin(), "network", @@ -50,8 +50,10 @@ func attachAppToNetwork(containerID string, networkName string, appName string, _, 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())) + return fmt.Errorf("Unable to attach container to network: %v", err.Error()) } + + return nil } // isConflictingPropertyValue returns true if the other attach property has a conflicting value diff --git a/plugins/network/src/subcommands/subcommands.go b/plugins/network/src/subcommands/subcommands.go index b3bbff98a99..8208299fdd5 100644 --- a/plugins/network/src/subcommands/subcommands.go +++ b/plugins/network/src/subcommands/subcommands.go @@ -68,10 +68,10 @@ func main() { value := args.Arg(2) err = network.CommandSet(appName, property, value) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/network/src/triggers/triggers.go b/plugins/network/src/triggers/triggers.go index 140dad623c6..0ce27d43ac5 100644 --- a/plugins/network/src/triggers/triggers.go +++ b/plugins/network/src/triggers/triggers.go @@ -19,49 +19,49 @@ func main() { var err error switch trigger { case "install": - network.TriggerInstall() + err = network.TriggerInstall() case "network-build-config": appName := flag.Arg(0) - network.BuildConfig(appName) + err = network.BuildConfig(appName) case "network-compute-ports": appName := flag.Arg(0) processType := flag.Arg(1) isHerokuishContainer := common.ToBool(flag.Arg(2)) - network.TriggerNetworkComputePorts(appName, processType, isHerokuishContainer) + err = network.TriggerNetworkComputePorts(appName, processType, isHerokuishContainer) case "network-config-exists": appName := flag.Arg(0) - network.TriggerNetworkConfigExists(appName) + err = network.TriggerNetworkConfigExists(appName) case "network-get-ipaddr": appName := flag.Arg(0) processType := flag.Arg(1) containerID := flag.Arg(2) - network.TriggerNetworkGetIppaddr(appName, processType, containerID) + err = network.TriggerNetworkGetIppaddr(appName, processType, containerID) case "network-get-listeners": appName := flag.Arg(0) processType := flag.Arg(1) - network.TriggerNetworkGetListeners(appName, processType) + err = network.TriggerNetworkGetListeners(appName, processType) case "network-get-port": appName := flag.Arg(0) processType := flag.Arg(1) containerID := flag.Arg(2) isHerokuishContainer := common.ToBool(flag.Arg(3)) - network.TriggerNetworkGetPort(appName, processType, containerID, isHerokuishContainer) + err = network.TriggerNetworkGetPort(appName, processType, containerID, isHerokuishContainer) case "network-get-property": appName := flag.Arg(0) property := flag.Arg(1) - network.TriggerNetworkGetProperty(appName, property) + err = network.TriggerNetworkGetProperty(appName, property) case "network-write-ipaddr": appName := flag.Arg(0) processType := flag.Arg(1) containerIndex := flag.Arg(2) ip := flag.Arg(3) - network.TriggerNetworkWriteIpaddr(appName, processType, containerIndex, ip) + err = network.TriggerNetworkWriteIpaddr(appName, processType, containerIndex, ip) case "network-write-port": appName := flag.Arg(0) processType := flag.Arg(1) containerIndex := flag.Arg(2) port := flag.Arg(3) - network.TriggerNetworkWritePort(appName, processType, containerIndex, port) + err = network.TriggerNetworkWritePort(appName, processType, containerIndex, port) case "post-app-clone-setup": oldAppName := flag.Arg(0) newAppName := flag.Arg(1) @@ -76,24 +76,24 @@ func main() { appName := flag.Arg(2) phase := flag.Arg(3) processType := flag.Arg(4) - network.TriggerPostContainerCreate(containerType, containerID, appName, phase, processType) + err = network.TriggerPostContainerCreate(containerType, containerID, appName, phase, processType) case "post-create": appName := flag.Arg(0) - network.TriggerPostCreate(appName) + err = network.TriggerPostCreate(appName) case "post-delete": appName := flag.Arg(0) - network.TriggerPostDelete(appName) + err = network.TriggerPostDelete(appName) case "core-post-deploy": appName := flag.Arg(0) - network.TriggerCorePostDeploy(appName) + err = network.TriggerCorePostDeploy(appName) case "report": appName := flag.Arg(0) err = network.ReportSingleApp(appName, "", "") default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/network/subcommands.go b/plugins/network/subcommands.go index 8d5c2e7a083..0e9e7bbd3cd 100644 --- a/plugins/network/subcommands.go +++ b/plugins/network/subcommands.go @@ -79,7 +79,7 @@ func CommandExists(networkName string) error { if exists { common.LogInfo1Quiet("Network exists") } else { - common.LogFail("Network does not exist") + return errors.New("Network does not exist") } return nil diff --git a/plugins/network/triggers.go b/plugins/network/triggers.go index d0c28c02f56..23b39bf0221 100644 --- a/plugins/network/triggers.go +++ b/plugins/network/triggers.go @@ -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 } diff --git a/plugins/proxy/src/subcommands/subcommands.go b/plugins/proxy/src/subcommands/subcommands.go index 25405964a21..d0a8b3960aa 100644 --- a/plugins/proxy/src/subcommands/subcommands.go +++ b/plugins/proxy/src/subcommands/subcommands.go @@ -83,10 +83,10 @@ func main() { proxyType := args.Arg(1) err = proxy.CommandSet(appName, proxyType) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/proxy/src/triggers/triggers.go b/plugins/proxy/src/triggers/triggers.go index 33bd2ea42ec..11c5bd277ab 100644 --- a/plugins/proxy/src/triggers/triggers.go +++ b/plugins/proxy/src/triggers/triggers.go @@ -20,24 +20,24 @@ func main() { switch trigger { case "proxy-is-enabled": appName := flag.Arg(0) - proxy.TriggerProxyIsEnabled(appName) + err = proxy.TriggerProxyIsEnabled(appName) case "proxy-type": appName := flag.Arg(0) - proxy.TriggerProxyType(appName) + err = proxy.TriggerProxyType(appName) case "post-certs-remove": appName := flag.Arg(0) - proxy.TriggerPostCertsRemove(appName) + err = proxy.TriggerPostCertsRemove(appName) case "post-certs-update": appName := flag.Arg(0) - proxy.TriggerPostCertsUpdate(appName) + err = proxy.TriggerPostCertsUpdate(appName) case "report": appName := flag.Arg(0) err = proxy.ReportSingleApp(appName, "", "") default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/ps/src/subcommands/subcommands.go b/plugins/ps/src/subcommands/subcommands.go index df8fb3a2618..b77b0d7baa7 100644 --- a/plugins/ps/src/subcommands/subcommands.go +++ b/plugins/ps/src/subcommands/subcommands.go @@ -86,10 +86,10 @@ func main() { appName := args.Arg(0) err = ps.CommandStop(appName, *allApps, *parallelCount) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/ps/src/triggers/triggers.go b/plugins/ps/src/triggers/triggers.go index 9c61a9d4424..d60afa0c4ed 100644 --- a/plugins/ps/src/triggers/triggers.go +++ b/plugins/ps/src/triggers/triggers.go @@ -75,10 +75,10 @@ func main() { appName := flag.Arg(0) err = ps.ReportSingleApp(appName, "", "") default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/repo/src/subcommands/subcommands.go b/plugins/repo/src/subcommands/subcommands.go index 9e46455d72a..55925129d34 100644 --- a/plugins/repo/src/subcommands/subcommands.go +++ b/plugins/repo/src/subcommands/subcommands.go @@ -29,10 +29,10 @@ func main() { appName := args.Arg(0) err = repo.CommandPurgeCache(appName) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/repo/src/triggers/triggers.go b/plugins/repo/src/triggers/triggers.go index 8e170f1a6f7..0dd1c21d0b4 100644 --- a/plugins/repo/src/triggers/triggers.go +++ b/plugins/repo/src/triggers/triggers.go @@ -25,10 +25,10 @@ func main() { appName := flag.Arg(0) err = repo.PurgeCache(appName) default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/resource/src/subcommands/subcommands.go b/plugins/resource/src/subcommands/subcommands.go index 0d472f81f02..c54697ba653 100644 --- a/plugins/resource/src/subcommands/subcommands.go +++ b/plugins/resource/src/subcommands/subcommands.go @@ -88,10 +88,10 @@ func main() { appName := args.Arg(0) err = resource.CommandReserveClear(appName, *processType) default: - common.LogFail(fmt.Sprintf("Invalid plugin subcommand call: %s", subcommand)) + err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/resource/src/triggers/triggers.go b/plugins/resource/src/triggers/triggers.go index d835c78a9ce..01e6a9cfe77 100644 --- a/plugins/resource/src/triggers/triggers.go +++ b/plugins/resource/src/triggers/triggers.go @@ -45,10 +45,10 @@ func main() { key := flag.Arg(3) err = resource.TriggerResourceGetProperty(appName, processType, resourceType, key) default: - common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger)) + err = fmt.Errorf("Invalid plugin trigger call: %s", trigger) } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } }