-
Notifications
You must be signed in to change notification settings - Fork 223
/
fork.go
157 lines (133 loc) · 3.71 KB
/
fork.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
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/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/flyutil"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newFork() *cobra.Command {
const (
short = "Fork the specified volume."
long = short + ` Volume forking creates an independent copy of a storage volume for backup, testing, and experimentation without altering the original data.`
usage = "fork <volume id>"
)
cmd := command.New(usage, short, long, runFork,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.MaximumNArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.String{
Name: "name",
Shorthand: "n",
Description: "The name of the new volume",
},
flag.Bool{
Name: "machines-only",
Description: "volume will be visible to Machines platform only",
Hidden: true,
},
flag.Bool{
Name: "require-unique-zone",
Description: "Place the volume in a separate hardware zone from existing volumes. This is the default.",
Default: true,
},
flag.String{
Name: "region",
Shorthand: "r",
Description: "The target region. By default, the new volume will be created in the source volume's region.",
},
flag.VMSizeFlags,
)
flag.Add(cmd, flag.JSONOutput())
return cmd
}
func runFork(ctx context.Context) error {
var (
cfg = config.FromContext(ctx)
appName = appconfig.NameFromContext(ctx)
volID = flag.FirstArg(ctx)
client = flyutil.ClientFromContext(ctx)
)
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
AppName: appName,
})
if err != nil {
return err
}
var vol *fly.Volume
if volID == "" {
app, err := client.GetAppBasic(ctx, appName)
if err != nil {
return err
}
vol, err = selectVolume(ctx, flapsClient, app)
if err != nil {
return err
}
} else {
vol, err = flapsClient.GetVolume(ctx, volID)
if err != nil {
return fmt.Errorf("failed to get volume: %w", err)
}
}
name := vol.Name
if flag.IsSpecified(ctx, "name") {
name = flag.GetString(ctx, "name")
}
var machinesOnly *bool
if flag.IsSpecified(ctx, "machines-only") {
machinesOnly = fly.Pointer(flag.GetBool(ctx, "machines-only"))
}
var requireUniqueZone *bool
if flag.IsSpecified(ctx, "require-unique-zone") {
requireUniqueZone = fly.Pointer(flag.GetBool(ctx, "require-unique-zone"))
}
region := flag.GetString(ctx, "region")
var attachedMachineImage string
var attachedMachineGuest *fly.MachineGuest
if vol.AttachedMachine != nil {
m, err := flapsClient.Get(ctx, *vol.AttachedMachine)
if err != nil {
return err
}
attachedMachineGuest = m.Config.Guest
attachedMachineImage = m.FullImageRef()
}
computeRequirements, err := flag.GetMachineGuest(ctx, attachedMachineGuest)
if err != nil {
return err
}
input := fly.CreateVolumeRequest{
Name: name,
MachinesOnly: machinesOnly,
RequireUniqueZone: requireUniqueZone,
SourceVolumeID: &vol.ID,
ComputeRequirements: computeRequirements,
ComputeImage: attachedMachineImage,
Region: region,
}
volume, err := flapsClient.CreateVolume(ctx, input)
if err != nil {
return fmt.Errorf("failed to fork 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
}
return nil
}