refactor: add ability to skip deploys and fix process scaling

This commit is contained in:
Jose Diaz-Gonzalez committed 2020-11-21 20:57:33 -05:00
1 parent a8ccc7d2a7
commit 23ad4bb230
3 files changed
+98 -60

No files matched your search

+93 -56
View File
@@ -181,6 +181,11 @@ func hasScaleFile(appName string) bool {
return common.FileExists(scalefilePath) return common.FileExists(scalefilePath)
} }
func hasProcfile(appName string) bool {
procfilePath := getProcfilePath(appName)
return common.FileExists(procfilePath)
}
func isValidRestartPolicy(policy string) bool { func isValidRestartPolicy(policy string) bool {
if policy == "" { if policy == "" {
return false return false
@@ -266,13 +271,39 @@ func scaleReport(appName string) error {
return nil return nil
} }
func scaleSet(appName string, processTuples []string) error { func parseProcessTuples(processTuples []string) (map[string]int, error) {
scale := make(map[string]int)
for _, processTuple := range processTuples {
s := strings.Split(processTuple, "=")
if len(s) == 1 {
return scale, fmt.Errorf("Missing count for process type %s", processTuple)
}
processType := s[0]
count, err := strconv.Atoi(s[1])
if err != nil {
return scale, fmt.Errorf("Invalid count for process type %s", s[0])
}
scale[processType] = count
}
return scale, nil
}
func scaleSet(appName string, skipDeploy bool, processTuples []string) error {
if !canScaleApp(appName) { if !canScaleApp(appName) {
return fmt.Errorf("App %s contains DOKKU_SCALE file and cannot be manually scaled", 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, " "))) common.LogInfo1(fmt.Sprintf("Scaling %s processes: %s", appName, strings.Join(processTuples, " ")))
if err := updateScalefile(appName, processTuples); err != nil { scale, err := parseProcessTuples(processTuples)
if err != nil {
return err
}
if err := updateScalefile(appName, scale); err != nil {
return err return err
} }
@@ -284,81 +315,87 @@ func scaleSet(appName string, processTuples []string) error {
if err != nil { if err != nil {
return err return err
} }
if skipDeploy {
return nil
}
return common.PlugnTrigger("release-and-deploy", []string{appName, imageTag}...) return common.PlugnTrigger("release-and-deploy", []string{appName, imageTag}...)
} }
func updateScalefile(appName string, processTuples []string) error { func readScaleFile(appName string) (map[string]int, error) {
procfilePath := getProcfilePath(appName) scale := make(map[string]int)
if !hasScaleFile(appName) {
return scale, errors.New("No DOKKU_SCALE file found")
}
scalefilePath := getScalefilePath(appName) scalefilePath := getScalefilePath(appName)
lines, err := common.FileToSlice(scalefilePath) lines, err := common.FileToSlice(scalefilePath)
if err != nil { if err != nil {
return err return scale, err
} }
processTypes := make(map[string]bool) for i, line := range lines {
hasProcfile := common.FileExists(procfilePath) s := strings.Split(line, "=")
if hasProcfile { if len(s) != 2 {
processTypes, err = processesInProcfile(procfilePath) common.LogWarn(fmt.Sprintf("Invalid scale entry on line %d, skipping", i))
if err != nil { continue
return err
}
}
newLines := []string{}
for processType := range processTypes {
found := false
for _, line := range lines {
if strings.HasPrefix(line, fmt.Sprintf("%s=", processType)) {
newLines = append(newLines, line)
found = true
break
}
}
if !found {
newLines = append(newLines, fmt.Sprintf("%s=0", processType))
}
}
scale := []string{}
for _, processTuple := range processTuples {
s := strings.Split(processTuple, "=")
if len(s) == 1 {
return fmt.Errorf("Missing count for process type %s", processTuple)
} }
processType := s[0] processType := s[0]
count, err := strconv.Atoi(s[1]) count, err := strconv.Atoi(s[1])
if err != nil { if err != nil {
return fmt.Errorf("Invalid count for process type %s", s[0]) common.LogWarn(fmt.Sprintf("Invalid count on line %d, skipping", i))
continue
} }
if _, ok := processTypes[processType]; !ok && hasProcfile && count != 0 { scale[processType] = count
}
common.LogWarn("No valid entries found in scale file, defaulting to web=1")
if len(scale) == 0 {
scale["web"] = 1
}
return scale, nil
}
func updateScalefile(appName string, scaleUpdates map[string]int) error {
if !hasScaleFile(appName) {
if err := generateScalefile(appName); err != nil {
return err
}
}
scale, err := readScaleFile(appName)
if err != nil {
return err
}
procfilePath := getProcfilePath(appName)
procfileExists := hasProcfile(appName)
validProcessTypes := make(map[string]bool)
if procfileExists {
validProcessTypes, err = processesInProcfile(procfilePath)
if err != nil {
return err
}
}
for processType, count := range scaleUpdates {
if procfileExists && !validProcessTypes[processType] && 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[processType] = count
scale = append(scale, fmt.Sprintf("%s=%d", processType, count))
} }
for _, line := range newLines { content := []string{}
s := strings.Split(line, "=") for processType, count := range scale {
processType := s[0] content = append(content, fmt.Sprintf("%s=%d", processType, count))
found := false
for _, s := range scale {
if strings.HasPrefix(s, fmt.Sprintf("%s=", processType)) {
found = true
break
}
}
if !found {
scale = append(scale, line)
}
} }
if err := common.WriteSliceToFile(scalefilePath, scale); err != nil { scalefilePath := getScalefilePath(appName)
if err := common.WriteSliceToFile(scalefilePath, content); err != nil {
return err return err
} }
+2 -1
View File
@@ -55,10 +55,11 @@ func main() {
err = ps.CommandRetire() err = ps.CommandRetire()
case "scale": case "scale":
args := flag.NewFlagSet("ps:scale", flag.ExitOnError) args := flag.NewFlagSet("ps:scale", flag.ExitOnError)
skipDeploy := args.Bool("skip-deploy", false, "--skip-deploy: skip deploy of the app")
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
appName := args.Arg(0) appName := args.Arg(0)
_, processTuples := common.ShiftString(args.Args()) _, processTuples := common.ShiftString(args.Args())
err = ps.CommandScale(appName, processTuples) err = ps.CommandScale(appName, *skipDeploy, processTuples)
case "set": case "set":
args := flag.NewFlagSet("ps:set", flag.ExitOnError) args := flag.NewFlagSet("ps:set", flag.ExitOnError)
args.Parse(os.Args[2:]) args.Parse(os.Args[2:])
+3 -3
View File
@@ -88,7 +88,7 @@ func CommandRetire() error {
return nil return nil
} }
func CommandScale(appName string, processTuples []string) error { func CommandScale(appName string, skipDeploy bool, processTuples []string) error {
if appName == "" { if appName == "" {
return errors.New("Please specify an app to run the command on") return errors.New("Please specify an app to run the command on")
} }
@@ -114,7 +114,7 @@ func CommandScale(appName string, processTuples []string) error {
return err return err
} }
} else if common.FileExists(procfilePath) { } else if common.FileExists(procfilePath) {
if err := updateScalefile(appName, []string{}); err != nil { if err := updateScalefile(appName, make(map[string]int)); err != nil {
return err return err
} }
} }
@@ -123,7 +123,7 @@ func CommandScale(appName string, processTuples []string) error {
return scaleReport(appName) return scaleReport(appName)
} }
return scaleSet(appName, processTuples) return scaleSet(appName, skipDeploy, processTuples)
} }
func CommandSet(appName string, property string, value string) error { func CommandSet(appName string, property string, value string) error {