func (script ConcreteDrainScript) Run(params DrainScriptParams) (int, error) { jobChange := params.JobChange() hashChange := params.HashChange() updatedPkgs := params.UpdatedPackages() command := boshsys.Command{ Name: script.drainScriptPath, Env: map[string]string{ "PATH": "/usr/sbin:/usr/bin:/sbin:/bin", }, } jobState, err := params.JobState() if err != nil { return 0, bosherr.WrapError(err, "Getting job state") } if jobState != "" { command.Env["BOSH_JOB_STATE"] = jobState } jobNextState, err := params.JobNextState() if err != nil { return 0, bosherr.WrapError(err, "Getting job next state") } if jobNextState != "" { command.Env["BOSH_JOB_NEXT_STATE"] = jobNextState } command.Args = append(command.Args, jobChange, hashChange) command.Args = append(command.Args, updatedPkgs...) stdout, _, _, err := script.runner.RunComplexCommand(command) if err != nil { return 0, bosherr.WrapError(err, "Running drain script") } value, err := strconv.Atoi(strings.TrimSpace(stdout)) if err != nil { return 0, bosherr.WrapError(err, "Script did not return a signed integer") } return value, nil }
func (f FileLoggingCmdRunner) RunCommand(jobName string, taskName string, cmd boshsys.Command) (*CmdResult, error) { logsDir := filepath.Join(f.baseDir, jobName) err := f.fs.RemoveAll(logsDir) if err != nil { return nil, bosherr.WrapError(err, "Removing log dir for job %s", jobName) } err = f.fs.MkdirAll(logsDir, os.FileMode(0750)) if err != nil { return nil, bosherr.WrapError(err, "Creating log dir for job %s", jobName) } stdoutPath := filepath.Join(logsDir, fmt.Sprintf("%s.stdout.log", taskName)) stderrPath := filepath.Join(logsDir, fmt.Sprintf("%s.stderr.log", taskName)) stdoutFile, err := f.fs.OpenFile(stdoutPath, fileOpenFlag, fileOpenPerm) if err != nil { return nil, bosherr.WrapError(err, "Opening stdout for task %s", taskName) } defer stdoutFile.Close() cmd.Stdout = stdoutFile stderrFile, err := f.fs.OpenFile(stderrPath, fileOpenFlag, fileOpenPerm) if err != nil { return nil, bosherr.WrapError(err, "Opening stderr for task %s", taskName) } defer stderrFile.Close() cmd.Stderr = stderrFile // Stdout/stderr are redirected to the files _, _, exitStatus, runErr := f.cmdRunner.RunComplexCommand(cmd) stdout, isStdoutTruncated, err := f.getTruncatedOutput(stdoutFile, f.truncateLength) if err != nil { return nil, bosherr.WrapError(err, "Truncating stdout for task %s", taskName) } stderr, isStderrTruncated, err := f.getTruncatedOutput(stderrFile, f.truncateLength) if err != nil { return nil, bosherr.WrapError(err, "Truncating stderr for task %s", taskName) } result := &CmdResult{ IsStdoutTruncated: isStdoutTruncated, IsStderrTruncated: isStderrTruncated, Stdout: stdout, Stderr: stderr, ExitStatus: exitStatus, } if runErr != nil { return nil, FileLoggingExecErr{result} } return result, nil }