2020-11-01 15:53:53 -05:00
|
|
|
package ps
|
|
|
|
|
|
|
|
|
|
import (
|
2023-05-28 23:22:17 -04:00
|
|
|
"errors"
|
2020-11-01 15:53:53 -05:00
|
|
|
"fmt"
|
|
|
|
|
"os"
|
2021-07-10 01:32:57 -04:00
|
|
|
"path"
|
2020-11-01 15:53:53 -05:00
|
|
|
"path/filepath"
|
2021-08-01 16:27:13 -04:00
|
|
|
"strconv"
|
2020-11-01 15:53:53 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/dokku/dokku/plugins/common"
|
|
|
|
|
"github.com/dokku/dokku/plugins/config"
|
|
|
|
|
dockeroptions "github.com/dokku/dokku/plugins/docker-options"
|
|
|
|
|
)
|
|
|
|
|
|
2020-11-18 13:45:08 -05:00
|
|
|
// TriggerAppRestart restarts an app
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerAppRestart(appName string) error {
|
|
|
|
|
return Restart(appName)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 17:12:51 -05:00
|
|
|
// TriggerCorePostDeploy sets a property to
|
|
|
|
|
// allow the app to be restored on boot
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerCorePostDeploy(appName string) error {
|
2022-11-25 00:41:55 -05:00
|
|
|
existingProcfile := getProcfilePath(appName)
|
|
|
|
|
processSpecificProcfile := fmt.Sprintf("%s.%s", existingProcfile, os.Getenv("DOKKU_PID"))
|
|
|
|
|
if common.FileExists(processSpecificProcfile) {
|
|
|
|
|
if err := os.Rename(processSpecificProcfile, existingProcfile); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-11-25 13:42:50 -05:00
|
|
|
} else if common.FileExists(fmt.Sprintf("%s.missing", processSpecificProcfile)) {
|
|
|
|
|
if err := os.Remove(fmt.Sprintf("%s.missing", processSpecificProcfile)); err != nil {
|
2022-11-25 00:41:55 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-25 14:35:14 -05:00
|
|
|
if common.FileExists(existingProcfile) {
|
|
|
|
|
if err := os.Remove(existingProcfile); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-11-25 00:41:55 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-27 15:02:45 -05:00
|
|
|
if err := common.PropertyDelete("ps", appName, "scale.old"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 15:53:53 -05:00
|
|
|
entries := map[string]string{
|
|
|
|
|
"DOKKU_APP_RESTORE": "1",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return common.SuppressOutput(func() error {
|
|
|
|
|
return config.SetMany(appName, entries, false)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-10 01:32:57 -04:00
|
|
|
// TriggerCorePostExtract ensures that the main Procfile is the one specified by procfile-path
|
|
|
|
|
func TriggerCorePostExtract(appName string, sourceWorkDir string) error {
|
|
|
|
|
procfilePath := strings.Trim(reportComputedProcfilePath(appName), "/")
|
2022-11-25 00:41:55 -05:00
|
|
|
if procfilePath == "" {
|
|
|
|
|
procfilePath = "Procfile"
|
2021-07-10 01:32:57 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-25 00:41:55 -05:00
|
|
|
existingProcfile := getProcfilePath(appName)
|
|
|
|
|
files, err := filepath.Glob(fmt.Sprintf("%s.*", existingProcfile))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, f := range files {
|
|
|
|
|
if err := os.Remove(f); err != nil {
|
|
|
|
|
return err
|
2021-10-16 16:34:34 -04:00
|
|
|
}
|
2021-07-10 01:32:57 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-25 00:41:55 -05:00
|
|
|
processSpecificProcfile := fmt.Sprintf("%s.%s", existingProcfile, os.Getenv("DOKKU_PID"))
|
2024-03-14 01:18:28 -04:00
|
|
|
results, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
|
|
|
|
Trigger: "git-get-property",
|
|
|
|
|
Args: []string{appName, "source-image"},
|
|
|
|
|
})
|
|
|
|
|
appSourceImage := results.StdoutContents()
|
2022-12-18 17:57:24 -05:00
|
|
|
|
2022-12-28 15:36:34 -05:00
|
|
|
repoDefaultProcfilePath := path.Join(sourceWorkDir, "Procfile")
|
2022-12-18 17:57:24 -05:00
|
|
|
if appSourceImage == "" {
|
2022-11-25 17:38:15 -05:00
|
|
|
repoProcfilePath := path.Join(sourceWorkDir, procfilePath)
|
|
|
|
|
if !common.FileExists(repoProcfilePath) {
|
2022-12-28 15:34:53 -05:00
|
|
|
if procfilePath != "Procfile" && common.FileExists(repoDefaultProcfilePath) {
|
|
|
|
|
if err := os.Remove(repoDefaultProcfilePath); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-25 17:38:15 -05:00
|
|
|
return common.TouchFile(fmt.Sprintf("%s.missing", processSpecificProcfile))
|
|
|
|
|
}
|
2021-07-10 01:32:57 -04:00
|
|
|
|
2024-01-24 22:43:43 -05:00
|
|
|
if err := common.Copy(repoProcfilePath, processSpecificProcfile); err != nil {
|
2022-11-25 17:38:15 -05:00
|
|
|
return fmt.Errorf("Unable to extract Procfile: %v", err.Error())
|
|
|
|
|
}
|
2022-12-28 15:36:34 -05:00
|
|
|
|
|
|
|
|
if procfilePath != "Procfile" {
|
2024-01-24 22:43:43 -05:00
|
|
|
if err := common.Copy(repoProcfilePath, repoDefaultProcfilePath); err != nil {
|
2022-12-28 15:36:34 -05:00
|
|
|
return fmt.Errorf("Unable to move Procfile into place: %v", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-25 17:38:15 -05:00
|
|
|
} else {
|
2022-12-18 17:57:24 -05:00
|
|
|
if err := common.CopyFromImage(appName, appSourceImage, procfilePath, processSpecificProcfile); err != nil {
|
2022-11-25 17:38:15 -05:00
|
|
|
return common.TouchFile(fmt.Sprintf("%s.missing", processSpecificProcfile))
|
|
|
|
|
}
|
2021-07-10 01:32:57 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-12 19:28:23 -05:00
|
|
|
result, err := common.CallExecCommand(common.ExecCommandInput{
|
2024-02-12 20:28:31 -05:00
|
|
|
Command: "procfile-util",
|
|
|
|
|
Args: []string{"check", "-P", processSpecificProcfile},
|
2024-02-12 19:28:23 -05:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf(result.StderrContents())
|
|
|
|
|
}
|
|
|
|
|
if result.ExitCode != 0 {
|
|
|
|
|
return fmt.Errorf("Invalid Procfile: %s", result.StderrContents())
|
2022-11-25 00:41:55 -05:00
|
|
|
}
|
2021-07-10 01:32:57 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:45:08 -05:00
|
|
|
// TriggerInstall initializes app restart policies
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerInstall() error {
|
2020-11-28 00:39:28 -05:00
|
|
|
if err := common.PropertySetup("ps"); err != nil {
|
|
|
|
|
return fmt.Errorf("Unable to install the ps plugin: %s", err.Error())
|
2020-11-21 02:32:28 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
if err := common.SetupAppData("ps"); err != nil {
|
2020-11-01 15:53:53 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-15 15:47:13 -04:00
|
|
|
apps, err := common.UnfilteredDokkuApps()
|
2020-11-01 15:53:53 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
for _, appName := range apps {
|
|
|
|
|
if err := common.CreateAppDataDirectory("ps", appName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 15:53:53 -05:00
|
|
|
for _, appName := range apps {
|
|
|
|
|
policies, err := getRestartPolicy(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(policies) != 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := dockeroptions.AddDockerOptionToPhases(appName, []string{"deploy"}, "--restart=on-failure:10"); err != nil {
|
|
|
|
|
common.LogWarn(err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 17:47:04 -04:00
|
|
|
for _, appName := range apps {
|
|
|
|
|
dokkuScaleFile := filepath.Join(common.AppRoot(appName), "DOKKU_SCALE")
|
|
|
|
|
if common.FileExists(dokkuScaleFile) {
|
|
|
|
|
os.Remove(dokkuScaleFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dokkuScaleExtracted := filepath.Join(common.AppRoot(appName), "DOKKU_SCALE.extracted")
|
|
|
|
|
if common.FileExists(dokkuScaleExtracted) {
|
2021-08-01 22:34:42 -04:00
|
|
|
os.Remove(dokkuScaleExtracted)
|
2021-08-01 17:47:04 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 15:53:53 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:45:08 -05:00
|
|
|
// TriggerPostAppClone rebuilds the new app
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerPostAppClone(oldAppName string, newAppName string) error {
|
|
|
|
|
if os.Getenv("SKIP_REBUILD") == "true" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Rebuild(newAppName)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-28 00:39:28 -05:00
|
|
|
// TriggerPostAppCloneSetup creates new ps files
|
|
|
|
|
func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
|
|
|
|
|
err := common.PropertyClone("ps", oldAppName, newAppName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
return common.CloneAppData("ps", oldAppName, newAppName)
|
2020-11-28 00:39:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:45:08 -05:00
|
|
|
// TriggerPostAppRename rebuilds the renamed app
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerPostAppRename(oldAppName string, newAppName string) error {
|
2023-01-28 15:50:31 -05:00
|
|
|
if err := common.MigrateAppDataDirectory("ps", oldAppName, newAppName); err != nil {
|
2023-01-08 23:24:15 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-01 15:53:53 -05:00
|
|
|
if os.Getenv("SKIP_REBUILD") == "true" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Rebuild(newAppName)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-28 00:39:28 -05:00
|
|
|
// TriggerPostAppRenameSetup renames ps files
|
|
|
|
|
func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
|
|
|
|
|
if err := common.PropertyClone("ps", oldAppName, newAppName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := common.PropertyDestroy("ps", oldAppName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
return common.CloneAppData("ps", oldAppName, newAppName)
|
2020-11-28 00:39:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:45:08 -05:00
|
|
|
// TriggerPostCreate ensures apps have a default restart policy
|
|
|
|
|
// and scale value for web
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerPostCreate(appName string) error {
|
|
|
|
|
if err := dockeroptions.AddDockerOptionToPhases(appName, []string{"deploy"}, "--restart=on-failure:10"); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-08 23:24:15 -05:00
|
|
|
if err := common.CreateAppDataDirectory("ps", appName); err != nil {
|
2020-11-01 15:53:53 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 19:06:25 -04:00
|
|
|
formations := FormationSlice{
|
|
|
|
|
&Formation{
|
|
|
|
|
ProcessType: "web",
|
|
|
|
|
Quantity: 1,
|
|
|
|
|
},
|
|
|
|
|
}
|
2021-08-01 17:47:04 -04:00
|
|
|
return updateScale(appName, false, formations)
|
2020-11-01 15:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TriggerPostDelete destroys the ps properties for a given app container
|
|
|
|
|
func TriggerPostDelete(appName string) error {
|
2023-01-08 23:24:15 -05:00
|
|
|
dataErr := common.RemoveAppDataDirectory("ps", appName)
|
2020-11-21 02:32:28 -05:00
|
|
|
propertyErr := common.PropertyDestroy("ps", appName)
|
|
|
|
|
|
|
|
|
|
if dataErr != nil {
|
|
|
|
|
return dataErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return propertyErr
|
2020-11-01 15:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:45:08 -05:00
|
|
|
// TriggerPostStop sets the restore property to false
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerPostStop(appName string) error {
|
|
|
|
|
entries := map[string]string{
|
|
|
|
|
"DOKKU_APP_RESTORE": "0",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return common.SuppressOutput(func() error {
|
|
|
|
|
return config.SetMany(appName, entries, false)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 17:47:04 -04:00
|
|
|
// TriggerPreDeploy ensures an app has an up to date scale parameters
|
2020-11-01 15:53:53 -05:00
|
|
|
func TriggerPreDeploy(appName string, imageTag string) error {
|
2021-08-01 17:47:04 -04:00
|
|
|
if err := updateScale(appName, false, FormationSlice{}); err != nil {
|
|
|
|
|
common.LogDebug(fmt.Sprintf("Error generating scale file: %s", err.Error()))
|
2020-11-01 15:53:53 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-28 23:22:17 -04:00
|
|
|
// TriggerProcfileExists checks if a procfile exists
|
|
|
|
|
func TriggerProcfileExists(appName string) error {
|
|
|
|
|
if hasProcfile(appName) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return errors.New("Procfile does not exist")
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-18 13:45:08 -05:00
|
|
|
// TriggerProcfileGetCommand fetches a command from the procfile
|
2020-11-01 17:30:24 -05:00
|
|
|
func TriggerProcfileGetCommand(appName string, processType string, port int) error {
|
2022-12-29 01:21:09 -05:00
|
|
|
if !hasProcfile(appName) {
|
2022-11-25 13:44:25 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 01:13:00 -05:00
|
|
|
command, err := getProcfileCommand(getProcessSpecificProcfilePath(appName), processType, port)
|
2020-11-01 15:53:53 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if command != "" {
|
|
|
|
|
fmt.Printf("%s\n", command)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 16:27:13 -04:00
|
|
|
// TriggerPsCanScale sets whether or not a user can scale an app with ps:scale
|
|
|
|
|
func TriggerPsCanScale(appName string, canScale bool) error {
|
|
|
|
|
return common.PropertyWrite("ps", appName, "can-scale", strconv.FormatBool(canScale))
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 01:43:35 -04:00
|
|
|
// TriggerPsCurrentScale prints out the current scale contents (process-type=quantity) delimited by newlines
|
|
|
|
|
func TriggerPsCurrentScale(appName string) error {
|
|
|
|
|
formations, err := getFormations(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-01 15:31:59 -04:00
|
|
|
lines := []string{}
|
2021-08-01 01:43:35 -04:00
|
|
|
for _, formation := range formations {
|
2021-08-01 15:31:59 -04:00
|
|
|
lines = append(lines, fmt.Sprintf("%s=%d", formation.ProcessType, formation.Quantity))
|
2021-08-01 01:43:35 -04:00
|
|
|
}
|
|
|
|
|
|
2021-08-01 15:31:59 -04:00
|
|
|
fmt.Print(strings.Join(lines, "\n"))
|
|
|
|
|
|
2021-08-01 01:43:35 -04:00
|
|
|
return nil
|
|
|
|
|
}
|
2021-08-01 16:11:09 -04:00
|
|
|
|
2021-08-01 16:14:12 -04:00
|
|
|
// TriggerPsSetScale configures the scale parameters for a given app
|
2021-08-01 17:53:57 -04:00
|
|
|
func TriggerPsSetScale(appName string, skipDeploy bool, clearExisting bool, processTuples []string) error {
|
|
|
|
|
return scaleSet(appName, skipDeploy, clearExisting, processTuples)
|
2021-08-01 16:11:09 -04:00
|
|
|
}
|