Skip to content

Commit

Permalink
launch: add scaffolding for the new Launch command, untested
Browse files Browse the repository at this point in the history
  • Loading branch information
alichay committed Jul 26, 2023
1 parent 903ee7d commit 608f470
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,21 @@ func (mg *MachineGuest) ToSize() string {
}
}

// String returns a string representation of the guest
// Formatted as "[cpu_kind], XGB RAM"
// Returns "" if nil
func (mg *MachineGuest) String() string {
if mg == nil {
return ""
}
size := mg.ToSize()
gbRam := mg.MemoryMB / 1024
if gbRam == 0 {
return fmt.Sprintf("%s, %dMB RAM", size, mg.MemoryMB)
}
return fmt.Sprintf("%s, %dGB RAM", size, gbRam)
}

const (
MIN_MEMORY_MB_PER_SHARED_CPU = 256
MIN_MEMORY_MB_PER_CPU = 2048
Expand Down
11 changes: 11 additions & 0 deletions internal/command/launch/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,23 @@ func New() (cmd *cobra.Command) {
Description: "Set internal_port for all services in the generated fly.toml",
Default: -1,
},
// Launch V2
flag.Bool{
Name: "ui",
Description: "Use the Launch V2 interface",
Hidden: true,
},
)

return
}

func run(ctx context.Context) (err error) {

if flag.GetBool(ctx, "ui") {
return runUi(ctx)
}

io := iostreams.FromContext(ctx)
client := client.FromContext(ctx).API()
workingDir := flag.GetString(ctx, "path")
Expand Down
62 changes: 62 additions & 0 deletions internal/command/launch/v2_cmd_entrypoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package launch

import (
"context"
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/scanner"
)

func familyToAppType(si *scanner.SourceInfo) string {
if si == nil {
return "app"
}
switch si.Family {
case "Dockerfile":
return "app"
case "":
return "app"
}
return fmt.Sprintf("%s app", si.Family)
}

func runUi(ctx context.Context) (err error) {

io := iostreams.FromContext(ctx)

// TODO: Metrics

plan, srcInfo, err := v2BuildPlan(ctx)
if err != nil {
return err
}

fmt.Fprintf(io.Out, "We're about to launch your %s on Fly.io. Here's what you're getting:\n\n", familyToAppType(srcInfo))
fmt.Fprintln(io.Out, plan.Summary())

confirm := false
prompt := &survey.Confirm{
Message: "Do you want to tweak these settings before proceeding?",
}
err = survey.AskOne(prompt, &confirm)
if err != nil {
// TODO(allison): This should probably not just return the error
return err
}

if confirm {
plan, err = v2TweakPlan(ctx, plan)
if err != nil {
return err
}
}

err = v2Launch(ctx, plan, srcInfo)
if err != nil {
return err
}

return nil
}
11 changes: 11 additions & 0 deletions internal/command/launch/v2_launch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package launch

import (
"context"

"github.com/superfly/flyctl/scanner"
)

func v2Launch(ctx context.Context, plan *launchPlan, srcInfo *scanner.SourceInfo) error {
return nil
}
89 changes: 89 additions & 0 deletions internal/command/launch/v2_plan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package launch

import (
"fmt"
"strings"

"github.com/superfly/flyctl/api"
)

type launchPlan struct {
AppName string
appNameSource string

Region *api.Region
regionSource string

Org *api.Organization
orgSource string

Guest *api.MachineGuest
guestSource string

Postgres *postgresPlan
postgresSource string

Redis *redisPlan
redisSource string

Env map[string]string
}

func (p *launchPlan) Summary() string {

guest := p.Guest
if guest == nil {
guest = api.MachinePresets["shared-cpu-1x"]
}

rows := [][]string{
{"Organization", p.Org.Name, p.orgSource},
{"Name", p.AppName, p.appNameSource},
{"Region", p.Region.Name, p.regionSource},
{"App Machines", guest.String(), p.guestSource},
{"Postgres", p.Postgres.String(), p.postgresSource},
{"Redis", p.Redis.String(), p.redisSource},
}

colLengths := []int{0, 0, 0}
for _, row := range rows {
for i, col := range row {
if len(col) > colLengths[i] {
colLengths[i] = len(col)
}
}
}

ret := ""
for _, row := range rows {

label := row[0]
value := row[1]
source := row[2]

labelSpaces := strings.Repeat(" ", colLengths[0]-len(label))
valueSpaces := strings.Repeat(" ", colLengths[1]-len(value))

ret += fmt.Sprintf("%s: %s%s %s(%s)\n", label, labelSpaces, value, valueSpaces, source)
}
return ret
}

// TODO
type postgresPlan struct{}

func (p *postgresPlan) String() string {
if p == nil {
return "<none>"
}
return "unimplemented"
}

type redisPlan struct{}

func (p *redisPlan) String() string {
if p == nil {
return "<none>"
}
return "unimplemented"
}
Loading

0 comments on commit 608f470

Please sign in to comment.