Просмотр исходного кода

check if validate apply v2 is enabled before running commands involving application deployments and releases (#3376)

Co-authored-by: David Townley <davidtownley@Davids-MacBook-Air.local>
d-g-town 2 лет назад
Родитель
Сommit
8a4339a0f4

+ 17 - 0
cli/cmd/apply.go

@@ -13,6 +13,8 @@ import (
 	"strings"
 	"time"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/cli/cli/git"
 	"github.com/fatih/color"
 	"github.com/mitchellh/mapstructure"
@@ -109,6 +111,21 @@ func init() {
 }
 
 func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) (err error) {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.Apply(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	fileBytes, err := ioutil.ReadFile(porterYAML)
 	if err != nil {
 		stackName := os.Getenv("PORTER_STACK_NAME")

+ 17 - 0
cli/cmd/bluegreen.go

@@ -6,6 +6,8 @@ import (
 	"os"
 	"time"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
@@ -60,6 +62,21 @@ func init() {
 }
 
 func bluegreenSwitch(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.BlueGreenSwitch(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	// get the web release
 	webRelease, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, app)
 	if err != nil {

+ 17 - 2
cli/cmd/create.go

@@ -8,6 +8,8 @@ import (
 	"path/filepath"
 	"strings"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
@@ -182,13 +184,26 @@ func init() {
 var supportedKinds = map[string]string{"web": "", "job": "", "worker": ""}
 
 func createFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.CreateFull(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	// check the kind
 	if _, exists := supportedKinds[args[0]]; !exists {
 		return fmt.Errorf("%s is not a supported type: specify web, job, or worker", args[0])
 	}
 
-	var err error
-
 	fullPath, err := filepath.Abs(localPath)
 	if err != nil {
 		return err

+ 47 - 0
cli/cmd/delete.go

@@ -6,6 +6,8 @@ import (
 	"os"
 	"strconv"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
@@ -112,6 +114,21 @@ func init() {
 }
 
 func deleteDeployment(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.DeleteDeployment(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	projectID := cliConf.Project
 
 	if projectID == 0 {
@@ -143,6 +160,21 @@ func deleteDeployment(_ *types.GetAuthenticatedUserResponse, client *api.Client,
 }
 
 func deleteApp(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.DeleteApp(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	name := args[0]
 
 	resp, err := client.GetRelease(
@@ -172,6 +204,21 @@ func deleteApp(_ *types.GetAuthenticatedUserResponse, client *api.Client, args [
 }
 
 func deleteJob(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.DeleteJob(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	name := args[0]
 
 	resp, err := client.GetRelease(

+ 47 - 0
cli/cmd/deploy.go

@@ -9,6 +9,8 @@ import (
 	"strings"
 	"time"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/briandowns/spinner"
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
@@ -441,6 +443,21 @@ func init() {
 }
 
 func updateFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.UpdateFull(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	fullPath, err := filepath.Abs(localPath)
 	if err != nil {
 		return err
@@ -520,6 +537,21 @@ func updateGetEnv(_ *types.GetAuthenticatedUserResponse, client *api.Client, arg
 }
 
 func updateBuild(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.UpdateBuild(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	updateAgent, err := updateGetAgent(client)
 	if err != nil {
 		return err
@@ -588,6 +620,21 @@ func updatePush(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 }
 
 func updateUpgrade(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.UpdateUpgrade(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	updateAgent, err := updateGetAgent(client)
 	if err != nil {
 		return err

+ 32 - 0
cli/cmd/get.go

@@ -6,6 +6,8 @@ import (
 	"fmt"
 	"os"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/spf13/cobra"
@@ -71,6 +73,21 @@ type getReleaseInfo struct {
 }
 
 func get(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.Get(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	rel, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, args[0])
 	if err != nil {
 		return err
@@ -110,6 +127,21 @@ func get(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []strin
 }
 
 func getValues(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.GetValues(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	rel, err := client.GetRelease(context.Background(), cliConf.Project, cliConf.Cluster, namespace, args[0])
 	if err != nil {
 		return err

+ 48 - 1
cli/cmd/job.go

@@ -5,6 +5,8 @@ import (
 	"fmt"
 	"os"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
@@ -175,6 +177,21 @@ func init() {
 }
 
 func batchImageUpdate(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.BatchImageUpdate(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	color.New(color.FgGreen).Println("Updating all jobs which use the image:", imageRepoURI)
 
 	return client.UpdateBatchImage(
@@ -191,6 +208,21 @@ func batchImageUpdate(_ *types.GetAuthenticatedUserResponse, client *api.Client,
 
 // waits for a job with a given name/namespace
 func waitForJob(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.WaitForJob(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	return wait.WaitForJob(client, &wait.WaitOpts{
 		ProjectID: cliConf.Project,
 		ClusterID: cliConf.Cluster,
@@ -200,6 +232,21 @@ func waitForJob(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 }
 
 func runJob(authRes *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.RunJob(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	color.New(color.FgGreen).Printf("Running job %s in namespace %s\n", name, namespace)
 
 	waitForSuccessfulDeploy = true
@@ -216,7 +263,7 @@ func runJob(authRes *types.GetAuthenticatedUserResponse, client *api.Client, arg
 		},
 	}
 
-	err := updateAgent.UpdateImageAndValues(map[string]interface{}{
+	err = updateAgent.UpdateImageAndValues(map[string]interface{}{
 		"paused": false,
 	})
 	if err != nil {

+ 50 - 3
cli/cmd/list.go

@@ -6,6 +6,8 @@ import (
 	"os"
 	"text/tabwriter"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
@@ -90,7 +92,22 @@ func init() {
 }
 
 func listAll(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
-	err := writeReleases(client, "all")
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.ListAll(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
+	err = writeReleases(client, "all")
 	if err != nil {
 		return err
 	}
@@ -99,7 +116,22 @@ func listAll(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []s
 }
 
 func listApps(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
-	err := writeReleases(client, "application")
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.ListApps(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
+	err = writeReleases(client, "application")
 	if err != nil {
 		return err
 	}
@@ -108,7 +140,22 @@ func listApps(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []
 }
 
 func listJobs(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
-	err := writeReleases(client, "job")
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.ListJobs(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
+	err = writeReleases(client, "job")
 	if err != nil {
 		return err
 	}

+ 32 - 0
cli/cmd/stack.go

@@ -5,6 +5,8 @@ import (
 	"fmt"
 	"os"
 
+	v2 "github.com/porter-dev/porter/cli/cmd/v2"
+
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
@@ -101,6 +103,21 @@ func init() {
 }
 
 func stackAddEnvGroup(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.StackAddEnvGroup(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	envGroupName := args[0]
 
 	if len(envGroupName) == 0 {
@@ -171,6 +188,21 @@ func stackAddEnvGroup(_ *types.GetAuthenticatedUserResponse, client *api.Client,
 }
 
 func stackRemoveEnvGroup(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	ctx := context.Background()
+
+	project, err := client.GetProject(ctx, cliConf.Project)
+	if err != nil {
+		return fmt.Errorf("could not retrieve project from Porter API. Please contact support@porter.run")
+	}
+
+	if project.ValidateApplyV2 {
+		err = v2.StackRemoveEnvGroup(ctx)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
 	envGroupName := args[0]
 
 	if len(envGroupName) == 0 {

+ 12 - 0
cli/cmd/v2/apply.go

@@ -0,0 +1,12 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// Apply implements the functionality of the `porter apply` command for validate apply v2 projects
+func Apply(ctx context.Context) error {
+	fmt.Println("Coming soon!")
+	return nil
+}

+ 12 - 0
cli/cmd/v2/bluegreen.go

@@ -0,0 +1,12 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// BlueGreenSwitch implements the functionality of the `porter deply blue-green-switch` command for validate apply v2 projects
+func BlueGreenSwitch(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}

+ 12 - 0
cli/cmd/v2/create.go

@@ -0,0 +1,12 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// CreateFull implements the functionality of the `porter create` command for validate apply v2 projects
+func CreateFull(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}

+ 24 - 0
cli/cmd/v2/delete.go

@@ -0,0 +1,24 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// DeleteDeployment implements the functionality of the `porter delete` command for validate apply v2 projects
+func DeleteDeployment(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// DeleteApp implements the functionality of the `porter delete apps` command for validate apply v2 projects
+func DeleteApp(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// DeleteJob implements the functionality of the `porter delete jobs` command for validate apply v2 projects
+func DeleteJob(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}

+ 24 - 0
cli/cmd/v2/deploy.go

@@ -0,0 +1,24 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// UpdateFull implements the functionality of the `porter build` command for validate apply v2 projects
+func UpdateFull(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// UpdateBuild implements the functionality of the `porter apply` command for validate apply v2 projects
+func UpdateBuild(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// UpdateUpgrade implements the functionality of the `porter config` command for validate apply v2 projects
+func UpdateUpgrade(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}

+ 18 - 0
cli/cmd/v2/get.go

@@ -0,0 +1,18 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// Get implements the functionality of the `porter get` command for validate apply v2 projects
+func Get(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// GetValues implements the functionality of the `porter get values` command for validate apply v2 projects
+func GetValues(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}

+ 24 - 0
cli/cmd/v2/job.go

@@ -0,0 +1,24 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// BatchImageUpdate implements the functionality of the `porter job update-images` command for validate apply v2 projects
+func BatchImageUpdate(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// WaitForJob implements the functionality of the `porter job wait` command for validate apply v2 projects
+func WaitForJob(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// RunJob implements the functionality of the `porter job run` command for validate apply v2 projects
+func RunJob(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}

+ 24 - 0
cli/cmd/v2/list.go

@@ -0,0 +1,24 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// ListAll implements the functionality of the `porter list all` command for validate apply v2 projects
+func ListAll(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// ListApps implements the functionality of the `porter list apps` command for validate apply v2 projects
+func ListApps(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// ListJobs implements the functionality of the `porter list jobs` command for validate apply v2 projects
+func ListJobs(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}

+ 18 - 0
cli/cmd/v2/stack.go

@@ -0,0 +1,18 @@
+package v2
+
+import (
+	"context"
+	"fmt"
+)
+
+// StackAddEnvGroup implements the functionality of the `porter stack add` command for validate apply v2 projects
+func StackAddEnvGroup(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}
+
+// StackRemoveEnvGroup implements the functionality of the `porter stack remove` command for validate apply v2 projects
+func StackRemoveEnvGroup(ctx context.Context) error {
+	fmt.Println("This command is not supported for your project. Contact support@porter.run for more information.")
+	return nil
+}