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 (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 }