-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathcd.go
More file actions
223 lines (192 loc) · 6.33 KB
/
Copy pathcd.go
File metadata and controls
223 lines (192 loc) · 6.33 KB
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
package command
import (
"errors"
"github.com/DefangLabs/defang/src/pkg/cli"
"github.com/DefangLabs/defang/src/pkg/cli/client"
"github.com/DefangLabs/defang/src/pkg/cli/client/byoc/aws"
"github.com/DefangLabs/defang/src/pkg/cli/compose"
"github.com/DefangLabs/defang/src/pkg/term"
"github.com/spf13/cobra"
)
var cdCmd = &cobra.Command{
Use: "cd",
Aliases: []string{"bootstrap", "pulumi"},
Args: cobra.NoArgs,
Short: "Manually run a command with the CD task (for BYOC only)",
Hidden: true,
}
func cdCommand(cmd *cobra.Command, command client.CdCommand, args []string, fabric client.FabricClient) error {
ctx := cmd.Context()
allowUpgrade, _ := cmd.Flags().GetBool("allow-upgrade")
session, err := newCommandSession(cmd)
if err != nil {
return err
}
if len(args) == 0 {
projectName, err := client.LoadProjectNameWithFallback(ctx, session.Loader, session.Provider)
if err != nil {
return err
}
args = []string{projectName}
}
var errs []error
for _, projectName := range args {
err := canIUseProvider(ctx, session.Provider, projectName, 0, allowUpgrade)
if err != nil {
return err
}
errs = append(errs, cli.CdCommandAndTail(ctx, session.Provider, projectName, global.Verbose, command, fabric))
}
return errors.Join(errs...)
}
var cdDestroyCmd = &cobra.Command{
Use: "destroy [PROJECT...]",
Annotations: authNeededAlways, // need subscription
Short: "Destroy the service stack",
RunE: func(cmd *cobra.Command, args []string) error {
return cdCommand(cmd, client.CdCommandDestroy, args, global.Client)
},
}
var cdDownCmd = &cobra.Command{
Use: "down [PROJECT...]",
Annotations: authNeededAlways, // need subscription
Short: "Refresh and then destroy the service stack",
RunE: func(cmd *cobra.Command, args []string) error {
return cdCommand(cmd, client.CdCommandDown, args, global.Client)
},
}
var cdRefreshCmd = &cobra.Command{
Use: "refresh [PROJECT...]",
Annotations: authNeededAlways, // need subscription
Short: "Refresh the service stack",
RunE: func(cmd *cobra.Command, args []string) error {
return cdCommand(cmd, client.CdCommandRefresh, args, global.Client)
},
}
var cdCancelCmd = &cobra.Command{
Use: "cancel [PROJECT...]",
Annotations: authNeededAlways, // need subscription
Short: "Cancel the current CD operation",
RunE: func(cmd *cobra.Command, args []string) error {
return cdCommand(cmd, client.CdCommandCancel, args, global.Client)
},
}
var cdOutputsCmd = &cobra.Command{
Use: "outputs [PROJECT...]",
Annotations: authNeededAlways, // need subscription
Short: "Get the outputs of the service stack",
RunE: func(cmd *cobra.Command, args []string) error {
return cdCommand(cmd, client.CdCommandOutputs, args, global.Client)
},
}
var cdTearDownCmd = &cobra.Command{
Use: "teardown",
Args: cobra.NoArgs,
Short: "Destroy the CD cluster without destroying the services",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
force, _ := cmd.Flags().GetBool("force")
session, err := newCommandSession(cmd)
if err != nil {
return err
}
err = cli.TearDownCD(ctx, session.Provider, force)
if errors.Is(err, cli.ErrExistingStacks) {
printDefangHint("Use `defang cd destroy --force` to force teardown the CD cluster, leaving existing projects orphaned")
}
return err
},
}
var cdListCmd = &cobra.Command{
Use: "ls",
Args: cobra.NoArgs,
Aliases: []string{"list"},
Short: "List all the projects and stacks in the CD cluster",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
remote, _ := cmd.Flags().GetBool("remote")
all, _ := cmd.Flags().GetBool("all")
session, err := newCommandSession(cmd)
if err != nil {
return err
}
if remote {
if all {
return errors.New("--all cannot be used with --remote")
}
err = canIUseProvider(ctx, session.Provider, "", 0, true) // safe to use latest CD image
if err != nil {
return err
}
// FIXME: this needs auth because it spawns the CD task
return cli.CdCommandAndTail(ctx, session.Provider, "", global.Verbose, client.CdCommandList, global.Client)
} else {
return cli.CdListFromStorage(ctx, session.Provider, all || global.Verbose)
}
},
}
var cdPreviewCmd = &cobra.Command{
Use: "preview",
Args: cobra.NoArgs,
Annotations: authNeededAlways, // FIXME: because it still needs a delegated domain
Short: "Preview the changes that will be made by the CD task",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
session, err := newCommandSession(cmd)
if err != nil {
return err
}
project, err := session.Loader.LoadProject(cmd.Context())
if err != nil {
return err
}
allowUpgrade, _ := cmd.Flags().GetBool("allow-upgrade")
err = canIUseProvider(ctx, session.Provider, project.Name, 1, allowUpgrade) // 1 SDU for BYOC preview
if err != nil {
return err
}
return cli.Preview(ctx, project, global.Client, session.Provider, cli.ComposeUpParams{
Mode: session.Stack.Mode,
Project: project,
UploadMode: compose.UploadModePreview,
})
},
}
var cdInstallCmd = &cobra.Command{
Use: "install",
Aliases: []string{"setup"},
Args: cobra.NoArgs,
Annotations: authNeededAlways,
Short: "Install the CD resources into the cluster",
Hidden: true, // users shouldn't have to run this manually, because it's done on deploy
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
session, err := newCommandSession(cmd)
if err != nil {
return err
}
if err := canIUseProvider(ctx, session.Provider, "", 0, false); err != nil {
return err
}
force, _ := cmd.Flags().GetBool("force")
return cli.InstallCD(ctx, session.Provider, force)
},
}
var cdCloudformationCmd = &cobra.Command{
Use: "cloudformation",
Short: "CloudFormation template related commands",
Annotations: authNeededAlways,
Args: cobra.NoArgs,
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
provider := aws.NewByocProvider(cmd.Context(), global.Client.GetTenantName(), global.Stack.Name, global.Client)
if err := canIUseProvider(cmd.Context(), provider, "", 0, false); err != nil {
return err
}
template, err := provider.PrintCloudFormationTemplate()
if err == nil {
_, err = term.Print(string(template))
}
return err
},
}