forked from astronomer/astro-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.go
More file actions
614 lines (539 loc) · 23.6 KB
/
Copy pathdeploy.go
File metadata and controls
614 lines (539 loc) · 23.6 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package deploy
import (
"errors"
"fmt"
neturl "net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types/versions"
"golang.org/x/mod/semver"
"github.com/astronomer/astro-cli/airflow"
"github.com/astronomer/astro-cli/airflow/types"
"github.com/astronomer/astro-cli/config"
"github.com/astronomer/astro-cli/docker"
"github.com/astronomer/astro-cli/houston"
"github.com/astronomer/astro-cli/pkg/fileutil"
"github.com/astronomer/astro-cli/pkg/input"
"github.com/astronomer/astro-cli/pkg/logger"
"github.com/astronomer/astro-cli/pkg/printutil"
"github.com/astronomer/astro-cli/software/auth"
)
var (
// this is used to monkey patch the function in order to write unit test cases
imageHandlerInit = airflow.ImageHandlerInit
dockerfile = "Dockerfile"
deployImagePlatformSupport = []string{"linux/amd64"}
gzipFile = fileutil.GzipFile
getDeploymentIDForCurrentCommandVar = getDeploymentIDForCurrentCommand
)
var (
ErrNoWorkspaceID = errors.New("no workspace id provided")
errNoDomainSet = errors.New("no domain set, re-authenticate")
errInvalidDeploymentID = errors.New("please specify a valid deployment ID")
errDeploymentNotFound = errors.New("no airflow deployments found")
errInvalidDeploymentSelected = errors.New("invalid deployment selection\n") //nolint
// ErrDagOnlyDeployDisabledInConfigLegacy is returned for Houston before 2.0.0 (flat feature-flag paths).
ErrDagOnlyDeployDisabledInConfigLegacy = errors.New("to perform this operation, set both deployments.dagOnlyDeployment and deployments.configureDagDeployment to true in your Astronomer cluster")
// ErrDagOnlyDeployDisabledInConfig is returned for Houston 2.0.0+ (deployMechanisms.*.enabled under merged deployments config).
ErrDagOnlyDeployDisabledInConfig = errors.New("to perform this operation, set both deployments.deployMechanisms.dagOnlyDeployment.enabled and deployments.deployMechanisms.configureDagDeployment.enabled to true in your Astronomer cluster")
ErrDagOnlyDeployNotEnabledForDeployment = errors.New("to perform this operation, first set the Deployment type to 'dag_deploy' via the UI or the API or the CLI")
ErrEmptyDagFolderUserCancelledOperation = errors.New("no DAGs found in the dags folder. User canceled the operation")
ErrBYORegistryDomainNotSet = errors.New("Custom registry host is not set in config. It can be set at astronomer.houston.config.deployments.registry.protectedCustomRegistry.updateRegistry.host") //nolint
ErrDeploymentTypeIncorrectForImageOnly = errors.New("--image only works for Dag-only, Git-sync-based and NFS-based deployments")
WarningInvalidImageNameMsg = "WARNING! The image in your Dockerfile '%s' is not based on Astro Runtime and is not supported. Change your Dockerfile with an image that pulls from 'quay.io/astronomer/astro-runtime' to proceed.\n"
ErrNoRuntimeLabelOnCustomImage = errors.New("the image should have label io.astronomer.docker.runtime.version")
ErrRuntimeVersionNotPassedForRemoteImage = errors.New("if --image-name and --remote is passed, it's mandatory to pass --runtime-version")
)
const (
houstonDeploymentHeader = "Authenticated to %s \n\n"
houstonSelectDeploymentPrompt = "Select which airflow deployment you want to deploy to:"
houstonDeploymentPrompt = "Deploying: %s\n"
imageBuildingPrompt = "Building image..."
warningInvalidImageName = "WARNING! The image in your Dockerfile is pulling from '%s', which is not supported. We strongly recommend that you use Astronomer Certified or Runtime images that pull from 'astronomerinc/ap-airflow', 'quay.io/astronomer/ap-airflow' or 'quay.io/astronomer/astro-runtime'. If you're running a custom image, you can override this. Are you sure you want to continue?\n"
warningInvalidNameTag = "WARNING! You are about to push an image using the '%s' tag. This is not recommended.\nPlease use one of the following tags: %s.\nAre you sure you want to continue?"
warningInvalidNameTagEmptyRecommendations = "WARNING! You are about to push an image using the '%s' tag. This is not recommended.\nAre you sure you want to continue?"
registryDomainPrefix = "registry."
runtimeImageLabel = "io.astronomer.docker.runtime.version"
airflowImageLabel = "io.astronomer.docker.airflow.version"
composeSkipImageBuildingPromptMsg = "Skipping building image since --image-name flag is used..."
)
var tab = printutil.Table{
Padding: []int{5, 30, 30, 50},
DynamicPadding: true,
Header: []string{"#", "LABEL", "DEPLOYMENT NAME", "WORKSPACE", "DEPLOYMENT ID"},
}
func Airflow(houstonClient houston.ClientInterface, path, deploymentID, wsID string, ignoreCacheDeploy, prompt bool, description string, isImageOnlyDeploy bool, imageName string) (string, error) {
deploymentID, deployments, err := getDeploymentIDForCurrentCommand(houstonClient, wsID, deploymentID, prompt)
if err != nil {
return deploymentID, err
}
c, _ := config.GetCurrentContext()
cloudDomain := c.Domain
nextTag := ""
releaseName := ""
for i := range deployments {
deployment := deployments[i]
if deployment.ID == deploymentID {
nextTag = deployment.DeploymentInfo.NextCli
releaseName = deployment.ReleaseName
}
}
deploymentInfo, err := houston.Call(houstonClient.GetDeployment)(deploymentID)
if err != nil {
return deploymentID, fmt.Errorf("failed to get deployment info: %w", err)
}
appCfgWs := resolvedWorkspaceUUIDForAppConfig(wsID, deploymentID, deployments, deploymentInfo)
appConfig, err := houston.Call(houstonClient.GetAppConfig)(houston.GetAppConfigRequest{ClusterID: deploymentInfo.ClusterID, WorkspaceUUID: appCfgWs, DeploymentUUID: deploymentID})
if err != nil {
return deploymentID, fmt.Errorf("failed to get app config: %w", err)
}
byoRegistryDomain := ""
byoRegistryEnabled := appConfig != nil && appConfig.Flags.BYORegistryEnabled
if byoRegistryEnabled {
// updating nextTag logic for private registry, since houston won't maintain next tag in case of BYO registry
nextTag = "deploy-" + time.Now().UTC().Format("2006-01-02T15-04")
byoRegistryDomain = appConfig.BYORegistryDomain
if byoRegistryDomain == "" {
return deploymentID, ErrBYORegistryDomainNotSet
}
}
// isImageOnlyDeploy is not valid for image-based deployments since image-based deployments inherently mean that the image itself contains dags.
// If we deploy only the image, the deployment will not have any dags for image-based deployments.
// Even on astro, image-based deployments are not allowed to be deployed with --image flag.
if isImageOnlyDeploy && deploymentInfo.DagDeployment.Type == houston.ImageDeploymentType {
return "", ErrDeploymentTypeIncorrectForImageOnly
}
// We don't need to exclude the dags from the image because the dags present in the image are not respected anyways for non-image based deployments
fmt.Printf(houstonDeploymentPrompt, releaseName)
// Build the image to deploy
err = buildPushDockerImage(houstonClient, &c, deploymentInfo, releaseName, path, nextTag, cloudDomain, byoRegistryDomain, ignoreCacheDeploy, byoRegistryEnabled, description, imageName)
if err != nil {
return deploymentID, err
}
deploymentLink := getAirflowUILink(deploymentID, deploymentInfo.Urls)
fmt.Printf("Successfully pushed Docker image to Astronomer registry, it can take a few minutes to update the deployment with the new image. Navigate to the Astronomer UI to confirm the state of your deployment (%s).\n", deploymentLink)
return deploymentID, nil
}
// Find deployment ID in deployments slice
func deploymentExists(deploymentID string, deployments []houston.Deployment) bool {
for idx := range deployments {
deployment := deployments[idx]
if deployment.ID == deploymentID {
return true
}
}
return false
}
// resolvedWorkspaceUUIDForAppConfig prefers the workspace ID Houston associates with the
// deployment so appConfig merges deployments config at the same tier as GetDeployment /
// workspaceDeployments. Falls back to wsID when the API response omits workspace (older queries).
func resolvedWorkspaceUUIDForAppConfig(wsID, deploymentID string, deployments []houston.Deployment, deploymentInfo *houston.Deployment) string {
if deploymentInfo != nil && deploymentInfo.Workspace.ID != "" {
return deploymentInfo.Workspace.ID
}
for i := range deployments {
if deployments[i].ID == deploymentID && deployments[i].Workspace.ID != "" {
return deployments[i].Workspace.ID
}
}
return wsID
}
func validateRuntimeVersion(houstonClient houston.ClientInterface, tag string, deploymentInfo *houston.Deployment) error {
// Get valid image tags for platform using Deployment Info request
deploymentConfig, err := houston.Call(houstonClient.GetDeploymentConfig)(nil)
if err != nil {
return err
}
vars := make(map[string]interface{})
vars["clusterId"] = deploymentInfo.ClusterID
// ignoring the error as user can be connected to platform where runtime is not enabled
runtimeReleases, _ := houston.Call(houstonClient.GetRuntimeReleases)(vars)
var validTags string
if config.CFG.ShowWarnings.GetBool() && deploymentInfo.DesiredAirflowVersion != "" && !deploymentConfig.IsValidTag(tag) {
validTags = strings.Join(deploymentConfig.GetValidTags(tag), ", ")
}
if config.CFG.ShowWarnings.GetBool() && deploymentInfo.DesiredRuntimeVersion != "" && !runtimeReleases.IsValidVersion(tag) {
validTags = strings.Join(runtimeReleases.GreaterVersions(tag), ", ")
}
if validTags != "" {
validTags := strings.Join(deploymentConfig.GetValidTags(tag), ", ")
msg := fmt.Sprintf(warningInvalidNameTag, tag, validTags)
if validTags == "" {
msg = fmt.Sprintf(warningInvalidNameTagEmptyRecommendations, tag)
}
i, _ := input.Confirm(msg)
if !i {
fmt.Println("Canceling deploy...")
os.Exit(1)
}
}
return nil
}
func UpdateDeploymentImage(houstonClient houston.ClientInterface, deploymentID, wsID, runtimeVersion, imageName string) (string, error) {
if runtimeVersion == "" {
return "", ErrRuntimeVersionNotPassedForRemoteImage
}
deploymentID, _, err := getDeploymentIDForCurrentCommandVar(houstonClient, wsID, deploymentID, deploymentID == "")
if err != nil {
return "", err
}
if deploymentID == "" {
return "", errInvalidDeploymentID
}
deploymentInfo, err := houston.Call(houstonClient.GetDeployment)(deploymentID)
if err != nil {
return "", fmt.Errorf("failed to get deployment info: %w", err)
}
fmt.Println("Skipping building the image since --image-name flag is used...")
req := houston.UpdateDeploymentImageRequest{ReleaseName: deploymentInfo.ReleaseName, Image: imageName, AirflowVersion: "", RuntimeVersion: runtimeVersion}
_, err = houston.Call(houstonClient.UpdateDeploymentImage)(req)
fmt.Println("Image successfully updated")
return deploymentID, err
}
func pushDockerImage(byoRegistryEnabled bool, deploymentInfo *houston.Deployment, byoRegistryDomain, name, nextTag, cloudDomain string, imageHandler airflow.ImageHandler, houstonClient houston.ClientInterface, c *config.Context, customImageName string) error {
var registry, remoteImage, token string
if byoRegistryEnabled {
registry = byoRegistryDomain
remoteImage = fmt.Sprintf("%s:%s", registry, fmt.Sprintf("%s-%s", name, nextTag))
} else {
token = c.Token
platformVersion, _ := houstonClient.GetPlatformVersion(nil)
if versions.GreaterThanOrEqualTo(platformVersion, "1.0.0") {
var err error
registry, err = getDeploymentRegistryURL(deploymentInfo.Urls)
if err != nil {
return err
}
// Switch to per deployment registry login
err = auth.RegistryAuth(houstonClient, os.Stdout, registry, houston.GetAppConfigRequest{
ClusterID: deploymentInfo.ClusterID,
WorkspaceUUID: deploymentInfo.Workspace.ID,
DeploymentUUID: deploymentInfo.ID,
})
if err != nil {
logger.Debugf("There was an error logging into registry: %s", err.Error())
return err
}
remoteImage = fmt.Sprintf("%s/%s", registry, airflow.ImageName(name, nextTag))
} else {
registry = registryDomainPrefix + cloudDomain
remoteImage = fmt.Sprintf("%s/%s", registry, airflow.ImageName(name, nextTag))
token = c.Token
}
}
if customImageName != "" {
if tagFromImageName := getGetTagFromImageName(customImageName); tagFromImageName != "" {
remoteImage = fmt.Sprintf("%s:%s", registry, tagFromImageName)
}
}
useShaAsTag := config.CFG.ShaAsTag.GetBool()
fmt.Println("Pushing image to configured registry")
sha, err := imageHandler.Push(remoteImage, "", token, useShaAsTag)
if err != nil {
return err
}
if byoRegistryEnabled {
if useShaAsTag {
remoteImage = fmt.Sprintf("%s@%s", registry, sha)
}
runtimeVersion, _ := imageHandler.GetLabel("", runtimeImageLabel)
airflowVersion, _ := imageHandler.GetLabel("", airflowImageLabel)
req := houston.UpdateDeploymentImageRequest{ReleaseName: name, Image: remoteImage, AirflowVersion: airflowVersion, RuntimeVersion: runtimeVersion}
_, err = houston.Call(houstonClient.UpdateDeploymentImage)(req)
return err
}
return nil
}
func buildDockerImageForCustomImage(imageHandler airflow.ImageHandler, customImageName string, deploymentInfo *houston.Deployment, houstonClient houston.ClientInterface) error {
fmt.Println(composeSkipImageBuildingPromptMsg)
err := imageHandler.TagLocalImage(customImageName)
if err != nil {
return err
}
runtimeLabel, err := imageHandler.GetLabel("", airflow.RuntimeImageLabel)
if err != nil {
fmt.Println("unable get runtime version from image")
return err
}
if runtimeLabel == "" {
return ErrNoRuntimeLabelOnCustomImage
}
err = validateRuntimeVersion(houstonClient, runtimeLabel, deploymentInfo)
return err
}
func buildDockerImageFromWorkingDir(path string, imageHandler airflow.ImageHandler, houstonClient houston.ClientInterface, deploymentInfo *houston.Deployment, ignoreCacheDeploy bool, description string) error {
// all these checks inside Dockerfile should happen only when no image-name is provided
// parse dockerfile
cmds, err := docker.ParseFile(filepath.Join(path, dockerfile))
if err != nil {
return fmt.Errorf("failed to parse dockerfile: %s: %w", filepath.Join(path, dockerfile), err)
}
_, tag := docker.GetImageTagFromParsedFile(cmds)
// Get valid image tags for platform using Deployment Info request
err = validateRuntimeVersion(houstonClient, tag, deploymentInfo)
if err != nil {
return err
}
// Build our image
fmt.Println(imageBuildingPrompt)
deployLabels := []string{"io.astronomer.skip.revision=true"}
if description != "" {
deployLabels = append(deployLabels, "io.astronomer.deploy.revision.description="+description)
}
buildConfig := types.ImageBuildConfig{
Path: config.WorkingPath,
NoCache: ignoreCacheDeploy,
TargetPlatforms: deployImagePlatformSupport,
Labels: deployLabels,
}
err = imageHandler.Build("", "", buildConfig)
return err
}
func buildDockerImage(ignoreCacheDeploy bool, deploymentInfo *houston.Deployment, customImageName, path string, imageHandler airflow.ImageHandler, houstonClient houston.ClientInterface, description string) error {
if customImageName == "" {
return buildDockerImageFromWorkingDir(path, imageHandler, houstonClient, deploymentInfo, ignoreCacheDeploy, description)
}
return buildDockerImageForCustomImage(imageHandler, customImageName, deploymentInfo, houstonClient)
}
func getGetTagFromImageName(imageName string) string {
parts := strings.Split(imageName, ":")
if len(parts) == 2 {
return parts[1]
}
return ""
}
func buildPushDockerImage(houstonClient houston.ClientInterface, c *config.Context, deploymentInfo *houston.Deployment, name, path, nextTag, cloudDomain, byoRegistryDomain string, ignoreCacheDeploy, byoRegistryEnabled bool, description, customImageName string) error {
imageName := airflow.ImageName(name, "latest")
imageHandler := imageHandlerInit(imageName)
err := buildDockerImage(ignoreCacheDeploy, deploymentInfo, customImageName, path, imageHandler, houstonClient, description)
if err != nil {
return err
}
return pushDockerImage(byoRegistryEnabled, deploymentInfo, byoRegistryDomain, name, nextTag, cloudDomain, imageHandler, houstonClient, c, customImageName)
}
func getAirflowUILink(deploymentID string, deploymentURLs []houston.DeploymentURL) string {
if deploymentID == "" {
return ""
}
for _, url := range deploymentURLs {
if url.Type == houston.AirflowURLType {
return url.URL
}
}
return ""
}
func getDeploymentRegistryURL(deploymentURLs []houston.DeploymentURL) (string, error) {
for _, url := range deploymentURLs {
if url.Type == houston.RegistryURLType {
return url.URL, nil
}
}
return "", errors.New("no valid registry url found failed to push")
}
func getDeploymentIDForCurrentCommand(houstonClient houston.ClientInterface, wsID, deploymentID string, prompt bool) (string, []houston.Deployment, error) {
if wsID == "" {
return deploymentID, []houston.Deployment{}, ErrNoWorkspaceID
}
// Validate workspace
currentWorkspace, err := houston.Call(houstonClient.GetWorkspace)(wsID)
if err != nil {
return deploymentID, []houston.Deployment{}, err
}
// Get Deployments from workspace ID
request := houston.ListDeploymentsRequest{
WorkspaceID: currentWorkspace.ID,
}
deployments, err := houston.Call(houstonClient.ListDeployments)(request)
if err != nil {
return deploymentID, deployments, err
}
c, err := config.GetCurrentContext()
if err != nil {
return deploymentID, deployments, err
}
cloudDomain := c.Domain
if cloudDomain == "" {
return deploymentID, deployments, errNoDomainSet
}
// Use config deployment if provided
if deploymentID == "" {
deploymentID = config.CFG.ProjectDeployment.GetProjectString()
}
if deploymentID != "" && !deploymentExists(deploymentID, deployments) {
return deploymentID, deployments, errInvalidDeploymentID
}
// Prompt user for deployment if no deployment passed in
if deploymentID == "" || prompt {
if len(deployments) == 0 {
return deploymentID, deployments, errDeploymentNotFound
}
fmt.Printf(houstonDeploymentHeader, cloudDomain)
fmt.Println(houstonSelectDeploymentPrompt)
deployMap := map[string]houston.Deployment{}
for i := range deployments {
deployment := deployments[i]
index := i + 1
tab.AddRow([]string{strconv.Itoa(index), deployment.Label, deployment.ReleaseName, currentWorkspace.Label, deployment.ID}, false)
deployMap[strconv.Itoa(index)] = deployment
}
tab.Print(os.Stdout)
choice := input.Text("\n> ")
selected, ok := deployMap[choice]
if !ok {
return deploymentID, deployments, errInvalidDeploymentSelected
}
deploymentID = selected.ID
}
return deploymentID, deployments, nil
}
func isDagOnlyDeploymentEnabled(appConfig *houston.AppConfig) bool {
return appConfig != nil && appConfig.Flags.DagOnlyDeployment
}
// IsDagOnlyDeployDisabledInClusterConfig reports whether err is the sentinel for DAG-only deploy
// disabled at cluster / merged-config level (either legacy or 2.0+ wording).
func IsDagOnlyDeployDisabledInClusterConfig(err error) bool {
return errors.Is(err, ErrDagOnlyDeployDisabledInConfig) || errors.Is(err, ErrDagOnlyDeployDisabledInConfigLegacy)
}
func isHoustonPlatformVersionGTE200(version string) bool {
if version == "" {
return false
}
v := version
if !strings.HasPrefix(v, "v") {
v = "v" + v
}
if pr := semver.Prerelease(v); pr != "" {
v = strings.TrimSuffix(v, pr)
}
if !semver.IsValid(v) {
return false
}
return semver.Compare(v, "v2.0.0") >= 0
}
func errDagOnlyDeployDisabledAtCluster(appConfig *houston.AppConfig) error {
if appConfig != nil && isHoustonPlatformVersionGTE200(appConfig.Version) {
return ErrDagOnlyDeployDisabledInConfig
}
return ErrDagOnlyDeployDisabledInConfigLegacy
}
func isDagOnlyDeploymentEnabledForDeployment(deploymentInfo *houston.Deployment) bool {
return deploymentInfo != nil && deploymentInfo.DagDeployment.Type == houston.DagOnlyDeploymentType
}
func validateIfDagDeployURLCanBeConstructed(deploymentInfo *houston.Deployment) error {
_, err := config.GetCurrentContext()
if err != nil {
return fmt.Errorf("could not get current context! Error: %w", err)
}
if deploymentInfo == nil || deploymentInfo.ReleaseName == "" {
return errInvalidDeploymentID
}
return nil
}
func getDagDeployURL(deploymentInfo *houston.Deployment) string {
// Checks if dagserver URL exists and returns the URL
for _, url := range deploymentInfo.Urls {
if url.Type == houston.DagServerURLType {
logger.Infof("Using dag deploy URL from dagserver: %s", url.URL)
return url.URL
}
}
// If no dagserver URL is found, we look for airflow URL to detect upload url
for _, url := range deploymentInfo.Urls {
if url.Type != houston.AirflowURLType {
continue
}
parsedAirflowURL, err := neturl.Parse(url.URL)
if err != nil {
logger.Infof("Error parsing airflow URL: %v", err)
break
}
// Use URL scheme and host from the airflow URL
dagUploadURL := fmt.Sprintf("https://%s/%s/dags/upload", parsedAirflowURL.Host, deploymentInfo.ReleaseName)
logger.Infof("Generated Dag Upload URL from airflow base URL: %s", dagUploadURL)
return dagUploadURL
}
return ""
}
func DagsOnlyDeploy(houstonClient houston.ClientInterface, wsID, deploymentID, dagsParentPath string, dagDeployURL *string, cleanUpFiles bool, description string) error {
deploymentID, deployments, err := getDeploymentIDForCurrentCommandVar(houstonClient, wsID, deploymentID, deploymentID == "")
if err != nil {
return err
}
if deploymentID == "" {
return errInvalidDeploymentID
}
// Throw error if the feature is disabled at Deployment level
deploymentInfo, err := houston.Call(houstonClient.GetDeployment)(deploymentID)
if err != nil {
return fmt.Errorf("failed to get deployment info: %w", err)
}
appCfgWs := resolvedWorkspaceUUIDForAppConfig(wsID, deploymentID, deployments, deploymentInfo)
appConfig, err := houston.Call(houstonClient.GetAppConfig)(houston.GetAppConfigRequest{ClusterID: deploymentInfo.ClusterID, WorkspaceUUID: appCfgWs, DeploymentUUID: deploymentID})
if err != nil {
return fmt.Errorf("failed to get app config: %w", err)
}
// Throw error if the feature is disabled at Houston level
if !isDagOnlyDeploymentEnabled(appConfig) {
return errDagOnlyDeployDisabledAtCluster(appConfig)
}
if !isDagOnlyDeploymentEnabledForDeployment(deploymentInfo) {
return ErrDagOnlyDeployNotEnabledForDeployment
}
uploadURL := ""
if dagDeployURL == nil {
// Throw error if the upload URL can't be constructed
err = validateIfDagDeployURLCanBeConstructed(deploymentInfo)
if err != nil {
return err
}
uploadURL = getDagDeployURL(deploymentInfo)
} else {
uploadURL = *dagDeployURL
}
dagsPath := filepath.Join(dagsParentPath, "dags")
dagsTarPath := filepath.Join(dagsParentPath, "dags.tar")
dagsTarGzPath := dagsTarPath + ".gz"
dagFiles := fileutil.GetFilesWithSpecificExtension(dagsPath, ".py")
// Alert the user if dags folder is empty
if len(dagFiles) == 0 && config.CFG.ShowWarnings.GetBool() {
i, _ := input.Confirm("Warning: No DAGs found. This will delete any existing DAGs. Are you sure you want to deploy?")
if !i {
return ErrEmptyDagFolderUserCancelledOperation
}
}
// Generate the dags tar
err = fileutil.Tar(dagsPath, dagsTarPath, true, nil)
if err != nil {
return err
}
if cleanUpFiles {
defer os.Remove(dagsTarPath)
}
// Gzip the tar
err = gzipFile(dagsTarPath, dagsTarGzPath)
if err != nil {
return err
}
if cleanUpFiles {
defer os.Remove(dagsTarGzPath)
}
c, _ := config.GetCurrentContext()
headers := map[string]string{
"authorization": c.Token,
}
uploadFileArgs := fileutil.UploadFileArguments{
FilePath: dagsTarGzPath,
TargetURL: uploadURL,
FormFileFieldName: "file",
Headers: headers,
Description: description,
MaxTries: 8,
InitialDelayInMS: 1 * 1000,
BackoffFactor: 2,
RetryDisplayMessage: "please wait, attempting to upload the dags",
}
return fileutil.UploadFile(&uploadFileArgs)
}