refactor: use CallPlugnTrigger instead of PlugnTriggerOutput

Refs #6422
This commit is contained in:
Jose Diaz-Gonzalez
2024-03-14 01:18:28 -04:00
parent a9933c7f02
commit cdff4ae867
15 changed files with 149 additions and 58 deletions

View File

@@ -102,8 +102,11 @@ func getPhaseScript(appName string, phase string) (string, error) {
func getReleaseCommand(appName string, image string) string { func getReleaseCommand(appName string, image string) string {
processType := "release" processType := "release"
port := "5000" port := "5000"
b, _ := common.PlugnTriggerOutput("procfile-get-command", []string{appName, processType, port}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
return strings.TrimSpace(string(b[:])) Trigger: "procfile-get-command",
Args: []string{appName, processType, port},
})
return results.StdoutContents()
} }
func getDokkuAppShell(appName string) string { func getDokkuAppShell(appName string) string {
@@ -114,13 +117,19 @@ func getDokkuAppShell(appName string) string {
ctx := context.Background() ctx := context.Background()
errs, ctx := errgroup.WithContext(ctx) errs, ctx := errgroup.WithContext(ctx)
errs.Go(func() error { errs.Go(func() error {
b, _ := common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_APP_SHELL"}...) results, _ := common.CallPlugnTriggerWithContext(ctx, common.PlugnTriggerInput{
globalShell = strings.TrimSpace(string(b[:])) Trigger: "config-get-global",
Args: []string{"DOKKU_APP_SHELL"},
})
globalShell = results.StdoutContents()
return nil return nil
}) })
errs.Go(func() error { errs.Go(func() error {
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_APP_SHELL"}...) results, _ := common.CallPlugnTriggerWithContext(ctx, common.PlugnTriggerInput{
appShell = strings.TrimSpace(string(b[:])) Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_SHELL"},
})
appShell = results.StdoutContents()
return nil return nil
}) })
@@ -398,12 +407,15 @@ func createdContainerID(appName string, dockerArgs []string, image string, comma
arguments = append(arguments, image) arguments = append(arguments, image)
arguments = append(arguments, command...) arguments = append(arguments, command...)
b, err := common.PlugnTriggerOutput("config-export", []string{appName, "false", "true", "json"}...) results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "config-export",
Args: []string{appName, "false", "true", "json"},
})
if err != nil { if err != nil {
return "", err return "", err
} }
var env map[string]string var env map[string]string
if err := json.Unmarshal(b, &env); err != nil { if err := json.Unmarshal(results.StdoutBytes(), &env); err != nil {
return "", err return "", err
} }

View File

@@ -101,11 +101,17 @@ func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
} }
processSpecificAppJSON := fmt.Sprintf("%s.%s", existingAppJSON, os.Getenv("DOKKU_PID")) processSpecificAppJSON := fmt.Sprintf("%s.%s", existingAppJSON, os.Getenv("DOKKU_PID"))
b, _ := common.PlugnTriggerOutput("git-get-property", []string{appName, "source-image"}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
appSourceImage := strings.TrimSpace(string(b[:])) Trigger: "git-get-property",
Args: []string{appName, "source-image"},
})
appSourceImage := results.StdoutContents()
b, _ = common.PlugnTriggerOutput("builder-get-property", []string{appName, "build-dir"}...) results, _ = common.CallPlugnTrigger(common.PlugnTriggerInput{
buildDir := strings.TrimSpace(string(b[:])) Trigger: "builder-get-property",
Args: []string{appName, "build-dir"},
})
buildDir := results.StdoutContents()
repoDefaultAppJSONPath := path.Join(sourceWorkDir, "app.json") repoDefaultAppJSONPath := path.Join(sourceWorkDir, "app.json")
if appSourceImage == "" { if appSourceImage == "" {

View File

@@ -4,7 +4,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"strings"
"time" "time"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
@@ -105,8 +104,11 @@ func maybeCreateApp(appName string) error {
return nil return nil
} }
b, _ := common.PlugnTriggerOutput("config-get-global", []string{"DOKKU_DISABLE_APP_AUTOCREATION"}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
disableAutocreate := strings.TrimSpace(string(b[:])) Trigger: "config-get-global",
Args: []string{"DOKKU_DISABLE_APP_AUTOCREATION"},
})
disableAutocreate := results.StdoutContents()
if disableAutocreate == "true" { if disableAutocreate == "true" {
common.LogWarn("App auto-creation disabled.") common.LogWarn("App auto-creation disabled.")
return fmt.Errorf("Re-enable app auto-creation or create an app with 'dokku apps:create %s'", appName) return fmt.Errorf("Re-enable app auto-creation or create an app with 'dokku apps:create %s'", appName)

View File

@@ -40,8 +40,11 @@ func reportComputedStack(appName string) string {
return stack return stack
} }
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_IMAGE"}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
if dokkuImage := strings.TrimSpace(string(b[:])); dokkuImage != "" { Trigger: "config-get",
Args: []string{appName, "DOKKU_IMAGE"},
})
if dokkuImage := results.StdoutContents(); dokkuImage != "" {
common.LogWarn("Deprecated: use buildpacks:set-property instead of specifying DOKKU_IMAGE environment variable") common.LogWarn("Deprecated: use buildpacks:set-property instead of specifying DOKKU_IMAGE environment variable")
return dokkuImage return dokkuImage
} }

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
@@ -22,8 +21,11 @@ func TriggerBuildpackStackName(appName string) error {
return nil return nil
} }
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_IMAGE"}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
dokkuImage := strings.TrimSpace(string(b[:])) Trigger: "config-get",
Args: []string{appName, "DOKKU_IMAGE"},
})
dokkuImage := results.StdoutContents()
if dokkuImage != "" { if dokkuImage != "" {
common.LogWarn("Deprecated: use buildpacks:set-property instead of specifying DOKKU_IMAGE environment variable") common.LogWarn("Deprecated: use buildpacks:set-property instead of specifying DOKKU_IMAGE environment variable")
fmt.Println(dokkuImage) fmt.Println(dokkuImage)

View File

@@ -124,8 +124,11 @@ func GetAppScheduler(appName string) string {
} }
func getAppScheduler(appName string) string { func getAppScheduler(appName string) string {
b, _ := PlugnTriggerOutput("scheduler-detect", []string{appName}...) results, _ := CallPlugnTrigger(PlugnTriggerInput{
value := strings.TrimSpace(string(b[:])) Trigger: "scheduler-detect",
Args: []string{appName},
})
value := results.StdoutContents()
if value != "" { if value != "" {
return value return value
} }
@@ -134,8 +137,11 @@ func getAppScheduler(appName string) string {
// GetGlobalScheduler fetchs the global scheduler // GetGlobalScheduler fetchs the global scheduler
func GetGlobalScheduler() string { func GetGlobalScheduler() string {
b, _ := PlugnTriggerOutput("scheduler-detect", []string{"--global"}...) results, _ := CallPlugnTrigger(PlugnTriggerInput{
value := strings.TrimSpace(string(b[:])) Trigger: "scheduler-detect",
Args: []string{"--global"},
})
value := results.StdoutContents()
if value != "" { if value != "" {
return value return value
} }
@@ -152,24 +158,34 @@ func GetDeployingAppImageName(appName, imageTag, imageRepo string) (string, erro
ctx := context.Background() ctx := context.Background()
errs, ctx := errgroup.WithContext(ctx) errs, ctx := errgroup.WithContext(ctx)
errs.Go(func() error { errs.Go(func() error {
b, err := PlugnTriggerOutput("deployed-app-repository", []string{appName}...) results, err := CallPlugnTrigger(PlugnTriggerInput{
Trigger: "deployed-app-repository",
Args: []string{appName},
})
if err == nil { if err == nil {
imageRemoteRepository = strings.TrimSpace(string(b[:])) imageRemoteRepository = results.StdoutContents()
} }
return err return err
}) })
errs.Go(func() error { errs.Go(func() error {
b, err := PlugnTriggerOutput("deployed-app-image-tag", []string{appName}...) results, err := CallPlugnTrigger(PlugnTriggerInput{
Trigger: "deployed-app-image-tag",
Args: []string{appName},
})
if err == nil { if err == nil {
newImageTag = strings.TrimSpace(string(b[:])) newImageTag = results.StdoutContents()
} }
return err return err
}) })
errs.Go(func() error { errs.Go(func() error {
b, err := PlugnTriggerOutput("deployed-app-image-repo", []string{appName}...) results, err := CallPlugnTrigger(PlugnTriggerInput{
Trigger: "deployed-app-image-repo",
Args: []string{appName},
})
if err == nil { if err == nil {
newImageRepo = strings.TrimSpace(string(b[:])) newImageRepo = results.StdoutContents()
} }
return err return err
}) })
@@ -251,11 +267,14 @@ func GetAppRunningContainerIDs(appName string, containerType string) ([]string,
// GetRunningImageTag retrieves current deployed image tag for a given app // GetRunningImageTag retrieves current deployed image tag for a given app
func GetRunningImageTag(appName string, imageTag string) (string, error) { func GetRunningImageTag(appName string, imageTag string) (string, error) {
b, err := PlugnTriggerOutput("deployed-app-image-tag", []string{appName}...) results, err := CallPlugnTrigger(PlugnTriggerInput{
Trigger: "deployed-app-image-tag",
Args: []string{appName},
})
if err != nil { if err != nil {
return imageTag, err return imageTag, err
} }
newImageTag := strings.TrimSpace(string(b[:])) newImageTag := results.StdoutContents()
if newImageTag != "" { if newImageTag != "" {
imageTag = newImageTag imageTag = newImageTag
} }
@@ -326,7 +345,10 @@ func IsDeployed(appName string) bool {
if deployed == "" { if deployed == "" {
deployed = "false" deployed = "false"
scheduler := GetAppScheduler(appName) scheduler := GetAppScheduler(appName)
_, err := PlugnTriggerOutput("scheduler-is-deployed", []string{scheduler, appName}...) _, err := CallPlugnTrigger(PlugnTriggerInput{
Trigger: "scheduler-is-deployed",
Args: []string{scheduler, appName},
})
if err == nil { if err == nil {
deployed = "true" deployed = "true"
} }

View File

@@ -182,9 +182,11 @@ func DockerCleanup(appName string, forceCleanup bool) error {
triggerArgs = []string{"DOKKU_SKIP_CLEANUP"} triggerArgs = []string{"DOKKU_SKIP_CLEANUP"}
} }
b, _ := PlugnTriggerOutput(triggerName, triggerArgs...) results, _ := CallPlugnTrigger(PlugnTriggerInput{
output := strings.TrimSpace(string(b[:])) Trigger: triggerName,
if output == "true" { Args: triggerArgs,
})
if results.StdoutContents() == "true" {
skipCleanup = true skipCleanup = true
} }
} }
@@ -309,9 +311,12 @@ func IsImageHerokuishBased(image string, appName string) bool {
dokkuAppUser := "" dokkuAppUser := ""
if len(appName) != 0 { if len(appName) != 0 {
b, err := PlugnTriggerOutput("config-get", []string{appName, "DOKKU_APP_USER"}...) results, err := CallPlugnTrigger(PlugnTriggerInput{
Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_USER"},
})
if err == nil { if err == nil {
dokkuAppUser = strings.TrimSpace(string(b)) dokkuAppUser = results.StdoutContents()
} }
} }

View File

@@ -77,6 +77,16 @@ func (ecr ExecCommandResponse) StderrContents() string {
return strings.TrimSpace(ecr.Stderr) return strings.TrimSpace(ecr.Stderr)
} }
// StderrBytes returns the trimmed stderr of the command as bytes
func (ecr ExecCommandResponse) StderrBytes() []byte {
return []byte(ecr.StderrContents())
}
// StdoutBytes returns the trimmed stdout of the command as bytes
func (ecr ExecCommandResponse) StdoutBytes() []byte {
return []byte(ecr.StdoutContents())
}
// CallExecCommand executes a command on the local host // CallExecCommand executes a command on the local host
func CallExecCommand(input ExecCommandInput) (ExecCommandResponse, error) { func CallExecCommand(input ExecCommandInput) (ExecCommandResponse, error) {
ctx := context.Background() ctx := context.Background()

View File

@@ -25,8 +25,11 @@ func filterApps(apps []string) ([]string, error) {
} }
args := append([]string{sshUser, sshName}, apps...) args := append([]string{sshUser, sshName}, apps...)
b, _ := PlugnTriggerOutput("user-auth-app", args...) results, _ := CallPlugnTrigger(PlugnTriggerInput{
filteredApps := strings.Split(strings.TrimSpace(string(b[:])), "\n") Trigger: "user-auth-app",
Args: args,
})
filteredApps := strings.Split(results.StdoutContents(), "\n")
filteredApps = removeEmptyEntries(filteredApps) filteredApps = removeEmptyEntries(filteredApps)
if len(filteredApps) == 0 { if len(filteredApps) == 0 {

View File

@@ -43,12 +43,15 @@ func BuildConfig(appName string) error {
} }
appRoot := common.AppRoot(appName) appRoot := common.AppRoot(appName)
s, err := common.PlugnTriggerOutput("ps-current-scale", []string{appName}...) results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "ps-current-scale",
Args: []string{appName},
})
if err != nil { if err != nil {
return err return err
} }
scale, err := common.ParseScaleOutput(s) scale, err := common.ParseScaleOutput(results.StdoutBytes())
if err != nil { if err != nil {
return err return err
} }
@@ -78,7 +81,10 @@ func BuildConfig(appName string) error {
ipAddress := GetContainerIpaddress(appName, processType, containerID) ipAddress := GetContainerIpaddress(appName, processType, containerID)
if ipAddress != "" { if ipAddress != "" {
args := []string{appName, processType, containerIndexString, ipAddress} args := []string{appName, processType, containerIndexString, ipAddress}
_, err := common.PlugnTriggerOutput("network-write-ipaddr", args...) _, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "network-write-ipaddr",
Args: args,
})
if err != nil { if err != nil {
common.LogWarn(err.Error()) common.LogWarn(err.Error())
} }

View File

@@ -69,15 +69,21 @@ func getRestartPolicy(appName string) (string, error) {
func getProcessCount(appName string) (int, error) { func getProcessCount(appName string) (int, error) {
scheduler := common.GetAppScheduler(appName) scheduler := common.GetAppScheduler(appName)
b, _ := common.PlugnTriggerOutput("scheduler-app-status", []string{scheduler, appName}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
count := strings.Split(strings.TrimSpace(string(b[:])), " ")[0] Trigger: "scheduler-app-status",
Args: []string{scheduler, appName},
})
count := strings.Split(results.StdoutContents(), " ")[0]
return strconv.Atoi(count) return strconv.Atoi(count)
} }
func getRunningState(appName string) string { func getRunningState(appName string) string {
scheduler := common.GetAppScheduler(appName) scheduler := common.GetAppScheduler(appName)
b, _ := common.PlugnTriggerOutput("scheduler-app-status", []string{scheduler, appName}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
return strings.Split(strings.TrimSpace(string(b[:])), " ")[1] Trigger: "scheduler-app-status",
Args: []string{scheduler, appName},
})
return strings.Split(results.StdoutContents(), " ")[1]
} }
func hasProcfile(appName string) bool { func hasProcfile(appName string) bool {

View File

@@ -2,7 +2,6 @@ package ps
import ( import (
"fmt" "fmt"
"strings"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
) )
@@ -129,8 +128,11 @@ func Restore(appName string) error {
return nil return nil
} }
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_APP_RESTORE"}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
restore := strings.TrimSpace(string(b[:])) Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_RESTORE"},
})
restore := results.StdoutContents()
if restore == "0" { if restore == "0" {
common.LogWarn(fmt.Sprintf("Skipping ps:restore for %s as DOKKU_APP_RESTORE=%s", appName, restore)) common.LogWarn(fmt.Sprintf("Skipping ps:restore for %s as DOKKU_APP_RESTORE=%s", appName, restore))
return nil return nil

View File

@@ -129,8 +129,11 @@ func reportRestartPolicy(appName string) string {
} }
func reportRestore(appName string) string { func reportRestore(appName string) string {
b, _ := common.PlugnTriggerOutput("config-get", []string{appName, "DOKKU_APP_RESTORE"}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
restore := strings.TrimSpace(string(b[:])) Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_RESTORE"},
})
restore := results.StdoutContents()
if restore == "0" { if restore == "0" {
restore = "false" restore = "false"
} else { } else {

View File

@@ -72,8 +72,11 @@ func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
} }
processSpecificProcfile := fmt.Sprintf("%s.%s", existingProcfile, os.Getenv("DOKKU_PID")) processSpecificProcfile := fmt.Sprintf("%s.%s", existingProcfile, os.Getenv("DOKKU_PID"))
b, _ := common.PlugnTriggerOutput("git-get-property", []string{appName, "source-image"}...) results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
appSourceImage := strings.TrimSpace(string(b[:])) Trigger: "git-get-property",
Args: []string{appName, "source-image"},
})
appSourceImage := results.StdoutContents()
repoDefaultProcfilePath := path.Join(sourceWorkDir, "Procfile") repoDefaultProcfilePath := path.Join(sourceWorkDir, "Procfile")
if appSourceImage == "" { if appSourceImage == "" {

View File

@@ -80,12 +80,15 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
if scheduler != "k3s" { if scheduler != "k3s" {
return nil return nil
} }
s, err := common.PlugnTriggerOutput("ps-current-scale", []string{appName}...) results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "ps-current-scale",
Args: []string{appName},
})
if err != nil { if err != nil {
return err return err
} }
processes, err := common.ParseScaleOutput(s) processes, err := common.ParseScaleOutput(results.StdoutBytes())
if err != nil { if err != nil {
return err return err
} }
@@ -226,12 +229,15 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
if _, ok := processes["web"]; ok { if _, ok := processes["web"]; ok {
err = common.PlugnTrigger("domains-vhost-enabled", []string{appName}...) err = common.PlugnTrigger("domains-vhost-enabled", []string{appName}...)
if err == nil { if err == nil {
b, err := common.PlugnTriggerOutput("domains-list", []string{appName}...) results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "domains-list",
Args: []string{appName},
})
if err != nil { if err != nil {
return fmt.Errorf("Error getting domains for deployment: %w", err) return fmt.Errorf("Error getting domains for deployment: %w", err)
} }
for _, domain := range strings.Split(string(b), "\n") { for _, domain := range strings.Split(results.StdoutContents(), "\n") {
domain = strings.TrimSpace(domain) domain = strings.TrimSpace(domain)
if domain != "" { if domain != "" {
domains = append(domains, domain) domains = append(domains, domain)