Kaynağa Gözat

Add porter job run (#2862)

Mohammed Nafees 3 yıl önce
ebeveyn
işleme
8cdcc6428e
1 değiştirilmiş dosya ile 81 ekleme ve 0 silme
  1. 81 0
      cli/cmd/job.go

+ 81 - 0
cli/cmd/job.go

@@ -8,6 +8,7 @@ import (
 	"github.com/fatih/color"
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
 	api "github.com/porter-dev/porter/api/client"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"github.com/porter-dev/porter/cli/cmd/deploy/wait"
 	"github.com/porter-dev/porter/cli/cmd/deploy/wait"
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 )
 )
@@ -77,12 +78,43 @@ use the --namespace flag:
 	},
 	},
 }
 }
 
 
+var runJobCmd = &cobra.Command{
+	Use:   "run",
+	Short: "Manually runs a job and waits for it to complete.",
+	Long: fmt.Sprintf(`
+%s
+
+Manually runs a job and waits for it to complete a run. If the job completes successfully,
+this command exits with exit code 0. Otherwise, this command exits with exit code 1.
+
+Example commands:
+
+  %s
+
+This command is namespace-scoped and uses the default namespace. To specify a different namespace,
+use the --namespace flag:
+
+  %s
+`,
+		color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter job run\":"),
+		color.New(color.FgGreen, color.Bold).Sprintf("porter job run --name job-example"),
+		color.New(color.FgGreen, color.Bold).Sprintf("porter job run --name job-example --namespace custom-namespace"),
+	),
+	Run: func(cmd *cobra.Command, args []string) {
+		err := checkLoginAndRun(args, runJob)
+		if err != nil {
+			os.Exit(1)
+		}
+	},
+}
+
 var imageRepoURI string
 var imageRepoURI string
 
 
 func init() {
 func init() {
 	rootCmd.AddCommand(jobCmd)
 	rootCmd.AddCommand(jobCmd)
 	jobCmd.AddCommand(batchImageUpdateCmd)
 	jobCmd.AddCommand(batchImageUpdateCmd)
 	jobCmd.AddCommand(waitCmd)
 	jobCmd.AddCommand(waitCmd)
+	jobCmd.AddCommand(runJobCmd)
 
 
 	batchImageUpdateCmd.PersistentFlags().StringVar(
 	batchImageUpdateCmd.PersistentFlags().StringVar(
 		&tag,
 		&tag,
@@ -124,6 +156,22 @@ func init() {
 	)
 	)
 
 
 	waitCmd.MarkPersistentFlagRequired("name")
 	waitCmd.MarkPersistentFlagRequired("name")
+
+	runJobCmd.PersistentFlags().StringVar(
+		&namespace,
+		"namespace",
+		"",
+		"The namespace of the job.",
+	)
+
+	runJobCmd.PersistentFlags().StringVar(
+		&name,
+		"name",
+		"",
+		"The name of the job.",
+	)
+
+	runJobCmd.MarkPersistentFlagRequired("name")
 }
 }
 
 
 func batchImageUpdate(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
 func batchImageUpdate(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
@@ -150,3 +198,36 @@ func waitForJob(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 		Name:      name,
 		Name:      name,
 	})
 	})
 }
 }
+
+func runJob(authRes *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
+	color.New(color.FgGreen).Printf("Running job %s in namespace %s\n", name, namespace)
+
+	waitForSuccessfulDeploy = true
+
+	updateAgent := &deploy.DeployAgent{
+		App:    name,
+		Client: client,
+		Opts: &deploy.DeployOpts{
+			SharedOpts: &deploy.SharedOpts{
+				ProjectID: cliConf.Project,
+				ClusterID: cliConf.Cluster,
+				Namespace: namespace,
+			},
+		},
+	}
+
+	err := updateAgent.UpdateImageAndValues(map[string]interface{}{
+		"paused": false,
+	})
+	if err != nil {
+		return fmt.Errorf("error running job: %w", err)
+	}
+
+	err = waitForJob(authRes, client, args)
+
+	if err != nil {
+		return fmt.Errorf("error waiting for job to complete: %w", err)
+	}
+
+	return nil
+}