-
Notifications
You must be signed in to change notification settings - Fork 240
/
move.go
131 lines (109 loc) · 3.35 KB
/
move.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
package apps
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/internal/flag/completion"
"github.com/superfly/flyctl/internal/flyutil"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/logger"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
func newMove() *cobra.Command {
const (
long = `Move an application to another
organization the current user belongs to.
`
short = "Move an app to another organization."
usage = "move <app name>"
)
move := command.New(usage, short, long, RunMove,
command.RequireSession,
)
move.Args = cobra.ExactArgs(1)
flag.Add(move,
flag.Yes(),
flag.Org(),
flag.Bool{
Name: "skip-health-checks",
Description: "Update machines without waiting for health checks",
Default: false,
},
)
move.ValidArgsFunction = completion.Adapt(completion.CompleteApps)
return move
}
// TODO: make internal once the move package is removed
func RunMove(ctx context.Context) error {
var (
appName = flag.FirstArg(ctx)
client = flyutil.ClientFromContext(ctx)
io = iostreams.FromContext(ctx)
colorize = io.ColorScheme()
logger = logger.FromContext(ctx)
)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("failed fetching app: %w", err)
}
logger.Infof("app %q is currently in organization %q", app.Name, app.Organization.Slug)
org, err := prompt.Org(ctx)
if err != nil {
return err
}
if app.Organization.Slug == org.Slug {
fmt.Fprintln(io.Out, "No changes to apply")
return nil
}
if !flag.GetYes(ctx) {
const msg = `Moving an app between organizations requires a complete shutdown and restart. This will result in some app downtime.
If the app relies on other services within the current organization, it may not come back up in a healthy manner.
Please confirm whether you wish to restart this app now.`
fmt.Fprintln(io.ErrOut, colorize.Red(msg))
switch confirmed, err := prompt.Confirmf(ctx, "Move app %s?", appName); {
case err == nil:
if !confirmed {
return nil
}
case prompt.IsNonInteractive(err):
return prompt.NonInteractiveError("yes flag must be specified when not running interactively")
default:
return err
}
}
return runMoveAppOnMachines(ctx, app, org)
}
func runMoveAppOnMachines(ctx context.Context, app *fly.AppCompact, targetOrg *fly.Organization) error {
var (
client = flyutil.ClientFromContext(ctx)
io = iostreams.FromContext(ctx)
skipHealthChecks = flag.GetBool(ctx, "skip-health-checks")
)
ctx, err := BuildContext(ctx, app)
if err != nil {
return err
}
machines, releaseLeaseFunc, err := mach.AcquireAllLeases(ctx)
defer releaseLeaseFunc()
if err != nil {
return err
}
if _, err := client.MoveApp(ctx, app.Name, targetOrg.ID); err != nil {
return fmt.Errorf("failed moving app: %w", err)
}
for _, machine := range machines {
input := &fly.LaunchMachineInput{
Name: machine.Name,
Region: machine.Region,
Config: machine.Config,
SkipHealthChecks: skipHealthChecks,
}
mach.Update(ctx, machine, input)
}
fmt.Fprintf(io.Out, "successfully moved %s to %s\n", app.Name, targetOrg.Name)
return nil
}