-
Notifications
You must be signed in to change notification settings - Fork 240
/
launch.go
478 lines (413 loc) · 13.3 KB
/
launch.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
475
476
477
478
package launch
import (
"context"
"fmt"
"path/filepath"
"regexp"
"strings"
"github.com/cavaliergopher/grab/v3"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/build/imgsrc"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/command/deploy"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/scanner"
"github.com/superfly/graphql"
)
func New() (cmd *cobra.Command) {
const (
long = `Create and configure a new app from source code or a Docker image.`
short = long
)
cmd = command.New("launch", short, long, run, command.RequireSession, command.LoadAppConfigIfPresent)
cmd.Args = cobra.NoArgs
flag.Add(cmd,
// Since launch can perform a deployment, we offer the full set of deployment flags for those using
// the launch command in CI environments. We may want to rescind this decision down the line, because
// the list of flags is long, but it follows from the precedent of already offering some deployment flags.
// See a proposed 'flag grouping' feature in Viper that could help with DX: https://github.com/spf13/cobra/pull/1778
deploy.CommonFlags,
flag.Org(),
flag.NoDeploy(),
flag.Bool{
Name: "generate-name",
Description: "Always generate a name for the app, without prompting",
},
flag.String{
Name: "path",
Description: `Path to the app source root, where fly.toml file will be saved`,
Default: ".",
},
flag.String{
Name: "name",
Description: `Name of the new app`,
},
flag.Bool{
Name: "copy-config",
Description: "Use the configuration file if present without prompting",
Default: false,
},
flag.Bool{
Name: "dockerignore-from-gitignore",
Description: "If a .dockerignore does not exist, create one from .gitignore files",
Default: false,
},
flag.Int{
Name: "internal-port",
Description: "Set internal_port for all services in the generated fly.toml",
Default: -1,
},
)
return
}
func run(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
client := client.FromContext(ctx).API()
workingDir := flag.GetString(ctx, "path")
existingConfig := appconfig.ConfigFromContext(ctx)
generateName := flag.GetBool(ctx, "generate-name")
copyConfig := flag.GetBool(ctx, "copy-config")
name := strings.TrimSpace(flag.GetString(ctx, "name"))
appConfig := appconfig.NewConfig()
launchIntoExistingApp := false
deployArgs := deploy.DeployWithConfigArgs{
ForceNomad: flag.GetBool(ctx, "force-nomad"),
ForceMachines: flag.GetBool(ctx, "force-machines"),
ForceYes: flag.GetBool(ctx, "now"),
}
// Determine the working directory
if absDir, err := filepath.Abs(workingDir); err == nil {
workingDir = absDir
}
configFilePath := filepath.Join(workingDir, appconfig.DefaultConfigFileName)
if existingConfig != nil {
if existingConfig.AppName != "" {
fmt.Fprintln(io.Out, "An existing fly.toml file was found for app", existingConfig.AppName)
} else {
fmt.Fprintln(io.Out, "An existing fly.toml file was found")
}
if !copyConfig {
copy, err := prompt.Confirm(ctx, "Would you like to copy its configuration to the new app?")
if err != nil {
return err
}
copyConfig = copy
}
if copyConfig {
appConfig = existingConfig
}
}
fmt.Fprintln(io.Out, "Creating app in", workingDir)
srcInfo := new(scanner.SourceInfo)
config := &scanner.ScannerConfig{
ExistingPort: appConfig.InternalPort(),
}
// Detect if --copy-config and --now flags are set. If so, limited set of
// fly.toml file updates. Helpful for deploying PRs when the project is
// already setup and we only need fly.toml config changes.
if flag.GetBool(ctx, "copy-config") && flag.GetBool(ctx, "now") {
config.Mode = "clone"
} else {
config.Mode = "launch"
}
if img := flag.GetString(ctx, "image"); img != "" {
fmt.Fprintln(io.Out, "Using image", img)
appConfig.Build = &appconfig.Build{
Image: img,
}
} else if dockerfile := flag.GetString(ctx, "dockerfile"); dockerfile != "" {
if strings.HasPrefix(dockerfile, "http://") || strings.HasPrefix(dockerfile, "https://") {
fmt.Fprintln(io.Out, "Downloading dockerfile", dockerfile)
resp, err := grab.Get("Dockerfile", dockerfile)
if err != nil {
return err
} else {
appConfig.Build = &appconfig.Build{
Dockerfile: resp.Filename,
}
// scan Dockerfile for port
if si, err := scanner.Scan(workingDir, config); err != nil {
return err
} else {
srcInfo = si
}
}
} else {
fmt.Fprintln(io.Out, "Using dockerfile", dockerfile)
appConfig.Build = &appconfig.Build{
Dockerfile: dockerfile,
}
}
} else {
fmt.Fprintln(io.Out, "Scanning source code")
if si, err := scanner.Scan(workingDir, config); err != nil {
return err
} else {
srcInfo = si
}
if srcInfo == nil {
fmt.Fprintln(io.Out, aurora.Green("Could not find a Dockerfile, nor detect a runtime or framework from source code. Continuing with a blank app."))
} else {
var article string = "a"
if matched, _ := regexp.MatchString(`^[aeiou]`, strings.ToLower(srcInfo.Family)); matched {
article += "n"
}
appType := srcInfo.Family
if srcInfo.Version != "" {
appType = appType + " " + srcInfo.Version
}
fmt.Fprintf(io.Out, "Detected %s %s app\n", article, aurora.Green(appType))
if srcInfo.Builder != "" {
fmt.Fprintln(io.Out, "Using the following build configuration:")
fmt.Fprintln(io.Out, "\tBuilder:", srcInfo.Builder)
if srcInfo.Buildpacks != nil && len(srcInfo.Buildpacks) > 0 {
fmt.Fprintln(io.Out, "\tBuildpacks:", strings.Join(srcInfo.Buildpacks, " "))
}
appConfig.Build = &appconfig.Build{
Builder: srcInfo.Builder,
Buildpacks: srcInfo.Buildpacks,
}
}
}
}
if generateName {
appConfig.AppName = ""
}
if name != "" {
appConfig.AppName = name
}
if !generateName && name == "" {
inputName, err := promptForAppName(ctx, appConfig)
if err != nil {
return err
}
appConfig.AppName = inputName
}
var org *api.Organization
if appConfig.AppName != "" {
exists, app, err := appExists(ctx, appConfig)
if err != nil {
return err
}
if exists {
msg := fmt.Sprintf("App %s already exists, do you want to launch into that app?", appConfig.AppName)
launchIntoExistingApp, err = prompt.Confirm(ctx, msg)
if err != nil {
return err
}
if !launchIntoExistingApp {
return nil
}
org = &api.Organization{
ID: app.Organization.ID,
Name: app.Organization.Name,
Slug: app.Organization.Slug,
PaidPlan: app.Organization.PaidPlan,
}
}
}
if !launchIntoExistingApp {
// Prompt for an org
// TODO: determine if eager remote builder is still required here
org, err = prompt.Org(ctx)
if err != nil {
return
}
}
// If we potentially are deploying, launch a remote builder to prepare for deployment.
if !flag.GetBool(ctx, "no-deploy") {
go imgsrc.EagerlyEnsureRemoteBuilder(ctx, client, org.Slug)
}
region, err := prompt.Region(ctx, !org.PaidPlan, prompt.RegionParams{
Message: "Choose a region for deployment:",
})
if err != nil {
return err
}
appConfig.PrimaryRegion = region.Code
shouldUseMachines, err := shouldAppUseMachinesPlatform(ctx, org.Slug)
if err != nil {
return err
}
if shouldUseMachines && copyConfig {
// Check imported fly.toml is a valid V2 config before creating the app
if err := appConfig.SetMachinesPlatform(); err != nil {
return fmt.Errorf("Can not use configuration for Apps V2, check fly.toml: %w", err)
}
}
if !launchIntoExistingApp {
input := api.CreateAppInput{
Name: appConfig.AppName,
OrganizationID: org.ID,
PreferredRegion: &appConfig.PrimaryRegion,
Machines: shouldUseMachines,
}
createdApp, err := client.CreateApp(ctx, input)
if err != nil {
return err
}
if !copyConfig {
// Use the default configuration template suggested by Web
newCfg, err := appconfig.FromDefinition(&createdApp.Config.Definition)
if err != nil {
return fmt.Errorf("Launch failed to get new app configuration: %w", err)
}
newCfg.AppName = createdApp.Name
newCfg.Build = appConfig.Build
newCfg.PrimaryRegion = appConfig.PrimaryRegion
appConfig = newCfg
} else {
appConfig.AppName = createdApp.Name
}
fmt.Fprintf(io.Out, "Created app '%s' in organization '%s'\n", appConfig.AppName, org.Slug)
}
fmt.Fprintf(io.Out, "Admin URL: https://fly.io/apps/%s\n", appConfig.AppName)
fmt.Fprintf(io.Out, "Hostname: %s.fly.dev\n", appConfig.AppName)
// If files are requested by the launch scanner, create them.
if err := createSourceInfoFiles(ctx, srcInfo, workingDir); err != nil {
return err
}
// If secrets are requested by the launch scanner, ask the user to input them
if err := createSecrets(ctx, srcInfo, appConfig.AppName); err != nil {
return err
}
// If volumes are requested by the launch scanner, create them
if err := createVolumes(ctx, srcInfo, appConfig.AppName, region.Code); err != nil {
return err
}
// If database are requested by the launch scanner, create them
options, err := createDatabases(ctx, srcInfo, appConfig.AppName, region, org)
if err != nil {
return err
}
// Invoke Callback, if any
if err := runCallback(ctx, srcInfo, options); err != nil {
return err
}
// Run any initialization commands
if err := runInitCommands(ctx, srcInfo); err != nil {
return err
}
// Complete the appConfig
if err := setAppconfigFromSrcinfo(ctx, srcInfo, appConfig); err != nil {
return err
}
// Attempt to create a .dockerignore from .gitignore
determineDockerIgnore(ctx, workingDir)
// Override internal port if requested using --internal-port flag
if n := flag.GetInt(ctx, "internal-port"); n > 0 {
appConfig.SetInternalPort(n)
}
// remove auto-rollback from machine fly.tomls
if shouldUseMachines {
appConfig.Experimental = nil
}
// Finally write application configuration to fly.toml
if err := appConfig.WriteToDisk(ctx, configFilePath); err != nil {
return err
}
if srcInfo == nil {
return nil
}
ctx = appconfig.WithName(ctx, appConfig.AppName)
ctx = appconfig.WithConfig(ctx, appConfig)
if shouldUseMachines && !deployArgs.ForceYes {
if !flag.GetBool(ctx, "no-deploy") && !flag.GetBool(ctx, "now") && !flag.GetBool(ctx, "auto-confirm") && appConfig.HasNonHttpAndHttpsStandardServices() {
hasUdpService := appConfig.HasUdpService()
ipStuffStr := "a dedicated ipv4 address"
if !hasUdpService {
ipStuffStr = "dedicated ipv4 and ipv6 addresses"
}
confirmDedicatedIp, err := prompt.Confirmf(ctx, "Would you like to allocate %s now?", ipStuffStr)
if confirmDedicatedIp && err == nil {
v4Dedicated, err := client.AllocateIPAddress(ctx, appConfig.AppName, "v4", "", nil, "")
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Allocated dedicated ipv4: %s\n", v4Dedicated.Address)
if !hasUdpService {
v6Dedicated, err := client.AllocateIPAddress(ctx, appConfig.AppName, "v6", "", nil, "")
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Allocated dedicated ipv6: %s\n", v6Dedicated.Address)
}
}
}
}
// Notices from a launcher about its behavior that should always be displayed
if srcInfo.Notice != "" {
fmt.Fprintln(io.Out, srcInfo.Notice)
}
deployNow := false
promptForDeploy := true
if srcInfo.SkipDeploy || flag.GetBool(ctx, "no-deploy") {
deployNow = false
promptForDeploy = false
}
if flag.GetBool(ctx, "now") {
deployNow = true
promptForDeploy = false
}
if promptForDeploy {
confirm, err := prompt.Confirm(ctx, "Would you like to deploy now?")
if confirm && err == nil {
deployNow = true
}
}
if deployNow {
return deploy.DeployWithConfig(ctx, appConfig, deployArgs)
}
// Alternative deploy documentation if our standard deploy method is not correct
if srcInfo.DeployDocs != "" {
fmt.Fprintln(io.Out, srcInfo.DeployDocs)
} else {
fmt.Fprintln(io.Out, "Your app is ready! Deploy with `flyctl deploy`")
}
return nil
}
func shouldAppUseMachinesPlatform(ctx context.Context, orgSlug string) (bool, error) {
apiClient := client.FromContext(ctx).API()
if flag.GetBool(ctx, "force-machines") {
return true, nil
} else if flag.GetBool(ctx, "force-nomad") {
return false, nil
}
orgDefault, err := apiClient.GetAppsV2DefaultOnForOrg(ctx, orgSlug)
if err != nil {
return false, err
}
return orgDefault, nil
}
func appExists(ctx context.Context, cfg *appconfig.Config) (bool, *api.AppBasic, error) {
client := client.FromContext(ctx).API()
app, err := client.GetAppBasic(ctx, cfg.AppName)
if err != nil {
if api.IsNotFoundError(err) || graphql.IsNotFoundError(err) {
return false, nil, nil
}
return false, nil, err
}
return true, app, nil
}
func promptForAppName(ctx context.Context, cfg *appconfig.Config) (name string, err error) {
if cfg.AppName == "" {
return prompt.SelectAppName(ctx)
}
msg := fmt.Sprintf("Choose an app name (leaving blank will default to '%s')", cfg.AppName)
name, err = prompt.SelectAppNameWithMsg(ctx, msg)
if err != nil {
return name, err
}
// default to cfg.name if user doesn't enter any name after copying the configuration
if name == "" {
name = cfg.AppName
}
return
}