-
Notifications
You must be signed in to change notification settings - Fork 221
/
clone.go
121 lines (95 loc) · 2.3 KB
/
clone.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
package machine
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
func newClone() *cobra.Command {
const (
short = "Clone a Fly machine"
long = short + "\n"
usage = "clone <id>"
)
cmd := command.New(usage, short, long, runMachineClone,
command.RequireSession,
command.LoadAppNameIfPresent,
)
cmd.Args = cobra.MaximumNArgs(1)
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
flag.String{
Name: "region",
Description: "Target region for the new machine",
},
flag.String{
Name: "name",
Description: "Optional name for the new machine",
},
)
return cmd
}
func runMachineClone(ctx context.Context) (err error) {
var (
args = flag.Args(ctx)
out = iostreams.FromContext(ctx).Out
appName = app.NameFromContext(ctx)
client = client.FromContext(ctx).API()
)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return err
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("could not make flaps client: %w", err)
}
var source *api.Machine
if len(args) > 0 {
source, err = flapsClient.Get(ctx, args[0])
if err != nil {
return err
}
} else {
fmt.Fprintf(out, "No machine ID specified, so picking one at random\n")
machines, err := flapsClient.List(ctx, "started")
if err != nil {
return err
}
source, err = flapsClient.Get(ctx, machines[0].ID)
if err != nil {
return err
}
fmt.Fprintf(out, "Picked %s for cloning\n", source.ID)
}
region := flag.GetString(ctx, "region")
if region == "" {
region = source.Region
}
// Ensure attached volumes are not copied to the clone
source.Config.Mounts = nil
input := api.LaunchMachineInput{
AppID: app.Name,
Name: flag.GetString(ctx, "name"),
Region: region,
Config: source.Config,
}
launchedMachine, err := flapsClient.Launch(ctx, input)
if err != nil {
return err
}
err = flapsClient.Wait(ctx, launchedMachine)
if err != nil {
return err
}
fmt.Fprintf(out, "Machine %s was cloned to %s\n", source.ID, launchedMachine.ID)
return
}