refactor: split ps:scale func into different functions

This commit is contained in:
Jose Diaz-Gonzalez committed 2020-11-21 20:57:33 -05:00
1 parent f6efb53491
commit 0a533c754f
2 files changed
+70 -57

No files matched your search

+66 -5
View File
@@ -6,11 +6,13 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"strings" "strings"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
dockeroptions "github.com/dokku/dokku/plugins/docker-options" dockeroptions "github.com/dokku/dokku/plugins/docker-options"
"github.com/ryanuber/columnize"
) )
func canScaleApp(appName string) bool { func canScaleApp(appName string) bool {
@@ -48,7 +50,6 @@ func extractProcfile(appName, image string, procfilePath string) error {
return nil return nil
} }
func extractOrGenerateScalefile(appName string, image string) error { func extractOrGenerateScalefile(appName string, image string) error {
destination := getScalefilePath(appName) destination := getScalefilePath(appName)
extracted := getScalefileExtractedPath(appName) extracted := getScalefileExtractedPath(appName)
@@ -64,18 +65,20 @@ func extractOrGenerateScalefile(appName string, image string) error {
} }
if common.FileExists(destination) { if common.FileExists(destination) {
common.LogInfo1Quiet("DOKKU_SCALE file exists")
return nil return nil
} }
common.LogInfo1Quiet("DOKKU_SCALE file not found in app image. Generating one based on Procfile...") common.LogInfo1Quiet("DOKKU_SCALE file not found in app image. Generating one based on Procfile...")
if err := generateScalefile(appName, destination); err != nil { if err := generateScalefile(appName); err != nil {
common.LogDebug(fmt.Sprintf("Error: %s", err.Error())) common.LogDebug(fmt.Sprintf("Error generating scale file: %s", err.Error()))
return err return err
} }
return nil return nil
} }
func generateScalefile(appName string, destination string) error { func generateScalefile(appName string) error {
destination := getScalefilePath(appName)
procfilePath := getProcfilePath(appName) procfilePath := getProcfilePath(appName)
content := []string{"web=1"} content := []string{"web=1"}
if !common.FileExists(procfilePath) { if !common.FileExists(procfilePath) {
@@ -173,6 +176,11 @@ func getScalefileExtractedPath(appName string) string {
return filepath.Join(common.AppRoot(appName), "DOKKU_SCALE.extracted") return filepath.Join(common.AppRoot(appName), "DOKKU_SCALE.extracted")
} }
func hasScaleFile(appName string) bool {
scalefilePath := getScalefilePath(appName)
return common.FileExists(scalefilePath)
}
func isValidRestartPolicy(policy string) bool { func isValidRestartPolicy(policy string) bool {
if policy == "" { if policy == "" {
return false return false
@@ -226,6 +234,59 @@ func removeProcfile(appName string) error {
return os.Remove(procfile) return os.Remove(procfile)
} }
func scaleReport(appName string) error {
scalefilePath := getScalefilePath(appName)
lines, err := common.FileToSlice(scalefilePath)
if err != nil {
return err
}
common.LogInfo1Quiet(fmt.Sprintf("Scaling for %s", appName))
config := columnize.DefaultConfig()
config.Delim = "="
config.Glue = ": "
config.Prefix = " "
config.Empty = ""
content := []string{}
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
content = append(content, "proctype=qty", "--------=---")
}
sort.Strings(lines)
for _, line := range lines {
content = append(content, line)
}
for _, line := range content {
s := strings.Split(line, "=")
common.Log(fmt.Sprintf("%s %s", common.RightPad(fmt.Sprintf("%s:", s[0]), 5, " "), s[1]))
}
return nil
}
func scaleSet(appName string, processTuples []string) error {
if !canScaleApp(appName) {
return fmt.Errorf("App %s contains DOKKU_SCALE file and cannot be manually scaled", appName)
}
common.LogInfo1(fmt.Sprintf("Scaling %s processes: %s", appName, strings.Join(processTuples, " ")))
if err := updateScalefile(appName, processTuples); err != nil {
return err
}
if !common.IsDeployed(appName) {
return nil
}
imageTag, err := common.GetRunningImageTag(appName)
if err != nil {
return err
}
return common.PlugnTrigger("release-and-deploy", []string{appName, imageTag}...)
}
func updateScalefile(appName string, processTuples []string) error { func updateScalefile(appName string, processTuples []string) error {
procfilePath := getProcfilePath(appName) procfilePath := getProcfilePath(appName)
scalefilePath := getScalefilePath(appName) scalefilePath := getScalefilePath(appName)
@@ -273,7 +334,7 @@ func updateScalefile(appName string, processTuples []string) error {
} }
if _, ok := processTypes[processType]; !ok && hasProcfile && count != 0 { if _, ok := processTypes[processType]; !ok && hasProcfile && count != 0 {
return fmt.Errorf("%s is not a valid process name to scale up", processType) return fmt.Errorf("%s is not a valid process name to scale up", processType)
} }
scale = append(scale, fmt.Sprintf("%s=%d", processType, count)) scale = append(scale, fmt.Sprintf("%s=%d", processType, count))
+4 -52
View File
@@ -3,14 +3,11 @@ package ps
import ( import (
"errors" "errors"
"fmt" "fmt"
"os"
"sort"
"strings" "strings"
dockeroptions "github.com/dokku/dokku/plugins/docker-options" dockeroptions "github.com/dokku/dokku/plugins/docker-options"
"github.com/dokku/dokku/plugins/common" "github.com/dokku/dokku/plugins/common"
"github.com/ryanuber/columnize"
) )
func CommandInspect(appName string) error { func CommandInspect(appName string) error {
@@ -101,7 +98,6 @@ func CommandScale(appName string, processTuples []string) error {
} }
procfilePath := getProcfilePath(appName) procfilePath := getProcfilePath(appName)
scalefilePath := getScalefilePath(appName)
if !common.FileExists(procfilePath) { if !common.FileExists(procfilePath) {
image := common.GetAppImageRepo(appName) image := common.GetAppImageRepo(appName)
common.SuppressOutput(func() error { common.SuppressOutput(func() error {
@@ -110,9 +106,9 @@ func CommandScale(appName string, processTuples []string) error {
}) })
} }
if !common.FileExists(scalefilePath) { if !hasScaleFile(appName) {
err := common.SuppressOutput(func() error { err := common.SuppressOutput(func() error {
return generateScalefile(appName, scalefilePath) return generateScalefile(appName)
}) })
if err != nil { if err != nil {
return err return err
@@ -124,54 +120,10 @@ func CommandScale(appName string, processTuples []string) error {
} }
if len(processTuples) == 0 { if len(processTuples) == 0 {
lines, err := common.FileToSlice(scalefilePath) return scaleReport(appName)
if err != nil {
return err
}
common.LogInfo1Quiet(fmt.Sprintf("Scaling for %s", appName))
config := columnize.DefaultConfig()
config.Delim = "="
config.Glue = ": "
config.Prefix = " "
config.Empty = ""
content := []string{}
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
content = append(content, "proctype=qty","--------=---")
}
sort.Strings(lines)
for _, line := range lines {
content = append(content, line)
}
for _, line := range content {
s := strings.Split(line, "=")
common.Log(fmt.Sprintf("%s %s", common.RightPad(fmt.Sprintf("%s:", s[0]), 5, " "), s[1]))
}
} else {
if !canScaleApp(appName) {
return fmt.Errorf("App %s contains DOKKU_SCALE file and cannot be manually scaled", appName)
}
common.LogInfo1(fmt.Sprintf("Scaling %s processes: %s", appName, strings.Join(processTuples, " ")))
if err := updateScalefile(appName, processTuples); err != nil {
return err
}
if !common.IsDeployed(appName) {
return nil
}
imageTag, err := common.GetRunningImageTag(appName)
if err != nil {
return err
}
return common.PlugnTrigger("release-and-deploy", []string{appName, imageTag}...)
} }
return nil return scaleSet(appName, processTuples)
} }
func CommandSet(appName string, property string, value string) error { func CommandSet(appName string, property string, value string) error {