-
Notifications
You must be signed in to change notification settings - Fork 218
/
vm.go
85 lines (72 loc) · 2.24 KB
/
vm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package scale
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
func newScaleVm() *cobra.Command {
const (
short = "Change an app's VM to a named size (eg. shared-cpu-1x, performance-1x, performance-2x...)"
long = `Change an application's VM size to one of the named VM sizes.
For a full list of supported sizes use the command 'flyctl platform vm-sizes'
Memory size can be set with --memory=number-of-MB
e.g. flyctl scale vm shared-cpu-1x --memory=2048
For pricing, see https://fly.io/docs/about/pricing/`
)
cmd := command.New("vm [size]", short, long, runScaleVM,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Int{
Name: "vm-memory",
Description: "Memory in MB for the VM",
Default: 0,
Aliases: []string{"memory"},
},
flag.ProcessGroup("The process group to apply the VM size to"),
)
return cmd
}
func runScaleVM(ctx context.Context) error {
sizeName := flag.FirstArg(ctx)
memoryMB := flag.GetInt(ctx, "vm-memory")
group := flag.GetProcessGroup(ctx)
return scaleVertically(ctx, group, sizeName, memoryMB)
}
func scaleVertically(ctx context.Context, group, sizeName string, memoryMB int) error {
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)
size, err := v2ScaleVM(ctx, appName, group, sizeName, memoryMB)
if err != nil {
return err
}
if group == "" {
fmt.Fprintf(io.Out, "Scaled VM Type to '%s'\n", size.Name)
} else {
fmt.Fprintf(io.Out, "Scaled VM Type for '%s' to '%s'\n", group, size.Name)
}
fmt.Fprintf(io.Out, "%15s: %s\n", "CPU Cores", formatCores(*size))
fmt.Fprintf(io.Out, "%15s: %s\n", "Memory", formatMemory(*size))
return nil
}
func formatCores(size fly.VMSize) string {
if size.CPUCores < 1.0 {
return fmt.Sprintf("%.2f", size.CPUCores)
}
return fmt.Sprintf("%d", int(size.CPUCores))
}
func formatMemory(size fly.VMSize) string {
if size.MemoryGB < 1.0 {
return fmt.Sprintf("%d MB", size.MemoryMB)
}
return fmt.Sprintf("%d GB", int(size.MemoryGB))
}