55 "log"
66 "os"
77 "os/exec"
8+ "path/filepath"
89 "regexp"
910 "strconv"
1011 "strings"
@@ -13,6 +14,7 @@ import (
1314)
1415
1516var healthcheck_channel = make (chan string )
17+ var bundle , ruby string
1618
1719func configureRails (sourceDir string , config * ScannerConfig ) (* SourceInfo , error ) {
1820 // `bundle init` will create a file with a commented out rails gem,
@@ -26,14 +28,39 @@ func configureRails(sourceDir string, config *ScannerConfig) (*SourceInfo, error
2628 return nil , nil
2729 }
2830
31+ // find absolute pat to bundle, ruby executables
32+ // see: https://tip.golang.org/doc/go1.19#os-exec-path
33+ var err error
34+ bundle , err = exec .LookPath ("bundle" )
35+ if err != nil {
36+ if errors .Is (err , exec .ErrDot ) {
37+ bundle , err = filepath .Abs (bundle )
38+ }
39+
40+ if err != nil {
41+ return nil , errors .Wrap (err , "failure finding bundle executable" )
42+ }
43+ }
44+
45+ ruby , err = exec .LookPath ("ruby" )
46+ if err != nil {
47+ if errors .Is (err , exec .ErrDot ) {
48+ ruby , err = filepath .Abs (ruby )
49+ }
50+
51+ if err != nil {
52+ return nil , errors .Wrap (err , "failure finding ruby executable" )
53+ }
54+ }
55+
2956 // verify that the bundle will install before proceeding
3057 args := []string {"install" }
3158
3259 if checksPass (sourceDir , fileExists ("Gemfile.lock" )) {
3360 args = append (args , "--quiet" )
3461 }
3562
36- cmd := exec .Command (" bundle" , args ... )
63+ cmd := exec .Command (bundle , args ... )
3764 cmd .Stdin = nil
3865 cmd .Stdout = os .Stdout
3966 cmd .Stderr = os .Stderr
@@ -71,9 +98,21 @@ func configureRails(sourceDir string, config *ScannerConfig) (*SourceInfo, error
7198 },
7299 }
73100 } else {
101+ // find absolute path to rake executable
102+ rake , err := exec .LookPath ("rake" )
103+ if err != nil {
104+ if errors .Is (err , exec .ErrDot ) {
105+ rake , err = filepath .Abs (rake )
106+ }
107+
108+ if err != nil {
109+ return nil , errors .Wrap (err , "failure finding rake executable" )
110+ }
111+ }
112+
74113 // support Rails 4 through 5.1 applications, or ones that started out
75114 // there and never were fully upgraded.
76- out , err := exec .Command (" rake" , "secret" ).Output ()
115+ out , err := exec .Command (rake , "secret" ).Output ()
77116
78117 if err == nil {
79118 s .Secrets = []Secret {
@@ -98,7 +137,13 @@ Once ready: run 'fly deploy' to deploy your Rails app.
98137
99138 // fetch healthcheck route in a separate thread
100139 go func () {
101- out , err := exec .Command ("ruby" , "./bin/rails" , "runner" ,
140+ ruby , err := exec .LookPath ("ruby" )
141+ if err != nil {
142+ healthcheck_channel <- ""
143+ return
144+ }
145+
146+ out , err := exec .Command (ruby , "./bin/rails" , "runner" ,
102147 "puts Rails.application.routes.url_helpers.rails_health_check_path" ).Output ()
103148
104149 if err == nil {
@@ -117,7 +162,7 @@ func RailsCallback(appName string, srcInfo *SourceInfo, options map[string]bool)
117162 if err != nil {
118163 panic (err )
119164 } else if ! strings .Contains (string (gemfile ), "dockerfile-rails" ) {
120- cmd := exec .Command (" bundle" , "add" , "dockerfile-rails" ,
165+ cmd := exec .Command (bundle , "add" , "dockerfile-rails" ,
121166 "--optimistic" , "--group" , "development" , "--skip-install" )
122167 cmd .Stdin = nil
123168 cmd .Stdout = os .Stdout
@@ -127,7 +172,7 @@ func RailsCallback(appName string, srcInfo *SourceInfo, options map[string]bool)
127172 return errors .Wrap (err , "Failed to add dockerfile-rails gem, exiting" )
128173 }
129174
130- cmd = exec .Command (" bundle" , "install" , "--quiet" )
175+ cmd = exec .Command (bundle , "install" , "--quiet" )
131176 cmd .Stdin = nil
132177 cmd .Stdout = os .Stdout
133178 cmd .Stderr = os .Stderr
@@ -138,9 +183,9 @@ func RailsCallback(appName string, srcInfo *SourceInfo, options map[string]bool)
138183 }
139184
140185 // ensure Gemfile.lock includes the x86_64-linux platform
141- if out , err := exec .Command (" bundle" , "platform" ).Output (); err == nil {
186+ if out , err := exec .Command (bundle , "platform" ).Output (); err == nil {
142187 if ! strings .Contains (string (out ), "x86_64-linux" ) {
143- cmd := exec .Command (" bundle" , "lock" , "--add-platform" , "x86_64-linux" )
188+ cmd := exec .Command (bundle , "lock" , "--add-platform" , "x86_64-linux" )
144189 if err := cmd .Run (); err != nil {
145190 return errors .Wrap (err , "Failed to add x86_64-linux platform, exiting" )
146191 }
@@ -178,7 +223,7 @@ func RailsCallback(appName string, srcInfo *SourceInfo, options map[string]bool)
178223 args = append (args , "--redis" )
179224 }
180225
181- cmd := exec .Command (" ruby" , args ... )
226+ cmd := exec .Command (ruby , args ... )
182227 cmd .Stdin = nil
183228 cmd .Stdout = os .Stdout
184229 cmd .Stderr = os .Stderr
@@ -188,14 +233,14 @@ func RailsCallback(appName string, srcInfo *SourceInfo, options map[string]bool)
188233 }
189234 } else {
190235 if options ["postgresql" ] && ! strings .Contains (string (gemfile ), "pg" ) {
191- cmd := exec .Command (" bundle" , "add" , "pg" )
236+ cmd := exec .Command (bundle , "add" , "pg" )
192237 if err := cmd .Run (); err != nil {
193238 return errors .Wrap (err , "Failed to install pg gem" )
194239 }
195240 }
196241
197242 if options ["redis" ] && ! strings .Contains (string (gemfile ), "redis" ) {
198- cmd := exec .Command (" bundle" , "add" , "redis" )
243+ cmd := exec .Command (bundle , "add" , "redis" )
199244 if err := cmd .Run (); err != nil {
200245 return errors .Wrap (err , "Failed to install redis gem" )
201246 }
0 commit comments