Merge pull request #7328 from dokku/k3s-command-once

Execute one-off commands with DOKKU_APP_SHELL as the initial command under the k3s scheduler
This commit is contained in:
Jose Diaz-Gonzalez authored and GitHub committed 2024-11-04 14:36:01 -05:00
commit 62125c5b72
4 files changed
+46 -80

No files matched your search

+6 -42
View File
@@ -1,7 +1,6 @@
package appjson package appjson
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -12,7 +11,6 @@ import (
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
shellquote "github.com/kballard/go-shellquote" shellquote "github.com/kballard/go-shellquote"
"golang.org/x/sync/errgroup"
) )
func constructScript(command string, shell string, isHerokuishImage bool, isCnbImage bool, dockerfileEntrypoint string) []string { func constructScript(command string, shell string, isHerokuishImage bool, isCnbImage bool, dockerfileEntrypoint string) []string {
@@ -99,7 +97,7 @@ func getPhaseScript(appName string, phase string) (string, error) {
} }
// getReleaseCommand extracts the release command from a given app's procfile // getReleaseCommand extracts the release command from a given app's procfile
func getReleaseCommand(appName string, image string) string { func getReleaseCommand(appName string) string {
processType := "release" processType := "release"
port := "5000" port := "5000"
results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{ results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
@@ -109,40 +107,6 @@ func getReleaseCommand(appName string, image string) string {
return results.StdoutContents() return results.StdoutContents()
} }
func getDokkuAppShell(appName string) string {
shell := "/bin/bash"
globalShell := ""
appShell := ""
ctx := context.Background()
errs, ctx := errgroup.WithContext(ctx)
errs.Go(func() error {
results, _ := common.CallPlugnTriggerWithContext(ctx, common.PlugnTriggerInput{
Trigger: "config-get-global",
Args: []string{"DOKKU_APP_SHELL"},
})
globalShell = results.StdoutContents()
return nil
})
errs.Go(func() error {
results, _ := common.CallPlugnTriggerWithContext(ctx, common.PlugnTriggerInput{
Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_SHELL"},
})
appShell = results.StdoutContents()
return nil
})
errs.Wait()
if appShell != "" {
shell = appShell
} else if globalShell != "" {
shell = globalShell
}
return shell
}
func hasAppJSON(appName string) bool { func hasAppJSON(appName string) bool {
appJSONPath := getAppJSONPath(appName) appJSONPath := getAppJSONPath(appName)
if common.FileExists(fmt.Sprintf("%s.%s.missing", appJSONPath, os.Getenv("DOKKU_PID"))) { if common.FileExists(fmt.Sprintf("%s.%s.missing", appJSONPath, os.Getenv("DOKKU_PID"))) {
@@ -156,7 +120,7 @@ func hasAppJSON(appName string) bool {
return common.FileExists(appJSONPath) return common.FileExists(appJSONPath)
} }
func cleanupDeploymentContainer(appName string, containerID string, phase string) error { func cleanupDeploymentContainer(containerID string, phase string) error {
if phase != "predeploy" { if phase != "predeploy" {
os.Setenv("DOKKU_SKIP_IMAGE_RETIRE", "true") os.Setenv("DOKKU_SKIP_IMAGE_RETIRE", "true")
} }
@@ -177,7 +141,7 @@ func executeScript(appName string, image string, imageTag string, phase string)
command := "" command := ""
phaseSource := "" phaseSource := ""
if phase == "release" { if phase == "release" {
command = getReleaseCommand(appName, image) command = getReleaseCommand(appName)
phaseSource = "Procfile" phaseSource = "Procfile"
} else { } else {
var err error var err error
@@ -207,7 +171,7 @@ func executeScript(appName string, image string, imageTag string, phase string)
dockerfileCommand, _ = getCommandFromImage(image) dockerfileCommand, _ = getCommandFromImage(image)
} }
dokkuAppShell := getDokkuAppShell(appName) dokkuAppShell := common.GetDokkuAppShell(appName)
script := constructScript(command, dokkuAppShell, isHerokuishImage, isCnbImage, dockerfileEntrypoint) script := constructScript(command, dokkuAppShell, isHerokuishImage, isCnbImage, dockerfileEntrypoint)
imageSourceType := "dockerfile" imageSourceType := "dockerfile"
@@ -304,7 +268,7 @@ func executeScript(appName string, image string, imageTag string, phase string)
return fmt.Errorf("Failed to create %s execution container: %s", phase, err.Error()) return fmt.Errorf("Failed to create %s execution container: %s", phase, err.Error())
} }
defer cleanupDeploymentContainer(appName, containerID, phase) defer cleanupDeploymentContainer(containerID, phase)
if !waitForExecution(containerID) { if !waitForExecution(containerID) {
common.LogInfo2Quiet(fmt.Sprintf("Start of %s %s task (%s) output", appName, phaseName, containerID[0:9])) common.LogInfo2Quiet(fmt.Sprintf("Start of %s %s task (%s) output", appName, phaseName, containerID[0:9]))
@@ -455,7 +419,7 @@ func createdContainerID(appName string, dockerArgs []string, image string, comma
return containerID, err return containerID, err
} }
func setScale(appName string, image string) error { func setScale(appName string) error {
appJSON, err := GetAppJSON(appName) appJSON, err := GetAppJSON(appName)
if err != nil { if err != nil {
return err return err
+1 -1
View File
@@ -226,7 +226,7 @@ func TriggerPostReleaseBuilder(builderType string, appName string, image string)
return err return err
} }
if err := setScale(appName, image); err != nil { if err := setScale(appName); err != nil {
return err return err
} }
+35
View File
@@ -285,6 +285,41 @@ func GetRunningImageTag(appName string, imageTag string) (string, error) {
return imageTag, nil return imageTag, nil
} }
// GetDokkuAppShell returns the shell for a given app
func GetDokkuAppShell(appName string) string {
shell := "/bin/bash"
globalShell := ""
appShell := ""
ctx := context.Background()
errs, ctx := errgroup.WithContext(ctx)
errs.Go(func() error {
results, _ := CallPlugnTriggerWithContext(ctx, PlugnTriggerInput{
Trigger: "config-get-global",
Args: []string{"DOKKU_APP_SHELL"},
})
globalShell = results.StdoutContents()
return nil
})
errs.Go(func() error {
results, _ := CallPlugnTriggerWithContext(ctx, PlugnTriggerInput{
Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_SHELL"},
})
appShell = results.StdoutContents()
return nil
})
errs.Wait()
if appShell != "" {
shell = appShell
} else if globalShell != "" {
shell = globalShell
}
return shell
}
// DokkuApps returns a list of all local apps // DokkuApps returns a list of all local apps
func DokkuApps() ([]string, error) { func DokkuApps() ([]string, error) {
apps, err := UnfilteredDokkuApps() apps, err := UnfilteredDokkuApps()
+4 -37
View File
@@ -788,24 +788,7 @@ func TriggerSchedulerEnter(scheduler string, appName string, processType string,
command := args command := args
if len(args) == 0 { if len(args) == 0 {
command = []string{"/bin/bash"} command = []string{common.GetDokkuAppShell(appName)}
results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "config-get-global",
Args: []string{"DOKKU_APP_SHELL"},
})
globalShell := results.StdoutContents()
if err == nil && globalShell != "" {
command = []string{globalShell}
}
results, err = common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_SHELL"},
})
appShell := results.StdoutContents()
if err == nil && appShell != "" {
command = []string{appShell}
}
} }
entrypoint := "" entrypoint := ""
@@ -1071,25 +1054,9 @@ func TriggerSchedulerRun(scheduler string, appName string, envCount int, args []
// todo: do something with docker args // todo: do something with docker args
command := args command := args
commandShell := common.GetDokkuAppShell(appName)
if len(args) == 0 { if len(args) == 0 {
command = []string{"/bin/bash"} command = []string{commandShell}
results, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "config-get-global",
Args: []string{"DOKKU_APP_SHELL"},
})
globalShell := results.StdoutContents()
if err == nil && globalShell != "" {
command = []string{globalShell}
}
results, err = common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "config-get",
Args: []string{appName, "DOKKU_APP_SHELL"},
})
appShell := results.StdoutContents()
if err == nil && appShell != "" {
command = []string{appShell}
}
} else if len(args) == 1 { } else if len(args) == 1 {
resp, err := common.CallPlugnTrigger(common.PlugnTriggerInput{ resp, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "procfile-get-command", Trigger: "procfile-get-command",
@@ -1135,7 +1102,7 @@ func TriggerSchedulerRun(scheduler string, appName string, envCount int, args []
workingDir := common.GetWorkingDir(appName, image) workingDir := common.GetWorkingDir(appName, image)
job, err := templateKubernetesJob(Job{ job, err := templateKubernetesJob(Job{
AppName: appName, AppName: appName,
Command: command, Command: []string{commandShell},
DeploymentID: deploymentID, DeploymentID: deploymentID,
Entrypoint: entrypoint, Entrypoint: entrypoint,
Env: extraEnv, Env: extraEnv,