Skip to content

Commit

Permalink
Add a new command to provision a Sentry project for an app
Browse files Browse the repository at this point in the history
  • Loading branch information
jsierles committed Apr 20, 2023
1 parent 67e1f89 commit aa80314
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gql/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package gql
import "github.com/superfly/flyctl/api"

// AppForFlaps converts the genqclient AppFragment to an AppCompact suitable for flaps, which only needs two fields
func AppForFlaps(app AppData) *api.AppCompact {
func ToAppCompact(app AppData) *api.AppCompact {
return &api.AppCompact{
Name: app.Name,
Organization: &api.OrganizationBasic{
Expand Down
22 changes: 22 additions & 0 deletions internal/command/extensions/extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Package logs implements the logs command chain.
package extensions

import (
"github.com/spf13/cobra"

"github.com/superfly/flyctl/internal/command"
)

func New() (cmd *cobra.Command) {
const (
long = `Extensions are additional functionality that can be added to your Fly apps`
)

cmd = command.New("extensions", long, long, nil)
cmd.Aliases = []string{"extensions", "ext"}

cmd.Args = cobra.NoArgs

cmd.AddCommand(newSentry())
return
}
87 changes: 87 additions & 0 deletions internal/command/extensions/sentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package extensions

import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/secrets"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)

func newSentry() (cmd *cobra.Command) {

const (
short = "Setup a Sentry project for this app"
long = short + "\n"
)

cmd = command.New("sentry", short, long, runSentry, command.RequireSession, command.RequireAppName)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
)
return cmd
}

func runSentry(ctx context.Context) (err error) {
client := client.FromContext(ctx).API().GenqClient
io := iostreams.FromContext(ctx)
appName := appconfig.NameFromContext(ctx)

if err != nil {
return err
}

// Fetch the target organization from the app
appResponse, err := gql.GetApp(ctx, client, appName)

if err != nil {
return err
}

targetApp := appResponse.App.AppData
targetOrg := targetApp.Organization

if err != nil {
return err
}

// Fetch or create the Logtail integration for the app

_, err = gql.GetAddOn(ctx, client, appName)

if err != nil {

input := gql.CreateAddOnInput{
OrganizationId: targetOrg.Id,
Name: appName,
AppId: targetApp.Id,
Type: "sentry",
}

createAddOnResponse, err := gql.CreateAddOn(ctx, client, input)

if err != nil {
return err
}

dsn := createAddOnResponse.CreateAddOn.AddOn.Token

fmt.Fprintln(io.Out, "A Sentry project was created. Now setting the SENTRY_DSN secret and deploying.")
secrets.SetSecretsAndDeploy(ctx, gql.ToAppCompact(targetApp), map[string]string{
"SENTRY_DSN": dsn,
}, false, false)

return nil
} else {
fmt.Fprintln(io.Out, "A Sentry project already exists for this app")
}

return
}
2 changes: 1 addition & 1 deletion internal/command/logs/ship.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func EnsureShipperMachine(ctx context.Context, targetOrg gql.AppDataOrganization
return nil, nil, err
}

flapsClient, err = flaps.New(ctx, gql.AppForFlaps(shipperApp))
flapsClient, err = flaps.New(ctx, gql.ToAppCompact(shipperApp))

if err != nil {
return
Expand Down
2 changes: 2 additions & 0 deletions internal/command/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/superfly/flyctl/internal/command/dig"
"github.com/superfly/flyctl/internal/command/docs"
"github.com/superfly/flyctl/internal/command/doctor"
"github.com/superfly/flyctl/internal/command/extensions"
"github.com/superfly/flyctl/internal/command/help"
"github.com/superfly/flyctl/internal/command/history"
"github.com/superfly/flyctl/internal/command/image"
Expand Down Expand Up @@ -172,6 +173,7 @@ func New() *cobra.Command {
scale.New(),
migrate_to_v2.New(),
tokens.New(),
extensions.New(),
}

// if os.Getenv("DEV") != "" {
Expand Down
7 changes: 1 addition & 6 deletions internal/command/secrets/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,5 @@ func runImport(ctx context.Context) (err error) {
return errors.New("requires at least one SECRET=VALUE pair")
}

release, err := client.SetSecrets(ctx, appName, secrets)
if err != nil {
return err
}

return deployForSecrets(ctx, app, release)
return SetSecretsAndDeploy(ctx, app, secrets, flag.GetBool(ctx, "stage"), flag.GetBool(ctx, "detach"))
}
6 changes: 3 additions & 3 deletions internal/command/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func New() *cobra.Command {
return secrets
}

func deployForSecrets(ctx context.Context, app *api.AppCompact, release *api.Release) (err error) {
func deployForSecrets(ctx context.Context, app *api.AppCompact, release *api.Release, stage bool, detach bool) (err error) {
out := iostreams.FromContext(ctx).Out

if flag.GetBool(ctx, "stage") {
if stage {

if app.PlatformVersion != "machines" {
return errors.New("--stage isn't available for Nomad apps")
Expand Down Expand Up @@ -89,7 +89,7 @@ func deployForSecrets(ctx context.Context, app *api.AppCompact, release *api.Rel
md, err := deploy.NewMachineDeployment(ctx, deploy.MachineDeploymentArgs{
AppCompact: app,
RestartOnly: true,
SkipHealthChecks: flag.GetBool(ctx, "detach"),
SkipHealthChecks: detach,
})
if err != nil {
sentry.CaptureExceptionWithAppInfo(err, "secrets", app)
Expand Down
10 changes: 8 additions & 2 deletions internal/command/secrets/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/appconfig"
Expand Down Expand Up @@ -62,10 +63,15 @@ func runSet(ctx context.Context) (err error) {
return errors.New("requires at least one SECRET=VALUE pair")
}

release, err := client.SetSecrets(ctx, appName, secrets)
return SetSecretsAndDeploy(ctx, app, secrets, flag.GetBool(ctx, "stage"), flag.GetBool(ctx, "detach"))
}

func SetSecretsAndDeploy(ctx context.Context, app *api.AppCompact, secrets map[string]string, stage bool, detach bool) error {
client := client.FromContext(ctx).API()
release, err := client.SetSecrets(ctx, app.Name, secrets)
if err != nil {
return err
}

return deployForSecrets(ctx, app, release)
return deployForSecrets(ctx, app, release, stage, detach)
}
10 changes: 8 additions & 2 deletions internal/command/secrets/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"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"
Expand Down Expand Up @@ -36,10 +37,15 @@ func runUnset(ctx context.Context) (err error) {
return err
}

release, err := client.UnsetSecrets(ctx, appName, flag.Args(ctx))
return UnsetSecretsAndDeploy(ctx, app, flag.Args(ctx), flag.GetBool(ctx, "stage"), flag.GetBool(ctx, "detach"))
}

func UnsetSecretsAndDeploy(ctx context.Context, app *api.AppCompact, secrets []string, stage bool, detach bool) error {
client := client.FromContext(ctx).API()
release, err := client.UnsetSecrets(ctx, app.Name, secrets)
if err != nil {
return err
}

return deployForSecrets(ctx, app, release)
return deployForSecrets(ctx, app, release, stage, detach)
}

0 comments on commit aa80314

Please sign in to comment.