func (script GenericScript) Run(errorChan chan RunScriptResult, doneChan chan RunScriptResult) { err := script.fs.MkdirAll(script.LogPath(), os.FileMode(0750)) if err != nil { errorChan <- RunScriptResult{script.JobName(), script.Path(), err} return } stdoutPath := fmt.Sprintf("%s.stdout.log", script.LogPath()) stdoutFile, err := script.fs.OpenFile(stdoutPath, fileOpenFlag, fileOpenPerm) if err != nil { errorChan <- RunScriptResult{script.JobName(), script.Path(), err} return } defer func() { _ = stdoutFile.Close() }() stderrPath := fmt.Sprintf("%s.stderr.log", script.LogPath()) stderrFile, err := script.fs.OpenFile(stderrPath, fileOpenFlag, fileOpenPerm) if err != nil { errorChan <- RunScriptResult{script.JobName(), script.Path(), err} return } defer func() { _ = stderrFile.Close() }() command := system.Command{ Name: script.Path(), Env: map[string]string{ "PATH": "/usr/sbin:/usr/bin:/sbin:/bin", }, } command.Stdout = stdoutFile command.Stderr = stderrFile _, _, _, runErr := script.runner.RunComplexCommand(command) if runErr == nil { doneChan <- RunScriptResult{script.JobName(), script.Path(), nil} } else { errorChan <- RunScriptResult{script.JobName(), script.Path(), runErr} } }
func (s ConcreteScript) runOnce(params ScriptParams) (int, error) { jobChange := params.JobChange() hashChange := params.HashChange() updatedPkgs := params.UpdatedPackages() command := boshsys.Command{ Name: s.path, 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 := s.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 (script GenericScript) Run() RunScriptResult { err := ensureContainingDir(script.fs, script.stdoutLogPath) if err != nil { return RunScriptResult{script.Tag(), script.Path(), err} } err = ensureContainingDir(script.fs, script.stderrLogPath) if err != nil { return RunScriptResult{script.Tag(), script.Path(), err} } stdoutFile, err := script.fs.OpenFile(script.stdoutLogPath, fileOpenFlag, fileOpenPerm) if err != nil { return RunScriptResult{script.Tag(), script.Path(), err} } defer func() { _ = stdoutFile.Close() }() stderrFile, err := script.fs.OpenFile(script.stderrLogPath, fileOpenFlag, fileOpenPerm) if err != nil { return RunScriptResult{script.Tag(), script.Path(), err} } defer func() { _ = stderrFile.Close() }() command := system.Command{ Name: script.Path(), Env: map[string]string{ "PATH": "/usr/sbin:/usr/bin:/sbin:/bin", }, } command.Stdout = stdoutFile command.Stderr = stderrFile _, _, _, runErr := script.runner.RunComplexCommand(command) return RunScriptResult{script.Tag(), script.Path(), runErr} }
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.WrapErrorf(err, "Removing log dir for job %s", jobName) } err = f.fs.MkdirAll(logsDir, os.FileMode(0750)) if err != nil { return nil, bosherr.WrapErrorf(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.WrapErrorf(err, "Opening stdout for task %s", taskName) } defer func() { _ = stdoutFile.Close() }() cmd.Stdout = stdoutFile stderrFile, err := f.fs.OpenFile(stderrPath, fileOpenFlag, fileOpenPerm) if err != nil { return nil, bosherr.WrapErrorf(err, "Opening stderr for task %s", taskName) } defer func() { _ = 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.WrapErrorf(err, "Truncating stdout for task %s", taskName) } stderr, isStderrTruncated, err := f.getTruncatedOutput(stderrFile, f.truncateLength) if err != nil { return nil, bosherr.WrapErrorf(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 }