Example #1
0
// Open prepare internal resources.
func (n *SQSNotify) Open() (err error) {
	awsSQS := sqs.New(n.auth, n.region)
	n.queue, err = awsSQS.GetQueue(n.name)
	if err != nil {
		return err
	}
	n.deleteQueue = make([]sqs.Message, 0, maxDelete)
	return nil
}
Example #2
0
// NewSQSClient returns a new SQS client
func (m *AwsMgr) NewSQSClient(accId, region string) (*sqs.SQS, error) {
	// Get Auth
	auth, err := m.GetAuth(accId)
	if err != nil {
		return nil, err
	}
	sqs := sqs.New(*auth, aws.Regions[region])
	return sqs, nil
}
Example #3
0
func openQueue(rname, qname string) (*sqs.Queue, error) {
	auth, err := awsutil.GetAuth("sqs-send")
	if err != nil {
		return nil, err
	}
	region, ok := aws.Regions[rname]
	if !ok {
		return nil, errors.New("unknown region: " + rname)
	}
	queue, err := sqs.New(auth, region).GetQueue(qname)
	if err != nil {
		return nil, err
	}
	return queue, nil
}
Example #4
0
File: task.go Project: GoToolz/Task
func init() {

	var auth = aws.Auth{
		AccessKey: env.Get("AWS_ACCESS_KEY_ID"),
		SecretKey: env.Get("AWS_SECRET_ACCESS_KEY"),
	}

	regionName := env.Get("AWS_DEFAULT_REGION")
	conn := sqs.New(auth, aws.Regions[regionName])

	var err error
	queue, err = conn.GetQueue(env.Get("AWS_SQS_QUEUE"))
	if err != nil {
		panic(err)
	}

	if cnt := env.GetInt("TASK_BATCH_SIZE"); cnt > 0 {
		batchSize = cnt
	}

	go taskRunner()
}
	"github.com/goamz/goamz/sqs"
	"github.com/julienschmidt/httprouter"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"regexp"
)

var (
	certFile = "./server.crt"
	auth     = aws.Auth{
		AccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
		SecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
	}
	conn       = sqs.New(auth, aws.USWest2)
	fromSteven = regexp.MustCompile(`stevenzeiler\/rippled`)
	fromRipple = regexp.MustCompile(`ripple\/rippled`)
)

type Body struct {
	Compare string `json:"compare"`
}

func GithubWebhook(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {

	requestBody, _ := ioutil.ReadAll(req.Body)

	fmt.Println(string(requestBody[:]))

	body := new(Body)
import (
	"fmt"
	"github.com/goamz/goamz/aws"
	"github.com/goamz/goamz/sqs"
	"os"
	"os/exec"
	"time"
)

var (
	queueName = "registry-container-restart-messages"
	auth      = aws.Auth{
		AccessKey: os.Getenv("AWS_ACCESS_KEY_ID"),
		SecretKey: os.Getenv("AWS_SECRET_ACCESS_KEY"),
	}
	conn = sqs.New(auth, aws.APSoutheast)
)

func restartDockerContainer(containerName string) {
	args := []string{"docker", "restart", containerName}

	if err := exec.Command("sudo", args...).Run(); err != nil {
		fmt.Println(os.Stderr, err)
		os.Exit(1)
	}
	fmt.Println("successfully called docker")
}

func messageReceived(queue sqs.Queue, message *sqs.Message) {
	fmt.Println("Message received")
	fmt.Println(message)
Example #7
0
// Open prepare internal resources.
func (n *SQSNotify) Open() (err error) {
	awsSQS := sqs.New(n.auth, n.region)
	n.queue, err = awsSQS.GetQueue(n.name)
	return
}
Example #8
0
func processLog(location *LocationResponse) {
	// log.Info("Line number: ", location.RandLine)
	lineNumber, err := strconv.Atoi(location.RandLine)
	if err != nil {
		log.Error("Error converting randLine to int: ", err)
		return
	}

	reader, err := s3.New(awsAuth, awsRegion).Bucket(s3Bucket).GetReader(location.Bucket)
	bufReader := bufio.NewReader(reader)
	defer reader.Close()

	s3KeyParts := strings.SplitN(location.Bucket, "_", 7)
	filenameTimestamp, err := time.Parse(filenameTimeLayout, s3KeyParts[5])
	if err != nil {
		log.Error("Error parsing filename timestamp: ", err)
		return
	}
	if infoLog {
		log.Info("Filename timestamp: ", filenameTimestamp.String())
	}

	var sumT int = 0
	sha := sha256.New()
	for i := 0; i < lineNumber; i++ {
		line, _, err := bufReader.ReadLine()
		if err != nil {
			log.Error("Error reading from S3 buffered: ", err)
			return
		}

		splitLine := bytes.SplitN(line, logSeparator, 14)
		sha.Write(splitLine[12])

		logTimestamp, err := time.Parse(logTimeLayout, string(splitLine[0]))
		if err != nil {
			log.Error("Error parsing log line timestamp: ", err)
			return
		}
		if infoLog {
			log.Info("Log timestamp: ", logTimestamp.String())
		}

		timeDifference := int(filenameTimestamp.Sub(logTimestamp).Seconds())
		if timeDifference < 0 {
			timeDifference = 0
		}

		if infoLog {
			log.Infof("Time diff: %d", timeDifference)
		}
		sumT += timeDifference
	}

	if infoLog {
		log.Infof("Time difference sum: %d", sumT)
	}

	cksum := sha.Sum(nil)

	if infoLog {
		log.Info("cksum1: ", hex.EncodeToString(cksum))
	}

	for i := 0; i < sumT+1; i++ {
		sha := sha256.New()
		sha.Write(cksum)
		cksum = sha.Sum(nil)
	}

	message, err := json.Marshal(SQSMessage{
		GuidHash:   location.Hash,
		ResultHash: hex.EncodeToString(cksum),
	})
	if err != nil {
		log.Error("Error marshalling SQS message JSON: ", err)
	}
	if infoLog {
		log.Info("SQS message: ", string(message))
	}

	queue, err := sqs.New(awsAuth, awsRegion).GetQueue(queueName)
	if err != nil {
		log.Error("Error getting queue: ", err)
	}
	queue.SendMessage(string(message))
}
Example #9
0
// lots of this code stolen brazenly from JP https://github.com/jprobinson
func (b *FromSQS) listener() {
	log.Println("in listener")
	b.lock.Lock()
	// OK I KNOW that everything inside this lock is bad. This is a quick fix. Promise to do better in the future.
	log.Println("in lock")
	accessKey, ok := b.auth["AccessKey"].(string)
	if !ok {
		b.Error("could not assert AccessKey to a string")
		b.listening = false
		b.lock.Unlock()
		return
	}
	accessSecret, ok := b.auth["AccessSecret"].(string)
	if !ok {
		b.Error("could not assert AccessSecret to a string")
		b.listening = false
		b.lock.Unlock()
		return
	}
	queueName, ok := b.auth["QueueName"].(string)
	if !ok {
		b.Error("could not assert queue name to a string")
		b.listening = false
		b.lock.Unlock()
		return
	}
	maxN, ok := b.auth["MaxNumberOfMessages"].(string)
	if !ok {
		b.Error("could not assert MaxNumberOfMessages to a string")
		b.listening = false
		b.lock.Unlock()
		return
	}
	log.Println("authenticating with aws")
	auth := aws.Auth{AccessKey: accessKey, SecretKey: accessSecret}
	sqsClient := sqs.New(auth, aws.USEast)
	log.Println("getting SQS queue")
	queue, err := sqsClient.GetQueue(queueName)
	if err != nil {
		b.Error(err)
		b.listening = false
		b.lock.Unlock()
		return
	}

	log.Println("setting listening flag")
	b.listening = true
	b.lock.Unlock()

	params := map[string]string{
		"WaitTimeSeconds":     "1",
		"MaxNumberOfMessages": maxN,
	}

	var resp *sqs.ReceiveMessageResponse
	log.Println("starting read loop")
	for {
		select {
		case <-b.stop:
			log.Println("Exiting SQS read loop")
			return
		default:
			resp, err = queue.ReceiveMessageWithParameters(params)
			if err != nil {
				b.Error(err)
			}
			if len(resp.Messages) == 0 {
				break
			}
			for _, m := range resp.Messages {
				select {
				case b.fromListener <- []byte(m.Body):
				default:
					log.Println("discarding messages")
					log.Println(len(b.fromListener))
					continue
				}

				if _, err = queue.DeleteMessage(&m); err != nil {
					b.Error(err)
				}
			}
		}
	}
}