-
Notifications
You must be signed in to change notification settings - Fork 223
/
launch.go
104 lines (89 loc) · 2.85 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
package launch
import (
"context"
"fmt"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/iostreams"
)
// Launch launches the app described by the plan. This is the main entry point for launching a plan.
func (state *launchState) Launch(ctx context.Context) error {
io := iostreams.FromContext(ctx)
// TODO(Allison): are we still supporting the launch-into usecase?
// I'm assuming *not* for now, because it's confusing UX and this
// is the perfect time to remove it.
state.updateConfig(ctx)
app, err := state.createApp(ctx)
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Created app '%s' in organization '%s'\n", app.Name, app.Organization.Slug)
fmt.Fprintf(io.Out, "Admin URL: https://fly.io/apps/%s\n", app.Name)
fmt.Fprintf(io.Out, "Hostname: %s.fly.dev\n", app.Name)
if err = state.satisfyScannerBeforeDb(ctx); err != nil {
return err
}
// TODO: Return rich info about provisioned DBs, including things
// like public URLs.
err = state.createDatabases(ctx)
if err != nil {
return err
}
if err = state.satisfyScannerAfterDb(ctx); err != nil {
return err
}
if err = state.createDockerIgnore(ctx); err != nil {
return err
}
// Override internal port if requested using --internal-port flag
if n := flag.GetInt(ctx, "internal-port"); n > 0 {
state.appConfig.SetInternalPort(n)
}
// Finally write application configuration to fly.toml
state.appConfig.SetConfigFilePath(state.configPath)
if err := state.appConfig.WriteToDisk(ctx, state.configPath); err != nil {
return err
}
if state.sourceInfo != nil {
if err := state.firstDeploy(ctx); err != nil {
return err
}
}
return nil
}
// updateConfig populates the appConfig with the plan's values
func (state *launchState) updateConfig(ctx context.Context) {
state.appConfig.AppName = state.Plan.AppName
state.appConfig.PrimaryRegion = state.Plan.RegionCode
if state.env != nil {
state.appConfig.SetEnvVariables(state.env)
}
if state.Plan.HttpServicePort != 0 {
state.appConfig.HTTPService = &appconfig.HTTPService{
InternalPort: state.Plan.HttpServicePort,
ForceHTTPS: true,
AutoStartMachines: api.Pointer(true),
AutoStopMachines: api.Pointer(true),
MinMachinesRunning: api.Pointer(1),
Processes: []string{"app"},
}
} else {
state.appConfig.HTTPService = nil
}
}
// createApp creates the fly.io app for the plan
func (state *launchState) createApp(ctx context.Context) (*api.App, error) {
apiClient := client.FromContext(ctx).API()
org, err := state.Org(ctx)
if err != nil {
return nil, err
}
return apiClient.CreateApp(ctx, api.CreateAppInput{
OrganizationID: org.ID,
Name: state.Plan.AppName,
PreferredRegion: &state.Plan.RegionCode,
Machines: true,
})
}