diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e690b868..035e22494 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ **Bugs** * `emp run` now works with unofficial Docker registries [#740](https://github.com/remind101/empire/pull/740). +* `emp scale -l` now lists configured scale, not the running processes [#769](https://github.com/remind101/empire/pull/769) **Security** diff --git a/cmd/emp/scale.go b/cmd/emp/scale.go index 552c03d09..e6687e101 100644 --- a/cmd/emp/scale.go +++ b/cmd/emp/scale.go @@ -92,23 +92,10 @@ func runScale(cmd *Command, args []string) { } func listScale(appname string) { - formationsMap := make(map[string]*heroku.Formation) - dynos, err := client.DynoList(appname, nil) + f, err := client.FormationList(appname, nil) must(err) - for _, d := range dynos { - if _, ok := formationsMap[d.Type]; !ok { - formationsMap[d.Type] = &heroku.Formation{Type: d.Type, Size: d.Size} - } - - f := formationsMap[d.Type] - f.Quantity++ - } - - formations := make(formationsByType, 0) - for _, f := range formationsMap { - formations = append(formations, *f) - } + formations := formationsByType(f) sort.Sort(formations) results := formatResults(formations) log.Println(strings.Join(results, " ")) @@ -119,7 +106,7 @@ func formatResults(formations []heroku.Formation) []string { rindex := 0 for _, f := range formations { results[rindex] = f.Type + "=" + strconv.Itoa(f.Quantity) + ":" + f.Size - rindex += 1 + rindex++ } return results } diff --git a/empire.go b/empire.go index 9f445f96b..9175fdbad 100644 --- a/empire.go +++ b/empire.go @@ -544,6 +544,11 @@ func (e *Empire) Scale(ctx context.Context, opts ScaleOpts) (*Process, error) { return p, tx.Commit().Error } +// ListScale lists the current scale settings for a given App +func (e *Empire) ListScale(ctx context.Context, app *App) (Formation, error) { + return currentFormation(e.db, app) +} + // Streamlogs streams logs from an app. func (e *Empire) StreamLogs(app *App, w io.Writer) error { return e.LogsStreamer.StreamLogs(app, w) diff --git a/releases.go b/releases.go index 61b75a04f..3880f0da8 100644 --- a/releases.go +++ b/releases.go @@ -230,6 +230,17 @@ func createFormation(db *gorm.DB, release *Release) error { return nil } +// currentFormations gets the current formations for an app +func currentFormation(db *gorm.DB, app *App) (Formation, error) { + // Get the current release + current, err := releasesFind(db, ReleasesQuery{App: app}) + if err != nil { + return nil, err + } + f := current.Formation() + return f, nil +} + // ReleasesLastVersion returns the last ReleaseVersion for the given App. func releasesLastVersion(db *gorm.DB, appID string) (int, error) { var version int diff --git a/server/heroku/formations.go b/server/heroku/formations.go index d97e52612..265d94d2c 100644 --- a/server/heroku/formations.go +++ b/server/heroku/formations.go @@ -3,8 +3,8 @@ package heroku import ( "net/http" - "github.com/remind101/empire/pkg/heroku" "github.com/remind101/empire" + "github.com/remind101/empire/pkg/heroku" "golang.org/x/net/context" ) @@ -57,3 +57,33 @@ func (h *PatchFormation) ServeHTTPContext(ctx context.Context, w http.ResponseWr w.WriteHeader(200) return Encode(w, resp) } + +// GetFormation returns the current Formation info for an App +type GetFormation struct { + *empire.Empire +} + +// ServeHTTPContext handles the http response +func (h *GetFormation) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error { + app, err := findApp(ctx, h) + if err != nil { + return err + } + + formation, err := h.ListScale(ctx, app) + if err != nil { + return err + } + + var resp []*Formation + for _, proc := range formation { + resp = append(resp, &Formation{ + Type: string(proc.Type), + Quantity: proc.Quantity, + Size: proc.Constraints.String(), + }) + } + + w.WriteHeader(200) + return Encode(w, resp) +} diff --git a/server/heroku/heroku.go b/server/heroku/heroku.go index 9dd135c2e..7bd9191e1 100644 --- a/server/heroku/heroku.go +++ b/server/heroku/heroku.go @@ -55,6 +55,7 @@ func New(e *empire.Empire, authenticator auth.Authenticator) httpx.Handler { r.Handle("/apps/{app}/dynos/{pid}", &DeleteProcesses{e}).Methods("DELETE") // hk restart web // Formations + r.Handle("/apps/{app}/formation", &GetFormation{e}).Methods("GET") // hk scale -l r.Handle("/apps/{app}/formation", &PatchFormation{e}).Methods("PATCH") // hk scale // OAuth diff --git a/tests/cli/scale_test.go b/tests/cli/scale_test.go index afe2aaa99..66bfa896a 100644 --- a/tests/cli/scale_test.go +++ b/tests/cli/scale_test.go @@ -15,6 +15,10 @@ func TestScale(t *testing.T) { "scale web=2 -a acme-inc", "Scaled acme-inc to web=2:1X.", }, + { + "scale -l -a acme-inc", + "web=2:1X", + }, { "dynos -a acme-inc", `v1.web.1 1X running 5d "./bin/web"