Skip to content

Commit 0493f22

Browse files
less graphql on launch/deploy (#4656)
1 parent 4cb2b03 commit 0493f22

File tree

171 files changed

+2298
-1305
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+2298
-1305
lines changed

agent/remote.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"fmt"
66

77
"github.com/pkg/errors"
8-
"github.com/superfly/fly-go"
8+
"github.com/superfly/fly-go/flaps"
99
"github.com/superfly/flyctl/internal/flyutil"
1010
"github.com/superfly/flyctl/iostreams"
1111
)
1212

13-
func BringUpAgent(ctx context.Context, client flyutil.Client, app *fly.AppCompact, network string, quiet bool) (*Client, Dialer, error) {
13+
func BringUpAgent(ctx context.Context, client flyutil.Client, app *flaps.App, quiet bool) (*Client, Dialer, error) {
1414
io := iostreams.FromContext(ctx)
1515

1616
agentclient, err := Establish(ctx, client)
@@ -21,7 +21,7 @@ func BringUpAgent(ctx context.Context, client flyutil.Client, app *fly.AppCompac
2121
return nil, nil, errors.Wrap(err, "can't establish agent")
2222
}
2323

24-
dialer, err := agentclient.Dialer(ctx, slug, network)
24+
dialer, err := agentclient.Dialer(ctx, slug, app.Network)
2525
if err != nil {
2626
captureError(ctx, err, "agent-remote", slug, name)
2727
return nil, nil, fmt.Errorf("ssh: can't build tunnel for %s: %s\n", slug, err)
@@ -30,7 +30,7 @@ func BringUpAgent(ctx context.Context, client flyutil.Client, app *fly.AppCompac
3030
if !quiet {
3131
io.StartProgressIndicatorMsg("Connecting to tunnel")
3232
}
33-
if err := agentclient.WaitForTunnel(ctx, slug, network); err != nil {
33+
if err := agentclient.WaitForTunnel(ctx, slug, app.Network); err != nil {
3434
captureError(ctx, err, "agent-remote", slug, name)
3535
return nil, nil, errors.Wrapf(err, "tunnel unavailable")
3636
}

agent/server/server.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"github.com/superfly/flyctl/internal/flyutil"
2222
"github.com/superfly/flyctl/internal/metrics/synthetics"
2323
"github.com/superfly/flyctl/internal/sentry"
24+
"github.com/superfly/flyctl/internal/uiex"
25+
"github.com/superfly/flyctl/internal/uiexutil"
2426
"github.com/superfly/flyctl/internal/wireguard"
2527
"github.com/superfly/flyctl/wg"
2628
"golang.org/x/sync/errgroup"
@@ -227,11 +229,16 @@ func (s *server) checkForConfigChange() (err error) {
227229
return
228230
}
229231

230-
func (s *server) buildTunnel(ctx context.Context, org *fly.Organization, reestablish bool, network string, client flyutil.Client) (tunnel *wg.Tunnel, err error) {
232+
func (s *server) buildTunnel(ctx context.Context, org *uiex.Organization, reestablish bool, network string, client flyutil.Client) (tunnel *wg.Tunnel, err error) {
231233
s.mu.Lock()
232234
defer s.mu.Unlock()
233235

234-
tk := tunnelKey{orgSlug: org.Slug, networkName: network}
236+
// todo: handle this correctly everywhere
237+
if network == "default" {
238+
network = ""
239+
}
240+
241+
tk := tunnelKey{orgSlug: org.RawSlug, networkName: network}
235242

236243
// not checking the region is intentional, it's static during the lifetime of the agent
237244
if tunnel = s.tunnels[tk]; tunnel != nil && !reestablish {
@@ -308,6 +315,14 @@ func (s *server) tunnelFor(slug, network string) *wg.Tunnel {
308315
s.mu.Lock()
309316
defer s.mu.Unlock()
310317

318+
// todo: handle this correctly everywhere
319+
if slug == "personal" {
320+
panic("personal slug should not be found here")
321+
}
322+
if network == "default" {
323+
network = ""
324+
}
325+
311326
tk := tunnelKey{orgSlug: slug, networkName: network}
312327

313328
return s.tunnels[tk]
@@ -406,6 +421,17 @@ func (s *server) GetClient(ctx context.Context) flyutil.Client {
406421
return flyutil.NewClientFromOptions(ctx, fly.ClientOptions{Tokens: s.tokens})
407422
}
408423

424+
func (s *server) GetUiExClient(ctx context.Context) uiexutil.Client {
425+
s.mu.Lock()
426+
defer s.mu.Unlock()
427+
428+
client, err := uiexutil.NewClientWithOptions(ctx, uiex.NewClientOpts{Tokens: s.tokens})
429+
if err != nil {
430+
s.Logger.Fatal(err)
431+
}
432+
return client
433+
}
434+
409435
// UpdateTokensFromClient replaces the server's tokens with those from the
410436
// client if the new ones seem better. Specifically, if the agent was started
411437
// with `FLY_API_TOKEN`, but a later client is using tokens form a config file.

agent/server/session.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/superfly/flyctl/internal/buildinfo"
2929
"github.com/superfly/flyctl/internal/config"
3030
"github.com/superfly/flyctl/internal/flyutil"
31+
"github.com/superfly/flyctl/internal/uiex"
32+
"github.com/superfly/flyctl/internal/uiexutil"
3133
)
3234

3335
type id uint64
@@ -200,14 +202,14 @@ func (s *session) reestablish(ctx context.Context, args ...string) {
200202

201203
var errNoSuchOrg = errors.New("no such organization")
202204

203-
func (s *session) fetchOrg(ctx context.Context, slug string) (*fly.Organization, error) {
204-
orgs, err := s.getClient(ctx).GetOrganizations(ctx)
205+
func (s *session) fetchOrg(ctx context.Context, slug string) (*uiex.Organization, error) {
206+
orgs, err := s.getUiExClient(ctx).ListOrganizations(ctx, false)
205207
if err != nil {
206208
return nil, err
207209
}
208210

209211
for _, org := range orgs {
210-
if org.Slug == slug {
212+
if org.RawSlug == slug {
211213
no := org // copy
212214
return &no, nil
213215
}
@@ -587,6 +589,18 @@ func (s *session) getClient(ctx context.Context) flyutil.Client {
587589
return flyutil.NewClientFromOptions(ctx, fly.ClientOptions{Tokens: s.tokens})
588590
}
589591

592+
func (s *session) getUiExClient(ctx context.Context) uiexutil.Client {
593+
if s.tokens == nil {
594+
return s.srv.GetUiExClient(ctx)
595+
}
596+
597+
client, err := uiexutil.NewClientWithOptions(ctx, uiex.NewClientOpts{Tokens: s.tokens})
598+
if err != nil {
599+
s.logger.Fatal(err)
600+
}
601+
return client
602+
}
603+
590604
func (s *session) error(err error) bool {
591605
return s.reply("err", err.Error())
592606
}

flypg/cmd.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"errors"
88
"fmt"
99

10-
fly "github.com/superfly/fly-go"
10+
"github.com/superfly/fly-go/flaps"
1111
"github.com/superfly/flyctl/agent"
1212
"github.com/superfly/flyctl/internal/command/ssh"
1313
"github.com/superfly/flyctl/internal/flag"
@@ -27,14 +27,20 @@ type commandResponse struct {
2727

2828
type Command struct {
2929
ctx context.Context
30-
app *fly.AppCompact
30+
app *flaps.App
3131
dialer agent.Dialer
3232
io *iostreams.IOStreams
33+
orgID string
3334
}
3435

35-
func NewCommand(ctx context.Context, app *fly.AppCompact) (*Command, error) {
36+
func NewCommand(ctx context.Context, app *flaps.App) (*Command, error) {
3637
client := flyutil.ClientFromContext(ctx)
3738

39+
org, err := client.GetOrganizationByApp(ctx, app.Name)
40+
if err != nil {
41+
return nil, fmt.Errorf("error getting organization: %w", err)
42+
}
43+
3844
agentclient, err := agent.Establish(ctx, client)
3945
if err != nil {
4046
return nil, fmt.Errorf("error establishing agent: %w", err)
@@ -48,6 +54,7 @@ func NewCommand(ctx context.Context, app *fly.AppCompact) (*Command, error) {
4854
return &Command{
4955
ctx: ctx,
5056
app: app,
57+
orgID: org.ID,
5158
dialer: dialer,
5259
io: iostreams.FromContext(ctx),
5360
}, nil
@@ -63,7 +70,7 @@ func (pc *Command) UpdateSettings(ctx context.Context, leaderIp string, config m
6370
subCmd := fmt.Sprintf("update --patch '%s'", string(configBytes))
6471
cmd := fmt.Sprintf("stolonctl-run %s", encodeCommand(subCmd))
6572

66-
resp, err := ssh.RunSSHCommand(ctx, pc.app, pc.dialer, leaderIp, cmd, ssh.DefaultSshUsername)
73+
resp, err := ssh.RunSSHCommand(ctx, pc.app, pc.dialer, leaderIp, cmd, ssh.DefaultSshUsername, pc.orgID)
6774
if err != nil {
6875
return err
6976
}
@@ -84,7 +91,7 @@ func (pc *Command) UnregisterMember(ctx context.Context, leaderIP string, standb
8491
payload := encodeCommand(standbyNodeName)
8592
cmd := fmt.Sprintf("pg_unregister %s", payload)
8693

87-
resp, err := ssh.RunSSHCommand(ctx, pc.app, pc.dialer, leaderIP, cmd, ssh.DefaultSshUsername)
94+
resp, err := ssh.RunSSHCommand(ctx, pc.app, pc.dialer, leaderIP, cmd, ssh.DefaultSshUsername, pc.orgID)
8895
if err != nil {
8996
return err
9097
}
@@ -110,7 +117,7 @@ func (pc *Command) ListEvents(ctx context.Context, leaderIP string, flagsName []
110117
cmd += fmt.Sprintf("--%s %s ", flagName, flag.GetString(ctx, flagName))
111118
}
112119

113-
resp, err := ssh.RunSSHCommand(ctx, pc.app, pc.dialer, leaderIP, cmd, ssh.DefaultSshUsername)
120+
resp, err := ssh.RunSSHCommand(ctx, pc.app, pc.dialer, leaderIP, cmd, ssh.DefaultSshUsername, pc.orgID)
114121
if err != nil {
115122
return err
116123
}

flypg/launcher.go

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/superfly/flyctl/internal/flapsutil"
1616
"github.com/superfly/flyctl/internal/flyutil"
1717
mach "github.com/superfly/flyctl/internal/machine"
18+
"github.com/superfly/flyctl/internal/uiex"
1819
"github.com/superfly/flyctl/internal/watch"
1920

2021
"github.com/superfly/fly-go/flaps"
@@ -38,15 +39,16 @@ const (
3839
)
3940

4041
type Launcher struct {
41-
client flyutil.Client
42+
client flyutil.Client
43+
flapsClient flapsutil.FlapsClient
4244
}
4345

4446
type CreateClusterInput struct {
4547
AppName string
4648
ConsulURL string
4749
ImageRef string
4850
InitialClusterSize int
49-
Organization *fly.Organization
51+
Organization *uiex.Organization
5052
Password string
5153
Region string
5254
VolumeSize *int
@@ -66,9 +68,10 @@ type CreateClusterInput struct {
6668
RestoreTargetInclusive bool
6769
}
6870

69-
func NewLauncher(client flyutil.Client) *Launcher {
71+
func NewLauncher(client flyutil.Client, flapsClient flapsutil.FlapsClient) *Launcher {
7072
return &Launcher{
71-
client: client,
73+
client: client,
74+
flapsClient: flapsClient,
7275
}
7376
}
7477

@@ -91,10 +94,17 @@ func (l *Launcher) LaunchMachinesPostgres(ctx context.Context, config *CreateClu
9194
config.Autostart = true
9295
}
9396

94-
app, err := l.createApp(ctx, config)
97+
fmt.Println("Creating app...")
98+
99+
app, err := l.flapsClient.CreateApp(ctx, flaps.CreateAppRequest{
100+
Name: config.AppName,
101+
Org: config.Organization.RawSlug,
102+
AppRoleID: "postgres_cluster",
103+
})
95104
if err != nil {
96105
return err
97106
}
107+
98108
// In case the user hasn't specified a name, use the app name generated by the API
99109
config.AppName = app.Name
100110

@@ -112,10 +122,13 @@ func (l *Launcher) LaunchMachinesPostgres(ctx context.Context, config *CreateClu
112122
config.ImageRef = imageRef
113123
}
114124

115-
var addr *fly.IPAddress
125+
var addr *flaps.IPAssignment
116126

117127
if config.Manager == ReplicationManager {
118-
addr, err = l.client.AllocateIPAddress(ctx, config.AppName, "private_v6", config.Region, config.Organization, "")
128+
addr, err = l.flapsClient.AssignIP(ctx, config.AppName, flaps.AssignIPRequest{
129+
Type: "private_v6",
130+
Organization: config.Organization.RawSlug,
131+
})
119132
if err != nil {
120133
return err
121134
}
@@ -130,8 +143,8 @@ func (l *Launcher) LaunchMachinesPostgres(ctx context.Context, config *CreateClu
130143
}
131144

132145
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
133-
AppCompact: app,
134-
AppName: app.Name,
146+
AppData: app,
147+
AppName: app.Name,
135148
})
136149
if err != nil {
137150
return err
@@ -297,7 +310,7 @@ func (l *Launcher) LaunchMachinesPostgres(ctx context.Context, config *CreateClu
297310
fmt.Fprintf(io.Out, " Password: %s\n", secrets["OPERATOR_PASSWORD"])
298311
fmt.Fprintf(io.Out, " Hostname: %s.internal\n", config.AppName)
299312
if addr != nil {
300-
fmt.Fprintf(io.Out, " Flycast: %s\n", addr.Address)
313+
fmt.Fprintf(io.Out, " Flycast: %s\n", addr.IP)
301314
}
302315
fmt.Fprintf(io.Out, " Proxy port: 5432\n")
303316
fmt.Fprintf(io.Out, " Postgres port: 5433\n")
@@ -391,41 +404,6 @@ func (l *Launcher) getPostgresConfig(config *CreateClusterInput) *fly.MachineCon
391404
return &machineConfig
392405
}
393406

394-
func (l *Launcher) createApp(ctx context.Context, config *CreateClusterInput) (*fly.AppCompact, error) {
395-
fmt.Println("Creating app...")
396-
appInput := fly.CreateAppInput{
397-
OrganizationID: config.Organization.ID,
398-
Name: config.AppName,
399-
PreferredRegion: &config.Region,
400-
AppRoleID: "postgres_cluster",
401-
}
402-
403-
app, err := l.client.CreateApp(ctx, appInput)
404-
if err != nil {
405-
return nil, err
406-
}
407-
408-
f, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{AppName: app.Name})
409-
if err != nil {
410-
return nil, err
411-
} else if err := f.WaitForApp(ctx, app.Name); err != nil {
412-
return nil, err
413-
}
414-
415-
return &fly.AppCompact{
416-
ID: app.ID,
417-
Name: app.Name,
418-
Status: app.Status,
419-
Deployed: app.Deployed,
420-
Hostname: app.Hostname,
421-
AppURL: app.AppURL,
422-
Organization: &fly.OrganizationBasic{
423-
ID: app.Organization.ID,
424-
Slug: app.Organization.Slug,
425-
},
426-
}, nil
427-
}
428-
429407
func (l *Launcher) setSecrets(ctx context.Context, config *CreateClusterInput) (map[string]string, error) {
430408
flapsClient := flapsutil.ClientFromContext(ctx)
431409
if flapsClient == nil {
@@ -475,7 +453,7 @@ func (l *Launcher) setSecrets(ctx context.Context, config *CreateClusterInput) (
475453
validHours := 876600
476454

477455
app := fly.App{Name: config.AppName}
478-
cert, err := l.client.IssueSSHCertificate(ctx, config.Organization, []string{"root", "fly", "postgres"}, []string{app.Name}, &validHours, pub)
456+
cert, err := l.client.IssueSSHCertificate(ctx, config.Organization.ID, []string{"root", "fly", "postgres"}, []string{app.Name}, &validHours, pub)
479457
if err != nil {
480458
return nil, err
481459
}

flypg/tigris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func CreateTigrisBucket(ctx context.Context, config *CreateClusterInput) error {
3232
name := config.AppName + "-postgres"
3333
params := extensions_core.ExtensionParams{
3434
AppName: config.AppName,
35-
Organization: config.Organization,
35+
OrgSlug: config.Organization.RawSlug,
3636
Provider: "tigris",
3737
OverrideName: &name,
3838
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ require (
7474
github.com/spf13/pflag v1.0.9
7575
github.com/spf13/viper v1.20.1
7676
github.com/stretchr/testify v1.11.1
77-
github.com/superfly/fly-go v0.1.66
77+
github.com/superfly/fly-go v0.1.67
7878
github.com/superfly/graphql v0.2.6
7979
github.com/superfly/lfsc-go v0.1.1
8080
github.com/superfly/macaroon v0.3.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
635635
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
636636
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
637637
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
638-
github.com/superfly/fly-go v0.1.66 h1:3Kw2fF7y14UXgZ1uhNwSgDICI7JSiufqqC5++60wiu0=
639-
github.com/superfly/fly-go v0.1.66/go.mod h1:wpq4XNor10w9KurA15CBYRnhtT2mnemAXYHuqkhp2vI=
638+
github.com/superfly/fly-go v0.1.67 h1:MM2hSPsUenjUgod2/q2xfJg5idNYwCoI5/BWxLTRJys=
639+
github.com/superfly/fly-go v0.1.67/go.mod h1:2gCFoNR3iUELADGTJtbBoviMa2jlh2vlPK3cKUajOp8=
640640
github.com/superfly/graphql v0.2.6 h1:zppbodNerWecoXEdjkhrqaNaSjGqobhXNlViHFuZzb4=
641641
github.com/superfly/graphql v0.2.6/go.mod h1:CVfDl31srm8HnJ9udwLu6hFNUW/P6GUM2dKcG1YQ8jc=
642642
github.com/superfly/lfsc-go v0.1.1 h1:dGjLgt81D09cG+aR9lJZIdmonjZSR5zYCi7s54+ZU2Q=

0 commit comments

Comments
 (0)