github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/run_task_command.go (about) 1 package v7 2 3 import ( 4 "fmt" 5 6 "code.cloudfoundry.org/cli/actor/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 9 "code.cloudfoundry.org/cli/command" 10 "code.cloudfoundry.org/cli/command/flag" 11 "code.cloudfoundry.org/cli/command/v7/shared" 12 "code.cloudfoundry.org/clock" 13 ) 14 15 //go:generate counterfeiter . RunTaskActor 16 17 type RunTaskActor interface { 18 GetApplicationByNameAndSpace(appName string, spaceGUID string) (v7action.Application, v7action.Warnings, error) 19 RunTask(appGUID string, task v7action.Task) (v7action.Task, v7action.Warnings, error) 20 GetProcessByTypeAndApplication(processType string, appGUID string) (v7action.Process, v7action.Warnings, error) 21 } 22 23 type RunTaskCommand struct { 24 RequiredArgs flag.RunTaskArgsV7 `positional-args:"yes"` 25 Command string `long:"command" short:"c" description:"The command to execute"` 26 Disk flag.Megabytes `short:"k" description:"Disk limit (e.g. 256M, 1024M, 1G)"` 27 Memory flag.Megabytes `short:"m" description:"Memory limit (e.g. 256M, 1024M, 1G)"` 28 Name string `long:"name" description:"Name to give the task (generated if omitted)"` 29 Process string `long:"process" description:"Process type to use as a template for command, memory, and disk for the created task."` 30 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"` 31 relatedCommands interface{} `related_commands:"logs, tasks, terminate-task"` 32 33 UI command.UI 34 Config command.Config 35 SharedActor command.SharedActor 36 Actor RunTaskActor 37 } 38 39 func (cmd *RunTaskCommand) Setup(config command.Config, ui command.UI) error { 40 cmd.UI = ui 41 cmd.Config = config 42 cmd.SharedActor = sharedaction.NewActor(config) 43 44 client, _, err := shared.GetNewClientsAndConnectToCF(config, ui, "") 45 if err != nil { 46 return err 47 } 48 cmd.Actor = v7action.NewActor(client, config, nil, nil, clock.NewClock()) 49 50 return nil 51 } 52 53 func (cmd RunTaskCommand) Execute(args []string) error { 54 err := cmd.SharedActor.CheckTarget(true, true) 55 if err != nil { 56 return err 57 } 58 59 space := cmd.Config.TargetedSpace() 60 61 user, err := cmd.Config.CurrentUser() 62 if err != nil { 63 return err 64 } 65 66 application, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, space.GUID) 67 cmd.UI.DisplayWarnings(warnings) 68 if err != nil { 69 return err 70 } 71 72 cmd.UI.DisplayTextWithFlavor("Creating task for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{ 73 "AppName": cmd.RequiredArgs.AppName, 74 "OrgName": cmd.Config.TargetedOrganization().Name, 75 "SpaceName": space.Name, 76 "CurrentUser": user.Name, 77 }) 78 79 inputTask := v7action.Task{ 80 Command: cmd.Command, 81 } 82 83 if cmd.Name != "" { 84 inputTask.Name = cmd.Name 85 } 86 if cmd.Disk.IsSet { 87 inputTask.DiskInMB = cmd.Disk.Value 88 } 89 if cmd.Memory.IsSet { 90 inputTask.MemoryInMB = cmd.Memory.Value 91 } 92 if cmd.Command == "" && cmd.Process == "" { 93 cmd.Process = "task" 94 } 95 if cmd.Process != "" { 96 process, warnings, err := cmd.Actor.GetProcessByTypeAndApplication(cmd.Process, application.GUID) 97 cmd.UI.DisplayWarnings(warnings) 98 if err != nil { 99 return err 100 } 101 102 inputTask.Template = &ccv3.TaskTemplate{ 103 Process: ccv3.TaskProcessTemplate{ 104 Guid: process.GUID, 105 }, 106 } 107 } 108 109 task, warnings, err := cmd.Actor.RunTask(application.GUID, inputTask) 110 cmd.UI.DisplayWarnings(warnings) 111 if err != nil { 112 return err 113 } 114 115 cmd.UI.DisplayOK() 116 cmd.UI.DisplayText("Task has been submitted successfully for execution.") 117 cmd.UI.DisplayKeyValueTable("", [][]string{ 118 {cmd.UI.TranslateText("task name:"), task.Name}, 119 {cmd.UI.TranslateText("task id:"), fmt.Sprint(task.SequenceID)}, 120 }, 3) 121 122 return nil 123 }