-
Notifications
You must be signed in to change notification settings - Fork 241
/
rails.go
440 lines (378 loc) · 12.8 KB
/
rails.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package scanner
import (
"fmt"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/superfly/flyctl/internal/command/launch/plan"
"github.com/superfly/flyctl/internal/flyerr"
"gopkg.in/yaml.v2"
)
var healthcheck_channel = make(chan string)
var bundle, ruby string
var binrails = filepath.Join(".", "bin", "rails")
func configureRails(sourceDir string, config *ScannerConfig) (*SourceInfo, error) {
// `bundle init` will create a file with a commented out rails gem,
// so checking for that can produce a false positive. Look for
// Rails three other ways...
rails := checksPass(sourceDir+"/bin", fileExists("rails")) ||
checksPass(sourceDir, dirContains("config.ru", "Rails")) ||
checksPass(sourceDir, dirContains("Gemfile.lock", " rails "))
if !rails {
return nil, nil
}
// find absolute pat to bundle, ruby executables
// see: https://tip.golang.org/doc/go1.19#os-exec-path
var err error
bundle, err = exec.LookPath("bundle")
if err != nil {
if errors.Is(err, exec.ErrDot) {
bundle, err = filepath.Abs(bundle)
}
if err != nil {
return nil, errors.Wrap(err, "failure finding bundle executable")
}
}
ruby, err = exec.LookPath("ruby")
if err != nil {
if errors.Is(err, exec.ErrDot) {
ruby, err = filepath.Abs(ruby)
}
if err != nil {
return nil, errors.Wrap(err, "failure finding ruby executable")
}
}
// verify that the bundle will install before proceeding
args := []string{"install"}
if checksPass(sourceDir, fileExists("Gemfile.lock")) {
args = append(args, "--quiet")
}
cmd := exec.Command(bundle, args...)
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return nil, errors.Wrap(err, "Failed to install bundle, exiting")
}
s := &SourceInfo{
Family: "Rails",
Callback: RailsCallback,
FailureCallback: RailsFailureCallback,
Port: 3000,
ConsoleCommand: "/rails/bin/rails console",
AutoInstrumentErrors: true,
}
if checksPass(sourceDir, dirContains("Gemfile", "litestack")) {
// don't prompt for pg, redis if litestack is in the Gemfile
s.DatabaseDesired = DatabaseKindSqlite
s.SkipDatabase = true
} else if checksPass(sourceDir, dirContains("Gemfile", "mysql")) {
// mysql
s.DatabaseDesired = DatabaseKindMySQL
s.SkipDatabase = false
} else if !checksPass(sourceDir, fileExists("Dockerfile")) || checksPass(sourceDir, dirContains("Dockerfile", "libpq-dev", "postgres")) {
// postgresql
s.DatabaseDesired = DatabaseKindPostgres
s.SkipDatabase = false
} else {
// sqlite
s.DatabaseDesired = DatabaseKindSqlite
s.SkipDatabase = true
}
// enable redis if there are any action cable / anycable channels
redis := false
files, err := filepath.Glob("app/channels/*.rb")
if err == nil && len(files) > 0 {
redis = true
}
// enable redis if redis is used for caching
prodEnv, err := os.ReadFile("config/environments/production.rb")
if err == nil && strings.Contains(string(prodEnv), "redis") {
redis = true
}
if redis {
s.RedisDesired = true
s.SkipDatabase = false
}
// enable object storage (Tigris) if any of the following are true...
// * aws-sdk-s3 is in the Gemfile or Gemfile.lock
// * active_storage_attachments is any file in the db/migrate directory
// * config/storage.yml is present and uses S3
if checksPass(sourceDir, dirContains("Gemfile", "aws-sdk-s3")) || checksPass(sourceDir, dirContains("Gemfile.lock", "aws-sdk-s3")) {
s.ObjectStorageDesired = true
} else if checksPass(sourceDir+"/db/migrate", dirContains("*.rb", ":active_storage_attachments")) {
s.ObjectStorageDesired = true
} else if checksPass(sourceDir+"/config", fileExists("storage.yml")) {
cfgMap := map[string]any{}
buf, err := os.ReadFile(path.Join(sourceDir, "config", "storage.yml"))
if err == nil {
err = yaml.Unmarshal(buf, &cfgMap)
}
if err == nil {
for _, v := range cfgMap {
submap, ok := v.(map[interface{}]interface{})
if ok {
service, ok := submap["service"].(string)
if ok && service == "S3" {
s.ObjectStorageDesired = true
}
}
}
}
}
// master.key comes with Rails apps from v5.2 onwards, but may not be present
// if the app does not use Rails encrypted credentials. Rails v6 added
// support for multi-environment credentials. Use the Rails searching
// sequence for production credentials to determine the RAILS_MASTER_KEY.
masterKey, err := os.ReadFile("config/credentials/production.key")
if err != nil {
masterKey, err = os.ReadFile("config/master.key")
}
if err == nil {
s.Secrets = []Secret{
{
Key: "RAILS_MASTER_KEY",
Help: "Secret key for accessing encrypted credentials",
Value: string(masterKey),
},
}
} else {
// find absolute path to rake executable
rake, err := exec.LookPath("rake")
if err != nil {
if errors.Is(err, exec.ErrDot) {
rake, err = filepath.Abs(rake)
}
if err != nil {
return nil, errors.Wrap(err, "failure finding rake executable")
}
}
// support Rails 4 through 5.1 applications, or ones that started out
// there and never were fully upgraded.
out, err := exec.Command(rake, "secret").Output()
if err == nil {
s.Secrets = []Secret{
{
Key: "SECRET_KEY_BASE",
Help: "Secret key used to verify the integrity of signed cookies",
Value: strings.TrimSpace(string(out)),
},
}
}
}
initializersPath := filepath.Join(sourceDir, "config", "initializers")
if checksPass(initializersPath, dirContains("*.rb", "ENV", "credentials")) {
s.SkipDeploy = true
s.DeployDocs = `
Your Rails app is prepared for deployment.
` + config.Colorize.Red(
`WARNING: One or more of your config initializer files appears to access
environment variables or Rails credentials. These values generally are not
available during the Docker build process, so you may need to update your
initializers to bypass portions of your setup during the build process.`) + `
More information on what needs to be done can be found at:
https://fly.io/docs/rails/getting-started/existing/#access-to-environment-variables-at-build-time.
Once ready: run 'fly deploy' to deploy your Rails app.
`
}
// fetch healthcheck route in a separate thread
go func() {
ruby, err := exec.LookPath("ruby")
if err != nil {
healthcheck_channel <- ""
return
}
out, err := exec.Command(ruby, binrails, "runner",
"puts Rails.application.routes.url_helpers.rails_health_check_path").Output()
if err == nil {
healthcheck_channel <- strings.TrimSpace(string(out))
} else {
healthcheck_channel <- ""
}
}()
return s, nil
}
func RailsCallback(appName string, srcInfo *SourceInfo, plan *plan.LaunchPlan, flags []string) error {
// Overall strategy: Install and use the dockerfile-rails gem to generate a Dockerfile.
//
// If a Dockerfile already exists, run the generator with the --skip flag to avoid overwriting it.
// This will still do interesting things like update the fly.toml file to add volumes, processes, etc.
//
// If the generator fails but a Dockerfile exists, warn the user and proceed. Only fail if no
// Dockerfile exists at the end of this process.
// install dockerfile-rails gem, if not already included and the gem directory is writable
// if an error occurrs, store it for later in pendingError
generatorInstalled := false
var pendingError error
gemfile, err := os.ReadFile("Gemfile")
if err != nil {
return errors.Wrap(err, "Failed to read Gemfile")
} else if !strings.Contains(string(gemfile), "dockerfile-rails") {
// check for writable gem installation directory
writable := false
out, err := exec.Command("gem", "environment").Output()
if err == nil {
regexp := regexp.MustCompile(`INSTALLATION DIRECTORY: (.*)\n`)
for _, match := range regexp.FindAllStringSubmatch(string(out), -1) {
// Testing to see if a directory is writable is OS dependent, so
// we use a brute force method: attempt it and see if it works.
file, err := os.CreateTemp(match[1], ".flyctl.probe")
if err == nil {
writable = true
file.Close()
defer os.Remove(file.Name())
}
}
}
// install dockerfile-rails gem if the gem installation directory is writable
if writable {
cmd := exec.Command(bundle, "add", "dockerfile-rails",
"--optimistic", "--group", "development", "--skip-install")
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
pendingError = cmd.Run()
if pendingError != nil {
pendingError = errors.Wrap(pendingError, "Failed to add dockerfile-rails gem")
} else {
cmd = exec.Command(bundle, "install", "--quiet")
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
pendingError = cmd.Run()
if pendingError != nil {
pendingError = errors.Wrap(pendingError, "Failed to install dockerfile-rails gem")
} else {
generatorInstalled = true
}
}
}
} else {
// proceed using the already installed gem
generatorInstalled = true
}
// ensure Gemfile.lock includes the x86_64-linux platform
if out, err := exec.Command(bundle, "platform").Output(); err == nil {
if !strings.Contains(string(out), "x86_64-linux") {
cmd := exec.Command(bundle, "lock", "--add-platform", "x86_64-linux")
if err := cmd.Run(); err != nil {
return errors.Wrap(err, "Failed to add x86_64-linux platform, exiting")
}
}
}
// ensure fly.toml exists. If present, the rails dockerfile generator will
// add volumes, processes, release command and potentailly other configuration.
flyToml := "fly.toml"
_, err = os.Stat(flyToml)
if os.IsNotExist(err) {
// "touch" fly.toml
file, err := os.Create(flyToml)
if err != nil {
return errors.Wrap(err, "Failed to create fly.toml")
}
file.Close()
// inform caller of the presence of this file
srcInfo.MergeConfig = &MergeConfigStruct{
Name: flyToml,
Temporary: true,
}
}
// base generate command
args := []string{binrails, "generate", "dockerfile",
"--label=fly_launch_runtime:rails"}
// skip prompt to replace files if Dockerfile already exists
_, err = os.Stat("Dockerfile")
if !errors.Is(err, fs.ErrNotExist) {
args = append(args, "--skip")
if !generatorInstalled {
return errors.Wrap(pendingError, "No Dockerfile found")
}
}
// add postgres
if plan.Postgres.Provider() != nil {
args = append(args, "--postgresql", "--no-prepare")
}
// add redis
if plan.Redis.Provider() != nil {
args = append(args, "--redis")
}
// add object storage
if plan.ObjectStorage.Provider() != nil {
args = append(args, "--tigris")
}
// add additional flags from launch command
if len(flags) > 0 {
args = append(args, flags...)
}
// run command if the generator is available
if generatorInstalled {
fmt.Printf("Running: %s\n", strings.Join(args, " "))
cmd := exec.Command(ruby, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
pendingError = cmd.Run()
if exitError, ok := pendingError.(*exec.ExitError); ok {
if exitError.ExitCode() == 42 {
// generator exited with code 42, which means existing
// Dockerfile contains errors which will prevent deployment.
pendingError = nil
srcInfo.SkipDeploy = true
srcInfo.DeployDocs = `
Correct the errors in your Dockerfile and run 'fly deploy' to
deploy your Rails app.
The following comand can be used to update your Dockerfile:
` + binrails + ` generate dockerfile
`
fmt.Println()
}
}
}
// read dockerfile
dockerfile, err := os.ReadFile("Dockerfile")
if err == nil {
if pendingError != nil {
// generator may have failed, but Dockerfile was created - warn user
fmt.Println("Error running dockerfile generator:", pendingError)
}
} else if pendingError != nil {
// generator failed and Dockerfile was not created - return original error
return errors.Wrap(pendingError, "Failed to generate Dockerfile")
} else {
// generator succeeded, but Dockerfile was not created - return error
return errors.Wrap(err, "Failed to read Dockerfile")
}
// extract volume - handle both plain string and JSON format, but only allow one path
re := regexp.MustCompile(`(?m)^VOLUME\s+(\[\s*")?(\/[\w\/]*?(\w+))("\s*\])?\s*$`)
m := re.FindStringSubmatch(string(dockerfile))
if len(m) > 0 {
srcInfo.Volumes = []Volume{
{
Source: m[3], // last part of path
Destination: m[2], // full path
},
}
}
// add HealthCheck (if found)
srcInfo.HttpCheckPath = <-healthcheck_channel
if srcInfo.HttpCheckPath != "" {
srcInfo.HttpCheckHeaders = map[string]string{"X-Forwarded-Proto": "https"}
}
return nil
}
func RailsFailureCallback(err error) error {
suggestion := flyerr.GetErrorSuggestion(err)
if suggestion == "" {
err = flyerr.GenericErr{
Err: err.Error(),
Suggest: "\nSee https://fly.io/docs/rails/getting-started/existing/#common-initial-deployment-issues\n" +
"for suggestions on how to resolve common deployment issues.",
}
}
return err
}