-
Notifications
You must be signed in to change notification settings - Fork 218
/
extend.go
123 lines (101 loc) · 2.77 KB
/
extend.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package volumes
import (
"context"
"fmt"
"github.com/docker/go-units"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newExtend() *cobra.Command {
const (
short = "Extend a volume to the specified size."
long = short + ` Most Machines don't require a restart. Some older Machines get a message to manually restart the Machine to increase the size of the file system.`
usage = "extend [id]"
)
cmd := command.New(usage, short, long, runExtend,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.MaximumNArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.String{
Name: "size",
Shorthand: "s",
Description: "Target volume size in gigabytes",
},
flag.Yes(),
)
flag.Add(cmd, flag.JSONOutput())
return cmd
}
func runExtend(ctx context.Context) error {
var (
cfg = config.FromContext(ctx)
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
appName = appconfig.NameFromContext(ctx)
client = fly.ClientFromContext(ctx)
volID = flag.FirstArg(ctx)
)
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
AppName: appName,
})
if err != nil {
return err
}
ctx = flaps.NewContext(ctx, flapsClient)
app, err := client.GetAppBasic(ctx, appName)
if err != nil {
return err
}
sizeFlag := flag.GetString(ctx, "size")
sizeGB, err := helpers.ParseSize(sizeFlag, units.FromHumanSize, units.GB)
if err != nil {
return err
}
if sizeGB == 0 {
return fmt.Errorf("Volume size must be specified")
}
if sizeFlag[0] == '+' {
volume, err := flapsClient.GetVolume(ctx, volID)
if err != nil {
return err
}
sizeGB += volume.SizeGb
}
if volID == "" {
volume, err := selectVolume(ctx, flapsClient, app)
if err != nil {
return err
}
volID = volume.ID
}
volume, needsRestart, err := flapsClient.ExtendVolume(ctx, volID, sizeGB)
if err != nil {
return fmt.Errorf("failed to extend volume: %w", err)
}
out := iostreams.FromContext(ctx).Out
if cfg.JSONOutput {
return render.JSON(out, volume)
}
if err := printVolume(out, volume, appName); err != nil {
return err
}
if needsRestart {
fmt.Fprintln(out, colorize.Yellow("You will need to stop and start your Machine to increase the size of the file system"))
} else {
fmt.Fprintln(out, colorize.Green("Your Machine got its volume size extended without needing a restart"))
}
return nil
}