-
Notifications
You must be signed in to change notification settings - Fork 224
/
add_flycast.go
149 lines (128 loc) · 3.04 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
137
138
139
140
141
142
143
144
145
146
147
148
149
package postgres
import (
"context"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"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/iostreams"
)
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 = client.FromContext(ctx).API()
appName = appconfig.NameFromContext(ctx)
io = iostreams.FromContext(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
}
switch app.PlatformVersion {
case "machines":
if err := doAddFlycast(ctx); err != nil {
return err
}
fmt.Fprintln(io.Out, "Flycast added!")
case "nomad":
return fmt.Errorf("not supported on nomad")
default:
return fmt.Errorf("unknown platform version")
}
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)
}
}
confirm := false
prompt := &survey.Confirm{
Message: "This will overwrite existing services you have manually added. Continue?",
Default: true,
}
if err := survey.AskOne(prompt, &confirm); err != nil {
return err
}
if !confirm {
return nil
}
conf := machine.Config
conf.Services =
[]api.MachineService{
{
Protocol: "tcp",
InternalPort: bouncerPort,
Ports: []api.MachinePort{
{
Port: &bouncerPort,
Handlers: []string{
"pg_tls",
},
ForceHTTPS: false,
},
},
Concurrency: nil,
},
{
Protocol: "tcp",
InternalPort: pgPort,
Ports: []api.MachinePort{
{
Port: &pgPort,
Handlers: []string{
"pg_tls",
},
ForceHTTPS: false,
},
},
Concurrency: nil,
},
}
err = mach.Update(ctx, machine, &api.LaunchMachineInput{
Config: conf,
})
if err != nil {
return err
}
}
return nil
}