-
Notifications
You must be signed in to change notification settings - Fork 68
/
deployment.go
283 lines (249 loc) · 9.25 KB
/
deployment.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
package cmd
import (
"io"
"strings"
"github.com/astronomer/astro-cli/deployment"
"github.com/astronomer/astro-cli/houston"
sa "github.com/astronomer/astro-cli/serviceaccount"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
allDeployments bool
executor string
deploymentId string
userId string
systemSA bool
category string
label string
releaseName string
CreateExample = `
# Create new deployment with Celery executor (default: celery without params).
$ astro deployment create new-deployment-name --executor=celery
# Create new deployment with Local executor.
$ astro deployment create new-deployment-name-local --executor=local
# Create new deployment with Kubernetes executor.
$ astro deployment create new-deployment-name-k8s --executor=k8s
`
deploymentSaCreateExample = `
# Create service-account
$ astro deployment service-account create --deployment-uuid=xxxxx --label=my_label --role=ROLE
`
deploymentSaGetExample = `
# Get deployment service-account
$ astro deployment service-account get <service-account-id> --deployment-id=<deployment-id>
# or using deployment-id
$ astro deployment service-account get --deployment-id=<deployment-id>
`
deploymentSaDeleteExample = `
$ astro deployment service-account delete <service-account-id> --deployment-id=<deployment-id>
`
deploymentUpdateAttrs = []string{"label"}
)
func newDeploymentRootCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "deployment",
Aliases: []string{"de"},
Short: "Manage airflow deployments",
Long: "Deployments are individual Airflow clusters running on an installation of the Astronomer platform.",
}
cmd.PersistentFlags().StringVar(&workspaceId, "workspace-id", "", "workspace assigned to deployment")
cmd.AddCommand(
newDeploymentCreateCmd(client, out),
newDeploymentListCmd(client, out),
newDeploymentUpdateCmd(client, out),
newDeploymentDeleteCmd(client, out),
newLogsCmd(client, out),
newDeploymentSaRootCmd(client, out),
)
return cmd
}
func newDeploymentCreateCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "create DEPLOYMENT",
Aliases: []string{"cr"},
Short: "Create a new Astronomer Deployment",
Long: "Create a new Astronomer Deployment",
Example: CreateExample,
Args: cobra.ExactArgs(1),
RunE: deploymentCreate,
}
cmd.Flags().StringVarP(&executor, "executor", "e", "", "Add executor parameter: local or celery")
cmd.Flags().StringVarP(&releaseName, "release-name", "r", "", "Set custom release-name if possible")
return cmd
}
func newDeploymentDeleteCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "delete DEPLOYMENT",
Aliases: []string{"de"},
Short: "Delete an airflow deployment",
Long: "Delete an airflow deployment",
Args: cobra.ExactArgs(1),
RunE: deploymentDelete,
}
return cmd
}
func newDeploymentListCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List airflow deployments",
Long: "List airflow deployments",
RunE: func(cmd *cobra.Command, args []string) error {
return deploymentList(cmd, args, client, out)
},
}
cmd.Flags().BoolVarP(&allDeployments, "all", "a", false, "Show deployments across all workspaces")
return cmd
}
func newDeploymentUpdateCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Aliases: []string{"up"},
Short: "Update airflow deployments",
Long: "Update airflow deployments",
Example: " astro deployment update UUID label=Production-Airflow description=example version=v1.0.0",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) <= 0 {
return errors.New("must specify a deployment ID and at least one attribute to update.")
}
return updateArgValidator(args[1:], deploymentUpdateAttrs)
},
RunE: deploymentUpdate,
}
return cmd
}
func newDeploymentSaRootCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "service-account",
Aliases: []string{"sa"},
Short: "Manage astronomer service accounts",
Long: "Service-accounts represent a revokable token with access to the Astronomer platform",
}
cmd.AddCommand(
newDeploymentSaCreateCmd(client, out),
newDeploymentSaGetCmd(client, out),
newDeploymentSaDeleteCmd(client, out),
)
return cmd
}
func newDeploymentSaCreateCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Aliases: []string{"cr"},
Short: "Create a service-account in the astronomer platform",
Long: "Create a service-account in the astronomer platform",
Example: deploymentSaCreateExample,
RunE: func(cmd *cobra.Command, args []string) error {
return deploymentSaCreate(cmd, args, client, out)
},
}
cmd.Flags().StringVarP(&deploymentId, "deployment-id", "d", "", "[ID]")
cmd.Flags().StringVarP(&userId, "user-id", "u", "", "[ID]")
cmd.Flags().BoolVarP(&systemSA, "system-sa", "s", false, "")
cmd.Flags().StringVarP(&category, "category", "c", "default", "CATEGORY")
cmd.Flags().StringVarP(&label, "label", "l", "", "LABEL")
cmd.Flags().StringVarP(&role, "role", "r", "viewer", "ROLE")
return cmd
}
func newDeploymentSaGetCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Get a service-account by entity type and entity id",
Long: "Get a service-account by entity type and entity id",
Example: deploymentSaGetExample,
RunE: func(cmd *cobra.Command, args []string) error {
return deploymentSaGet(cmd, client, out)
},
}
cmd.Flags().StringVarP(&deploymentId, "deployment-id", "d", "", "[ID]")
cmd.MarkFlagRequired("deployment-id")
return cmd
}
func newDeploymentSaDeleteCmd(client *houston.Client, out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "delete [SA-ID]",
Aliases: []string{"de"},
Short: "Delete a service-account in the astronomer platform",
Long: "Delete a service-account in the astronomer platform",
Example: deploymentSaDeleteExample,
RunE: func(cmd *cobra.Command, args []string) error {
return deploymentSaDelete(cmd, args, client, out)
},
Args: cobra.ExactArgs(1),
}
cmd.Flags().StringVarP(&deploymentId, "deployment-id", "d", "", "[ID]")
cmd.MarkFlagRequired("deployment-id")
return cmd
}
func deploymentCreate(cmd *cobra.Command, args []string) error {
ws, err := coalesceWorkspace()
if err != nil {
return errors.Wrap(err, "failed to find a valid workspace")
// fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag")
}
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
deploymentConfig := make(map[string]string)
switch executor {
case "local":
deploymentConfig["executor"] = "LocalExecutor"
case "celery":
deploymentConfig["executor"] = "CeleryExecutor"
case "kubernetes", "k8s":
deploymentConfig["executor"] = "KubernetesExecutor"
default:
return errors.New("please specify correct executor, one of: local, celery, kubernetes, k8s")
}
return deployment.Create(args[0], ws, releaseName, deploymentConfig)
}
func deploymentDelete(cmd *cobra.Command, args []string) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return deployment.Delete(args[0])
}
func deploymentList(cmd *cobra.Command, args []string, client *houston.Client, out io.Writer) error {
ws, err := coalesceWorkspace()
if err != nil {
return errors.Wrap(err, "failed to find a valid workspace")
// fmt.Println("Default workspace id not set, set default workspace id or pass a workspace in via the --workspace-id flag")
}
// Don't validate workspace if viewing all deployments
if allDeployments {
ws = ""
}
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return deployment.List(ws, allDeployments, client, out)
}
func deploymentUpdate(cmd *cobra.Command, args []string) error {
argsMap, err := argsToMap(args[1:])
if err != nil {
return err
}
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return deployment.Update(args[0], argsMap)
}
func deploymentSaCreate(cmd *cobra.Command, args []string, client *houston.Client, out io.Writer) error {
if len(label) == 0 {
return errors.New("must provide a service-account label with the --label (-l) flag")
}
if err := validateRole(role); err != nil {
return errors.Wrap(err, "failed to find a valid role")
}
fullRole := strings.Join([]string{"DEPLOYMENT", strings.ToUpper(role)}, "_")
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return sa.CreateUsingDeploymentUUID(deploymentId, label, category, fullRole, client, out)
}
func deploymentSaGet(cmd *cobra.Command, client *houston.Client, out io.Writer) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return sa.Get("DEPLOYMENT", deploymentId, client, out)
}
func deploymentSaDelete(cmd *cobra.Command, args []string, client *houston.Client, out io.Writer) error {
// Silence Usage as we have now validated command input
cmd.SilenceUsage = true
return sa.DeleteUsingDeploymentUUID(args[0], deploymentId, client, out)
}