Esempio n. 1
0
// Result attempts to simulate the running of a command by filling the
// appropriate buffers from its Test fields and forwarding its Test exit codes
func (c *DummyCommand) Result() (*Result, error) {
	//command := exec.Command(c.Name, c.Args...)
	outbuf := &bytes.Buffer{}
	errbuf := &bytes.Buffer{}
	combinedbuf := &bytes.Buffer{}
	outWriters := []io.Writer{outbuf, combinedbuf}
	errWriters := []io.Writer{errbuf, combinedbuf}
	if c.Command.TeeOut != nil {
		outWriters = append(outWriters, c.TeeOut)
	}
	if c.Command.TeeErr != nil {
		errWriters = append(errWriters, c.TeeErr)
	}

	outWr := io.MultiWriter(outWriters...)
	errWr := io.MultiWriter(errWriters...)

	outWr.Write(c.DummyResult.SO)
	errWr.Write(c.DummyResult.SE)

	return &Result{
		Command:  &c.Command, // XXX thus the live command...
		Stdout:   &Output{outbuf},
		Stderr:   &Output{errbuf},
		Combined: &Output{combinedbuf},
		Err:      c.DummyResult.Err,
		ExitCode: c.DummyResult.Status,
	}, nil
}
// hashCopyN - Calculates Md5sum and SHA256sum for upto partSize amount of bytes.
func (c Client) hashCopyN(writer io.ReadWriter, reader io.Reader, partSize int64) (md5Sum, sha256Sum []byte, size int64, err error) {
	// MD5 and SHA256 hasher.
	var hashMD5, hashSHA256 hash.Hash
	// MD5 and SHA256 hasher.
	hashMD5 = md5.New()
	hashWriter := io.MultiWriter(writer, hashMD5)
	if c.signature.isV4() {
		hashSHA256 = sha256.New()
		hashWriter = io.MultiWriter(writer, hashMD5, hashSHA256)
	}

	// Copies to input at writer.
	size, err = io.CopyN(hashWriter, reader, partSize)
	if err != nil {
		// If not EOF return error right here.
		if err != io.EOF {
			return nil, nil, 0, err
		}
	}

	// Finalize md5shum and sha256 sum.
	md5Sum = hashMD5.Sum(nil)
	if c.signature.isV4() {
		sha256Sum = hashSHA256.Sum(nil)
	}
	return md5Sum, sha256Sum, size, err
}
func ExecuteCommand(command *ExecCmd) (output string, err error) {

	// Execute the command using a shell
	var shell, flag string
	if runtime.GOOS == "windows" {
		shell = "cmd"
		flag = "/C"
	} else {
		shell = "/bin/sh"
		flag = "-c"
	}

	// Setup the reader that will read the lines from the command
	pr, pw := io.Pipe()
	copyDoneCh := make(chan struct{})
	go copyOutput(pr, copyDoneCh)

	// Setup the command
	cmd := exec.Command(shell, flag, command.Cmd)
	out, _ := circbuf.NewBuffer(maxBufSize)
	cmd.Stderr = io.MultiWriter(out, pw)
	cmd.Stdout = io.MultiWriter(out, pw)

	// Run the command to completion
	runErr := cmd.Run()
	pw.Close()
	<-copyDoneCh

	if runErr != nil {
		return string(out.Bytes()), fmt.Errorf("Error running command '%s': %v. Output: %s", command, runErr, out.Bytes())
	}

	return string(out.Bytes()), nil
}
Esempio n. 4
0
func (p *Project) ProjectCmd() *exec.Cmd {
	name := p.Name()
	if runtime.GOOS != "windows" {
		name = "./" + name
	}
	args := []string{"-config", p.configPath, fmt.Sprintf("-port=%d", p.port)}
	if p.noDebug {
		args = append(args, "-debug=false", "-template-debug=false", "-log-debug=false")
	} else {
		if p.profile {
			args = append(args, "-debug=false", "-template-debug=false", "-log-debug")
		} else {
			args = append(args, "-debug", "-template-debug", "-log-debug")
		}
	}
	if p.noCache {
		args = append(args, "-cache=dummy://")
	}
	cmd := exec.Command(name, args...)
	cmd.Stdin = os.Stdin
	cmd.Stdout = io.MultiWriter(os.Stdout, &p.out)
	cmd.Stderr = io.MultiWriter(os.Stderr, &p.out)
	cmd.Dir = p.dir
	cmd.Env = append(cmd.Env, os.Environ()...)
	cmd.Env = append(cmd.Env, devserver.EnvVar+"=1")
	cmd.Env = append(cmd.Env, "GONDOLA_FORCE_TTY=1")
	if p.profile {
		cmd.Env = append(cmd.Env, "GONDOLA_NO_CACHE_LAYER=1")
	}
	return cmd
}
Esempio n. 5
0
func (s *sshConn) runSudoCommand(sess *ssh.Session, stdin io.Writer, cmd string) error {
	// Wrap the output
	if s.DebugEnabled() {
		debugOutWriter := util.NewDebugLogWriter("SSH OUT:", s.Context)
		if sess.Stdout != nil {
			sess.Stdout = io.MultiWriter(sess.Stdout, debugOutWriter)
		} else {
			sess.Stdout = debugOutWriter
		}
		debugErrWriter := util.NewDebugLogWriter("SSH ERR:", s.Context)
		if sess.Stderr != nil {
			sess.Stderr = io.MultiWriter(sess.Stderr, debugErrWriter)
		} else {
			sess.Stderr = debugErrWriter
		}
	}
	// We need a checker to enter the password
	passwordTyper := util.NewExpectListener(stdin, shell.SudoPasswordPromptMatch, s.server.SSH.Pass+"\n")

	if sess.Stdout == nil {
		sess.Stdout = passwordTyper
	} else {
		sess.Stdout = io.MultiWriter(sess.Stdout, passwordTyper)
	}
	if sess.Stderr == nil {
		sess.Stderr = passwordTyper
	} else {
		sess.Stderr = io.MultiWriter(sess.Stderr, passwordTyper)
	}
	if err := sess.Run("sudo -S " + cmd); err != nil {
		return fmt.Errorf("Error running command %v: %v", cmd, err)
	}
	return nil
}
Esempio n. 6
0
func InitLog() {
	// ディレクトリの作成
	err := os.MkdirAll("log", 0777)
	if err != nil {
		util.Perror(err)
	}

	accessLogFile, err := os.OpenFile("log/access.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		panic(fmt.Sprintf("[Error]: %s", err))
	}

	errorLogFile, err := os.OpenFile("log/error.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		panic(fmt.Sprintf("[Error]: %s", err))
	}

	access_out := io.MultiWriter(os.Stdout, accessLogFile)
	accessLog.Formatter = &logrus.TextFormatter{FullTimestamp: true, DisableColors: true}
	accessLog.Out = access_out

	error_out := io.MultiWriter(os.Stdout, errorLogFile)
	errorLog.Formatter = &logrus.TextFormatter{FullTimestamp: true, DisableColors: true}
	errorLog.Out = error_out
}
Esempio n. 7
0
func runCmd(cmd string, args []string, env []string, stdOut, errOut io.Writer) error {
	glog.V(5).Infof("Executing local command:")
	glog.V(5).Infof("  %s", cmd)
	for _, a := range args {
		glog.V(5).Infof("  %s", a)
	}
	if len(env) > 0 {
		glog.V(5).Infof("Environment:")
		for _, e := range env {
			glog.V(5).Infof("  %s", e)
		}
	}
	c := exec.Command(cmd, args...)
	outLog := &bytes.Buffer{}
	errLog := &bytes.Buffer{}
	c.Stdout = io.MultiWriter(stdOut, outLog)
	c.Stderr = io.MultiWriter(errOut, errLog)
	c.Env = env
	err := c.Run()
	if glog.V(5) {
		if err != nil {
			glog.Infof("Error from execution: %v", err)
		}
		if outLog.Len() > 0 {
			glog.Infof("Stdout:\n%s", outLog.String())
		}
		if errLog.Len() > 0 {
			glog.Infof("Errout:\n%s", errLog.String())
		}
	}
	if err != nil {
		return newExecError(append([]string{cmd}, args...), err, outLog.Bytes(), errLog.Bytes())
	}
	return nil
}
Esempio n. 8
0
func setupLogger(logDir string) *log.Logger {
	var logger *log.Logger

	log.SetFlags(log.Ldate | log.Lmicroseconds)

	if logDir != "" {
		logFile := filepath.Join(logDir, "host.log")
		if err := os.MkdirAll(filepath.Dir(logFile), 0755); err != nil {
			log.Fatalf("could not not mkdir for logs: %s", err)
		}

		hostlog, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
		if err != nil {
			log.Fatal(err)
		}
		defer hostlog.Close()

		log.Printf("Logging to %s\n", logFile)
		log.SetOutput(io.MultiWriter(hostlog, os.Stdout))
		logger = log.New(io.MultiWriter(hostlog, os.Stderr), "", log.Flags())
	} else {
		logger = log.New(os.Stderr, "", log.Flags())
	}

	return logger
}
Esempio n. 9
0
// Sh runs a shell command
func Sh(L *lua.LState, options ...func(opts *shOpts)) int {
	stdoutBuf := new(bytes.Buffer)
	stderrBuf := new(bytes.Buffer)
	opts := &shOpts{stdout: os.Stdout}
	for _, option := range options {
		option(opts)
	}

	cmd := exec.Command(shell, "-c", L.ToString(1))
	cmd.Stdout = io.MultiWriter(stdoutBuf, opts.stdout)
	cmd.Stderr = io.MultiWriter(stderrBuf, os.Stderr)
	if !opts.noEcho {
		fmt.Printf("%v\n", L.ToString(1))
	}
	err := cmd.Run()
	if err != nil {
		if exitErr, ok := err.(*exec.ExitError); ok {
			if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
				if opts.noAbort {
					L.Push(lua.LNumber(status.ExitStatus()))
					L.Push(lua.LString(stdoutBuf.String()))
					L.Push(lua.LString(stderrBuf.String()))
					return 3
				}

				L.Error(lua.LString(fmt.Sprintf("blade: Target: [%v] Error: %v", currentTarget, status.ExitStatus())), 0)
				os.Exit(1)
			}
		}
	}
	L.Push(lua.LNumber(0))
	L.Push(lua.LString(stdoutBuf.String()))
	L.Push(lua.LString(stderrBuf.String()))
	return 3
}
Esempio n. 10
0
// Run executes a Config and returns associated results.
func (c Config) Run() (r Result, err error) {
	dockerized, err := dockerize(c.Volume)

	if err != nil {
		return
	}

	cmd := exec.Command(
		"docker",
		"run",
		"-v",
		dockerized+":/run",
		c.Image)

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return
	}

	stderr, err := cmd.StderrPipe()
	if err != nil {
		return
	}

	if err = cmd.Start(); err != nil {
		return
	}

	go io.Copy(io.MultiWriter(os.Stdout, &r.Stdout), stdout)
	go io.Copy(io.MultiWriter(os.Stdout, &r.Stderr), stderr)

	cmd.Wait()
	return
}
Esempio n. 11
0
// hashCopyBuffer is identical to hashCopyN except that it stages
// through the provided buffer (if one is required) rather than
// allocating a temporary one. If buf is nil, one is allocated for 5MiB.
func (c Client) hashCopyBuffer(writer io.Writer, reader io.Reader, buf []byte) (md5Sum, sha256Sum []byte, size int64, err error) {
	// MD5 and SHA256 hasher.
	var hashMD5, hashSHA256 hash.Hash
	// MD5 and SHA256 hasher.
	hashMD5 = md5.New()
	hashWriter := io.MultiWriter(writer, hashMD5)
	if c.signature.isV4() {
		hashSHA256 = sha256.New()
		hashWriter = io.MultiWriter(writer, hashMD5, hashSHA256)
	}

	// Allocate buf if not initialized.
	if buf == nil {
		buf = make([]byte, optimalReadBufferSize)
	}

	// Using copyBuffer to copy in large buffers, default buffer
	// for io.Copy of 32KiB is too small.
	size, err = copyBuffer(hashWriter, reader, buf)
	if err != nil {
		return nil, nil, 0, err
	}

	// Finalize md5 sum and sha256 sum.
	md5Sum = hashMD5.Sum(nil)
	if c.signature.isV4() {
		sha256Sum = hashSHA256.Sum(nil)
	}
	return md5Sum, sha256Sum, size, err
}
Esempio n. 12
0
func Setup(config *Config) error {
	var logOutput io.Writer

	// Setup the default logging
	logFilter := NewLogFilter()
	logFilter.MinLevel = logutils.LogLevel(strings.ToUpper(config.Level))
	logFilter.Writer = config.Writer
	if !ValidateLevelFilter(logFilter.MinLevel, logFilter) {
		levels := make([]string, 0, len(logFilter.Levels))
		for _, level := range logFilter.Levels {
			levels = append(levels, string(level))
		}
		return fmt.Errorf("invalid log level %q, valid log levels are %s",
			config.Level, strings.Join(levels, ", "))
	}

	// Check if syslog is enabled
	if config.Syslog {
		log.Printf("[DEBUG] (logging) enabling syslog on %s", config.SyslogFacility)

		l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, config.SyslogFacility, config.Name)
		if err != nil {
			return fmt.Errorf("error setting up syslog logger: %s", err)
		}
		syslog := &SyslogWrapper{l, logFilter}
		logOutput = io.MultiWriter(logFilter, syslog)
	} else {
		logOutput = io.MultiWriter(logFilter)
	}

	log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC)
	log.SetOutput(logOutput)

	return nil
}
//Continually processes postback objects from the local Redis database server
//Precondition: The file at LOG_FILE_NAME must be acessable to write logs
//              A local Redis database server must be running on port 6379
//Postcondition: Any Postback objects received from the Redis database's List, REDIS_LIST_NAME, are processed
//               Log levels are set up for the rest of the program to use, all pertinent inforamtion is logged
func main() {
	logFile, logFileError := os.OpenFile(LOG_FILE_NAME, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if logFileError != nil {
		log.Fatalln("Failed to open log file: ", logFileError)
	}
	defer logFile.Close()

	var traceOutput io.Writer
	if SHOW_TRACES {
		traceOutput = os.Stdout
	} else {
		traceOutput = ioutil.Discard
	}
	infoOutput := io.MultiWriter(logFile, os.Stdout)
	warningOutput := io.MultiWriter(logFile, os.Stdout)
	errorOutput := io.MultiWriter(logFile, os.Stderr)
	InitLoggers(traceOutput, infoOutput, warningOutput, errorOutput)

	redisServer, err := redis.Dial("tcp", ":6379")
	if err != nil {
		Error.Fatalln(err)
	}
	defer redisServer.Close()
	for {
		processPostbackObject(redisServer)
	}
}
Esempio n. 14
0
// New creates a new Logger which writes to a file and to stderr
func New(path string, production bool) *Logger {
	var logWriter io.Writer
	stdlog.SetFlags(stdlog.Llongfile)
	// doubleWriter writes to stdErr and to a file
	logFile, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0640)
	if err != nil {
		logWriter = io.MultiWriter(os.Stderr)
	} else {

		// Do not write to Stderr in production
		if production {
			logWriter = io.MultiWriter(logFile)
		} else {
			logWriter = io.MultiWriter(os.Stderr, logFile)
		}

	}

	// By default logger logs to console and a file
	l := stdlog.New(logWriter, "", stdlog.Ldate|stdlog.Ltime)
	if l == nil {
		stdlog.Printf("Error setting up log at path %s", path)
	}

	logger := &Logger{
		log:    l,
		Filter: "",
	}

	logger.Printf("#info Opened log file at %s", path)
	return logger
}
Esempio n. 15
0
func initLoggers() {
	var writer io.Writer = ioutil.Discard
	var errWriter io.Writer = ioutil.Discard

	if Config.GetBool("log.enableStdOut") {
		writer = io.MultiWriter(writer, os.Stdout)
	}
	if Config.GetBool("log.enableStdErr") {
		errWriter = io.MultiWriter(errWriter, os.Stderr)
	}

	logFilePath := Config.GetString("log.filePath")
	if len(logFilePath) > 0 {
		if file, err := os.Open(logFilePath); err == nil {
			writer = io.MultiWriter(writer, file)
			errWriter = io.MultiWriter(errWriter, file)
		}
	}

	LogV = log.New(writer, "[VERBOSE]:", log.Ldate|log.Ltime|log.Lshortfile)
	LogD = log.New(writer, "[DEBUG]:", log.Ldate|log.Ltime|log.Lshortfile)
	LogE = log.New(errWriter, "[ERROR]:", log.Ldate|log.Ltime|log.Lshortfile)
	LogW = log.New(errWriter, "[WARNING]:", log.Ldate|log.Ltime|log.Lshortfile)

	//fmt.Printf("Log enable stdout: %v\n", Config.GetBool("log.enableStdOut"))
	//fmt.Printf("Log enable stderr: %v\n", Config.GetBool("log.enableStdErr"))
}
Esempio n. 16
0
func (C *CMD) execute() (code int, err error) {
	c := exec.Command(C.Name, C.Args...)
	c.Stdout = C.Stdout
	c.Stderr = C.Stderr
	c.Env = os.Environ()
	if C.EchoStdout {
		c.Stdout = io.MultiWriter(os.Stdout, c.Stdout)
	}
	if C.EchoStderr {
		c.Stderr = io.MultiWriter(os.Stderr, c.Stderr)
	}
	if C.WriteStdout != nil {
		c.Stdout = io.MultiWriter(C.WriteStdout, c.Stdout)
	}
	if C.WriteStderr != nil {
		c.Stderr = io.MultiWriter(C.WriteStderr, c.Stderr)
	}
	if C.EchoStdout || C.EchoStderr {
		cli.Logf("shell> %s", C)
	}
	if err := c.Start(); err != nil {
		cli.Fatalf("Unable to begin command execution; %s", err)
	}
	err = c.Wait()
	if err != nil {
		if exiterr, ok := err.(*exec.ExitError); ok {
			if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
				return status.ExitStatus(), err
			}
		}
		cli.Fatalf("Command failed, unable to get exit code: %s", C)
	}
	return 0, nil
}
Esempio n. 17
0
// Run executes the job and blocks until the job completes.
// If the job returns a failure status, an error is returned
// which includes the status.
func (job *Job) Run() error {
	defer func() {
		var wg sync.WaitGroup
		for _, f := range job.onExit {
			wg.Add(1)
			go func(f func()) {
				f()
				wg.Done()
			}(f)
		}
		wg.Wait()
	}()
	if job.Stdout != nil && job.Stdout != os.Stdout {
		job.Stdout = io.MultiWriter(job.Stdout, os.Stdout)
	}
	if job.Stderr != nil && job.Stderr != os.Stderr {
		job.Stderr = io.MultiWriter(job.Stderr, os.Stderr)
	}
	job.Eng.Logf("+job %s", job.CallString())
	defer func() {
		job.Eng.Logf("-job %s%s", job.CallString(), job.StatusString())
	}()
	if job.handler == nil {
		job.status = "command not found"
	} else {
		job.status = job.handler(job)
	}
	if job.status != "0" {
		return fmt.Errorf("%s: %s", job.Name, job.status)
	}
	return nil
}
Esempio n. 18
0
func cammountTest(t *testing.T, fn func(env *mountEnv)) {
	dupLog := io.MultiWriter(os.Stderr, testLog{t})
	log.SetOutput(dupLog)
	defer log.SetOutput(os.Stderr)

	w := test.GetWorld(t)
	mountPoint, err := ioutil.TempDir("", "fs-test-mount")
	if err != nil {
		t.Fatal(err)
	}
	verbose := "false"
	var stderrDest io.Writer = ioutil.Discard
	if v, _ := strconv.ParseBool(os.Getenv("VERBOSE_FUSE")); v {
		verbose = "true"
		stderrDest = testLog{t}
	}
	if v, _ := strconv.ParseBool(os.Getenv("VERBOSE_FUSE_STDERR")); v {
		stderrDest = io.MultiWriter(stderrDest, os.Stderr)
	}

	mount := w.Cmd("cammount", "--debug="+verbose, mountPoint)
	mount.Stderr = stderrDest
	mount.Env = append(mount.Env, "CAMLI_TRACK_FS_STATS=1")

	stdin, err := mount.StdinPipe()
	if err != nil {
		t.Fatal(err)
	}
	if err := mount.Start(); err != nil {
		t.Fatal(err)
	}
	waitc := make(chan error, 1)
	go func() { waitc <- mount.Wait() }()
	defer func() {
		log.Printf("Sending quit")
		stdin.Write([]byte("q\n"))
		select {
		case <-time.After(5 * time.Second):
			log.Printf("timeout waiting for cammount to finish")
			mount.Process.Kill()
			Unmount(mountPoint)
		case err := <-waitc:
			log.Printf("cammount exited: %v", err)
		}
		if !test.WaitFor(not(dirToBeFUSE(mountPoint)), 5*time.Second, 1*time.Second) {
			// It didn't unmount. Try again.
			Unmount(mountPoint)
		}
	}()

	if !test.WaitFor(dirToBeFUSE(mountPoint), 5*time.Second, 100*time.Millisecond) {
		t.Fatalf("error waiting for %s to be mounted", mountPoint)
	}
	fn(&mountEnv{
		t:          t,
		mountPoint: mountPoint,
		process:    mount.Process,
	})

}
Esempio n. 19
0
/*
Start starts the passed-in *exec.Cmd command.  It wraps the command in a *gexec.Session.

The session pipes the command's stdout and stderr to two *gbytes.Buffers available as properties on the session: session.Out and session.Err.
These buffers can be used with the gbytes.Say matcher to match against unread output:

	Ω(session.Out).Should(gbytes.Say("foo-out"))
	Ω(session.Err).Should(gbytes.Say("foo-err"))

In addition, Session satisfies the gbytes.BufferProvider interface and provides the stdout *gbytes.Buffer.  This allows you to replace the first line, above, with:

	Ω(session).Should(gbytes.Say("foo-out"))

When outWriter and/or errWriter are non-nil, the session will pipe stdout and/or stderr output both into the session *gybtes.Buffers and to the passed-in outWriter/errWriter.
This is useful for capturing the process's output or logging it to screen.  In particular, when using Ginkgo it can be convenient to direct output to the GinkgoWriter:

	session, err := Start(command, GinkgoWriter, GinkgoWriter)

This will log output when running tests in verbose mode, but - otherwise - will only log output when a test fails.

The session wrapper is responsible for waiting on the *exec.Cmd command.  You *should not* call command.Wait() yourself.
Instead, to assert that the command has exited you can use the gexec.Exit matcher:

	Ω(session).Should(gexec.Exit())

When the session exits it closes the stdout and stderr gbytes buffers.  This will short circuit any
Eventuallys waiting fo the buffers to Say something.
*/
func Start(command *exec.Cmd, outWriter io.Writer, errWriter io.Writer) (*Session, error) {
	exited := make(chan struct{})

	session := &Session{
		Command:  command,
		Out:      gbytes.NewBuffer(),
		Err:      gbytes.NewBuffer(),
		Exited:   exited,
		lock:     &sync.Mutex{},
		exitCode: -1,
	}

	var commandOut, commandErr io.Writer

	commandOut, commandErr = session.Out, session.Err

	if outWriter != nil && !reflect.ValueOf(outWriter).IsNil() {
		commandOut = io.MultiWriter(commandOut, outWriter)
	}

	if errWriter != nil && !reflect.ValueOf(errWriter).IsNil() {
		commandErr = io.MultiWriter(commandErr, errWriter)
	}

	command.Stdout = commandOut
	command.Stderr = commandErr

	err := command.Start()
	if err == nil {
		go session.monitorForExit(exited)
	}

	return session, err
}
Esempio n. 20
0
func (matcher *haveFileMatcher) Match(actual interface{}) (success bool, err error) {
	container, ok := actual.(garden.Container)
	if !ok {
		return false, fmt.Errorf("HaveFile matcher expects an garden.Container")
	}

	out := gbytes.NewBuffer()
	proc, err := container.Run(
		garden.ProcessSpec{
			Path: "ls",
			Args: []string{matcher.expected},
		},
		garden.ProcessIO{
			Stdout: io.MultiWriter(ginkgo.GinkgoWriter, out),
			Stderr: io.MultiWriter(ginkgo.GinkgoWriter, out),
		})
	if err != nil {
		return false, err
	}

	exitCode, err := proc.Wait()
	if err != nil {
		return false, err
	}
	if exitCode != 0 {
		return false, nil
	}

	return true, nil
}
Esempio n. 21
0
func handleConnection(conn net.Conn, src *gitSource, stdout, stderr io.Writer) {
	log.Printf("server: handleConnection")
	defer conn.Close()

	for {
		bufbytes := make([]byte, 1024)
		nr, err := conn.Read(bufbytes)

		// handle error
		if err == io.EOF {
			log.Printf("server: client connect closed")
			return
		} else if err != nil {
			log.Printf("server read error: %#v", err.Error())
			return
		}

		data := bufbytes[0:nr]
		log.Printf("server got: %s", data)

		rw := io.MultiWriter(conn, stdout)
		ew := io.MultiWriter(conn, stderr)
		ctx := src.Context(rw, ew)
		ctx.Logf("command received: %s", data)

		if err := ctx.HardPull(); err == io.EOF {
			ctx.Logf("command completed")
			log.Printf("server: connection terminated")
			return
		} else if err != nil {
			log.Printf("callback error: %s", err.Error())
			return
		}
	}
}
Esempio n. 22
0
// ForkTee spawns a command and args and returns a function to wait
// for completion. The returned wait function returns both STDOUT
// and STDERR in a `[]byte`, along with `error` should one occur.
// Additionally, it pipes STDOUT and STDERR to the respective
// `io.WriteCloser`'s.
func ForkTee(stream io.WriteCloser, command string, args ...string) (wait func() ([]byte, error), err error) {
	cmd := exec.Command(command, args...)
	read, write, _ := os.Pipe()

	cmd.Stdout = io.MultiWriter(write, stream)
	cmd.Stderr = io.MultiWriter(write, stream)
	err = cmd.Start()

	wait = func() (out []byte, err error) {
		defer func() { read.Close() }()

		waitErr := cmd.Wait()
		write.Close()
		out, readErr := ioutil.ReadAll(read)

		if waitErr != nil {
			return out, waitErr
		}

		if readErr != nil {
			return out, readErr
		}

		return
	}

	return
}
Esempio n. 23
0
//TeeRead writes the data from the reader into the writer, and returns a reader
func TeeRead(w io.Writer, r io.Reader, maxMemory int64) (io.ReadCloser, error) {
	b := bytes.NewBuffer(nil)
	if maxMemory <= 0 {
		maxMemory = 1 << 20 // 1Mb
	}
	size, err := io.CopyN(io.MultiWriter(w, b), r, maxMemory+1)
	if err != nil && err != io.EOF {
		return nil, err
	}
	if size <= maxMemory {
		return ioutil.NopCloser(bytes.NewReader(b.Bytes())), nil
	}
	// too big, write to disk and flush buffer
	file, err := ioutil.TempFile("", "reader-")
	if err != nil {
		return nil, err
	}
	nm := file.Name()
	size, err = io.Copy(io.MultiWriter(w, file), io.MultiReader(b, r))
	if err != nil {
		file.Close()
		os.Remove(nm)
		return nil, err
	}
	file.Close()
	fh, err := os.Open(nm)
	return tempFile{File: fh}, err
}
Esempio n. 24
0
// ExecTee runs a command and arguments and returns both STDERR and STDOUT in a
// two `[]btye`'s. Errors are turned as `error`. Additionally, it pipes STDOUT
// and STDERR to the respective `io.WriteCloser`'s.
func ExecTee2(ostream, estream io.WriteCloser, command string, args ...string) (oout, eout []byte, err error) {
	cmd := exec.Command(command, args...)
	oread, owrite, _ := os.Pipe()
	eread, ewrite, _ := os.Pipe()

	defer func() {
		oread.Close()
		eread.Close()
	}()

	cmd.Stdout = io.MultiWriter(owrite, ostream)
	cmd.Stderr = io.MultiWriter(ewrite, estream)

	err = cmd.Run()
	owrite.Close()
	ewrite.Close()

	oout, oreadErr := ioutil.ReadAll(oread)
	eout, ereadErr := ioutil.ReadAll(eread)

	if oreadErr != nil {
		return oout, eout, oreadErr
	}

	if ereadErr != nil {
		return oout, eout, ereadErr
	}

	return
}
Esempio n. 25
0
// NewMain returns a new instance of Main.
func NewMain() *Main {
	// Create a temporary data directory.
	dataDir, err := ioutil.TempDir("", "flynn-redis-")
	if err != nil {
		panic(err)
	}

	// Create test wrapper with random port and temporary data directory.
	m := &Main{
		Main:            main.NewMain(),
		DiscoverdClient: NewDiscoverdClient(),
	}
	m.Main.Addr = "127.0.0.1:0"
	m.Main.DataDir = dataDir
	m.Main.DiscoverdClient = m.DiscoverdClient

	m.Main.Stdin = &m.Stdin
	m.Main.Stdout = &m.Stdout
	m.Main.Stderr = &m.Stderr

	if testing.Verbose() {
		m.Main.Stdout = io.MultiWriter(os.Stdout, m.Main.Stdout)
		m.Main.Stderr = io.MultiWriter(os.Stderr, m.Main.Stderr)
	}

	return m
}
Esempio n. 26
0
func LogInit(debug_flag bool) {
	logfile, err := os.OpenFile("/tmp/vito.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		log.Fatal("Error opening log file")
	}
	infowriter := io.MultiWriter(logfile, os.Stdout)

	if debug_flag {
		debuglogfile, err := os.OpenFile("/tmp/vito.debug.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err != nil {
			log.Fatal("Error opening debug log file")
		}

		infowriter = io.MultiWriter(logfile, os.Stdout, debuglogfile)

		debugwriter := io.MultiWriter(debuglogfile, os.Stdout)
		debugLog = log.New(debugwriter, "[DEBUG] ", log.Ldate|log.Ltime)

	} else {
		debugLog = log.New(ioutil.Discard, "", 0)
	}

	infoLog = log.New(infowriter, "", log.Ldate|log.Ltime)

}
Esempio n. 27
0
// computeHash - Calculates MD5 and SHA256 for an input read Seeker.
func (c Client) computeHash(reader io.ReadSeeker) (md5Sum, sha256Sum []byte, size int64, err error) {
	// MD5 and SHA256 hasher.
	var hashMD5, hashSHA256 hash.Hash
	// MD5 and SHA256 hasher.
	hashMD5 = md5.New()
	hashWriter := io.MultiWriter(hashMD5)
	if c.signature.isV4() {
		hashSHA256 = sha256.New()
		hashWriter = io.MultiWriter(hashMD5, hashSHA256)
	}

	size, err = io.Copy(hashWriter, reader)
	if err != nil {
		return nil, nil, 0, err
	}

	// Seek back reader to the beginning location.
	if _, err := reader.Seek(0, 0); err != nil {
		return nil, nil, 0, err
	}

	// Finalize md5shum and sha256 sum.
	md5Sum = hashMD5.Sum(nil)
	if c.signature.isV4() {
		sha256Sum = hashSHA256.Sum(nil)
	}
	return md5Sum, sha256Sum, size, nil
}
Esempio n. 28
0
func NewBuilder(project, ref string, goos, arch string, wbc *utils.WriteBroadcaster) *Builder {
	b := &Builder{
		wbc:      wbc,
		sh:       sh.NewSession(),
		project:  project,
		ref:      ref,
		os:       goos,
		arch:     arch,
		fullname: strings.Join([]string{project, ref, goos, arch}, "-"),
	}
	b.sh.ShowCMD = true
	if wbc != nil {
		b.sh.Stdout = io.MultiWriter(logfd, wbc)
		b.sh.Stderr = io.MultiWriter(logfd, wbc)
	}
	selfbin := beeutils.SelfDir() + "/bin"
	env := map[string]string{
		"PATH":    strings.Join([]string{"/bin:/usr/bin", selfbin, os.Getenv("PATH")}, ":"),
		"PROJECT": project,
		"GOROOT":  opts.GOROOT,
	}
	// enable cgo on current os-arch
	if goos == runtime.GOOS && arch == runtime.GOARCH {
		env["CGO_ENABLED"] = "1"
	}

	b.sh.Env = env
	return b
}
Esempio n. 29
0
func main() {
	delay := flag.Duration("delay", time.Second*5, "Delay between each restart")
	killon := flag.String("killon", "", "Kill program when text appear")
	flag.Parse()

	if flag.NArg() == 0 {
		fmt.Println("Usage: %s [-delay 5s] <commands ...>")
		return
	}
	for {
		killCh := make(chan bool, 1)
		cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...)
		bufs := make([]string, 4)
		hwr := &HookWriter{
			hook: func(data []byte) {
				bufs = append(bufs, string(data))
				if len(bufs) > 4 {
					bufs = bufs[len(bufs)-4 : len(bufs)]
				}
				if strings.Contains(strings.Join(bufs, ""), *killon) {
					killCh <- true
				}
			},
		}
		cmd.Stdin = os.Stdin
		cmd.Stdout = io.MultiWriter(hwr, os.Stdout)
		cmd.Stderr = io.MultiWriter(hwr, os.Stderr)

		select {
		case err := <-Go(cmd.Run):
			if err != nil {
				log.Println(err)
			} else {
				log.Println("Normal exit")
				return
			}
		case <-killCh:
			log.Println("Trigger killon")
			if cmd.Process != nil {
				log.Println("Trigger killon, kill process")
				cmd.Process.Kill()
				return
			}
		case sig := <-sigCh:
			log.Printf("recv signal: %v", sig)
			if cmd.Process != nil {
				cmd.Process.Kill()
				return
			}
		}

		select {
		case <-time.After(*delay):
		case sig := <-sigCh:
			log.Printf("recv signal: %v", sig)
			return
		}
	}
}
Esempio n. 30
0
func multiWriter(file string) {
	logFile := openFile(file)

	traceHandle = io.MultiWriter(logFile, ioutil.Discard)
	infoHandle = io.MultiWriter(logFile, os.Stdout)
	warningHandle = io.MultiWriter(logFile, os.Stdout)
	errorHandle = io.MultiWriter(logFile, os.Stderr)
}