-
Notifications
You must be signed in to change notification settings - Fork 224
/
detach.go
168 lines (139 loc) · 4.21 KB
/
detach.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
package postgres
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/agent"
"github.com/superfly/flyctl/flypg"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/apps"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flyutil"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
func newDetach() *cobra.Command {
const (
short = "Detach a postgres cluster from an app"
long = short + "\n"
usage = "detach <POSTGRES APP>"
)
cmd := command.New(usage, short, long, runDetach,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}
func runDetach(ctx context.Context) error {
var (
client = flyutil.ClientFromContext(ctx)
pgAppName = flag.FirstArg(ctx)
appName = appconfig.NameFromContext(ctx)
)
pgApp, err := client.GetAppCompact(ctx, pgAppName)
if err != nil {
return fmt.Errorf("get postgres app: %w", err)
}
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
ctx, err = apps.BuildContext(ctx, pgApp)
if err != nil {
return err
}
return runMachineDetach(ctx, app, pgApp)
}
func runMachineDetach(ctx context.Context, app *fly.AppCompact, pgApp *fly.AppCompact) error {
var (
MinPostgresHaVersion = "0.0.19"
MinPostgresFlexVersion = "0.0.3"
MinPostgresStandaloneVersion = "0.0.7"
)
machines, err := mach.ListActive(ctx)
if err != nil {
return fmt.Errorf("machines could not be retrieved %w", err)
}
if err := hasRequiredVersionOnMachines(machines, MinPostgresHaVersion, MinPostgresFlexVersion, MinPostgresStandaloneVersion); err != nil {
return err
}
if len(machines) == 0 {
return fmt.Errorf("no 6pn ips founds for %s app", pgApp.Name)
}
leader, err := pickLeader(ctx, machines)
if err != nil {
return err
}
return detachAppFromPostgres(ctx, leader.PrivateIP, app, pgApp)
}
// TODO - This process needs to be re-written to suppport non-interactive terminals.
func detachAppFromPostgres(ctx context.Context, leaderIP string, app *fly.AppCompact, pgApp *fly.AppCompact) error {
var (
client = flyutil.ClientFromContext(ctx)
dialer = agent.DialerFromContext(ctx)
io = iostreams.FromContext(ctx)
)
attachments, err := client.ListPostgresClusterAttachments(ctx, app.ID, pgApp.ID)
if err != nil {
return err
}
if len(attachments) == 0 {
return fmt.Errorf("no attachments found")
}
selected := 0
msg := "Select the attachment that you would like to detach (Database will remain intact): "
options := []string{}
for _, opt := range attachments {
str := fmt.Sprintf("PG Database: %s, PG User: %s, Environment variable: %s",
opt.DatabaseName,
opt.DatabaseUser,
opt.EnvironmentVariableName,
)
options = append(options, str)
}
if err = prompt.Select(ctx, &selected, msg, "", options...); err != nil {
return err
}
targetAttachment := attachments[selected]
pgclient := flypg.NewFromInstance(leaderIP, dialer)
// Remove user if exists
exists, err := pgclient.UserExists(ctx, targetAttachment.DatabaseUser)
if err != nil {
return err
}
if exists {
err := pgclient.DeleteUser(ctx, targetAttachment.DatabaseUser)
if err != nil {
return fmt.Errorf("error running user-delete: %w", err)
}
}
// Remove secret from consumer app.
_, err = client.UnsetSecrets(ctx, app.Name, []string{targetAttachment.EnvironmentVariableName})
if err != nil {
// This will error if secret doesn't exist, so just send to stdout.
fmt.Fprintln(io.Out, err.Error())
} else {
fmt.Fprintf(io.Out, "Secret %q was scheduled to be removed from app %s\n",
targetAttachment.EnvironmentVariableName,
app.Name,
)
}
input := fly.DetachPostgresClusterInput{
AppID: app.Name,
PostgresClusterId: pgApp.Name,
PostgresClusterAttachmentId: targetAttachment.ID,
}
if err = client.DetachPostgresCluster(ctx, input); err != nil {
return err
}
fmt.Fprintln(io.Out, "Detach completed successfully!")
return nil
}