in agentendpoint/exec_task.go [128:229]
func (e *execTask) run(ctx context.Context) error {
clog.Infof(ctx, "Beginning ExecStepTask")
e.StartedAt = time.Now()
req := &agentendpointpb.ReportTaskProgressRequest{
TaskId: e.TaskID,
TaskType: agentendpointpb.TaskType_EXEC_STEP_TASK,
Progress: &agentendpointpb.ReportTaskProgressRequest_ExecStepTaskProgress{
ExecStepTaskProgress: &agentendpointpb.ExecStepTaskProgress{State: agentendpointpb.ExecStepTaskProgress_STARTED},
},
}
res, err := e.client.reportTaskProgress(ctx, req)
if err != nil {
return fmt.Errorf("error reporting state %s: %v", agentendpointpb.ExecStepTaskProgress_STARTED, err)
}
if res.GetTaskDirective() == agentendpointpb.TaskDirective_STOP {
return e.reportCompletedState(ctx, errServerCancel.Error(), &agentendpointpb.ReportTaskCompleteRequest_ExecStepTaskOutput{
ExecStepTaskOutput: &agentendpointpb.ExecStepTaskOutput{State: agentendpointpb.ExecStepTaskOutput_CANCELLED},
})
}
stepConfig := e.Task.GetExecStep().GetLinuxExecStepConfig()
if goos == "windows" {
stepConfig = e.Task.GetExecStep().GetWindowsExecStepConfig()
}
if stepConfig == nil {
// The given ExecTask does not apply to this OS.
return e.reportCompletedState(ctx, "", &agentendpointpb.ReportTaskCompleteRequest_ExecStepTaskOutput{
ExecStepTaskOutput: &agentendpointpb.ExecStepTaskOutput{
State: agentendpointpb.ExecStepTaskOutput_COMPLETED,
ExitCode: 0,
},
})
}
localPath := stepConfig.GetLocalPath()
if gcsObject := stepConfig.GetGcsObject(); gcsObject != nil {
var err error
localPath, err = getGCSObject(ctx, gcsObject.GetBucket(), gcsObject.GetObject(), gcsObject.GetGenerationNumber())
if err != nil {
msg := fmt.Sprintf("Error downloading GCS object: %v", err)
clog.Errorf(ctx, "%v", msg)
return e.reportCompletedState(ctx, msg, &agentendpointpb.ReportTaskCompleteRequest_ExecStepTaskOutput{
ExecStepTaskOutput: &agentendpointpb.ExecStepTaskOutput{
State: agentendpointpb.ExecStepTaskOutput_COMPLETED,
ExitCode: -1,
},
})
}
defer func() {
if err := os.Remove(localPath); err != nil {
clog.Errorf(ctx, "error removing downloaded file %s", err)
}
}()
}
exitCode := int32(-1)
switch stepConfig.GetInterpreter() {
case agentendpointpb.ExecStepConfig_INTERPRETER_UNSPECIFIED:
if goos == "windows" {
err = errWinNoInt
} else {
exitCode, err = executeCommand(ctx, localPath, nil)
}
case agentendpointpb.ExecStepConfig_SHELL:
if goos == "windows" {
exitCode, err = executeCommand(ctx, winCmd, []string{"/c", localPath})
} else {
exitCode, err = executeCommand(ctx, sh, []string{localPath})
}
case agentendpointpb.ExecStepConfig_POWERSHELL:
if goos == "windows" {
exitCode, err = executeCommand(ctx, winPowershell, append(winPowershellArgs, "-File", localPath))
} else {
err = errLinuxPowerShell
}
default:
err = fmt.Errorf("invalid interpreter %q", stepConfig.GetInterpreter())
}
if err != nil {
msg := fmt.Sprintf("Error running ExecStepTask: %v", err)
clog.Errorf(ctx, "%v", msg)
return e.reportCompletedState(ctx, msg, &agentendpointpb.ReportTaskCompleteRequest_ExecStepTaskOutput{
ExecStepTaskOutput: &agentendpointpb.ExecStepTaskOutput{
State: agentendpointpb.ExecStepTaskOutput_COMPLETED,
ExitCode: exitCode,
},
})
}
if err := e.reportCompletedState(ctx, "", &agentendpointpb.ReportTaskCompleteRequest_ExecStepTaskOutput{
ExecStepTaskOutput: &agentendpointpb.ExecStepTaskOutput{
State: agentendpointpb.ExecStepTaskOutput_COMPLETED,
ExitCode: exitCode,
},
}); err != nil {
return err
}
clog.Infof(ctx, "Successfully completed ApplyConfigTask")
return nil
}