Ejemplo n.º 1
0
func (i ClusterInstance) runRemoteCommand(log *logging.Logger, command, stdin string, quiet bool) (string, error) {
	hostAddress := i.hostAddress(false)
	if hostAddress == "" {
		return "", maskAny(fmt.Errorf("don't have any address to communicate with instance %s", i.Name))
	}
	cmd := exec.Command("ssh", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", i.User()+"@"+hostAddress, command)
	var stdOut, stdErr bytes.Buffer
	cmd.Stdout = &stdOut
	cmd.Stderr = &stdErr

	if stdin != "" {
		cmd.Stdin = strings.NewReader(stdin)
	}

	if err := cmd.Run(); err != nil {
		if !quiet {
			log.Errorf("SSH failed: %s %s", cmd.Path, strings.Join(cmd.Args, " "))
		}
		return "", errgo.NoteMask(err, stdErr.String())
	}

	out := stdOut.String()
	out = strings.TrimSuffix(out, "\n")
	return out, nil
}
Ejemplo n.º 2
0
// RunRemoteCommand connects to the given host and runs the command, returning any output.
func (ssh *SSHShellExecutor) RunRemoteCommand(host, command string) (string, error) {
	cmd := exec.Command(ssh.Binary, "-o", "StrictHostKeyChecking=no", ssh.Username+"@"+host, command)
	var stdOut, stdErr bytes.Buffer
	cmd.Stdout = &stdOut
	cmd.Stderr = &stdErr

	if err := cmd.Run(); err != nil {
		return "", errgo.NoteMask(err, stdErr.String())
	}

	out := stdOut.String()
	out = strings.TrimSuffix(out, "\n")
	return out, nil
}
Ejemplo n.º 3
0
func (i ClusterInstance) CopyTo(log *logging.Logger, localPath, instancePath string) error {
	hostAddress := i.hostAddress(true)
	if hostAddress == "" {
		return maskAny(fmt.Errorf("don't have any address to communicate with instance %s", i.Name))
	}
	cmd := exec.Command("scp", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", localPath, i.User()+"@"+hostAddress+":"+instancePath)
	var stdErr bytes.Buffer
	cmd.Stderr = &stdErr

	if err := cmd.Run(); err != nil {
		log.Errorf("SCP failed: %s %s", cmd.Path, strings.Join(cmd.Args, " "))
		return errgo.NoteMask(err, stdErr.String())
	}

	return nil
}
Ejemplo n.º 4
0
func runRemoteCommand(member service.ClusterMember, userName string, log *logging.Logger, command, stdin string, quiet bool) (string, error) {
	hostAddress := member.ClusterIP
	cmd := exec.Command("ssh", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", userName+"@"+hostAddress, command)
	var stdOut, stdErr bytes.Buffer
	cmd.Stdout = &stdOut
	cmd.Stderr = &stdErr

	if stdin != "" {
		cmd.Stdin = strings.NewReader(stdin)
	}

	if err := cmd.Run(); err != nil {
		if !quiet {
			log.Errorf("SSH failed: %s %s", cmd.Path, strings.Join(cmd.Args, " "))
		}
		return "", errgo.NoteMask(err, stdErr.String())
	}

	out := stdOut.String()
	out = strings.TrimSuffix(out, "\n")
	return out, nil
}
Ejemplo n.º 5
0
// Read enumerates all of the journal entries and calls
// fn with each entry found. Errors returned by fn
// are returned by Read. fn can return StopError
// to halt enumeration at any point.
func (j *Journal) Read(fn ReadFunc) error {

	var err error
	var e *Entry

	files, err := ioutil.ReadDir(j.getEntriesDir())
	if err != nil {
		if os.IsNotExist(err) {
			return err
		} else {
			return errgo.Mask(err)
		}
	}

	for _, f := range files {
		if f.IsDir() {
			continue
		}

		if !isEntryFile(f.Name()) {
			continue
		}

		uuid := strings.TrimSuffix(filepath.Base(f.Name()), filepath.Ext(f.Name()))
		e, err = j.ReadEntry(uuid)
		err = fn(e, err)

		if err == ErrStopRead {
			return nil
		} else if err != nil {
			return errgo.NoteMask(err, "file: "+f.Name())
		}
	}

	return nil
}