Skip to content

Commit

Permalink
Remove IsMachinesPlatform and cleanup "fly scale" (#3255)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangra committed Feb 8, 2024
1 parent cf3066a commit a887a10
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 200 deletions.
10 changes: 0 additions & 10 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,6 @@ func loadCache(ctx context.Context) (context.Context, error) {
return cache.NewContext(ctx, c), nil
}

func IsMachinesPlatform(ctx context.Context, appName string) (bool, error) {
apiClient := client.FromContext(ctx).API()
app, err := apiClient.GetAppBasic(ctx, appName)
if err != nil {
return false, fmt.Errorf("failed to retrieve app: %w", err)
}

return app.PlatformVersion == appconfig.MachinesPlatform, nil
}

func startQueryingForNewRelease(ctx context.Context) (context.Context, error) {
logger := logger.FromContext(ctx)

Expand Down
5 changes: 0 additions & 5 deletions internal/command/doctor/app_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ func NewAppChecker(ctx context.Context, jsonOutput bool, color *iostreams.ColorS
appConfig: nil,
}

if !appCompact.Deployed && appCompact.PlatformVersion != "machines" {
ac.lprint(color.Yellow, "%s app has not been deployed yet. Skipping app checks. Deploy using `flyctl deploy`.\n", appName)
return nil, nil
}

ac.app = appCompact
ac.appConfig = appconfig.ConfigFromContext(ctx)

Expand Down
36 changes: 1 addition & 35 deletions internal/command/scale/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (

"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flag/completion"
"github.com/superfly/flyctl/iostreams"
)

func newScaleCount() *cobra.Command {
Expand Down Expand Up @@ -80,14 +78,7 @@ func runScaleCount(ctx context.Context) error {

maxPerRegion := flag.GetInt(ctx, "max-per-region")

isV2, err := command.IsMachinesPlatform(ctx, appName)
if err != nil {
return err
}
if isV2 {
return runMachinesScaleCount(ctx, appName, appConfig, groups, maxPerRegion)
}
return runNomadScaleCount(ctx, appName, groups, maxPerRegion)
return runMachinesScaleCount(ctx, appName, appConfig, groups, maxPerRegion)
}

func parseGroupCounts(args []string, defaultGroupName string) (map[string]int, error) {
Expand Down Expand Up @@ -119,28 +110,3 @@ func parseGroupCounts(args []string, defaultGroupName string) (map[string]int, e

return groups, nil
}

func runNomadScaleCount(ctx context.Context, appName string, groups map[string]int, maxPerRegion int) error {
io := iostreams.FromContext(ctx)
apiClient := client.FromContext(ctx).API()

var maxPerRegionPtr *int
if maxPerRegion >= 0 {
maxPerRegionPtr = &maxPerRegion
}

counts, warnings, err := apiClient.SetAppVMCount(ctx, appName, groups, maxPerRegionPtr)
if err != nil {
return err
}

if len(warnings) > 0 {
for _, warning := range warnings {
fmt.Fprintln(io.Out, "Warning:", warning)
}
fmt.Fprintln(io.Out)
}

fmt.Fprintf(io.Out, "Count changed to %s\n", countMessage(counts))
return nil
}
123 changes: 1 addition & 122 deletions internal/command/scale/show.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
package scale

import (
"context"
"encoding/json"
"fmt"
"strconv"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)

func newScaleShow() *cobra.Command {
const (
short = "Show current resources"
long = `Show current VM size and counts`
)
cmd := command.New("show", short, long, runScaleShow,
cmd := command.New("show", short, long, runMachinesScaleShow,
command.RequireSession,
command.RequireAppName,
)
Expand All @@ -32,115 +23,3 @@ func newScaleShow() *cobra.Command {
)
return cmd
}

func runScaleShow(ctx context.Context) error {
appName := appconfig.NameFromContext(ctx)

isV2, err := command.IsMachinesPlatform(ctx, appName)
if err != nil {
return err
}
if isV2 {
return runMachinesScaleShow(ctx)
}
return runNomadScaleShow(ctx)
}

func runNomadScaleShow(ctx context.Context) error {
apiClient := client.FromContext(ctx).API()
appName := appconfig.NameFromContext(ctx)

size, tgCounts, processGroups, err := apiClient.AppVMResources(ctx, appName)
if err != nil {
return err
}

countMsg := countMessage(tgCounts)
maxPerRegionMsg := maxPerRegionMessage(processGroups)
printVMResources(ctx, size, countMsg, maxPerRegionMsg, processGroups)
return nil
}

func countMessage(counts []api.TaskGroupCount) string {
msg := ""

if len(counts) == 1 {
for _, tg := range counts {
if tg.Name == "app" {
msg = fmt.Sprint(tg.Count)
}
}
}

if msg == "" {
for _, tg := range counts {
msg += fmt.Sprintf("%s=%d ", tg.Name, tg.Count)
}
}

return msg
}

func maxPerRegionMessage(groups []api.ProcessGroup) string {
msg := ""

if len(groups) == 1 {
for _, pg := range groups {
if pg.Name == "app" {
if pg.MaxPerRegion == 0 {
msg = "Not set"
} else {
msg = fmt.Sprint(pg.MaxPerRegion)
}
}
}
}

if msg == "" {
for _, pg := range groups {
msg += fmt.Sprintf("%s=%d ", pg.Name, pg.MaxPerRegion)
}
}

return msg
}

func printVMResources(ctx context.Context, vmSize api.VMSize, count string, maxPerRegion string, processGroups []api.ProcessGroup) {
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)

if flag.GetBool(ctx, "json") {
out := struct {
api.VMSize
Count string
MaxPerRegion string
}{
VMSize: vmSize,
Count: count,
MaxPerRegion: maxPerRegion,
}

prettyJSON, _ := json.MarshalIndent(out, "", " ")
fmt.Fprintln(io.Out, string(prettyJSON))
return
}

fmt.Fprintf(io.Out, "VM Resources for %s\n", appName)

if len(processGroups) <= 1 {
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Size", vmSize.Name)
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Memory", formatMemory(vmSize))
}

fmt.Fprintf(io.Out, "%15s: %s\n", "Count", count)
fmt.Fprintf(io.Out, "%15s: %s\n", "Max Per Region", maxPerRegion)

if len(processGroups) > 1 {
for _, pg := range processGroups {
fmt.Fprintf(io.Out, "\nProcess group %s\n", pg.Name)
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Size", pg.VMSize.Name)
fmt.Fprintf(io.Out, "%15s: %s\n", "VM Memory", formatMemory(*pg.VMSize))
fmt.Fprintf(io.Out, "%15s: %s\n", "Max Per Region", strconv.Itoa(pg.MaxPerRegion))
}
}
}
29 changes: 1 addition & 28 deletions internal/command/scale/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
Expand Down Expand Up @@ -55,17 +54,7 @@ func scaleVertically(ctx context.Context, group, sizeName string, memoryMB int)
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)

isV2, err := command.IsMachinesPlatform(ctx, appName)
if err != nil {
return err
}

var size *api.VMSize
if isV2 {
size, err = v2ScaleVM(ctx, appName, group, sizeName, memoryMB)
} else {
size, err = v1ScaleVM(ctx, appName, group, sizeName, memoryMB)
}
size, err := v2ScaleVM(ctx, appName, group, sizeName, memoryMB)
if err != nil {
return err
}
Expand Down Expand Up @@ -94,19 +83,3 @@ func formatMemory(size api.VMSize) string {
}
return fmt.Sprintf("%d GB", int(size.MemoryGB))
}

func v1ScaleVM(ctx context.Context, appName, group, sizeName string, memoryMB int) (*api.VMSize, error) {
apiClient := client.FromContext(ctx).API()

// API doesn't allow memory setting on own yet, so get get the current size for the mutation
if sizeName == "" {
currentsize, _, _, err := apiClient.AppVMResources(ctx, appName)
if err != nil {
return nil, err
}
sizeName = currentsize.Name
}

size, err := apiClient.SetAppVMSize(ctx, appName, group, sizeName, int64(memoryMB))
return &size, err
}

0 comments on commit a887a10

Please sign in to comment.