-
Notifications
You must be signed in to change notification settings - Fork 225
/
launch.go
462 lines (393 loc) · 13 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
package legacy
import (
"context"
"fmt"
"path/filepath"
"slices"
"strings"
"github.com/logrusorgru/aurora"
"github.com/pkg/errors"
"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/buildinfo"
"github.com/superfly/flyctl/internal/cmdutil"
"github.com/superfly/flyctl/internal/command/deploy"
"github.com/superfly/flyctl/internal/command/launch/plan"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/metrics"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/internal/set"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/scanner"
"github.com/superfly/graphql"
)
func Run(ctx context.Context) (err error) {
io := iostreams.FromContext(ctx)
client := client.FromContext(ctx).API()
workingDir := flag.GetString(ctx, "path")
// Note: this also disables --force-nomad when launching into an existing nomad app.
// we're fast-tracking the removal of nomad support, so this should be fine.
if flag.GetBool(ctx, "force-nomad") && !buildinfo.IsDev() {
return fmt.Errorf("creating new apps on the nomad platform is no longer supported")
}
metrics.Started(ctx, "launch")
defer func() {
metrics.Status(ctx, "launch", err == nil)
}()
// Determine the working directory
if absDir, err := filepath.Abs(workingDir); err == nil {
workingDir = absDir
}
configFilePath := filepath.Join(workingDir, appconfig.DefaultConfigFileName)
fmt.Fprintln(io.Out, "Creating app in", workingDir)
appConfig, copyConfig, err := determineBaseAppConfig(ctx)
if err != nil {
return err
}
var srcInfo *scanner.SourceInfo
srcInfo, appConfig.Build, err = determineSourceInfo(ctx, appConfig, copyConfig, workingDir)
if err != nil {
return err
}
appConfig.AppName, err = determineAppName(ctx, appConfig)
if err != nil {
return err
}
var org *api.Organization
var existingAppPlatform string
var launchIntoExistingApp bool
if appConfig.AppName != "" {
exists, app, err := appExists(ctx, appConfig)
if err != nil {
return err
}
if exists {
launchIntoExistingApp = flag.GetBool(ctx, "reuse-app")
if !flag.IsSpecified(ctx, "reuse-app") {
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
}
}
existingAppPlatform = app.PlatformVersion
org = &api.Organization{
ID: app.Organization.ID,
Name: app.Organization.Name,
Slug: app.Organization.Slug,
PaidPlan: app.Organization.PaidPlan,
}
}
}
// Prompt for an org
if org == nil {
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") {
// TODO: determine if eager remote builder is still required here
go imgsrc.EagerlyEnsureRemoteBuilder(ctx, client, org.Slug)
}
region, err := computeRegionToUse(ctx, appConfig, org.PaidPlan)
if err != nil {
return err
}
// Do not change PrimaryRegion after this line
appConfig.PrimaryRegion = region.Code
fmt.Fprintf(io.Out, "App will use '%s' region as primary\n\n", appConfig.PrimaryRegion)
shouldUseMachines, err := shouldAppUseMachinesPlatform(ctx, org.Slug, existingAppPlatform)
if err != nil {
return err
}
using_appsv1_only_feature := false
if s := flag.GetString(ctx, "strategy"); s != "" && !slices.Contains(appconfig.MachinesDeployStrategies, s) {
using_appsv1_only_feature = true
}
if !shouldUseMachines && !using_appsv1_only_feature {
fmt.Fprintf(io.ErrOut, "%s Apps v1 Platform is deprecated. We recommend using the --force-machines flag, or setting\nyour organization's default for new apps to Apps v2 with 'fly orgs apps-v2 default-on <org-name>'\n", aurora.Yellow("WARN"))
}
var envVars map[string]string = nil
envFlags := flag.GetStringArray(ctx, "env")
if len(envFlags) > 0 {
envVars, err = cmdutil.ParseKVStringsToMap(envFlags)
if err != nil {
return errors.Wrap(err, "parsing --env flags")
}
}
if copyConfig && shouldUseMachines {
// 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)
}
}
switch {
// App exists and it is not importing existing fly.toml
case launchIntoExistingApp && !copyConfig:
appConfig, err = freshV2Config(appConfig.AppName, appConfig)
if err != nil {
return err
}
// App doesn't exist, just create a new app
case !launchIntoExistingApp:
createdApp, err := client.CreateApp(ctx, api.CreateAppInput{
Name: appConfig.AppName,
OrganizationID: org.ID,
PreferredRegion: &appConfig.PrimaryRegion,
Machines: shouldUseMachines,
})
if err != nil {
return err
}
switch {
case copyConfig:
appConfig.AppName = createdApp.Name
case shouldUseMachines:
appConfig, err = freshV2Config(createdApp.Name, appConfig)
if err != nil {
return fmt.Errorf("failed to create new V2 app configuration: %w", err)
}
default:
// Use the default configuration template suggested by Web
appConfig, err = freshV1Config(createdApp.Name, appConfig, &createdApp.Config.Definition)
if err != nil {
return fmt.Errorf("failed to get new configuration: %w", err)
}
}
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 envVars != nil {
appConfig.SetEnvVariables(envVars)
}
// 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, appConfig.PrimaryRegion); 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
}
plan := buildPlanFromLegacyOptions(appConfig, region, org, srcInfo, options)
// Invoke Callback, if any
if err := runCallback(ctx, appConfig.AppName, srcInfo, plan); 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)
}
// Finally write application configuration to fly.toml
appConfig.SetConfigFilePath(configFilePath)
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)
// 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
}
// Reload and validate the app config in case the user edited it before confirming
if deployNow {
path := appConfig.ConfigFilePath()
newCfg, err := appconfig.LoadConfig(path)
if err != nil {
return fmt.Errorf("failed to reload configuration file %s: %w", path, err)
}
if appConfig.ForMachines() {
if err := newCfg.SetMachinesPlatform(); err != nil {
return fmt.Errorf("failed to parse fly.toml, check the configuration format: %w", err)
}
}
appConfig = newCfg
}
}
err, extraInfo := appConfig.Validate(ctx)
if extraInfo != "" {
fmt.Fprintf(io.ErrOut, extraInfo)
}
if err != nil {
return fmt.Errorf("invalid configuration file: %w", err)
}
if deployNow {
return deploy.DeployWithConfig(ctx, appConfig, flag.GetBool(ctx, "now"), nil)
}
// 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, existingAppPlatform string) (bool, error) {
forceMachines := flag.GetBool(ctx, "force-machines")
forceNomad := flag.GetBool(ctx, "force-nomad")
// Keep if we are reusing an app and it has platform version set
if existingAppPlatform != "" {
isV2 := existingAppPlatform == appconfig.MachinesPlatform
switch {
case forceMachines && !isV2:
return false, fmt.Errorf("--force-machines won't work for existing app in nomad platform")
case forceNomad && isV2:
return false, fmt.Errorf("--force-nomad won't work for existing app in machines platform")
default:
return isV2, nil
}
}
// Otherwise looks for CLI flags and organization defaults
if forceMachines {
return true, nil
} else if forceNomad {
return false, nil
}
// Default to Apps v2
return true, 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
}
// determineBaseAppConfig looks for existing app config, ask to reuse or returns an empty config
func determineBaseAppConfig(ctx context.Context) (*appconfig.Config, bool, error) {
io := iostreams.FromContext(ctx)
existingConfig := appconfig.ConfigFromContext(ctx)
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")
}
copyConfig := flag.GetBool(ctx, "copy-config")
if !flag.IsSpecified(ctx, "copy-config") {
var err error
copyConfig, err = prompt.Confirm(ctx, "Would you like to copy its configuration to the new app?")
switch {
case prompt.IsNonInteractive(err) && !flag.GetYes(ctx):
return nil, false, err
case err != nil:
return nil, false, err
}
}
if copyConfig {
return existingConfig, true, nil
}
}
return appconfig.NewConfig(), false, nil
}
func buildPlanFromLegacyOptions(
appConfig *appconfig.Config,
region *api.Region,
org *api.Organization,
sourceInfo *scanner.SourceInfo,
options set.Set[string],
) *plan.LaunchPlan {
// Sensible default, but not necessarily what's about to be launched.
// Should work fine though, at least until we (hopefully soon) rip this legacy code out.
guest := api.MachinePresets["shared-cpu-1x"]
launchPlan := &plan.LaunchPlan{
AppName: appConfig.AppName,
RegionCode: region.Code,
OrgSlug: org.Slug,
CPUKind: guest.CPUKind,
CPUs: guest.CPUs,
MemoryMB: guest.MemoryMB,
VmSize: guest.ToSize(),
}
if sourceInfo != nil {
launchPlan.ScannerFamily = sourceInfo.Family
}
if options.Has("postgres") {
launchPlan.Postgres = plan.DefaultPostgres(launchPlan)
}
if options.Has("redis") {
launchPlan.Redis = plan.DefaultRedis(launchPlan)
}
return launchPlan
}
func determineAppName(ctx context.Context, appConfig *appconfig.Config) (string, error) {
generateName := flag.GetBool(ctx, "generate-name")
if generateName {
return "", nil
}
name := strings.TrimSpace(flag.GetString(ctx, "name"))
if name != "" {
return name, nil
}
switch name, err := promptForAppName(ctx, appConfig); {
case prompt.IsNonInteractive(err):
return "", fmt.Errorf("--name or --generate-name flags must be specified when not running interactively")
case err != nil:
return "", err
default:
return name, nil
}
}