refactor: cleanup zero'd out processes when a Procfile omitting those process types is set

This change also moves the referenced Procfile out to a host path once on deploy vs potentially several times, which should speed up deploys a small amount and simplify reasoning about the file.

Closes #5112
This commit is contained in:
Jose Diaz-Gonzalez
2022-11-25 00:41:55 -05:00
parent 3100eeb151
commit fe4d80a82b
9 changed files with 139 additions and 166 deletions

View File

@@ -24,6 +24,22 @@ func TriggerAppRestart(appName string) error {
// TriggerCorePostDeploy sets a property to
// allow the app to be restored on boot
func TriggerCorePostDeploy(appName string) error {
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
}
} else if common.FileExists(fmt.Sprintf("%s.missing", existingProcfile)) {
if err := os.Remove(fmt.Sprintf("%s.missing", existingProcfile)); err != nil {
return err
}
if err := os.Remove(existingProcfile); err != nil {
return err
}
}
entries := map[string]string{
"DOKKU_APP_RESTORE": "1",
}
@@ -36,26 +52,36 @@ func TriggerCorePostDeploy(appName string) error {
// 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), "/")
if procfilePath == "" || procfilePath == "Procfile" {
return nil
if procfilePath == "" {
procfilePath = "Procfile"
}
defaultProcfilePath := path.Join(sourceWorkDir, "Procfile")
if common.FileExists(defaultProcfilePath) {
if err := os.Remove(defaultProcfilePath); err != nil {
return fmt.Errorf("Unable to remove existing Procfile: %v", err.Error())
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
}
}
fullProcfilePath := path.Join(sourceWorkDir, procfilePath)
if !common.FileExists(fullProcfilePath) {
return nil
repoProcfilePath := path.Join(sourceWorkDir, procfilePath)
processSpecificProcfile := fmt.Sprintf("%s.%s", existingProcfile, os.Getenv("DOKKU_PID"))
if !common.FileExists(repoProcfilePath) {
return common.TouchFile(fmt.Sprintf("%s.missing", processSpecificProcfile))
}
if err := copy.Copy(fullProcfilePath, path.Join(sourceWorkDir, "Procfile")); err != nil {
return fmt.Errorf("Unable to move specified Procfile into place: %v", err.Error())
if err := copy.Copy(repoProcfilePath, processSpecificProcfile); err != nil {
return fmt.Errorf("Unable to extract Procfile: %v", err.Error())
}
b, err := sh.Command("procfile-util", "check", "-P", processSpecificProcfile).CombinedOutput()
if err != nil {
return fmt.Errorf(strings.TrimSpace(string(b[:])))
}
return nil
}
@@ -137,6 +163,7 @@ func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
return err
}
// TODO: Copy data dir
return nil
}
@@ -159,6 +186,7 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
return err
}
// TODO: Move data dir
return nil
}
@@ -200,20 +228,6 @@ func TriggerPostDelete(appName string) error {
return propertyErr
}
// TriggerPostExtract validates a procfile
func TriggerPostExtract(appName string, tempWorkDir string) error {
procfile := filepath.Join(tempWorkDir, "Procfile")
if !common.FileExists(procfile) {
return nil
}
b, err := sh.Command("procfile-util", "check", "-P", procfile).CombinedOutput()
if err != nil {
return fmt.Errorf(strings.TrimSpace(string(b[:])))
}
return nil
}
// TriggerPostStop sets the restore property to false
func TriggerPostStop(appName string) error {
entries := map[string]string{
@@ -227,16 +241,6 @@ func TriggerPostStop(appName string) error {
// TriggerPreDeploy ensures an app has an up to date scale parameters
func TriggerPreDeploy(appName string, imageTag string) error {
image := common.GetAppImageName(appName, imageTag, "")
if err := removeProcfile(appName); err != nil {
return err
}
if err := extractProcfile(appName, image); err != nil {
return err
}
if err := updateScale(appName, false, FormationSlice{}); err != nil {
common.LogDebug(fmt.Sprintf("Error generating scale file: %s", err.Error()))
return err
@@ -245,41 +249,9 @@ func TriggerPreDeploy(appName string, imageTag string) error {
return nil
}
// TriggerProcfileExtract extracted the procfile
func TriggerProcfileExtract(appName string, image string) error {
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "ps", appName)
if err := os.MkdirAll(directory, 0755); err != nil {
return err
}
if err := common.SetPermissions(directory, 0755); err != nil {
return err
}
if err := removeProcfile(appName); err != nil {
return err
}
return extractProcfile(appName, image)
}
// TriggerProcfileGetCommand fetches a command from the procfile
func TriggerProcfileGetCommand(appName string, processType string, port int) error {
procfilePath := getProcfilePath(appName)
if !common.FileExists(procfilePath) {
extract := func() error {
image, err := common.GetDeployingAppImageName(appName, "", "")
if err != nil {
return err
}
return extractProcfile(appName, image)
}
if err := common.SuppressOutput(extract); err != nil {
return err
}
}
command, err := getProcfileCommand(procfilePath, processType, port)
if err != nil {
return err
@@ -292,11 +264,6 @@ func TriggerProcfileGetCommand(appName string, processType string, port int) err
return nil
}
// TriggerProcfileRemove removes the procfile if it exists
func TriggerProcfileRemove(appName string) error {
return removeProcfile(appName)
}
// 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))