-
Notifications
You must be signed in to change notification settings - Fork 241
/
extend.go
116 lines (94 loc) · 2.63 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
package volumes
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/render"
)
func newExtend() *cobra.Command {
const (
long = `Extends a target volume to the size specified. Volumes with attached nomad allocations
will be restarted automatically. Machines will require a manual restart to increase the size
of the FS.`
short = "Extend a target volume"
usage = "extend <id>"
)
cmd := command.New(usage, short, long, runExtend,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Int{
Name: "size",
Shorthand: "s",
Description: "Target volume size in gigabytes",
},
flag.Bool{
Name: "auto-confirm",
Description: "Will automatically confirm changes without an interactive prompt.",
},
)
return cmd
}
func runExtend(ctx context.Context) error {
var (
cfg = config.FromContext(ctx)
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
appName = app.NameFromContext(ctx)
client = client.FromContext(ctx).API()
volID = flag.FirstArg(ctx)
)
app, err := client.GetApp(ctx, appName)
if err != nil {
return err
}
sizeGB := flag.GetInt(ctx, "size")
if sizeGB == 0 {
return fmt.Errorf("Volume size must be specified")
}
if app.PlatformVersion == "nomad" {
if !flag.GetBool(ctx, "auto-confirm") {
switch confirmed, err := prompt.Confirm(ctx, "Extending this volume will result in a VM restart. Continue?"); {
case err == nil:
if !confirmed {
return nil
}
case prompt.IsNonInteractive(err):
return prompt.NonInteractiveError("auto-confirm flag must be specified when not running interactively")
default:
return err
}
}
}
input := api.ExtendVolumeInput{
VolumeID: volID,
SizeGb: flag.GetInt(ctx, "size"),
}
volume, err := client.ExtendVolume(ctx, input)
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); err != nil {
return err
}
if app.PlatformVersion == "machines" {
fmt.Fprintln(out, colorize.Yellow("You will need to stop and start your machine to increase the size of the FS"))
}
return nil
}