code.cloudfoundry.org/cli@v7.1.0+incompatible/command/v7/run_task_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     8  	"code.cloudfoundry.org/cli/command/flag"
     9  )
    10  
    11  type RunTaskCommand struct {
    12  	BaseCommand
    13  
    14  	RequiredArgs    flag.RunTaskArgsV7 `positional-args:"yes"`
    15  	Command         string             `long:"command" short:"c" description:"The command to execute"`
    16  	Disk            flag.Megabytes     `short:"k" description:"Disk limit (e.g. 256M, 1024M, 1G)"`
    17  	Memory          flag.Megabytes     `short:"m" description:"Memory limit (e.g. 256M, 1024M, 1G)"`
    18  	Name            string             `long:"name" description:"Name to give the task (generated if omitted)"`
    19  	Process         string             `long:"process" description:"Process type to use as a template for command, memory, and disk for the created task."`
    20  	usage           interface{}        `usage:"CF_NAME run-task APP_NAME [--command COMMAND] [-k DISK] [-m MEMORY] [--name TASK_NAME] [--process PROCESS_TYPE]\n\nTIP:\n   Use 'cf logs' to display the logs of the app and all its tasks. If your task name is unique, grep this command's output for the task name to view task-specific logs.\n\nEXAMPLES:\n   CF_NAME run-task my-app --command \"bundle exec rake db:migrate\" --name migrate\n\n   CF_NAME run-task my-app --process batch_job\n\n   CF_NAME run-task my-app"`
    21  	relatedCommands interface{}        `related_commands:"logs, tasks, terminate-task"`
    22  }
    23  
    24  func (cmd RunTaskCommand) Execute(args []string) error {
    25  	err := cmd.SharedActor.CheckTarget(true, true)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	space := cmd.Config.TargetedSpace()
    31  
    32  	user, err := cmd.Config.CurrentUser()
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	application, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, space.GUID)
    38  	cmd.UI.DisplayWarnings(warnings)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	cmd.UI.DisplayTextWithFlavor("Creating task for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{
    44  		"AppName":     cmd.RequiredArgs.AppName,
    45  		"OrgName":     cmd.Config.TargetedOrganization().Name,
    46  		"SpaceName":   space.Name,
    47  		"CurrentUser": user.Name,
    48  	})
    49  
    50  	inputTask := v7action.Task{
    51  		Command: cmd.Command,
    52  	}
    53  
    54  	if cmd.Name != "" {
    55  		inputTask.Name = cmd.Name
    56  	}
    57  	if cmd.Disk.IsSet {
    58  		inputTask.DiskInMB = cmd.Disk.Value
    59  	}
    60  	if cmd.Memory.IsSet {
    61  		inputTask.MemoryInMB = cmd.Memory.Value
    62  	}
    63  	if cmd.Command == "" && cmd.Process == "" {
    64  		cmd.Process = "task"
    65  	}
    66  	if cmd.Process != "" {
    67  		process, warnings, err := cmd.Actor.GetProcessByTypeAndApplication(cmd.Process, application.GUID)
    68  		cmd.UI.DisplayWarnings(warnings)
    69  		if err != nil {
    70  			return err
    71  		}
    72  
    73  		inputTask.Template = &ccv3.TaskTemplate{
    74  			Process: ccv3.TaskProcessTemplate{
    75  				Guid: process.GUID,
    76  			},
    77  		}
    78  	}
    79  
    80  	task, warnings, err := cmd.Actor.RunTask(application.GUID, inputTask)
    81  	cmd.UI.DisplayWarnings(warnings)
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	cmd.UI.DisplayText("Task has been submitted successfully for execution.")
    87  	cmd.UI.DisplayOK()
    88  
    89  	cmd.UI.DisplayKeyValueTable("", [][]string{
    90  		{cmd.UI.TranslateText("task name:"), task.Name},
    91  		{cmd.UI.TranslateText("task id:"), fmt.Sprint(task.SequenceID)},
    92  	}, 3)
    93  
    94  	return nil
    95  }