Пример #1
0
// Satisfies the Listener interface and calls the relevant binary file
func (this *Cli) Exec(evt event.Event) {

	this.Log.Handler(this, &evt)

	bin := magicString(this.Config.GetBin(), evt)
	stdin := magicString(this.Config.GetStdin(), evt)
	args := append([]string(nil), this.Config.GetArgs()...)
	for i, arg := range this.Config.GetArgs() {
		args[i] = magicString(arg, evt)
	}

	cmd := exec.Command(bin, args...)
	cmd.Stdin = strings.NewReader(stdin)
	var out bytes.Buffer
	var sErr bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &sErr

	this.Log.Info("CLI CMD %s %s \"%s %s\"", evt.GetId(), this.GetName(), bin, strings.Join(args, " "))
	err := cmd.Run()
	if err != nil {
		this.Log.HandlerError(this, err.Error(), &evt)
		return
	}
	this.Log.Debug("CLI OUT %s %s \"%s\"", evt.GetId(), this.GetName(), out.String())
}
Пример #2
0
// Satisfies the Listener interface and calls the Lambda function
func (this *Lambda) Exec(evt event.Event) {

	this.Log.Handler(this, &evt)

	msg := magicString(this.Config.GetMessage(), evt)
	reg := magicString(this.Config.GetRegion(), evt)
	fun := magicString(this.Config.GetFunction(), evt)

	svc := lambda.New(&aws.Config{Region: reg})
	params := &lambda.InvokeInput{
		FunctionName: aws.String(fun), // Required
		Payload:      []byte(msg),
	}
	resp, err := svc.Invoke(params)

	if err != nil {
		if awsErr, ok := err.(awserr.Error); ok {

			msg := fmt.Sprintf("%s %s", awsErr.Code(), awsErr.Message())
			this.Log.HandlerError(this, msg, &evt)
			if _, ok := err.(awserr.RequestFailure); ok {
			}
		} else {
			this.Log.HandlerError(this, err.Error(), &evt)
		}
		return
	}
	this.Log.Debug("LAMBDA OUT %s %s \"%s\"", evt.GetId(), this.GetName(), awsutil.StringValue(resp))
}
Пример #3
0
// Replaces variables ("${}") in the string to their actual value
func magicString(s string, evt event.Event) string {

	rep := regexp.MustCompile("(\\${|})")
	rst := findMagicStrings(s)

	for _, v := range rst {
		variable := rep.ReplaceAllString(v, "")
		opt := strings.Split(variable, ".")
		switch opt[0] {
		case "event":
			b, _ := json.Marshal(evt)
			s = strings.Replace(s, v, string(b), -1)
		case "message":
			str := ""
			if len(opt) <= 1 {
				b, _ := json.Marshal(evt.Message)
				str = string(b)
			} else {
				str = evt.GetString(strings.Join(opt[1:], "."))
			}
			s = strings.Replace(s, v, str, -1)
		}
	}
	return s
}
Пример #4
0
// Convert a raw json string into an Event struct
func (s *Base) processRaw(msg string) event.Event {

	var evt event.Event
	if err := json.Unmarshal([]byte(msg), &evt); err != nil {
		s.Log.Error("Can not turn string into event: %s", msg)
		return evt
	}
	evt.Id = generateId()
	s.Log.Event(&evt)
	s.processEvent(evt)

	return evt
}
Пример #5
0
// Satisfies the Listener interface and calls the relevant binary file
func (this *Cli) Exec(evt event.Event) {

	this.Log.Handler(this, &evt)

	bin := magicString(this.Config.GetBin(), evt)
	stdin := magicString(this.Config.GetStdin(), evt)
	args := append([]string(nil), this.Config.GetArgs()...)
	for i, arg := range this.Config.GetArgs() {
		args[i] = magicString(arg, evt)
	}

	cmd := exec.Command(bin, args...)
	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
	cmd.Stdin = strings.NewReader(stdin)
	var out bytes.Buffer
	var sErr bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &sErr

	this.Log.Info("CLI CMD %s %s \"%s %s\"", evt.GetId(), this.GetName(), bin, strings.Join(args, " "))
	err := cmd.Start()
	if err != nil {
		this.Log.Error("CLI ERR %s %s \"%s\"", evt.GetId(), this.GetName(), sErr.String())
		this.Log.HandlerError(this, err.Error(), &evt)
		return
	}

	timeOut := this.Config.GetTimeout()
	if timeOut == 0 {
		if err := cmd.Wait(); err != nil {
			this.Log.Error("CLI ERROR %s", err.Error())
		}
	} else {
		// this allows us to set a timeout on the cli (phantomjs def needs this)
		done := make(chan error, 1)
		go func() {
			done <- cmd.Wait()
		}()
		select {
		case <-time.After(time.Duration(timeOut) * time.Second):
			pgid, err := syscall.Getpgid(cmd.Process.Pid)
			if err == nil {
				syscall.Kill(-pgid, 15) // note the minus sign
				this.Log.Warning("CLITIMEOUT %s %s", evt.GetId(), this.GetName())
			}
			<-done // allow goroutine to exit
		case err := <-done:
			if err != nil {
				this.Log.Error("CLI ERROR %s", err.Error())
			}
		}

	}

	this.Log.Debug("CLI OUT %s %s \"%s\"", evt.GetId(), this.GetName(), out.String())
}
Пример #6
0
// Convert a raw json string into an Event struct
func (s *Base) processRaw(msg string) event.Event {

	var evt event.Event

	if umsg, err := strconv.Unquote(msg); err == nil {
		msg = umsg
	}
	if err := json.Unmarshal([]byte(msg), &evt); err != nil {
		s.Log.Error("Can not turn string into event: %s", msg)
		return evt
	}
	evt.Id = generateId()
	s.Log.Event(&evt)
	s.processEvent(evt)

	s.Log.Info("Process %s \"%s\"", evt.Id, msg)

	return evt
}