Skip to content

Commit

Permalink
scale -l now shows configured scale, not running
Browse files Browse the repository at this point in the history
* scale -l now shows configured scale, not running

Fixes #769

* Clean up based on @ejholmes review
  • Loading branch information
phobologic committed Apr 4, 2016
1 parent 5facae7 commit ea66b87
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
19 changes: 3 additions & 16 deletions cmd/emp/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " "))
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions empire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion server/heroku/formations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions server/heroku/heroku.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/cli/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ea66b87

Please sign in to comment.