-
Notifications
You must be signed in to change notification settings - Fork 221
/
restart.go
109 lines (85 loc) · 2.63 KB
/
restart.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
package postgres
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/internal/app"
"github.com/superfly/flyctl/internal/client"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/pkg/agent"
"github.com/superfly/flyctl/pkg/flaps"
"github.com/superfly/flyctl/pkg/flypg"
"github.com/superfly/flyctl/pkg/iostreams"
)
func newRestart() (cmd *cobra.Command) {
const (
long = `Restarts each member of the Postgres cluster one by one. Downtime should be minimal.
`
short = "Restarts the Postgres cluster"
usage = "restart"
)
cmd = command.New(usage, short, long, runRestart,
command.RequireSession,
command.RequireAppName,
)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return
}
func runRestart(ctx context.Context) error {
appName := app.NameFromContext(ctx)
client := client.FromContext(ctx).API()
io := iostreams.FromContext(ctx)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("get app: %w", err)
}
flapsClient, err := flaps.New(ctx, app)
if err != nil {
return fmt.Errorf("list of machines could not be retrieved: %w", err)
}
// map of machine lease to machine
var machines = make(map[string]*api.V1Machine)
out, err := flapsClient.List(ctx, "started")
if err != nil {
return fmt.Errorf("machines could not be retrieved %w", err)
}
if len(out) == 0 {
return fmt.Errorf("no machines found")
}
fmt.Fprintf(io.Out, "Acquiring lease on postgres cluster\n")
for _, machine := range out {
lease, err := flapsClient.GetLease(ctx, machine.ID, api.IntPointer(40))
if err != nil {
return fmt.Errorf("failed to obtain lease: %w", err)
}
machines[lease.Data.Nonce] = machine
}
agentclient, err := agent.Establish(ctx, client)
if err != nil {
return fmt.Errorf("can't establish agent %w", err)
}
dialer, err := agentclient.Dialer(ctx, app.Organization.Slug)
if err != nil {
return fmt.Errorf("ssh: can't build tunnel for %s: %s", app.Organization.Slug, err)
}
fmt.Fprintf(io.Out, "Restarting Postgres\n")
for lease, machine := range machines {
fmt.Fprintf(io.Out, " Restarting %s with lease %s\n", machine.ID, lease)
address := formatAddress(machine)
pgclient := flypg.NewFromInstance(address, dialer)
if err := pgclient.RestartNodePG(ctx); err != nil {
fmt.Fprintf(io.Out, "postgres on node: %s failed\n", machine.ID)
return err
}
if err := flapsClient.ReleaseLease(ctx, machine.ID, lease); err != nil {
return fmt.Errorf("failed to release lease: %w", err)
}
}
fmt.Fprintf(io.Out, "Restart complete\n")
return nil
}