Skip to content

Commit 186d91e

Browse files
authored
Merge pull request #2437 from superfly/full-path-exec-command
use full paths when calling exec command
2 parents 06edc74 + f1882ce commit 186d91e

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

scanner/nodeFramework.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"os"
1010
"os/exec"
11+
"path/filepath"
1112
"regexp"
1213
"strconv"
1314
"strings"
@@ -46,8 +47,20 @@ func configureNodeFramework(sourceDir string, config *ScannerConfig) (*SourceInf
4647
}
4748
}
4849

50+
// ensure node is in $PATH
51+
node, err := exec.LookPath("node")
52+
if err != nil && !errors.Is(err, exec.ErrDot) {
53+
return nil, nil
54+
}
55+
56+
// resolve to absolute path, see: https://tip.golang.org/doc/go1.19#os-exec-path
57+
node, err = filepath.Abs(node)
58+
if err != nil {
59+
return nil, nil
60+
}
61+
4962
// ensure node version is at least 16.0.0
50-
out, err := exec.Command("node", "-v").Output()
63+
out, err := exec.Command(node, "-v").Output()
5164
if err != nil {
5265
return nil, nil
5366
} else {
@@ -154,7 +167,19 @@ func NodeFrameworkCallback(appName string, srcInfo *SourceInfo, options map[stri
154167

155168
// run the package if we haven't already
156169
if args[0] != "npx" {
157-
cmd := exec.Command("npx", "dockerfile")
170+
// find npx in PATH
171+
npx, err := exec.LookPath("npx")
172+
if err != nil && !errors.Is(err, exec.ErrDot) {
173+
return fmt.Errorf("failure finding npx executable in PATH")
174+
}
175+
176+
// resolve to absolute path, see: https://tip.golang.org/doc/go1.19#os-exec-path
177+
npx, err = filepath.Abs(npx)
178+
if err != nil {
179+
return fmt.Errorf("failure finding npx executable in PATH")
180+
}
181+
182+
cmd := exec.Command(npx, "dockerfile")
158183
cmd.Stdin = nil
159184
cmd.Stdout = os.Stdout
160185
cmd.Stderr = os.Stderr

scanner/rails.go

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"os"
77
"os/exec"
8+
"path/filepath"
89
"regexp"
910
"strconv"
1011
"strings"
@@ -13,6 +14,7 @@ import (
1314
)
1415

1516
var healthcheck_channel = make(chan string)
17+
var bundle, ruby string
1618

1719
func 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

Comments
 (0)