Skip to content

Commit

Permalink
Merge aede710 into ec683eb
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn authored Nov 5, 2024
2 parents ec683eb + aede710 commit 3a4f888
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 33 deletions.
18 changes: 13 additions & 5 deletions deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
Dir.chdir("/usr/src/app")

DEPLOY_NOW = !get_env("DEPLOY_NOW").nil?
DEPLOY_CUSTOMIZE = !get_env("NO_DEPLOY_CUSTOMIZE")
DEPLOY_CUSTOMIZE_PATH = get_env("DEPLOY_CUSTOMIZE_PATH")
DEPLOY_CUSTOMIZE = !get_env("NO_DEPLOY_CUSTOMIZE") || !DEPLOY_CUSTOMIZE_PATH.nil?
DEPLOY_ONLY = !get_env("DEPLOY_ONLY").nil?
CREATE_AND_PUSH_BRANCH = !get_env("DEPLOY_CREATE_AND_PUSH_BRANCH").nil?
FLYIO_BRANCH_NAME = "flyio-new-files"
Expand Down Expand Up @@ -239,15 +240,22 @@

if DEPLOY_CUSTOMIZE
manifest = in_step Step::CUSTOMIZE do
cmd = "flyctl launch sessions create --session-path /tmp/session.json --manifest-path #{MANIFEST_PATH} --from-manifest #{MANIFEST_PATH}"
if DEPLOY_CUSTOMIZE_PATH.nil?
cmd = "flyctl launch sessions create --session-path /tmp/session.json --manifest-path #{MANIFEST_PATH} --from-manifest #{MANIFEST_PATH}"

exec_capture(cmd)
session = JSON.parse(File.read("/tmp/session.json"))
exec_capture(cmd)
session = JSON.parse(File.read("/tmp/session.json"))

artifact Artifact::SESSION, session
artifact Artifact::SESSION, session
end

cmd = "flyctl launch sessions finalize --session-path /tmp/session.json --manifest-path #{MANIFEST_PATH}"

if !DEPLOY_CUSTOMIZE_PATH.nil?
cmd += " --from-file #{DEPLOY_CUSTOMIZE_PATH}"
artifact Artifact::SESSION, JSON.parse(File.read(DEPLOY_CUSTOMIZE_PATH))
end

exec_capture(cmd)
manifest = JSON.parse(File.read("/tmp/manifest.json"))

Expand Down
18 changes: 18 additions & 0 deletions internal/command/launch/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ func updateConfig(plan *plan.LaunchPlan, env map[string]string, appConfig *appco
appConfig.HTTPService = nil
}
appConfig.Compute = plan.Compute

if plan.CPUKind != "" {
for _, c := range appConfig.Compute {
c.CPUKind = plan.CPUKind
}
}

if plan.CPUs != 0 {
for _, c := range appConfig.Compute {
c.CPUs = plan.CPUs
}
}

if plan.MemoryMB != 0 {
for _, c := range appConfig.Compute {
c.MemoryMB = plan.MemoryMB
}
}
}

// createApp creates the fly.io app for the plan
Expand Down
76 changes: 48 additions & 28 deletions internal/command/launch/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func newSessions() *cobra.Command {
Description: "Path to write the manifest info to",
Default: "manifest.json",
},
flag.String{
Name: "from-file",
Description: "Path to a CLI session JSON file",
Default: "",
},
)

// not that useful anywhere else yet
Expand Down Expand Up @@ -192,14 +197,41 @@ func runSessionFinalize(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
logger := logger.FromContext(ctx)

sessionBytes, err := os.ReadFile(flag.GetString(ctx, "session-path"))
if err != nil {
return err
}
var finalMeta map[string]interface{}

var session fly.CLISession
if err := json.Unmarshal(sessionBytes, &session); err != nil {
return err
if customizePath := flag.GetString(ctx, "from-file"); customizePath != "" {
sessionBytes, err := os.ReadFile(customizePath)
if err != nil {
return err
}

if err := json.Unmarshal(sessionBytes, &finalMeta); err != nil {
return err
}
} else {
sessionBytes, err := os.ReadFile(flag.GetString(ctx, "session-path"))
if err != nil {
return err
}

var session fly.CLISession
if err := json.Unmarshal(sessionBytes, &session); err != nil {
return err
}

// FIXME: better timeout here
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

finalSession, err := waitForCLISession(ctx, logger, io.ErrOut, session.ID)
switch {
case errors.Is(err, context.DeadlineExceeded):
return errors.New("session expired, please try again")
case err != nil:
return err
}

finalMeta = finalSession.Metadata
}

manifestBytes, err := os.ReadFile(flag.GetString(ctx, "manifest-path"))
Expand All @@ -219,27 +251,8 @@ func runSessionFinalize(ctx context.Context) (err error) {
warnedNoCcHa: true,
}

// FIXME: better timeout here
ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

finalSession, err := waitForCLISession(ctx, logger, io.ErrOut, session.ID)
switch {
case errors.Is(err, context.DeadlineExceeded):
return errors.New("session expired, please try again")
case err != nil:
return err
}

// Hack because somewhere from between UI and here, the numbers get converted to strings
if err := patchNumbers(finalSession.Metadata, "vm_cpus", "vm_memory"); err != nil {
return err
}

// Wasteful, but gets the job done without uprooting the session types.
// Just round-trip the map[string]interface{} back into json, so we can re-deserialize it into a complete type.
metaJson, err := json.Marshal(finalSession.Metadata)
if err != nil {
if err := patchNumbers(finalMeta, "vm_cpus", "vm_memory"); err != nil {
return err
}

Expand All @@ -254,6 +267,13 @@ func runSessionFinalize(ctx context.Context) (err error) {

oldPlan := helpers.Clone(state.Plan)

// Wasteful, but gets the job done without uprooting the session types.
// Just round-trip the map[string]interface{} back into json, so we can re-deserialize it into a complete type.
metaJson, err := json.Marshal(finalMeta)
if err != nil {
return err
}

err = json.Unmarshal(metaJson, &state.Plan)
if err != nil {
return err
Expand All @@ -262,7 +282,7 @@ func runSessionFinalize(ctx context.Context) (err error) {
// Patch in some fields that we keep in the plan that aren't persisted by the UI.
// Technically, we should probably just be persisting this, but there's
// no clear value to the UI having these fields currently.
if _, ok := finalSession.Metadata["ha"]; !ok {
if _, ok := finalMeta["ha"]; !ok {
state.Plan.HighAvailability = oldPlan.HighAvailability
}
// This should never be changed by the UI!!
Expand Down
29 changes: 29 additions & 0 deletions test/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ func TestLaunchGoFromRepo(t *testing.T) {
require.Contains(t, string(body), "I'm running in the yyz region")
}

func TestLaunchPreCustomized(t *testing.T) {
customize := map[string]interface{}{
"vm_memory": 2048,
}

deploy := testDeployer(t,
createRandomApp,
testlib.WithRegion("yyz"),
testlib.WithPreCustomize(&customize),
testlib.WithouExtensions,
testlib.DeployNow,
testlib.WithGitRepo("https://github.com/fly-apps/go-example"),
)

appName := deploy.Extra["appName"].(string)

manifest, err := deploy.Output().ArtifactManifest()
require.NoError(t, err)
require.NotNil(t, manifest)

require.Equal(t, manifest.Plan.Guest().MemoryMB, 2048)
require.Equal(t, manifest.Config.Compute[0].MemoryMB, 2048)

body, err := testlib.RunHealthCheck(fmt.Sprintf("https://%s.fly.dev", appName))
require.NoError(t, err)

require.Contains(t, string(body), "I'm running in the yyz region")
}

func TestLaunchRails70(t *testing.T) {
deploy := testDeployer(t,
withFixtureApp("deploy-rails-7.0"),
Expand Down
22 changes: 22 additions & 0 deletions test/testlib/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -94,6 +95,7 @@ type DeployTestRun struct {
skipExtensions bool
copyConfig bool
optOutGha bool
customizePath string

deployOnly bool
deployNow bool
Expand Down Expand Up @@ -127,6 +129,22 @@ func WithApp(app string) func(*DeployTestRun) {
}
}

func WithPreCustomize(customize interface{}) func(*DeployTestRun) {
b, err := json.Marshal(customize)
if err != nil {
panic(err)
}
return func(d *DeployTestRun) {
p := filepath.Join(d.WorkDir(), "customize.json")
if err := os.WriteFile(p, b, 0666); err != nil {
panic(err)
}
dst := "/opt/customize.json"
d.containerBinds = append(d.containerBinds, fmt.Sprintf("%s:%s", p, dst))
d.customizePath = dst
}
}

func WithGitRepo(repo string) func(*DeployTestRun) {
return func(d *DeployTestRun) {
d.gitRepo = repo
Expand Down Expand Up @@ -237,6 +255,10 @@ func (d *DeployTestRun) Start(ctx context.Context) error {
env = append(env, "DEPLOYER_CLEANUP_BEFORE_EXIT=1")
}

if d.customizePath != "" {
env = append(env, fmt.Sprintf("DEPLOY_CUSTOMIZE_PATH=%s", d.customizePath))
}

fmt.Printf("creating container... image=%s\n", d.deployerImage)
cont, err := d.dockerClient.ContainerCreate(ctx, &container.Config{
Image: d.deployerImage,
Expand Down

0 comments on commit 3a4f888

Please sign in to comment.