Skip to content

Commit

Permalink
POR-2123: support job run on cli (#3989)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-g-town authored Nov 16, 2023
1 parent 8b0eb53 commit 7fbba9a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
6 changes: 3 additions & 3 deletions api/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (c *Client) postRequest(relPath string, data interface{}, response interfac
}

var httpErr *types.ExternalError
var err error
var sendErr error

for i := 0; i < int(retryCount); i++ {
strData, err := json.Marshal(data)
Expand All @@ -184,10 +184,10 @@ func (c *Client) postRequest(relPath string, data interface{}, response interfac
}

httpErr, err = c.sendRequest(req, response, true)

if httpErr == nil && err == nil {
return nil
}
sendErr = err

if i != int(retryCount)-1 {
if httpErr != nil {
Expand All @@ -202,7 +202,7 @@ func (c *Client) postRequest(relPath string, data interface{}, response interfac
return fmt.Errorf("%v", httpErr.Error)
}

return err
return sendErr
}

type patchRequestOpts struct {
Expand Down
27 changes: 27 additions & 0 deletions api/client/porter_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,30 @@ func (c *Client) UseNewApplyLogic(

return resp, err
}

// RunAppJob runs a job for an app
func (c *Client) RunAppJob(
ctx context.Context,
projectID, clusterID uint,
appName string, jobName string,
deploymentTargetID string,
) (*porter_app.AppRunResponse, error) {
resp := &porter_app.AppRunResponse{}

req := &porter_app.AppRunRequest{
ServiceName: jobName,
DeploymentTargetID: deploymentTargetID,
}

err := c.postRequest(
fmt.Sprintf(
"/projects/%d/clusters/%d/apps/%s/run",
projectID, clusterID,
appName,
),
req,
resp,
)

return resp, err
}
29 changes: 27 additions & 2 deletions cli/cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
appTag string
appCpuMilli int
appMemoryMi int
jobName string
)

const (
Expand All @@ -60,7 +61,7 @@ func registerCommand_App(cliConf config.CLIConfig) *cobra.Command {
// appRunCmd represents the "porter app run" subcommand
appRunCmd := &cobra.Command{
Use: "run [application] -- COMMAND [args...]",
Args: cobra.MinimumNArgs(2),
Args: cobra.MinimumNArgs(1),
Short: "Runs a command inside a connected cluster container.",
Run: func(cmd *cobra.Command, args []string) {
err := checkLoginAndRunWithConfig(cmd, cliConf, args, appRun)
Expand Down Expand Up @@ -169,6 +170,13 @@ func appRunFlags(appRunCmd *cobra.Command) {
"",
"name of the container inside pod to run the command in",
)

appRunCmd.PersistentFlags().StringVar(
&jobName,
"job",
"",
"name of the job to run (will run the job as defined instead of the provided command, and returns the job run id without waiting for the job to complete or displaying logs)",
)
}

func appRollback(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, args []string) error {
Expand Down Expand Up @@ -198,7 +206,24 @@ func appRollback(ctx context.Context, _ *types.GetAuthenticatedUserResponse, cli
return nil
}

func appRun(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, _ config.FeatureFlags, _ *cobra.Command, args []string) error {
func appRun(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, ff config.FeatureFlags, _ *cobra.Command, args []string) error {
if jobName != "" {
if !ff.ValidateApplyV2Enabled {
return fmt.Errorf("job flag is not supported on this project")
}

return v2.RunAppJob(ctx, v2.RunAppJobInput{
CLIConfig: cliConfig,
Client: client,
AppName: args[0],
JobName: jobName,
})
}

if len(args) < 2 {
return fmt.Errorf("porter app run requires at least 2 arguments")
}

execArgs := args[1:]

color.New(color.FgGreen).Println("Attempting to run", strings.Join(execArgs, " "), "for application", args[0])
Expand Down
39 changes: 39 additions & 0 deletions cli/cmd/v2/run_job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package v2

import (
"context"
"fmt"

"github.com/fatih/color"

api "github.com/porter-dev/porter/api/client"
"github.com/porter-dev/porter/cli/cmd/config"
)

// RunAppJobInput is the input for the RunAppJob function
type RunAppJobInput struct {
// CLIConfig is the CLI configuration
CLIConfig config.CLIConfig
// Client is the Porter API client
Client api.Client

AppName string
JobName string
}

// RunAppJob triggers a job run for an app and returns without waiting for the job to complete
func RunAppJob(ctx context.Context, inp RunAppJobInput) error {
targetResp, err := inp.Client.DefaultDeploymentTarget(ctx, inp.CLIConfig.Project, inp.CLIConfig.Cluster)
if err != nil {
return fmt.Errorf("error calling default deployment target endpoint: %w", err)
}

resp, err := inp.Client.RunAppJob(ctx, inp.CLIConfig.Project, inp.CLIConfig.Cluster, inp.AppName, inp.JobName, targetResp.DeploymentTargetID)
if err != nil {
return fmt.Errorf("unable to run job: %w", err)
}

color.New(color.FgGreen).Println("Triggered job with id:", resp.JobRunID) // nolint:errcheck,gosec

return nil
}

0 comments on commit 7fbba9a

Please sign in to comment.