-
Notifications
You must be signed in to change notification settings - Fork 227
/
create.go
196 lines (171 loc) · 5.29 KB
/
create.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package volumes
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/deploy"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/future"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newCreate() *cobra.Command {
const (
short = "Create a new volume for an app."
long = "Create a new volume for an app. Volumes are persistent storage for Fly Machines. Learn how to add a volume to your app: https://fly.io/docs/apps/volume-storage/."
usage = "create <volume name>"
)
cmd := command.New(usage, short, long, runCreate,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Region(),
flag.Int{
Name: "size",
Shorthand: "s",
Default: deploy.DefaultVolumeInitialSizeGB,
Description: "The size of volume in gigabytes",
},
flag.Int{
Name: "snapshot-retention",
Default: 5,
Description: "Snapshot retention in days",
},
flag.Bool{
Name: "no-encryption",
Description: "Do not encrypt the volume contents. Volume contents are encrypted by default.",
Default: false,
},
flag.Bool{
Name: "require-unique-zone",
Description: "Place the volume in a separate hardware zone from existing volumes to help ensure availability",
Default: true,
},
flag.String{
Name: "snapshot-id",
Description: "Create the volume from the specified snapshot",
},
flag.Yes(),
flag.Int{
Name: "count",
Shorthand: "n",
Default: 1,
Description: "The number of volumes to create",
},
flag.VMSizeFlags,
)
flag.Add(cmd, flag.JSONOutput())
return cmd
}
func runCreate(ctx context.Context) error {
var (
cfg = config.FromContext(ctx)
client = flyutil.ClientFromContext(ctx)
volumeName = flag.FirstArg(ctx)
appName = appconfig.NameFromContext(ctx)
count = flag.GetInt(ctx, "count")
)
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
AppName: appName,
})
if err != nil {
return err
}
ctx = flapsutil.NewContextWithClient(ctx, flapsClient)
// pre-fetch platform regions from API in background
prompt.PlatformRegions(ctx)
// fetch AppBasic in the background while we prompt for confirmation
appFuture := future.Spawn(func() (*fly.AppBasic, error) {
return client.GetAppBasic(ctx, appName)
})
if confirm, err := confirmVolumeCreate(ctx, appName); err != nil {
return err
} else if !confirm {
return nil
}
app, err := appFuture.Get()
if err != nil {
return err
}
var region *fly.Region
if region, err = prompt.Region(ctx, !app.Organization.PaidPlan, prompt.RegionParams{
Message: "",
}); err != nil {
return err
}
var snapshotID *string
if flag.GetString(ctx, "snapshot-id") != "" {
snapshotID = fly.StringPointer(flag.GetString(ctx, "snapshot-id"))
}
computeRequirements, err := flag.GetMachineGuest(ctx, nil)
if err != nil {
return err
}
input := fly.CreateVolumeRequest{
Name: volumeName,
Region: region.Code,
SizeGb: fly.Pointer(flag.GetInt(ctx, "size")),
Encrypted: fly.Pointer(!flag.GetBool(ctx, "no-encryption")),
RequireUniqueZone: fly.Pointer(flag.GetBool(ctx, "require-unique-zone")),
SnapshotID: snapshotID,
ComputeRequirements: computeRequirements,
SnapshotRetention: fly.Pointer(flag.GetInt(ctx, "snapshot-retention")),
}
out := iostreams.FromContext(ctx).Out
for i := 0; i < count; i++ {
volume, err := flapsClient.CreateVolume(ctx, input)
if err != nil {
return err
}
if cfg.JSONOutput {
err = render.JSON(out, volume)
} else {
err = printVolume(out, volume, appName)
}
if err != nil {
return err
}
}
return nil
}
func confirmVolumeCreate(ctx context.Context, appName string) (bool, error) {
volumeName := flag.FirstArg(ctx)
// If the Yes flag has been supplied we skip the warning entirely, so no
// need to query for the set of volumes
if flag.GetYes(ctx) {
return true, nil
}
if flag.GetInt(ctx, "count") > 1 {
return true, nil
}
// If we have more than 0 volumes with this name already, return early
if matches, err := countVolumesMatchingName(ctx, volumeName); err != nil {
return false, err
} else if matches > 0 {
return true, nil
}
io := iostreams.FromContext(ctx)
colorize := io.ColorScheme()
const msg = "Warning! Every volume is pinned to a specific physical host. You should create two or more volumes per application to avoid downtime. Learn more at https://fly.io/docs/reference/volumes/"
fmt.Fprintln(io.ErrOut, colorize.Red(msg))
switch confirmed, err := prompt.Confirm(ctx, "Do you still want to use the volumes feature?"); {
case err == nil:
return confirmed, nil
case prompt.IsNonInteractive(err):
return false, prompt.NonInteractiveError("yes flag must be specified when not running interactively")
default:
return false, err
}
}