-
Notifications
You must be signed in to change notification settings - Fork 234
/
scan.go
474 lines (404 loc) · 10.5 KB
/
scan.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package sourcecode
import (
"bufio"
"embed"
"io/fs"
"os"
"path/filepath"
"regexp"
"github.com/pkg/errors"
"github.com/superfly/flyctl/helpers"
)
//go:embed templates/** templates/**/.dockerignore
var content embed.FS
type InitCommand struct {
Command string
Args []string
Description string
}
type Secret struct {
Key string
Help string
Generate bool
}
type SourceInfo struct {
Family string
Version string
DockerfilePath string
Builder string
ReleaseCmd string
DockerCommand string
DockerEntrypoint string
KillSignal string
Buildpacks []string
Secrets []Secret
Files []SourceFile
Port int
Env map[string]string
Statics []Static
Processes map[string]string
DeployDocs string
Notice string
SkipDeploy bool
Volumes []Volume
DockerfileAppendix []string
InitCommands []InitCommand
CreatePostgresCluster bool
}
type SourceFile struct {
Path string
Contents []byte
}
type Static struct {
GuestPath string `toml:"guest_path" json:"guest_path"`
UrlPrefix string `toml:"url_prefix" json:"url_prefix"`
}
type Volume struct {
Source string `toml:"source" json:"source"`
Destination string `toml:"destination" json:"destination"`
}
func Scan(sourceDir string) (*SourceInfo, error) {
scanners := []sourceScanner{
configureRedwood,
/* frameworks scanners are placed before generic scanners,
since they might mix languages or have a Dockerfile that
doesn't work with Fly */
configureDockerfile,
configureRuby,
configureGo,
configurePhoenix,
configureElixir,
configurePython,
configureDeno,
configureRemix,
configureNode,
configureStatic,
}
for _, scanner := range scanners {
si, err := scanner(sourceDir)
if err != nil {
return nil, err
}
if si != nil {
return si, nil
}
}
return nil, nil
}
func SuggestAppName(sourceDir string) string {
return filepath.Base(sourceDir)
}
type sourceScanner func(sourceDir string) (*SourceInfo, error)
func fileExists(filenames ...string) checkFn {
return func(dir string) bool {
for _, filename := range filenames {
info, err := os.Stat(filepath.Join(dir, filename))
if err != nil {
continue
}
if !info.IsDir() {
return true
}
}
return false
}
}
func fileContains(path string, pattern string) bool {
file, err := os.Open(path)
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
re := regexp.MustCompile(pattern)
if re.MatchString(scanner.Text()) {
return true
}
}
return false
}
func dirContains(glob string, patterns ...string) checkFn {
return func(dir string) bool {
for _, pattern := range patterns {
filenames, _ := filepath.Glob(filepath.Join(dir, glob))
for _, filename := range filenames {
if fileContains(filename, pattern) {
return true
}
}
}
return false
}
}
type checkFn func(dir string) bool
func checksPass(sourceDir string, checks ...checkFn) bool {
for _, check := range checks {
if check(sourceDir) {
return true
}
}
return false
}
func configureDockerfile(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("Dockerfile")) {
return nil, nil
}
s := &SourceInfo{
DockerfilePath: filepath.Join(sourceDir, "Dockerfile"),
Family: "Dockerfile",
}
return s, nil
}
func configureRuby(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("Gemfile", "config.ru")) {
return nil, nil
}
s := &SourceInfo{
Builder: "heroku/buildpacks:20",
Family: "Ruby",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
},
}
return s, nil
}
func configureGo(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("go.mod", "Gopkg.lock")) {
return nil, nil
}
s := &SourceInfo{
Builder: "paketobuildpacks/builder:base",
Buildpacks: []string{"gcr.io/paketo-buildpacks/go"},
Family: "Go",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
},
}
return s, nil
}
func configurePython(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("requirements.txt", "environment.yml")) {
return nil, nil
}
s := &SourceInfo{
Files: templates("templates/python"),
Builder: "paketobuildpacks/builder:base",
Family: "Python",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
},
SkipDeploy: true,
DeployDocs: `We have generated a simple Procfile for you. Modify it to fit your needs and run "fly deploy" to deploy your application.`,
}
return s, nil
}
func configureNode(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("package.json")) {
return nil, nil
}
s := &SourceInfo{
Builder: "heroku/buildpacks:20",
Family: "NodeJS",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
},
}
return s, nil
}
func configureDeno(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, dirContains("*.ts", "denopkg")) {
return nil, nil
}
s := &SourceInfo{
Files: templates("templates/deno"),
Family: "Deno",
Port: 8080,
Processes: map[string]string{
"app": "run --allow-net ./example.ts",
},
Env: map[string]string{
"PORT": "8080",
},
}
return s, nil
}
func configureStatic(sourceDir string) (*SourceInfo, error) {
// No index.html detected, move on
if !helpers.FileExists(filepath.Join(sourceDir, "index.html")) {
return nil, nil
}
s := &SourceInfo{
Family: "Static",
Port: 8080,
Files: templates("templates/static"),
}
return s, nil
}
func configurePhoenix(sourceDir string) (*SourceInfo, error) {
// Not phoenix, move on
if !helpers.FileExists(filepath.Join(sourceDir, "mix.exs")) || !checksPass(sourceDir, dirContains("mix.exs", "phoenix")) {
return nil, nil
}
s := &SourceInfo{
Family: "Phoenix",
Secrets: []Secret{
{
Key: "SECRET_KEY_BASE",
Help: "Phoenix needs a random, secret key. Use the random default we've generated, or generate your own.",
Generate: true,
},
},
KillSignal: "SIGTERM",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
"PHX_HOST": "APP_FQDN",
},
DockerfileAppendix: []string{
"ENV ECTO_IPV6 true",
"ENV ERL_AFLAGS \"-proto_dist inet6_tcp\"",
},
InitCommands: []InitCommand{
{
Command: "mix",
Args: []string{"local.rebar", "--force"},
Description: "Preparing system for Elixir builds",
},
{
Command: "mix",
Args: []string{"deps.get"},
Description: "Installing application dependencies",
},
{
Command: "mix",
Args: []string{"phx.gen.release", "--docker"},
Description: "Running Docker release generator",
},
},
}
// We found Phoenix 1.6.3 or higher, so try running the Docker generator
if checksPass(sourceDir, dirContains("mix.exs", "phoenix.*"+regexp.QuoteMeta("1.6"))) {
s.DeployDocs = `
Your Phoenix app should be ready for deployment!.
If you need something else, post on our community forum at https://community.fly.io.
When you're ready to deploy, use 'fly deploy --remote-only'.
`
}
// We found Phoenix 1.6.0 - 1.6.2
if checksPass(sourceDir, dirContains("mix.exs", "phoenix.*"+regexp.QuoteMeta("1.6.")+"[0-2]")) {
s.SkipDeploy = true
s.DeployDocs = `
We recommend upgrading to Phoenix 1.6.3 which includes a release configuration for Docker-based deployment.
If you do upgrade, you can run 'fly launch' again to get the required deployment setup.
If you don't want to uprade, you'll need to add a few files and configuration options manually.
W've placed Dockerfile compatible with other Phoenix 1.6 apps in this directory. See
https://hexdocs.pm/phoenix/fly.html for details, including instructions for setting up
a Postgresql database.
`
}
// Add migration task if we find ecto
if checksPass(sourceDir, dirContains("mix.exs", "ecto")) {
s.ReleaseCmd = "/app/bin/migrate"
}
// Ask to create a postgres database if we find the postgres adapter
if checksPass(sourceDir, dirContains("mix.lock", "postgrex")) {
s.CreatePostgresCluster = true
}
return s, nil
}
func configureElixir(sourceDir string) (*SourceInfo, error) {
if !helpers.FileExists(filepath.Join(sourceDir, "mix.exs")) {
return nil, nil
}
s := &SourceInfo{
Builder: "heroku/buildpacks:20",
Buildpacks: []string{"https://cnb-shim.herokuapp.com/v1/hashnuke/elixir"},
Family: "Elixir",
Port: 8080,
Env: map[string]string{
"PORT": "8080",
},
}
return s, nil
}
func configureRedwood(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("redwood.toml")) {
return nil, nil
}
s := &SourceInfo{
Family: "RedwoodJS",
Files: templates("templates/redwood"),
Port: 8911,
Env: map[string]string{
"PORT": "8911",
},
Statics: []Static{
{
GuestPath: "/app/public",
UrlPrefix: "/",
},
},
}
return s, nil
}
func configureRemix(sourceDir string) (*SourceInfo, error) {
if !checksPass(sourceDir, fileExists("remix.config.js")) {
return nil, nil
}
env := map[string]string{
"PORT": "8080",
}
s := &SourceInfo{
Family: "Remix",
Port: 8080,
}
if checksPass(sourceDir+"/prisma", dirContains("*.prisma", "sqlite")) {
env["DATABASE_URL"] = "file:/data/sqlite.db"
s.Files = templates("templates/remix_prisma")
s.DockerCommand = "start_with_migrations.sh"
s.DockerEntrypoint = "sh"
s.Volumes = []Volume{
{
Source: "data",
Destination: "/data",
},
}
s.Notice = "\nThis launch configuration uses SQLite on a single, dedicated volume. It will not scale beyond a single VM. Look into 'fly postgres' for a more robust production database. \n"
} else {
s.Files = templates("templates/remix")
}
s.Env = env
return s, nil
}
// templates recursively returns files from the templates directory within the named directory
// will panic on errors since these files are embedded and should work
func templates(name string) (files []SourceFile) {
err := fs.WalkDir(content, name, func(path string, d fs.DirEntry, e error) error {
if d.IsDir() {
return nil
}
relPath, err := filepath.Rel(name, path)
if err != nil {
return errors.Wrap(err, "error removing template prefix")
}
data, err := fs.ReadFile(content, path)
if err != nil {
return err
}
f := SourceFile{
Path: relPath,
Contents: data,
}
files = append(files, f)
return nil
})
if err != nil {
panic(err)
}
return
}