Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: runsim pushes logs to S3 bucket #4677

Merged
merged 7 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Merge PR #4471: Migrate genesis cmd
  • Loading branch information
sabau authored and mircea-c committed Jul 3, 2019
commit 7c2951cf915422d46c21ded45828a827040c4a06
26 changes: 20 additions & 6 deletions contrib/runsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
genesis string
exitOnFail bool
githubConfig string
gitRevision string
logObjKey string
slackConfig string

// integration with Slack and Github
Expand Down Expand Up @@ -82,7 +82,7 @@ func init() {
flag.StringVar(&genesis, "g", "", "Genesis file")
flag.StringVar(&seedOverrideList, "seeds", "", "run the supplied comma-separated list of seeds instead of defaults")
flag.BoolVar(&exitOnFail, "e", false, "Exit on fail during multi-sim, print error")
flag.StringVar(&gitRevision, "rev", "", "git revision")
flag.StringVar(&logObjKey, "log", "", "S3 object key for log files")
flag.StringVar(&githubConfig, "github", "", "Report results to Github's PR")
flag.StringVar(&slackConfig, "slack", "", "Report results to slack channel")

Expand Down Expand Up @@ -224,7 +224,17 @@ func makeCmd(cmdStr string) *exec.Cmd {
}

func makeFilename(seed int) string {
return fmt.Sprintf("app-simulation-seed-%d-date-%s", seed, time.Now().Format("01-02-2006_15:04:05.000000000"))
return fmt.Sprintf("app-simulation-seed-%d-date-%s", seed, time.Now().Format("01-02-2006_150405"))
}

func makeFailSlackMsg(seed int, stdoutKey, stderrKey, bucket string, logsPushed bool) string {
if logsPushed {
return fmt.Sprintf("*Seed %s: FAILED*. *<https://%s.s3.amazonaws.com/%s|stdout>* *<https://%s.s3.amazonaws.com/%s|stderr>*\nTo reproduce run: ```\n%s\n```",
strconv.Itoa(seed), bucket, stdoutKey, bucket, stderrKey, buildCommand(testname, blocks, period, genesis, seed))
} else {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf("*Seed %s: FAILED*. Could not upload logs: %s\nTo reproduce run: ```\n%s\n```",
strconv.Itoa(seed), bucket, buildCommand(testname, blocks, period, genesis, seed))
}
}

func worker(id int, seeds <-chan int) {
Expand All @@ -236,18 +246,22 @@ func worker(id int, seeds <-chan int) {
log.Printf("[W%d] Seed %d: FAILED", id, seed)
log.Printf("To reproduce run: %s", buildCommand(testname, blocks, period, genesis, seed))
if slackConfigSupplied() {
slackMessage(slackToken, slackChannel, nil, "Seed "+strconv.Itoa(seed)+" failed. To reproduce, run: "+buildCommand(testname, blocks, period, genesis, seed))
objKeys, bucket, awsErr := pushLogs(stdOut, stdErr, logObjKey)
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
if awsErr != nil {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
slackMessage(slackToken, slackChannel, nil, makeFailSlackMsg(seed, "", "", awsErr.Error(), false))
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
} else {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
slackMessage(slackToken, slackChannel, nil, makeFailSlackMsg(seed, objKeys[0], objKeys[1], *bucket, true))
}
}
if exitOnFail {
log.Printf("\bERROR OUTPUT \n\n%s", err)
panic("halting simulations")
}
} else {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("[W%d] Seed %d: OK", id, seed)
_, _, _ = pushLogs(stdOut, stdErr, logObjKey)
}
pushLogs(stdOut, stdErr, gitRevision)
}

log.Printf("[W%d] no seeds left, shutting down", id)
}

Expand Down
62 changes: 32 additions & 30 deletions contrib/runsim/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -20,61 +19,64 @@ const (
awsRegion = "us-east-1"
)

var (
simTimeStamp = time.Now().Format("01-02-2006_15:05:05")
)

func awsErrHandler(err error) {
func awsErrHandler(err error) error {
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Code() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this switch statement?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not right now, but I want it in there for later when I'll get around to handling some of these error codes better.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd delete it and re-add later it if necessary

default:
log.Println(awsErr.Error())
return awsErr
}
} else {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
log.Println(err.Error())
return err
}
}

func makeObjKey(folderName string, fileName string) string {
return fmt.Sprintf("%s/%s/%s", folderName, simTimeStamp, fileName)
func makeObjKey(objKeyPrefix string, fileName string) string {
return fmt.Sprintf("%s/%s", objKeyPrefix, fileName)
}

func putObj(fileHandle *os.File, svc *s3.S3, folderName string, bucketName string) {
_, _ = fileHandle.Seek(0, 0)

stdOutObjInput := &s3.PutObjectInput{
Body: aws.ReadSeekCloser(fileHandle),
Bucket: aws.String(bucketName),
Key: aws.String(makeObjKey(folderName, filepath.Base(fileHandle.Name()))),
}
if output, err := svc.PutObject(stdOutObjInput); err != nil {
awsErrHandler(err)
} else {
log.Printf("Log file pushed: %s", output.String())
func putObjects(svc *s3.S3, objKeyPrefix string, bucketName string, fileHandles ...*os.File) ([]string, error) {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
objKeys := make([]string, len(fileHandles))
for index, fileHandle := range fileHandles {
_, _ = fileHandle.Seek(0, 0)
objKey := makeObjKey(objKeyPrefix, filepath.Base(fileHandle.Name()))
stdOutObjInput := &s3.PutObjectInput{
Body: aws.ReadSeekCloser(fileHandle),
Bucket: aws.String(bucketName),
Key: aws.String(objKey),
}
_, err := svc.PutObject(stdOutObjInput)
if err != nil {
return nil, awsErrHandler(err)
}
objKeys[index] = objKey
}
return objKeys, nil
}

func pushLogs(stdOut *os.File, stdErr *os.File, folderName string) {
func pushLogs(stdOut *os.File, stdErr *os.File, folderName string) ([]string, *string, error) {
var logBucket *string

sessionS3 := s3.New(session.Must(session.NewSession(&aws.Config{
Region: aws.String(awsRegion),
})))
if listBucketsOutput, err := sessionS3.ListBuckets(&s3.ListBucketsInput{}); err != nil {
awsErrHandler(err)
awsErr := awsErrHandler(err)
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
if awsErr != nil {
return nil, nil, awsErr
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
for _, bucket := range listBucketsOutput.Buckets {
if strings.Contains(*bucket.Name, logBucketPrefix) {
logBucket = bucket.Name
putObj(stdOut, sessionS3, folderName, *logBucket)
putObj(stdErr, sessionS3, folderName, *logBucket)
break
objKeys, putObjErr := putObjects(sessionS3, folderName, *logBucket, stdOut, stdErr)
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
if putObjErr != nil {
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
return nil, nil, putObjErr
mircea-c marked this conversation as resolved.
Show resolved Hide resolved
}
return objKeys, bucket.Name, nil
}
}
}
if logBucket == nil {
log.Println("Log bucket not found")
}
return nil, nil, nil
}

func slackMessage(token string, channel string, threadTS *string, message string) {
Expand Down