feat: add support for formation key in app.json file
Still needs triggers for ps-can-scale and ps-scale-set. Refs #4500
This commit is contained in:
1 parent
9d0bc930a6
commit
6d9520e599
3 files changed
+52
-8
No files matched your search
@@ -20,8 +20,9 @@ var (
|
|||||||
|
|
||||||
// AppJSON is a struct that represents an app.json file as understood by Dokku
|
// AppJSON is a struct that represents an app.json file as understood by Dokku
|
||||||
type AppJSON struct {
|
type AppJSON struct {
|
||||||
Cron []CronCommand `json:"cron"`
|
Cron []CronCommand `json:"cron"`
|
||||||
Scripts struct {
|
Formation map[string]Formation `json:"formation"`
|
||||||
|
Scripts struct {
|
||||||
Dokku struct {
|
Dokku struct {
|
||||||
Predeploy string `json:"predeploy"`
|
Predeploy string `json:"predeploy"`
|
||||||
Postdeploy string `json:"postdeploy"`
|
Postdeploy string `json:"postdeploy"`
|
||||||
@@ -36,6 +37,10 @@ type CronCommand struct {
|
|||||||
Schedule string `json:"schedule"`
|
Schedule string `json:"schedule"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Formation struct {
|
||||||
|
Quantity int `json:"quantity"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetAppjsonDirectory returns the directory containing a given app's extracted app.json file
|
// GetAppjsonDirectory returns the directory containing a given app's extracted app.json file
|
||||||
func GetAppjsonDirectory(appName string) string {
|
func GetAppjsonDirectory(appName string) string {
|
||||||
return filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "app-json", appName)
|
return filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "app-json", appName)
|
||||||
|
|||||||
@@ -79,24 +79,33 @@ func constructScript(command string, shell string, isHerokuishImage bool, isCnbI
|
|||||||
return []string{shell, "-c", strings.Join(script, " ")}
|
return []string{shell, "-c", strings.Join(script, " ")}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPhaseScript extracts app.json from app image and returns the appropriate json key/value
|
func getAppJson(appName string) (AppJSON, error) {
|
||||||
func getPhaseScript(appName string, phase string) (string, error) {
|
|
||||||
if !common.FileExists(GetAppjsonPath(appName)) {
|
if !common.FileExists(GetAppjsonPath(appName)) {
|
||||||
return "", nil
|
return AppJSON{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(GetAppjsonPath(appName))
|
b, err := ioutil.ReadFile(GetAppjsonPath(appName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Cannot read app.json file: %v", err)
|
return AppJSON{}, fmt.Errorf("Cannot read app.json file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(string(b)) == "" {
|
if strings.TrimSpace(string(b)) == "" {
|
||||||
return "", nil
|
return AppJSON{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var appJSON AppJSON
|
var appJSON AppJSON
|
||||||
if err = json.Unmarshal(b, &appJSON); err != nil {
|
if err = json.Unmarshal(b, &appJSON); err != nil {
|
||||||
return "", fmt.Errorf("Cannot parse app.json: %v", err)
|
return AppJSON{}, fmt.Errorf("Cannot parse app.json: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return AppJSON{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getPhaseScript extracts app.json from app image and returns the appropriate json key/value
|
||||||
|
func getPhaseScript(appName string, phase string) (string, error) {
|
||||||
|
appJSON, err := getAppJson(appName)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if phase == "heroku.postdeploy" {
|
if phase == "heroku.postdeploy" {
|
||||||
@@ -475,3 +484,29 @@ func refreshAppJSON(appName string, image string) error {
|
|||||||
common.CopyFromImage(appName, image, reportComputedAppjsonpath(appName), appjsonPath)
|
common.CopyFromImage(appName, image, reportComputedAppjsonpath(appName), appjsonPath)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setScale(appName string, image string) error {
|
||||||
|
appJSON, err := getAppJson(appName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{appName}
|
||||||
|
for processType, formation := range appJSON.Formation {
|
||||||
|
args = append(args, fmt.Sprintf("%s=%d", processType, formation.Quantity))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) == 1 {
|
||||||
|
return common.PlugnTrigger("ps-can-scale", []string{appName, "true"}...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := common.PlugnTrigger("ps-can-scale", []string{appName, "false"}...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := common.PlugnTrigger("ps-scale-set", args...); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -64,6 +64,10 @@ func TriggerPreDeploy(appName string, imageTag string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := setScale(appName, image); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if common.PropertyGet("common", appName, "deployed") == "true" {
|
if common.PropertyGet("common", appName, "deployed") == "true" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user