2020-07-18 14:02:53 -04:00
|
|
|
package appjson
|
|
|
|
|
|
2021-01-18 18:15:42 -05:00
|
|
|
import (
|
2025-11-18 01:41:52 -05:00
|
|
|
"encoding/json"
|
2025-06-19 02:19:08 -04:00
|
|
|
"errors"
|
2021-02-21 21:00:14 -05:00
|
|
|
"fmt"
|
2022-12-29 01:24:40 -05:00
|
|
|
"strings"
|
2021-01-18 18:15:42 -05:00
|
|
|
|
|
|
|
|
"github.com/dokku/dokku/plugins/common"
|
|
|
|
|
)
|
|
|
|
|
|
2021-10-09 19:41:06 -04:00
|
|
|
// TriggerAppJSONProcessDeployParallelism returns the max number of processes to deploy in parallel
|
|
|
|
|
func TriggerAppJSONProcessDeployParallelism(appName string, processType string) error {
|
2024-01-18 19:05:14 -05:00
|
|
|
appJSON, err := GetAppJSON(appName)
|
2021-10-09 19:41:06 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parallelism := 1
|
|
|
|
|
for procType, formation := range appJSON.Formation {
|
|
|
|
|
if procType != processType {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if formation.MaxParallel == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if *formation.MaxParallel > 0 {
|
|
|
|
|
parallelism = *formation.MaxParallel
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(parallelism)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-05 12:28:11 -04:00
|
|
|
// TriggerAppJSONGetContent outputs the contents of the app-json file, if any
|
|
|
|
|
func TriggerAppJSONGetContent(appName string) error {
|
|
|
|
|
if !hasAppJSON(appName) {
|
|
|
|
|
fmt.Print("{}")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-18 01:41:52 -05:00
|
|
|
appJSON, err := ReadAppJSON(getProcessSpecificAppJSONPath(appName))
|
2023-08-05 12:28:11 -04:00
|
|
|
if err != nil {
|
2025-11-18 01:41:52 -05:00
|
|
|
return err
|
2023-08-05 12:28:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-18 01:41:52 -05:00
|
|
|
content, err := json.Marshal(appJSON)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2023-08-05 12:28:11 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-18 01:41:52 -05:00
|
|
|
fmt.Print(string(content))
|
2023-08-05 12:28:11 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 17:55:45 -04:00
|
|
|
// TriggerCorePostDeploy moves the extracted app.json to the app data directory
|
|
|
|
|
// allowing the app to be restored on boot
|
2022-12-29 01:24:40 -05:00
|
|
|
func TriggerCorePostDeploy(appName string) error {
|
2025-07-23 17:55:45 -04:00
|
|
|
return common.CorePostDeploy(common.CorePostDeployInput{
|
|
|
|
|
AppName: appName,
|
|
|
|
|
Destination: common.GetAppDataDirectory("app-json", appName),
|
|
|
|
|
PluginName: "app-json",
|
|
|
|
|
ExtractedPaths: []common.CorePostDeployPath{
|
|
|
|
|
{Path: "app.json", IsDirectory: false},
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-12-29 01:24:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TriggerCorePostExtract ensures that the main app.json is the one specified by app-json-path
|
|
|
|
|
func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
|
2026-01-07 03:21:04 -05:00
|
|
|
// Clear the env-processed property on new source extraction to allow re-processing
|
|
|
|
|
if err := common.PropertyDelete("app-json", appName, "appjson-env-processed"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 17:55:45 -04:00
|
|
|
destination := common.GetAppDataDirectory("app-json", appName)
|
2022-12-29 01:24:40 -05:00
|
|
|
appJSONPath := strings.Trim(reportComputedAppjsonpath(appName), "/")
|
|
|
|
|
if appJSONPath == "" {
|
|
|
|
|
appJSONPath = "app.json"
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 17:55:45 -04:00
|
|
|
validator := func(appName string, path string) error {
|
|
|
|
|
if !common.FileExists(path) {
|
|
|
|
|
return nil
|
2022-12-29 01:24:40 -05:00
|
|
|
}
|
|
|
|
|
|
2025-06-19 02:19:08 -04:00
|
|
|
result, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
2025-06-19 21:19:24 -04:00
|
|
|
Trigger: "app-json-is-valid",
|
2025-07-23 17:55:45 -04:00
|
|
|
Args: []string{appName, path},
|
2025-06-19 21:19:24 -04:00
|
|
|
StreamStdout: true,
|
|
|
|
|
StreamStderr: true,
|
2025-06-19 02:19:08 -04:00
|
|
|
})
|
2025-07-23 17:55:45 -04:00
|
|
|
|
2025-06-10 23:55:06 -04:00
|
|
|
if err != nil {
|
2025-06-19 02:19:08 -04:00
|
|
|
if result.StderrContents() != "" {
|
|
|
|
|
return errors.New(result.StderrContents())
|
2025-06-10 23:55:06 -04:00
|
|
|
}
|
2025-06-19 02:19:08 -04:00
|
|
|
|
|
|
|
|
return err
|
2025-06-10 23:55:06 -04:00
|
|
|
}
|
2025-07-23 17:55:45 -04:00
|
|
|
return nil
|
2025-06-10 23:55:06 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-23 17:55:45 -04:00
|
|
|
results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
|
|
|
|
Trigger: "builder-get-property",
|
|
|
|
|
Args: []string{appName, "build-dir"},
|
|
|
|
|
})
|
|
|
|
|
buildDir := results.StdoutContents()
|
|
|
|
|
return common.CorePostExtract(common.CorePostExtractInput{
|
|
|
|
|
AppName: appName,
|
|
|
|
|
BuildDir: buildDir,
|
|
|
|
|
Destination: destination,
|
|
|
|
|
PluginName: "app-json",
|
|
|
|
|
SourceWorkDir: sourceWorkDir,
|
|
|
|
|
ToExtract: []common.CorePostExtractToExtract{
|
|
|
|
|
{
|
|
|
|
|
Path: appJSONPath,
|
|
|
|
|
IsDirectory: false,
|
|
|
|
|
Name: "app.json",
|
|
|
|
|
Destination: "app.json",
|
|
|
|
|
Validator: validator,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2022-12-29 01:24:40 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
// TriggerInstall initializes app-json directory structures
|
2021-01-18 18:15:42 -05:00
|
|
|
func TriggerInstall() error {
|
2021-09-04 22:43:10 -04:00
|
|
|
if err := common.PropertySetup("app-json"); err != nil {
|
|
|
|
|
return fmt.Errorf("Unable to install the app-json plugin: %s", err.Error())
|
2021-01-18 18:15:42 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
if err := common.SetupAppData("app-json"); err != nil {
|
2021-01-18 18:15:42 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
return nil
|
2021-01-18 18:15:42 -05:00
|
|
|
}
|
|
|
|
|
|
2021-10-09 23:49:46 -04:00
|
|
|
// TriggerPostAppCloneSetup creates new app-json files
|
|
|
|
|
func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
|
|
|
|
|
err := common.PropertyClone("app-json", oldAppName, newAppName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
return common.CloneAppData("app-json", oldAppName, newAppName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TriggerPostAppRename removes the old app data
|
|
|
|
|
func TriggerPostAppRename(oldAppName string, newAppName string) error {
|
2023-01-28 15:50:31 -05:00
|
|
|
return common.MigrateAppDataDirectory("app-json", oldAppName, newAppName)
|
2021-10-09 23:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TriggerPostAppRenameSetup renames app-json files
|
|
|
|
|
func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
|
|
|
|
|
if err := common.PropertyClone("app-json", oldAppName, newAppName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := common.PropertyDestroy("app-json", oldAppName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
return common.CloneAppData("app-json", oldAppName, newAppName)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 13:10:45 -04:00
|
|
|
// TriggerPostCreate ensures apps have the correct data directory structure
|
2023-01-08 23:24:15 -05:00
|
|
|
func TriggerPostCreate(appName string) error {
|
|
|
|
|
return common.CreateAppDataDirectory("app-json", appName)
|
2021-10-09 23:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
2021-01-18 18:15:42 -05:00
|
|
|
// TriggerPostDelete destroys the app-json data for a given app container
|
|
|
|
|
func TriggerPostDelete(appName string) error {
|
2023-01-28 15:50:31 -05:00
|
|
|
dataErr := common.RemoveAppDataDirectory("app-json", appName)
|
2021-01-18 18:15:42 -05:00
|
|
|
propertyErr := common.PropertyDestroy("app-json", appName)
|
|
|
|
|
|
|
|
|
|
if dataErr != nil {
|
|
|
|
|
return dataErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return propertyErr
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 18:44:00 -04:00
|
|
|
// TriggerPostDeploy is a trigger to execute the postdeploy deployment task
|
2026-08-12 02:14:56 -04:00
|
|
|
//
|
|
|
|
|
// The task is looked up before the image name is resolved because resolving it
|
|
|
|
|
// asserts the image exists on the local docker daemon. That assertion does not
|
|
|
|
|
// hold for schedulers running workloads off this host, which are free to reap
|
|
|
|
|
// the local copy while the app keeps running in a cluster. An app with no
|
|
|
|
|
// postdeploy task needs no image at all, so it should not be made to fail here.
|
2020-07-18 14:02:53 -04:00
|
|
|
func TriggerPostDeploy(appName string, imageTag string) error {
|
2026-08-12 02:14:56 -04:00
|
|
|
command, err := getPhaseScript(appName, "postdeploy")
|
|
|
|
|
if err == nil && command == "" {
|
|
|
|
|
common.LogInfo1("Checking for postdeploy task")
|
|
|
|
|
common.LogVerbose("No postdeploy task found, skipping")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 18:15:42 -05:00
|
|
|
image, err := common.GetDeployingAppImageName(appName, imageTag, "")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 21:00:14 -05:00
|
|
|
return executeScript(appName, image, imageTag, "postdeploy")
|
2020-07-18 14:02:53 -04:00
|
|
|
}
|
|
|
|
|
|
2026-01-07 03:21:04 -05:00
|
|
|
// TriggerPreReleaseBuilder processes app.json env vars and executes the predeploy task
|
2024-04-04 02:34:09 -04:00
|
|
|
func TriggerPreReleaseBuilder(builderType string, appName string, image string) error {
|
2026-01-07 03:21:04 -05:00
|
|
|
// Process app.json env vars before predeploy script
|
|
|
|
|
if err := processAppJSONEnv(appName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 02:34:09 -04:00
|
|
|
parts := strings.Split(image, ":")
|
|
|
|
|
imageTag := parts[len(parts)-1]
|
|
|
|
|
return executeScript(appName, image, imageTag, "predeploy")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-04 02:27:43 -04:00
|
|
|
// TriggerPostReleaseBuilder is a trigger to execute predeploy and release deployment tasks
|
|
|
|
|
func TriggerPostReleaseBuilder(builderType string, appName string, image string) error {
|
|
|
|
|
parts := strings.Split(image, ":")
|
|
|
|
|
imageTag := parts[len(parts)-1]
|
2021-01-18 18:15:42 -05:00
|
|
|
if err := executeScript(appName, image, imageTag, "release"); err != nil {
|
2020-08-30 13:37:30 -04:00
|
|
|
return err
|
|
|
|
|
}
|
2021-02-21 21:00:14 -05:00
|
|
|
|
2024-11-04 14:29:34 -05:00
|
|
|
if err := setScale(appName); err != nil {
|
2021-03-22 00:24:56 -04:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 21:00:14 -05:00
|
|
|
if common.PropertyGet("common", appName, "deployed") == "true" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure that a failed postdeploy does not trigger twice
|
|
|
|
|
if common.PropertyGet("app-json", appName, "heroku.postdeploy") == "executed" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := common.PropertyWrite("app-json", appName, "heroku.postdeploy", "executed"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return executeScript(appName, image, imageTag, "heroku.postdeploy")
|
2020-07-18 14:02:53 -04:00
|
|
|
}
|