/
env_init.go
363 lines (321 loc) · 12.2 KB
/
env_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
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package cli
import (
"errors"
"fmt"
"strings"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/aws/cloudformation"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/aws/identity"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/aws/profile"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/aws/session"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/config"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/deploy"
deploycfn "github.com/aws/amazon-ecs-cli-v2/internal/pkg/deploy/cloudformation"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/term/color"
"github.com/aws/amazon-ecs-cli-v2/internal/pkg/term/log"
termprogress "github.com/aws/amazon-ecs-cli-v2/internal/pkg/term/progress"
"github.com/spf13/cobra"
)
const (
envInitNamePrompt = "What is your environment's name?"
envInitNameHelpPrompt = "A unique identifier for an environment (e.g. dev, test, prod)."
fmtEnvInitProfilePrompt = "Which named profile should we use to create %s?"
envInitProfileHelpPrompt = "The AWS CLI named profile with the permissions to create an environment."
)
const (
fmtDeployEnvStart = "Proposing infrastructure changes for the %s environment."
fmtDeployEnvComplete = "Environment %s already exists in application %s.\n"
fmtDeployEnvFailed = "Failed to accept changes for the %s environment.\n"
fmtDNSDelegationStart = "Sharing DNS permissions for this application to account %s."
fmtDNSDelegationFailed = "Failed to grant DNS permissions to account %s.\n"
fmtDNSDelegationComplete = "Shared DNS permissions for this application to account %s.\n"
fmtStreamEnvStart = "Creating the infrastructure for the %s environment."
fmtStreamEnvFailed = "Failed to create the infrastructure for the %s environment.\n"
fmtStreamEnvComplete = "Created the infrastructure for the %s environment.\n"
fmtAddEnvToAppStart = "Linking account %s and region %s to application %s."
fmtAddEnvToAppFailed = "Failed to link account %s and region %s to application %s.\n"
fmtAddEnvToAppComplete = "Linked account %s and region %s to application %s.\n"
)
var (
errNamedProfilesNotFound = fmt.Errorf("no named AWS profiles found, run %s first please", color.HighlightCode("aws configure"))
)
type initEnvVars struct {
*GlobalOpts
EnvName string // Name of the environment.
EnvProfile string // AWS profile used to create an environment.
IsProduction bool // Marks the environment as "production" to create it with additional guardrails.
}
type initEnvOpts struct {
initEnvVars
// Interfaces to interact with dependencies.
store store
envDeployer deployer
appDeployer deployer
identity identityService
envIdentity identityService
profileConfig profileNames
prog progress
// initialize profile-specific env clients
initProfileClients func(*initEnvOpts) error
}
var initEnvProfileClients = func(o *initEnvOpts) error {
profileSess, err := session.NewProvider().FromProfile(o.EnvProfile)
if err != nil {
return fmt.Errorf("create session from profile %s: %w", o.EnvProfile, err)
}
o.envIdentity = identity.New(profileSess)
o.envDeployer = deploycfn.New(profileSess)
return nil
}
func newInitEnvOpts(vars initEnvVars) (*initEnvOpts, error) {
store, err := config.NewStore()
if err != nil {
return nil, err
}
sessProvider := session.NewProvider()
defaultSession, err := sessProvider.Default()
if err != nil {
return nil, err
}
cfg, err := profile.NewConfig()
if err != nil {
return nil, fmt.Errorf("read named profiles: %w", err)
}
return &initEnvOpts{
initEnvVars: vars,
store: store,
appDeployer: deploycfn.New(defaultSession),
identity: identity.New(defaultSession),
profileConfig: cfg,
prog: termprogress.NewSpinner(),
initProfileClients: initEnvProfileClients,
}, nil
}
// Validate returns an error if the values passed by flags are invalid.
func (o *initEnvOpts) Validate() error {
if o.EnvName != "" {
if err := validateEnvironmentName(o.EnvName); err != nil {
return err
}
}
if o.AppName() == "" {
return fmt.Errorf("no application found: run %s or %s into your workspace please", color.HighlightCode("app init"), color.HighlightCode("cd"))
}
return nil
}
// Ask asks for fields that are required but not passed in.
func (o *initEnvOpts) Ask() error {
if err := o.askEnvName(); err != nil {
return err
}
return o.askEnvProfile()
}
// Execute deploys a new environment with CloudFormation and adds it to SSM.
func (o *initEnvOpts) Execute() error {
app, err := o.store.GetApplication(o.AppName())
if err != nil {
// Ensure the app actually exists before we do a deployment.
return err
}
if err = o.initProfileClients(o); err != nil {
return err
}
if app.RequiresDNSDelegation() {
if err := o.delegateDNSFromApp(app); err != nil {
return fmt.Errorf("granting DNS permissions: %w", err)
}
}
// 1. Start creating the CloudFormation stack for the environment.
if err := o.deployEnv(app); err != nil {
return err
}
// 2. Get the environment
env, err := o.envDeployer.GetEnvironment(o.AppName(), o.EnvName)
if err != nil {
return fmt.Errorf("get environment struct for %s: %w", o.EnvName, err)
}
env.Prod = o.IsProduction
// 3. Add the stack set instance to the app stackset.
if err := o.addToStackset(app, env); err != nil {
return err
}
// 4. Store the environment in SSM.
if err := o.store.CreateEnvironment(env); err != nil {
return fmt.Errorf("store environment: %w", err)
}
log.Successf("Created environment %s in region %s under application %s.\n",
color.HighlightUserInput(env.Name), color.Emphasize(env.Region), color.HighlightUserInput(env.App))
return nil
}
func (o *initEnvOpts) deployEnv(app *config.Application) error {
caller, err := o.identity.Get()
if err != nil {
return fmt.Errorf("get identity: %w", err)
}
deployEnvInput := &deploy.CreateEnvironmentInput{
Name: o.EnvName,
AppName: o.AppName(),
Prod: o.IsProduction,
PublicLoadBalancer: true, // TODO: configure this based on user input or service Type needs?
ToolsAccountPrincipalARN: caller.RootUserARN,
AppDNSName: app.Domain,
AdditionalTags: app.Tags,
}
o.prog.Start(fmt.Sprintf(fmtDeployEnvStart, color.HighlightUserInput(o.EnvName)))
if err := o.envDeployer.DeployEnvironment(deployEnvInput); err != nil {
var existsErr *cloudformation.ErrStackAlreadyExists
if errors.As(err, &existsErr) {
// Do nothing if the stack already exists.
o.prog.Stop(log.Ssuccessf(fmtDeployEnvComplete,
color.HighlightUserInput(o.EnvName), color.HighlightUserInput(o.AppName())))
return nil
}
o.prog.Stop(log.Serrorf(fmtDeployEnvFailed, color.HighlightUserInput(o.EnvName)))
return err
}
// Display updates while the deployment is happening.
o.prog.Start(fmt.Sprintf(fmtStreamEnvStart, color.HighlightUserInput(o.EnvName)))
stackEvents, responses := o.envDeployer.StreamEnvironmentCreation(deployEnvInput)
for stackEvent := range stackEvents {
o.prog.Events(o.humanizeEnvironmentEvents(stackEvent))
}
resp := <-responses
if resp.Err != nil {
o.prog.Stop(log.Serrorf(fmtStreamEnvFailed, color.HighlightUserInput(o.EnvName)))
return resp.Err
}
o.prog.Stop(log.Ssuccessf(fmtStreamEnvComplete, color.HighlightUserInput(o.EnvName)))
return nil
}
func (o *initEnvOpts) addToStackset(app *config.Application, env *config.Environment) error {
o.prog.Start(fmt.Sprintf(fmtAddEnvToAppStart, color.Emphasize(env.AccountID), color.Emphasize(env.Region), color.HighlightUserInput(o.AppName())))
if err := o.appDeployer.AddEnvToApp(app, env); err != nil {
o.prog.Stop(log.Serrorf(fmtAddEnvToAppFailed, color.Emphasize(env.AccountID), color.Emphasize(env.Region), color.HighlightUserInput(o.AppName())))
return fmt.Errorf("deploy env %s to application %s: %w", env.Name, app.Name, err)
}
o.prog.Stop(log.Ssuccessf(fmtAddEnvToAppComplete, color.Emphasize(env.AccountID), color.Emphasize(env.Region), color.HighlightUserInput(o.AppName())))
return nil
}
func (o *initEnvOpts) delegateDNSFromApp(app *config.Application) error {
envAccount, err := o.envIdentity.Get()
if err != nil {
return fmt.Errorf("getting environment account ID for DNS Delegation: %w", err)
}
// By default, our DNS Delegation permits same account delegation.
if envAccount.Account == app.AccountID {
return nil
}
o.prog.Start(fmt.Sprintf(fmtDNSDelegationStart, color.HighlightUserInput(envAccount.Account)))
if err := o.appDeployer.DelegateDNSPermissions(app, envAccount.Account); err != nil {
o.prog.Stop(log.Serrorf(fmtDNSDelegationFailed, color.HighlightUserInput(envAccount.Account)))
return err
}
o.prog.Stop(log.Ssuccessf(fmtDNSDelegationComplete, color.HighlightUserInput(envAccount.Account)))
return nil
}
func (o *initEnvOpts) askEnvName() error {
if o.EnvName != "" {
return nil
}
envName, err := o.prompt.Get(envInitNamePrompt, envInitNameHelpPrompt, validateEnvironmentName)
if err != nil {
return fmt.Errorf("prompt to get environment name: %w", err)
}
o.EnvName = envName
return nil
}
func (o *initEnvOpts) askEnvProfile() error {
// TODO: add this behavior to selector pkg.
if o.EnvProfile != "" {
return nil
}
names := o.profileConfig.Names()
if len(names) == 0 {
return errNamedProfilesNotFound
}
profile, err := o.prompt.SelectOne(
fmt.Sprintf(fmtEnvInitProfilePrompt, color.HighlightUserInput(o.EnvName)),
envInitProfileHelpPrompt,
names)
if err != nil {
return fmt.Errorf("get the profile name: %w", err)
}
o.EnvProfile = profile
return nil
}
func (o *initEnvOpts) humanizeEnvironmentEvents(resourceEvents []deploy.ResourceEvent) []termprogress.TabRow {
matcher := map[termprogress.Text]termprogress.ResourceMatcher{
textVPC: func(event deploy.Resource) bool {
return event.Type == "AWS::EC2::VPC"
},
textInternetGateway: func(event deploy.Resource) bool {
return event.Type == "AWS::EC2::InternetGateway" ||
event.Type == "AWS::EC2::VPCGatewayAttachment"
},
textPublicSubnets: func(event deploy.Resource) bool {
return event.Type == "AWS::EC2::Subnet" &&
strings.HasPrefix(event.LogicalName, "Public")
},
textPrivateSubnets: func(event deploy.Resource) bool {
return event.Type == "AWS::EC2::Subnet" &&
strings.HasPrefix(event.LogicalName, "Private")
},
textRouteTables: func(event deploy.Resource) bool {
return strings.Contains(event.LogicalName, "Route")
},
textECSCluster: func(event deploy.Resource) bool {
return event.Type == "AWS::ECS::Cluster"
},
textALB: func(event deploy.Resource) bool {
return strings.Contains(event.LogicalName, "LoadBalancer") ||
strings.Contains(event.Type, "ElasticLoadBalancingV2")
},
}
resourceCounts := map[termprogress.Text]int{
textVPC: 1,
textInternetGateway: 2,
textPublicSubnets: 2,
textPrivateSubnets: 2,
textRouteTables: 4,
textECSCluster: 1,
textALB: 4,
}
return termprogress.HumanizeResourceEvents(envProgressOrder, resourceEvents, matcher, resourceCounts)
}
// RecommendedActions returns follow-up actions the user can take after successfully executing the command.
func (o *initEnvOpts) RecommendedActions() []string {
return nil
}
// BuildEnvInitCmd builds the command for adding an environment.
func BuildEnvInitCmd() *cobra.Command {
vars := initEnvVars{
GlobalOpts: NewGlobalOpts(),
}
cmd := &cobra.Command{
Use: "init",
Short: "Creates a new environment in your application.",
Example: `
Creates a test environment in your "default" AWS profile.
/code $ copilot env init --name test --profile default
Creates a prod-iad environment using your "prod-admin" AWS profile.
/code $ copilot env init --name prod-iad --profile prod-admin --prod`,
RunE: runCmdE(func(cmd *cobra.Command, args []string) error {
opts, err := newInitEnvOpts(vars)
if err != nil {
return err
}
if err := opts.Validate(); err != nil {
return err
}
if err := opts.Ask(); err != nil {
return err
}
return opts.Execute()
}),
}
cmd.Flags().StringVarP(&vars.EnvName, nameFlag, nameFlagShort, "", envFlagDescription)
cmd.Flags().StringVar(&vars.EnvProfile, profileFlag, "", profileFlagDescription)
cmd.Flags().BoolVar(&vars.IsProduction, prodEnvFlag, false, prodEnvFlagDescription)
return cmd
}