-
Notifications
You must be signed in to change notification settings - Fork 9
/
setupautopush.go
119 lines (98 loc) · 3.37 KB
/
setupautopush.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package eks
import (
"context"
"fmt"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/prototext"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/compute"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/console/colors"
"namespacelabs.dev/foundation/internal/fnerrors"
awsprovider "namespacelabs.dev/foundation/internal/providers/aws"
"namespacelabs.dev/foundation/internal/providers/aws/auth"
"namespacelabs.dev/foundation/internal/providers/aws/eks"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/std/execution"
)
const appURL = "https://github.com/apps/obsolete-namespace-ci/installations/new"
func newSetupAutopushCmd() *cobra.Command {
var iamRole string
var dryRun bool
cmd := fncobra.CmdWithEnv(&cobra.Command{
Use: "setup-autopush",
Short: "Sets up production cluster for automatic deployments to a staging environment.",
Args: cobra.NoArgs,
}, func(ctx context.Context, env cfg.Context, args []string) error {
cluster, err := runtime.ClusterFor(ctx, env)
if err != nil {
return err
}
acc, err := getAwsAccount(ctx, env)
if err != nil {
return err
}
roleArn := fmt.Sprintf("arn:aws:iam::%s:role/%s", acc, iamRole)
s, err := eks.NewSession(ctx, env.Configuration())
if err != nil {
return err
}
eksCluster, err := eks.PrepareClusterInfo(ctx, s)
if err != nil {
return err
}
if eksCluster == nil {
return fnerrors.New("not an eks cluster")
}
result, err := eks.SetupAutopush(eksCluster, iamRole, roleArn)
if err != nil {
return err
}
stdout := console.Stdout(ctx)
p := execution.NewEmptyPlan()
for _, inv := range result {
def, err := inv.ToDefinition()
if err != nil {
return err
}
if dryRun {
fmt.Fprintln(stdout, prototext.Format(def))
} else {
p.Add(def)
}
}
if dryRun {
fmt.Fprintf(stdout, "Not making changes to the cluster, as --dry_run=true.\n\n")
} else {
if err := execution.Execute(ctx, "eks.autopush.apply", p, nil, execution.FromContext(env), runtime.ClusterInjection.With(cluster)); err != nil {
return err
}
}
fmt.Fprintf(stdout, "Success!\nNext steps:\n")
fmt.Fprintf(stdout, " 1. Please inform a Namespace Labs dev that %q has been set up for autopush so they can whitelist it for deployment. %s\n", roleArn, colors.Ctx(ctx).Comment.Apply("This step will be automated in future."))
fmt.Fprintf(stdout, " 2. Install %s into your Github repository.\n", appURL)
return nil
})
cmd.Flags().StringVar(&iamRole, "iam_role", "namespace-ci", "IAM Role to create.")
cmd.Flags().BoolVar(&dryRun, "dry_run", true, "If true, print invocations, rather than executing them.")
return cmd
}
func getAwsAccount(ctx context.Context, env cfg.Context) (string, error) {
cfg, err := awsprovider.MustConfiguredSession(ctx, env.Configuration())
if err != nil {
return "", err
}
caller := auth.ResolveWithConfig(cfg)
res, err := compute.Get(ctx, caller)
if err != nil {
return "", err
}
if res.Value.Account == nil {
return "", fmt.Errorf("unable to fetch AWS account")
}
return *res.Value.Account, nil
}