-
Notifications
You must be signed in to change notification settings - Fork 240
/
init.go
331 lines (273 loc) · 8.4 KB
/
init.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
package cmd
import (
"fmt"
"os"
"strconv"
"github.com/superfly/flyctl/cmdctx"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/cmd/presenters"
"github.com/superfly/flyctl/docstrings"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/helpers"
)
//TODO: Move all output to status styled begin/done updates
func newInitCommand() *Command {
initStrings := docstrings.Get("init")
cmd := BuildCommandKS(nil, runInit, initStrings, os.Stdout, requireSession)
cmd.Args = cobra.RangeArgs(0, 1)
// TODO: Move flag descriptions into the docStrings
cmd.AddStringFlag(StringFlagOpts{
Name: "name",
Description: "The app name to use",
})
cmd.AddStringFlag(StringFlagOpts{
Name: "org",
Description: `The organization that will own the app`,
})
cmd.AddStringFlag(StringFlagOpts{
Name: "port",
Shorthand: "p",
Description: "Internal port on application to connect to external services",
})
cmd.AddStringFlag(StringFlagOpts{
Name: "builder",
Description: `The Cloud Native Buildpacks builder to use when deploying the app`,
})
cmd.AddStringFlag(StringFlagOpts{
Name: "builtin",
Description: `The Fly Runtime to use for building the app`,
})
cmd.AddStringFlag(StringFlagOpts{
Name: "image",
Description: `Deploy this named image`,
})
cmd.AddBoolFlag(BoolFlagOpts{
Name: "dockerfile",
Description: `Use a dockerfile when deploying the app`,
Default: false,
})
cmd.AddStringFlag(StringFlagOpts{
Name: "import",
Description: "Create but import all settings from the given file",
})
cmd.AddBoolFlag(BoolFlagOpts{
Name: "overwrite",
Description: "Always silently overwrite an existing fly.toml file",
})
cmd.AddBoolFlag(BoolFlagOpts{
Name: "nowrite",
Description: "Never write a fly.toml file",
})
cmd.AddBoolFlag(BoolFlagOpts{
Name: "generatename",
Description: "Always generate a name for the app", Hidden: true,
})
return cmd
}
func runInit(commandContext *cmdctx.CmdContext) error {
var appName = ""
var internalPort = 0
if len(commandContext.Args) > 0 {
appName = commandContext.Args[0]
}
configPort, _ := commandContext.Config.GetString("port")
// If ports set, validate
if configPort != "" {
var err error
internalPort, err = strconv.Atoi(configPort)
if err != nil {
return fmt.Errorf(`-p ports must be numeric`)
}
}
overwrite := commandContext.Config.GetBool("overwrite")
nowrite := commandContext.Config.GetBool("nowrite")
configfilename, err := flyctl.ResolveConfigFileFromPath(commandContext.WorkingDir)
if err != nil {
return err
}
newAppConfig := flyctl.NewAppConfig()
if !nowrite {
if helpers.FileExists(configfilename) {
if !overwrite {
commandContext.Status("init", cmdctx.SERROR, "An existing configuration file has been found.")
confirmation := confirm(fmt.Sprintf("Overwrite file '%s'", configfilename))
if !confirmation {
return nil
}
}
}
}
name := ""
if !commandContext.Config.GetBool("generatename") {
name, _ = commandContext.Config.GetString("name")
if name != "" && appName != "" {
return fmt.Errorf(`two app names specified %s and %s. Select and specify only one`, appName, name)
}
if name == "" && appName != "" {
name = appName
}
fmt.Println()
if name == "" {
prompt := &survey.Input{
Message: "App Name (leave blank to use an auto-generated name)",
}
if err := survey.AskOne(prompt, &name); err != nil {
if isInterrupt(err) {
return nil
}
}
} else {
fmt.Printf("Selected App Name: %s\n", name)
}
}
fmt.Println()
targetOrgSlug, _ := commandContext.Config.GetString("org")
org, err := selectOrganization(commandContext.Client.API(), targetOrgSlug)
switch {
case isInterrupt(err):
return nil
case err != nil || org == nil:
return fmt.Errorf("Error setting organization: %s", err)
}
fmt.Println()
builder, err := commandContext.Config.GetString("builder")
if err != nil {
return err
}
builtinname, _ := commandContext.Config.GetString("builtin")
if err != nil {
return err
}
importfile, err := commandContext.Config.GetString("import")
if err != nil {
return err
}
imagename, err := commandContext.Config.GetString("image")
if err != nil {
return err
}
if !nowrite {
// If we are importing or using a builtin, assume builders are set in the template
if importfile == "" && builtinname == "" && imagename == "" {
// Otherwise get a Builder from the user while checking the dockerfile setting
dockerfileSet := commandContext.Config.IsSet("dockerfile")
dockerfile := commandContext.Config.GetBool("dockerfile")
if builder == "" && !dockerfileSet {
builder, builtin, err := selectBuildtype(commandContext)
switch {
case isInterrupt(err):
return nil
case err != nil || builder == "":
return fmt.Errorf("Error setting builder: %s", err)
}
// If image, prompt for name
if builder == "Image" {
imagename, err = selectImage(commandContext)
if err != nil {
return err
}
} else if builder != "Dockerfile" && builder != "None" && !builtin {
// Not a dockerfile setting and not set to none. This is a classic buildpack
newAppConfig.Build = &flyctl.Build{Builder: builder}
} else if builder != "None" && builtin {
// Builder not none and the user apparently selected a builtin builder
builtinname = builder
}
} else if builder != "" {
// If the builder was set and there's not dockerfile setting, write the builder
if !dockerfile {
newAppConfig.Build = &flyctl.Build{Builder: builder}
}
}
}
}
// The creation magic happens here....
app, err := commandContext.Client.API().CreateApp(name, org.ID)
if err != nil {
return err
}
if !nowrite {
if imagename != "" {
newAppConfig.AppName = app.Name
newAppConfig.Build = &flyctl.Build{Image: imagename}
newAppConfig.Definition = app.Config.Definition
} else if importfile != "" {
if !commandContext.OutputJSON() {
fmt.Printf("Importing configuration from %s\n", importfile)
}
tmpappconfig, err := flyctl.LoadAppConfig(importfile)
if err != nil {
return err
}
newAppConfig = tmpappconfig
// And then overwrite the app name
newAppConfig.AppName = app.Name
} else if builtinname != "" {
newAppConfig.AppName = app.Name
newAppConfig.Build = &flyctl.Build{Builtin: builtinname}
newAppConfig.Definition = app.Config.Definition
} else if builder != "None" {
newAppConfig.AppName = app.Name
newAppConfig.Definition = app.Config.Definition
}
if configPort != "" { // If the config port has been set externally, set that
newAppConfig.SetInternalPort(internalPort)
} else if importfile != "" {
if newAppConfig.HasServices() {
currentport, err := newAppConfig.GetInternalPort()
if err != nil {
return err
}
if !commandContext.OutputJSON() {
fmt.Printf("Importing port %d\n", currentport)
}
}
} else if builtinname != "" {
if !commandContext.OutputJSON() {
fmt.Printf("Builtins use port 8080\n")
}
newAppConfig.SetInternalPort(8080)
} else {
// If we are not importing and not running a builtin, get the default, ask for new setting
currentport, err := newAppConfig.GetInternalPort()
if err != nil {
return err
}
internalPort, err = selectPort(commandContext, currentport)
if err != nil {
return err
}
newAppConfig.SetInternalPort(internalPort)
}
if commandContext.OutputJSON() {
commandContext.WriteJSON(app)
return nil
}
err = commandContext.Frender(cmdctx.PresenterOption{Presentable: &presenters.AppInfo{App: *app}, HideHeader: true, Vertical: true, Title: "New app created"})
if err != nil {
return err
}
fmt.Printf("App will initially deploy to %s (%s) region\n\n", (*app.Regions)[0].Code, (*app.Regions)[0].Name)
if commandContext.ConfigFile == "" {
newCfgFile, err := flyctl.ResolveConfigFileFromPath(commandContext.WorkingDir)
if err != nil {
return err
}
commandContext.ConfigFile = newCfgFile
}
commandContext.AppName = app.Name
commandContext.AppConfig = newAppConfig
return writeAppConfig(commandContext.ConfigFile, newAppConfig)
}
fmt.Printf("New app created: %s", app.Name)
f, err := os.OpenFile("fly.alias", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err = f.WriteString(app.Name + "\n"); err != nil {
return err
}
return nil
}