-
Notifications
You must be signed in to change notification settings - Fork 234
/
launch_frameworks.go
291 lines (247 loc) · 7.63 KB
/
launch_frameworks.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
package launch
import (
"context"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/samber/lo"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/helpers"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/scanner"
)
// satisfyScannerBeforeDb performs operations that the scanner requests that must be done before databases are created
func (state *launchState) satisfyScannerBeforeDb(ctx context.Context) error {
if err := state.scannerCreateFiles(ctx); err != nil {
return err
}
if err := state.scannerCreateSecrets(ctx); err != nil {
return err
}
return nil
}
// satisfyScannerBeforeDb performs operations that the scanner requests that must be done after databases are created
func (state *launchState) satisfyScannerAfterDb(ctx context.Context) error {
if err := state.scannerRunCallback(ctx); err != nil {
return err
}
if err := state.scannerRunInitCommands(ctx); err != nil {
return err
}
if err := state.scannerSetAppconfig(ctx); err != nil {
return err
}
return nil
}
func (state *launchState) scannerCreateFiles(ctx context.Context) error {
if state.sourceInfo == nil {
return nil
}
io := iostreams.FromContext(ctx)
for _, f := range state.sourceInfo.Files {
path := filepath.Join(state.workingDir, f.Path)
if helpers.FileExists(path) {
if flag.GetBool(ctx, "now") {
fmt.Fprintf(io.Out, "You specified --now, so not overwriting %s\n", path)
continue
}
confirm, err := prompt.ConfirmOverwrite(ctx, path)
if !confirm || err != nil {
continue
}
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
perms := 0o600
if strings.Contains(string(f.Contents), "#!") {
perms = 0o700
}
if err := os.WriteFile(path, f.Contents, fs.FileMode(perms)); err != nil {
return err
}
}
return nil
}
func (state *launchState) scannerCreateSecrets(ctx context.Context) error {
if state.sourceInfo == nil || len(state.sourceInfo.Secrets) == 0 {
return nil
}
var err error
io := iostreams.FromContext(ctx)
secrets := map[string]string{}
for _, secret := range state.sourceInfo.Secrets {
val := ""
// If a secret should be a random default, just generate it without displaying
// Otherwise, prompt to type it in
if secret.Generate != nil {
if val, err = secret.Generate(); err != nil {
return fmt.Errorf("could not generate random string: %w", err)
}
} else if secret.Value != "" {
val = secret.Value
} else {
prompt := fmt.Sprintf("Set secret %s:", secret.Key)
surveyInput := &survey.Input{Message: prompt, Help: secret.Help}
survey.AskOne(surveyInput, &val)
}
if val != "" {
secrets[secret.Key] = val
}
}
if len(secrets) > 0 {
apiClient := client.FromContext(ctx).API()
_, err := apiClient.SetSecrets(ctx, state.Plan.AppName, secrets)
if err != nil {
return err
}
fmt.Fprintf(io.Out, "Set secrets on %s: %s\n", state.Plan.AppName, strings.Join(lo.Keys(secrets), ", "))
}
return nil
}
func (state *launchState) scannerRunCallback(ctx context.Context) error {
if state.sourceInfo == nil || state.sourceInfo.Callback == nil {
return nil
}
err := state.sourceInfo.Callback(state.Plan.AppName, state.sourceInfo, state.Plan)
if state.sourceInfo.MergeConfig != nil {
if err == nil {
cfg, err := appconfig.LoadConfig(state.sourceInfo.MergeConfig.Name)
if err == nil {
// In theory, any part of the configuration could be merged here, but for now
// we will only copy over the processes, release command, and volume
if state.sourceInfo.Processes == nil {
state.sourceInfo.Processes = cfg.Processes
}
if state.sourceInfo.ReleaseCmd == "" && cfg.Deploy != nil {
state.sourceInfo.ReleaseCmd = cfg.Deploy.ReleaseCommand
}
if len(state.sourceInfo.Volumes) == 0 && len(cfg.Mounts) > 0 {
state.sourceInfo.Volumes = []scanner.Volume{cfg.Mounts[0]}
}
}
}
if state.sourceInfo.MergeConfig.Temporary {
_ = os.Remove(state.sourceInfo.MergeConfig.Name)
}
}
return err
}
func (state *launchState) scannerRunInitCommands(ctx context.Context) error {
if state.sourceInfo != nil && len(state.sourceInfo.InitCommands) > 0 {
for _, cmd := range state.sourceInfo.InitCommands {
if err := execInitCommand(ctx, cmd); err != nil {
return err
}
}
}
return nil
}
func execInitCommand(ctx context.Context, command scanner.InitCommand) (err error) {
io := iostreams.FromContext(ctx)
binary, err := exec.LookPath(command.Command)
if err != nil {
return fmt.Errorf("%s not found in $PATH - make sure app dependencies are installed and try again", command.Command)
}
fmt.Fprintln(io.Out, command.Description)
// Run a requested generator command, for example to generate a Dockerfile
cmd := exec.CommandContext(ctx, binary, command.Args...)
if err = cmd.Start(); err != nil {
return err
}
if err = cmd.Wait(); err != nil {
err = fmt.Errorf("failed running %s: %w ", cmd.String(), err)
}
return err
}
func (state *launchState) scannerSetAppconfig(ctx context.Context) error {
srcInfo := state.sourceInfo
appConfig := state.appConfig
// Complete the appConfig
if srcInfo == nil {
return nil
}
if srcInfo.Port > 0 {
appConfig.SetInternalPort(srcInfo.Port)
}
if srcInfo.HttpCheckPath != "" {
appConfig.SetHttpCheck(srcInfo.HttpCheckPath, srcInfo.HttpCheckHeaders)
}
if srcInfo.Concurrency != nil {
appConfig.SetConcurrency(srcInfo.Concurrency["soft_limit"], srcInfo.Concurrency["hard_limit"])
}
for envName, envVal := range srcInfo.Env {
if envVal == "APP_FQDN" {
appConfig.SetEnvVariable(envName, appConfig.AppName+".fly.dev")
} else {
appConfig.SetEnvVariable(envName, envVal)
}
}
if len(srcInfo.Statics) > 0 {
var appStatics []appconfig.Static
for _, s := range srcInfo.Statics {
appStatics = append(appStatics, appconfig.Static{
GuestPath: s.GuestPath,
UrlPrefix: s.UrlPrefix,
})
}
appConfig.SetStatics(appStatics)
}
if len(srcInfo.Volumes) > 0 {
var appVolumes []appconfig.Mount
for _, v := range srcInfo.Volumes {
appVolumes = append(appVolumes, appconfig.Mount{
Source: v.Source,
Destination: v.Destination,
})
}
appConfig.SetMounts(appVolumes)
}
if len(srcInfo.Processes) > 0 {
for procName, procCommand := range srcInfo.Processes {
appConfig.SetProcess(procName, procCommand)
// if processes are defined, associate HTTPService with the app service
// (if defined) or the first service if no app service is defined.
if appConfig.HTTPService != nil &&
(procName == "app" || appConfig.HTTPService.Processes == nil) {
appConfig.HTTPService.Processes = []string{procName}
}
}
}
if srcInfo.ReleaseCmd != "" {
appConfig.SetReleaseCommand(srcInfo.ReleaseCmd)
}
if srcInfo.DockerCommand != "" {
appConfig.SetDockerCommand(srcInfo.DockerCommand)
}
if srcInfo.ConsoleCommand != "" {
// no V1 compatibility for this feature so bypass setters
appConfig.ConsoleCommand = srcInfo.ConsoleCommand
}
if srcInfo.DockerEntrypoint != "" {
appConfig.SetDockerEntrypoint(srcInfo.DockerEntrypoint)
}
if srcInfo.KillSignal != "" {
appConfig.SetKillSignal(srcInfo.KillSignal)
}
// Append any requested Dockerfile entries
if len(srcInfo.DockerfileAppendix) > 0 {
if err := appendDockerfileAppendix(srcInfo.DockerfileAppendix); err != nil {
return fmt.Errorf("failed appending Dockerfile appendix: %w", err)
}
}
if len(srcInfo.BuildArgs) > 0 {
if appConfig.Build == nil {
appConfig.Build = &appconfig.Build{}
}
appConfig.Build.Args = srcInfo.BuildArgs
}
return nil
}