-
Notifications
You must be signed in to change notification settings - Fork 230
/
launch.go
170 lines (145 loc) · 4.88 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
package launch
import (
"context"
"fmt"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/tracing"
"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 {
ctx, span := tracing.GetTracer().Start(ctx, "state.launch")
defer span.End()
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.
if err := state.updateComputeFromDeprecatedGuestFields(ctx); err != nil {
return err
}
state.updateConfig(ctx)
if err := state.validateExtensions(ctx); err != nil {
return err
}
org, err := state.Org(ctx)
if err != nil {
return err
}
if !planValidateHighAvailability(ctx, state.Plan, org, !state.warnedNoCcHa) {
state.Plan.HighAvailability = false
state.warnedNoCcHa = true
}
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)
}
// Sentry
if err = state.launchSentry(ctx, app.Name); err != nil {
return err
}
// 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
}
// Apply the freestanding Guest fields to the appConfig's Compute field
// This is temporary, but allows us to start using Compute-based plans in flyctl *now* while the UI catches up in time.
func (state *launchState) updateComputeFromDeprecatedGuestFields(ctx context.Context) error {
if len(state.Plan.Compute) != 0 {
// If the UI returns a compute field, then we don't need to do any forward-compat patching.
return nil
}
// Fallback for versions of the UI that don't support a Compute field in the Plan.
defer func() {
// Set the plan's compute field to the calculated compute field.
// This makes sure that code expecting a compute definition in the plan is able to find it
// (and that it's up-to-date)
state.Plan.Compute = state.appConfig.Compute
}()
if compute := state.appConfig.ComputeForGroup(state.appConfig.DefaultProcessName()); compute != nil {
applyGuestToCompute(compute, state.Plan.Guest())
} else {
state.appConfig.Compute = append(state.appConfig.Compute, guestToCompute(state.Plan.Guest()))
}
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 {
if state.appConfig.HTTPService == nil {
state.appConfig.HTTPService = &appconfig.HTTPService{
ForceHTTPS: true,
AutoStartMachines: fly.Pointer(true),
AutoStopMachines: fly.Pointer(true),
MinMachinesRunning: fly.Pointer(0),
Processes: []string{"app"},
}
}
state.appConfig.HTTPService.InternalPort = state.Plan.HttpServicePort
} else {
state.appConfig.HTTPService = nil
}
state.appConfig.Compute = state.Plan.Compute
}
// createApp creates the fly.io app for the plan
func (state *launchState) createApp(ctx context.Context) (*fly.App, error) {
apiClient := fly.ClientFromContext(ctx)
org, err := state.Org(ctx)
if err != nil {
return nil, err
}
app, err := apiClient.CreateApp(ctx, fly.CreateAppInput{
OrganizationID: org.ID,
Name: state.Plan.AppName,
PreferredRegion: &state.Plan.RegionCode,
Machines: true,
})
if err != nil {
return nil, err
}
f, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{AppName: app.Name})
if err != nil {
return nil, err
} else if err := f.WaitForApp(ctx, app.Name); err != nil {
return nil, err
}
return app, nil
}