Example #1
0
// handleKiteErr handles a kite transport error, filtering out common connection
// related errors. If the given error is *not* a kiteErr, the normal error is
// returned.
//
// If nil is given as an arg, nil is returned.
func (s *Status) handleKiteErr(err error) error {
	// By checking for nil, we allow usage of this func like:
	// handleKiteErr(Dial()), if desired.
	if err == nil {
		return nil
	}

	kErr, ok := err.(*kite.Error)

	// If we fail to cast the kite error, this is a normal error type. We currently
	// have no cases to handle for that, so return it.
	if !ok {
		return err
	}

	// Authentication errors come from the kite lib, so parse the messages to return
	// easy to decpiher types.
	if kErr.Type == kiteerrortypes.AuthenticationError {
		switch {
		case strings.Contains(kErr.Message, tokenExpiredMessage):
			return util.NewKiteError(kiteerrortypes.AuthErrTokenIsExpired, err)
		case strings.Contains(kErr.Message, tokenNotValidYetMessage):
			return util.NewKiteError(kiteerrortypes.AuthErrTokenIsNotValidYet, err)
		}
	}

	// If we get here, the kite error type is not handled. Return the original
	// error.
	return err
}
Example #2
0
// Dial dials the internal dialer.
func (m *Machine) Dial() (err error) {
	// set the resulting dial based on the success of the Dial method.
	// Note that repeated calls to Dial creates a new XHR transport, so failing
	// dial on an existing sets a new local client transport session.
	// In otherwords, a failed dial will result in a not-connected sessuin. Due to
	// this, we track the state of the dialed by result, regardless of original state.
	defer func() {
		m.hasDialed = err == nil
	}()

	if m.Transport == nil {
		m.Log.Error("Dial was attempted with a nil Transport")
		return util.KiteErrorf(
			kiteerrortypes.MachineNotValidYet, "Machine.Transport is nil",
		)
	}

	// Log the failure here, because this logger has machine context.
	if err := m.Transport.Dial(); err != nil {
		m.Log.Error("Dialer returned error. err:%s", err)
		return util.NewKiteError(kiteerrortypes.DialingFailed, err)
	}

	return nil
}
Example #3
0
func (m *Mounter) mountExisting(mount *Mount) error {
	if err := m.IsConfigured(); err != nil {
		m.Log.Error("Mounter improperly configured. err:%s", err)
		return err
	}

	if err := m.Machine.DialOnce(); err != nil {
		m.Log.Error("Error dialing remote klient. err:%s", err)
		return util.NewKiteError(kiteerrortypes.DialingFailed, err)
	}

	if mount.KiteTracker == nil {
		mount.KiteTracker = m.KiteTracker
	}

	if mount.Intervaler == nil {
		mount.Intervaler = m.Intervaler
	}

	if mount.Type == UnknownMount {
		setTo := FuseMount
		m.Log.Notice(
			"Mount.Type is %q, setting to default:%s", mount.Type, setTo,
		)
		mount.Type = setTo
	}

	if mount.EventSub == nil {
		mount.EventSub = m.EventSub
	}

	// Create our changes channel, so that fuseMount can be told when we lose and
	// reconnect to klient.
	changeSummaries := make(chan kitepinger.ChangeSummary, 1)

	if mount.Type == FuseMount {
		// TODO: Uncomment this once fuseklient can accept a change channel.
		//changes := changeSummaryToBool(changeSummaries)
		//if err := fuseMountFolder(mount, changes); err != nil {
		if err := m.fuseMountFolder(mount); err != nil {
			return err
		}
	}

	go m.startKiteTracker(mount, changeSummaries)

	return nil
}
Example #4
0
// NewOuput runs the given cmd and returns the output
func NewOutput(cmd *exec.Cmd) (*Output, error) {
	stdoutBuffer, stderrBuffer := new(bytes.Buffer), new(bytes.Buffer)
	cmd.Stdout, cmd.Stderr = stdoutBuffer, stderrBuffer
	var exitStatus int

	err := cmd.Run()
	if err != nil {
		if exitErr, ok := err.(*exec.ExitError); !ok {
			// return if it's not an exitError
			return nil, util.NewKiteError(kiteerrortypes.ProcessError, err)
		} else {
			exitStatus = exitErr.Sys().(syscall.WaitStatus).ExitStatus()
		}
	}

	return &Output{
		Stdout:     stdoutBuffer.String(),
		Stderr:     stderrBuffer.String(),
		ExitStatus: exitStatus,
	}, nil
}