Skip to content

Commit

Permalink
Merge pull request #2352 from superfly/rails-processes
Browse files Browse the repository at this point in the history
Enable Rails scanner to define [processes] in fly.toml
  • Loading branch information
rubys committed May 29, 2023
2 parents 9b1fc64 + d736238 commit cb025b0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
33 changes: 30 additions & 3 deletions internal/command/launch/srcinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,17 @@ func setAppconfigFromSrcinfo(ctx context.Context, srcInfo *scanner.SourceInfo, a
appConfig.SetMounts(appVolumes)
}

for procName, procCommand := range srcInfo.Processes {
appConfig.SetProcess(procName, procCommand)
if len(srcInfo.Processes) > 0 {
for procName, procCommand := range srcInfo.Processes {
appConfig.SetProcess(procName, procCommand)

// if processes are defined, associate HTTPService with the app service
// (if defined) or the first service if no app service is defined.
if appConfig.HTTPService != nil &&
(procName == "app" || appConfig.HTTPService.Processes == nil) {
appConfig.HTTPService.Processes = []string{procName}
}
}
}

if srcInfo.ReleaseCmd != "" {
Expand Down Expand Up @@ -309,7 +318,25 @@ func runCallback(ctx context.Context, srcInfo *scanner.SourceInfo, options map[s
if srcInfo == nil || srcInfo.Callback == nil {
return nil
}
return srcInfo.Callback(srcInfo, options)

err := srcInfo.Callback(srcInfo, options)

if srcInfo.MergeConfig != nil {
if err == nil {
cfg, err := appconfig.LoadConfig(srcInfo.MergeConfig.Name)
if err == nil {
// In theory, any part of the configuration could be merged here, but for now
// we will only copy over the processes
srcInfo.Processes = cfg.Processes
}
}

if srcInfo.MergeConfig.Temporary {
_ = os.Remove(srcInfo.MergeConfig.Name)
}
}

return err
}

func runInitCommands(ctx context.Context, srcInfo *scanner.SourceInfo) error {
Expand Down
18 changes: 18 additions & 0 deletions scanner/rails.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scanner

import (
"io/fs"
"log"
"os"
"os/exec"
"regexp"
Expand Down Expand Up @@ -128,6 +129,23 @@ func RailsCallback(srcInfo *SourceInfo, options map[string]bool) error {
// generate Dockerfile if it doesn't already exist
_, err = os.Stat("Dockerfile")
if errors.Is(err, fs.ErrNotExist) {
flyToml := "fly.toml"
_, err := os.Stat(flyToml)
if os.IsNotExist(err) {
// "touch" fly.toml
file, err := os.Create("fly.toml")
if err != nil {
log.Fatal(err)
}
file.Close()

// inform caller of the presence of this file
srcInfo.MergeConfig = &MergeConfigStruct{
Name: "fly.toml",
Temporary: true,
}
}

args := []string{"./bin/rails", "generate", "dockerfile",
"--label=fly_launch_runtime:rails"}

Expand Down
6 changes: 6 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ type Secret struct {
Generate func() (string, error)
}

type MergeConfigStruct struct {
Name string
Temporary bool
}

type SourceInfo struct {
Family string
Version string
Expand Down Expand Up @@ -57,6 +62,7 @@ type SourceInfo struct {
Callback func(srcInfo *SourceInfo, options map[string]bool) error
HttpCheckPath string
ConsoleCommand string
MergeConfig *MergeConfigStruct
}

type SourceFile struct {
Expand Down

0 comments on commit cb025b0

Please sign in to comment.