/
barman.go
247 lines (204 loc) · 5.7 KB
/
barman.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package postgres
import (
"context"
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flaps"
"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"
mach "github.com/superfly/flyctl/internal/machine"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
)
var (
volumeName = "barman_data"
volumePath = "/data"
Duration10s, _ = time.ParseDuration("10s")
Duration15s, _ = time.ParseDuration("15s")
CheckPathConnection = "/flycheck/connection"
CheckPathRole = "/flycheck/role"
CheckPathVm = "/flycheck/vm"
)
func newBarman() *cobra.Command {
const (
short = "Manage databases in a cluster"
long = short + "\n"
)
cmd := command.New("barman", short, long, nil)
cmd.AddCommand(
newCreateBarman(),
)
flag.Add(cmd, flag.JSONOutput())
return cmd
}
func newCreateBarman() *cobra.Command {
const (
short = "create barman machine"
long = short + "\n"
usage = "create"
)
cmd := command.New(usage, short, long, runBarmanCreate,
command.RequireSession,
command.RequireAppName,
)
flag.Add(
cmd,
flag.Region(),
flag.App(),
flag.AppConfig(),
flag.String{
Name: "vm-size",
Description: "the size of the VM",
},
flag.Int{
Name: "volume-size",
Description: "The volume size in GB",
},
)
return cmd
}
func runBarmanCreate(ctx context.Context) error {
var (
io = iostreams.FromContext(ctx)
client = client.FromContext(ctx).API()
appName = appconfig.NameFromContext(ctx)
)
// pre-fetch platform regions for later use
prompt.PlatformRegions(ctx)
app, err := client.GetAppCompact(ctx, appName)
if err != nil {
return fmt.Errorf("failed retrieving app %s: %w", appName, err)
}
if !app.IsPostgresApp() {
return fmt.Errorf("app %s is not a postgres app", appName)
}
ctx, err = apps.BuildContext(ctx, app)
if err != nil {
return err
}
if app.PlatformVersion != "machines" {
return fmt.Errorf("wrong platform version")
}
var region *api.Region
region, err = prompt.Region(ctx, !app.Organization.PaidPlan, prompt.RegionParams{
Message: "Select a region. Prefer closer to the primary",
})
if err != nil {
return err
}
machineConfig := api.MachineConfig{}
machineConfig.Env = map[string]string{
"IS_BARMAN": "true",
"PRIMARY_REGION": region.Code,
}
// Set VM resources
vmSizeString := flag.GetString(ctx, "vm-size")
vmSize, err := resolveVMSize(ctx, vmSizeString)
if err != nil {
return err
}
machineConfig.Guest = &api.MachineGuest{
CPUKind: vmSize.CPUClass,
CPUs: int(vmSize.CPUCores),
MemoryMB: vmSize.MemoryMB,
}
// Metrics
machineConfig.Metrics = &api.MachineMetrics{
Path: "/metrics",
Port: 9187,
}
machineConfig.Checks = map[string]api.MachineCheck{
"connection": {
Port: api.Pointer(5500),
Type: api.Pointer("http"),
HTTPPath: &CheckPathConnection,
Interval: &api.Duration{Duration: Duration15s},
Timeout: &api.Duration{Duration: Duration10s},
},
"role": {
Port: api.Pointer(5500),
Type: api.Pointer("http"),
HTTPPath: &CheckPathRole,
Interval: &api.Duration{Duration: Duration15s},
Timeout: &api.Duration{Duration: Duration10s},
},
"vm": {
Port: api.Pointer(5500),
Type: api.Pointer("http"),
HTTPPath: &CheckPathVm,
Interval: &api.Duration{Duration: Duration15s},
Timeout: &api.Duration{Duration: Duration10s},
},
}
// Metadata
machineConfig.Metadata = map[string]string{
api.MachineConfigMetadataKeyFlyPlatformVersion: api.MachineFlyPlatformVersion2,
api.MachineConfigMetadataKeyFlyManagedPostgres: "true",
"managed-by-fly-deploy": "true",
"fly-barman": "true",
}
// Restart policy
machineConfig.Restart.Policy = api.MachineRestartPolicyAlways
imageRepo := "flyio/postgres-flex"
imageRef, err := client.GetLatestImageTag(ctx, imageRepo, nil)
if err != nil {
return err
}
machineConfig.Image = imageRef
var vol *api.Volume
volInput := api.CreateVolumeInput{
AppID: app.ID,
Name: volumeName,
Region: region.Code,
SizeGb: flag.GetInt(ctx, "volume-size"),
Encrypted: true,
RequireUniqueZone: true,
}
if volInput.SizeGb == 0 {
otherVolumes, err := client.GetVolumes(ctx, app.Name)
if err != nil {
return err
}
suggestedSize := 1
for _, volume := range otherVolumes {
if volume.Name == "pg_data" {
suggestedSize = volume.SizeGb
}
}
if err = prompt.Int(ctx, &volInput.SizeGb, "Volume size (should be at least the size of the other volumes)", suggestedSize, false); err != nil {
return err
}
}
fmt.Fprintf(io.Out, "Provisioning volume with %dGB\n", volInput.SizeGb)
vol, err = client.CreateVolume(ctx, volInput)
if err != nil {
return fmt.Errorf("failed to create volume: %w", err)
}
machineConfig.Mounts = append(machineConfig.Mounts, api.MachineMount{
Volume: vol.ID,
Path: volumePath,
})
launchInput := api.LaunchMachineInput{
Region: volInput.Region,
Config: &machineConfig,
}
fmt.Fprintf(io.Out, "Provisioning barman machine with image %s\n", machineConfig.Image)
flapsClient, err := flaps.New(ctx, app)
machine, err := flapsClient.Launch(ctx, launchInput)
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Waiting for machine to start...\n")
waitTimeout := time.Minute * 5
err = mach.WaitForStartOrStop(ctx, machine, "start", waitTimeout)
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Machine %s is %s\n", machine.ID, machine.State)
return nil
}