-
Notifications
You must be signed in to change notification settings - Fork 233
/
launch_databases.go
177 lines (156 loc) · 5.17 KB
/
launch_databases.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
package launch
import (
"context"
"fmt"
"time"
"github.com/samber/lo"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/flypg"
"github.com/superfly/flyctl/internal/command/postgres"
"github.com/superfly/flyctl/internal/command/redis"
"github.com/superfly/flyctl/iostreams"
)
// createDatabases creates databases requested by the plan
func (state *launchState) createDatabases(ctx context.Context) error {
if state.Plan.Postgres.FlyPostgres != nil {
err := state.createFlyPostgres(ctx)
if err != nil {
// TODO(Ali): Make error printing here better.
fmt.Fprintf(iostreams.FromContext(ctx).ErrOut, "Error creating Postgres cluster: %s\n", err)
}
}
if state.Plan.Redis.UpstashRedis != nil {
err := state.createUpstashRedis(ctx)
if err != nil {
// TODO(Ali): Make error printing here better.
fmt.Fprintf(iostreams.FromContext(ctx).ErrOut, "Error creating Redis database: %s\n", err)
}
}
// Run any initialization commands required for Postgres if it was installed
if state.Plan.Postgres.Provider() != nil && state.sourceInfo != nil {
for _, cmd := range state.sourceInfo.PostgresInitCommands {
if cmd.Condition {
if err := execInitCommand(ctx, cmd); err != nil {
return err
}
}
}
}
return nil
}
func (state *launchState) createFlyPostgres(ctx context.Context) error {
var (
pgPlan = state.Plan.Postgres.FlyPostgres
apiClient = client.FromContext(ctx).API()
io = iostreams.FromContext(ctx)
)
attachToExisting := false
if pgPlan.AppName == "" {
pgPlan.AppName = fmt.Sprintf("%s-db", state.appConfig.AppName)
}
if apps, err := apiClient.GetApps(ctx, nil); err == nil {
for _, app := range apps {
if app.Name == pgPlan.AppName {
attachToExisting = true
}
}
}
if attachToExisting {
// If we try to attach to a PG cluster with the usual username
// format, we'll get an error (since that username already exists)
// by generating a new username with a sufficiently random number
// (in this case, the nanon second that the database is being attached)
currentTime := time.Now().Nanosecond()
dbUser := fmt.Sprintf("%s-%d", pgPlan.AppName, currentTime)
err := postgres.AttachCluster(ctx, postgres.AttachParams{
PgAppName: pgPlan.AppName,
AppName: state.Plan.AppName,
DbUser: dbUser,
})
if err != nil {
msg := "Failed attaching %s to the Postgres cluster %s: %s.\nTry attaching manually with 'fly postgres attach --app %s %s'\n"
fmt.Fprintf(io.Out, msg, state.Plan.AppName, pgPlan.AppName, err, state.Plan.AppName, pgPlan.AppName)
return err
} else {
fmt.Fprintf(io.Out, "Postgres cluster %s is now attached to %s\n", pgPlan.AppName, state.Plan.AppName)
}
} else {
// Create new PG cluster
org, err := state.Org(ctx)
if err != nil {
return err
}
region, err := state.Region(ctx)
if err != nil {
return err
}
err = postgres.CreateCluster(ctx, org, ®ion, &postgres.ClusterParams{
PostgresConfiguration: postgres.PostgresConfiguration{
Name: pgPlan.AppName,
DiskGb: pgPlan.DiskSizeGB,
InitialClusterSize: pgPlan.Nodes,
VMSize: pgPlan.VmSize,
MemoryMb: pgPlan.VmRam,
},
ScaleToZero: &pgPlan.AutoStop,
Autostart: true, // TODO(Ali): Do we want this?
Manager: flypg.ReplicationManager,
})
if err != nil {
fmt.Fprintf(io.Out, "Failed creating the Postgres cluster %s: %s\n", pgPlan.AppName, err)
} else {
err = postgres.AttachCluster(ctx, postgres.AttachParams{
PgAppName: pgPlan.AppName,
AppName: state.Plan.AppName,
SuperUser: true,
})
if err != nil {
msg := "Failed attaching %s to the Postgres cluster %s: %s.\nTry attaching manually with 'fly postgres attach --app %s %s'\n"
fmt.Fprintf(io.Out, msg, state.Plan.AppName, pgPlan.AppName, err, state.Plan.AppName, pgPlan.AppName)
} else {
fmt.Fprintf(io.Out, "Postgres cluster %s is now attached to %s\n", pgPlan.AppName, state.Plan.AppName)
}
}
if err != nil {
const msg = "Error creating Postgresql database. Be warned that this may affect deploys"
fmt.Fprintln(io.Out, io.ColorScheme().Red(msg))
}
return err
}
return nil
}
func (state *launchState) createUpstashRedis(ctx context.Context) error {
redisPlan := state.Plan.Redis.UpstashRedis
if redisPlan.AppName == "" {
redisPlan.AppName = fmt.Sprintf("%s-redis", state.appConfig.AppName)
}
org, err := state.Org(ctx)
if err != nil {
return err
}
region, err := state.Region(ctx)
if err != nil {
return err
}
var readReplicaRegions []api.Region
{
client := client.FromContext(ctx).API()
regions, _, err := client.PlatformRegions(ctx)
if err != nil {
return err
}
for _, code := range redisPlan.ReadReplicas {
if region, ok := lo.Find(regions, func(r api.Region) bool { return r.Code == code }); ok {
readReplicaRegions = append(readReplicaRegions, region)
} else {
return fmt.Errorf("region %s not found", code)
}
}
}
db, err := redis.Create(ctx, org, redisPlan.AppName, ®ion, redisPlan.PlanId, len(readReplicaRegions) == 0, redisPlan.Eviction, &readReplicaRegions)
if err != nil {
return err
}
return redis.AttachDatabase(ctx, db, state.Plan.AppName)
}