-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
459 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package mpg | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/superfly/flyctl/iostreams" | ||
"github.com/superfly/flyctl/proxy" | ||
|
||
"github.com/superfly/flyctl/internal/command" | ||
"github.com/superfly/flyctl/internal/flag" | ||
) | ||
|
||
func newConnect() (cmd *cobra.Command) { | ||
const ( | ||
long = `Connect to a Redis database using psql` | ||
|
||
short = long | ||
usage = "connect" | ||
) | ||
|
||
cmd = command.New(usage, short, long, runConnect, command.RequireSession, command.RequireUiex) | ||
|
||
flag.Add(cmd, | ||
flag.Org(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func runConnect(ctx context.Context) (err error) { | ||
io := iostreams.FromContext(ctx) | ||
|
||
localProxyPort := "16380" | ||
|
||
cluster, params, password, err := getMpgProxyParams(ctx, localProxyPort) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
psqlPath, err := exec.LookPath("psql") | ||
if err != nil { | ||
fmt.Fprintf(io.Out, "Could not find psql in your $PATH. Install it or point your psql at: %s", "someurl") | ||
return | ||
} | ||
|
||
err = proxy.Start(ctx, params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
name := fmt.Sprintf("pgdb-%s", cluster.Id) | ||
connectUrl := fmt.Sprintf("postgres://%s:%s@localhost:%s/%s?sslmode=require", name, password, localProxyPort, name) | ||
cmd := exec.CommandContext(ctx, psqlPath, connectUrl) | ||
cmd.Stdout = io.Out | ||
cmd.Stderr = io.ErrOut | ||
cmd.Stdin = io.In | ||
|
||
cmd.Start() | ||
cmd.Wait() | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mpg | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/superfly/flyctl/internal/command" | ||
) | ||
|
||
func New() *cobra.Command { | ||
const ( | ||
short = `Manage Managed Postgres clusters.` | ||
|
||
long = short + "\n" | ||
) | ||
|
||
cmd := command.New("managed-postgres", short, long, nil) | ||
|
||
cmd.Aliases = []string{"mpg"} | ||
|
||
cmd.AddCommand( | ||
newProxy(), | ||
newConnect(), | ||
) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package mpg | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/superfly/flyctl/agent" | ||
"github.com/superfly/flyctl/gql" | ||
"github.com/superfly/flyctl/internal/command" | ||
"github.com/superfly/flyctl/internal/command/orgs" | ||
"github.com/superfly/flyctl/internal/flag" | ||
"github.com/superfly/flyctl/internal/flyutil" | ||
"github.com/superfly/flyctl/internal/prompt" | ||
"github.com/superfly/flyctl/internal/uiex" | ||
"github.com/superfly/flyctl/internal/uiexutil" | ||
"github.com/superfly/flyctl/proxy" | ||
"github.com/superfly/flyctl/terminal" | ||
) | ||
|
||
func newProxy() (cmd *cobra.Command) { | ||
const ( | ||
long = `Proxy to a MPG database` | ||
|
||
short = long | ||
usage = "proxy" | ||
) | ||
|
||
cmd = command.New(usage, short, long, runProxy, command.RequireSession, command.RequireUiex) | ||
|
||
flag.Add(cmd, | ||
flag.Org(), | ||
flag.Region(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func runProxy(ctx context.Context) (err error) { | ||
|
||
localProxyPort := "16380" | ||
_, params, password, err := getMpgProxyParams(ctx, localProxyPort) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
terminal.Infof("Proxying postgres to port \"%s\" with password \"%s\"", localProxyPort, password) | ||
|
||
return proxy.Connect(ctx, params) | ||
} | ||
|
||
func getMpgProxyParams(ctx context.Context, localProxyPort string) (*uiex.ManagedCluster, *proxy.ConnectParams, string, error) { | ||
// This `org.Slug` could be "personal" and we need that for wireguard connections | ||
org, err := orgs.OrgFromFlagOrSelect(ctx) | ||
if err != nil { | ||
return nil, nil, "", err | ||
} | ||
|
||
client := flyutil.ClientFromContext(ctx) | ||
genqClient := flyutil.ClientFromContext(ctx).GenqClient() | ||
|
||
// For ui-ex request we need the real org slug | ||
var fullOrg *gql.GetOrganizationResponse | ||
if fullOrg, err = gql.GetOrganization(ctx, genqClient, org.Slug); err != nil { | ||
err = fmt.Errorf("failed fetching org: %w", err) | ||
return nil, nil, "", err | ||
} | ||
|
||
uiexClient := uiexutil.ClientFromContext(ctx) | ||
|
||
var index int | ||
var options []string | ||
|
||
clustersResponse, err := uiexClient.ListManagedClusters(ctx, fullOrg.Organization.RawSlug) | ||
if err != nil { | ||
return nil, nil, "", err | ||
} | ||
|
||
if len(clustersResponse.Data) == 0 { | ||
err := fmt.Errorf("No Managed Postgres clusters found on this organization") | ||
return nil, nil, "", err | ||
} | ||
|
||
for _, cluster := range clustersResponse.Data { | ||
options = append(options, fmt.Sprintf("%s (%s)", cluster.Name, cluster.Region)) | ||
} | ||
|
||
selectErr := prompt.Select(ctx, &index, "Select a database to connect to", "", options...) | ||
if selectErr != nil { | ||
return nil, nil, "", selectErr | ||
} | ||
|
||
selectedCluster := clustersResponse.Data[index] | ||
|
||
response, err := uiexClient.GetManagedCluster(ctx, selectedCluster.Organization.Slug, selectedCluster.Id) | ||
if err != nil { | ||
return nil, nil, "", err | ||
} | ||
cluster := response.Data | ||
|
||
if response.Password.Status == "initializing" { | ||
return nil, nil, "", fmt.Errorf("Cluster is still initializing, wait a bit more") | ||
} | ||
|
||
if response.Password.Status == "error" { | ||
return nil, nil, "", fmt.Errorf("Error getting cluster password") | ||
} | ||
|
||
agentclient, err := agent.Establish(ctx, client) | ||
if err != nil { | ||
return nil, nil, "", err | ||
} | ||
|
||
dialer, err := agentclient.ConnectToTunnel(ctx, org.Slug, "", false) | ||
if err != nil { | ||
return nil, nil, "", err | ||
} | ||
|
||
return &cluster, &proxy.ConnectParams{ | ||
Ports: []string{localProxyPort, "5432"}, | ||
OrganizationSlug: org.Slug, | ||
Dialer: dialer, | ||
RemoteHost: cluster.IpAssignments.Direct, | ||
}, response.Password.Value, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.