-
Notifications
You must be signed in to change notification settings - Fork 224
/
add_flycast.go
136 lines (114 loc) · 2.68 KB
/
add_flycast.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
package postgres
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"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"
)
func newAddFlycast() *cobra.Command {
const (
short = "Switch from DNS to flycast based pg connections"
long = short + "\n"
usage = "add_flycast"
)
cmd := command.New(usage, short, long, runAddFlycast,
command.RequireSession,
command.RequireAppName,
)
flag.Add(
cmd,
flag.App(),
flag.AppConfig(),
)
cmd.Hidden = true
return cmd
}
func runAddFlycast(ctx context.Context) error {
var (
client = flyutil.ClientFromContext(ctx)
appName = appconfig.NameFromContext(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 err := doAddFlycast(ctx); err != nil {
return err
}
return nil
}
func doAddFlycast(ctx context.Context) error {
machines, err := mach.ListActive(ctx)
if err != nil {
return fmt.Errorf("machines could not be retrieved %w", err)
}
var bouncerPort int = 5432
var pgPort int = 5433
for _, machine := range machines {
for _, service := range machine.Config.Services {
if service.InternalPort == 5432 || service.InternalPort == 5433 {
return fmt.Errorf("failed to enable flycast for pg machine %s because a service already exists on the postgres port(s)", machine.ID)
}
}
message := "This will overwrite existing services you have manually added. Continue?"
confirm, err := prompt.Confirm(ctx, message)
if err != nil {
return err
}
if !confirm {
return nil
}
conf := machine.Config
conf.Services = []fly.MachineService{
{
Protocol: "tcp",
InternalPort: bouncerPort,
Ports: []fly.MachinePort{
{
Port: &bouncerPort,
Handlers: []string{
"pg_tls",
},
ForceHTTPS: false,
},
},
Concurrency: nil,
},
{
Protocol: "tcp",
InternalPort: pgPort,
Ports: []fly.MachinePort{
{
Port: &pgPort,
Handlers: []string{
"pg_tls",
},
ForceHTTPS: false,
},
},
Concurrency: nil,
},
}
err = mach.Update(ctx, machine, &fly.LaunchMachineInput{
Config: conf,
})
if err != nil {
return err
}
}
return nil
}