Merge pull request #41 from simple-rules/ricl-log

Ricl log
pull/42/head
7z7 6 years ago committed by GitHub
commit 0d7c26cc9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      aws-experiment-launch/experiment/commander/main.go
  2. 67
      aws-experiment-launch/experiment/soldier/main.go
  3. 93
      aws-experiment-launch/experiment/soldier/s3/s3.go

@ -103,7 +103,7 @@ func handleCommand(command string) {
dictateNodes(fmt.Sprintf("init %v %v %v %v", setting.ip, setting.port, setting.configURL, session.id)) dictateNodes(fmt.Sprintf("init %v %v %v %v", setting.ip, setting.port, setting.configURL, session.id))
} }
case "ping", "kill", "log": case "ping", "kill", "log", "log2":
{ {
dictateNodes(command) dictateNodes(command)
} }

@ -5,6 +5,7 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"harmony-benchmark/aws-experiment-launch/experiment/soldier/s3"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -30,9 +31,14 @@ type sessionInfo struct {
logFolder string logFolder string
} }
const (
bucketName = "richard-bucket-test"
logFolderPrefix = "../tmp_log/"
)
var ( var (
setting soliderSetting setting soliderSetting
session sessionInfo globalSession sessionInfo
) )
func socketServer() { func socketServer() {
@ -111,6 +117,10 @@ func handleCommand(command string, w *bufio.Writer) {
{ {
handleLogCommand(w) handleLogCommand(w)
} }
case "log2":
{
handleLog2Command(w)
}
} }
} }
@ -120,17 +130,17 @@ func handleInitCommand(args []string, w *bufio.Writer) {
// read arguments // read arguments
ip := args[0] ip := args[0]
session.commanderIP = ip globalSession.commanderIP = ip
port := args[1] port := args[1]
session.commanderPort = port globalSession.commanderPort = port
configURL := args[2] configURL := args[2]
sessionID := args[3] sessionID := args[3]
session.id = sessionID globalSession.id = sessionID
session.logFolder = fmt.Sprintf("../tmp_log/log-%v", sessionID) globalSession.logFolder = fmt.Sprintf("%slog-%v", logFolderPrefix, sessionID)
// create local config file // create local config file
session.localConfigFileName = fmt.Sprintf("node_config_%v_%v.txt", setting.port, session.id) globalSession.localConfigFileName = fmt.Sprintf("node_config_%v_%v.txt", setting.port, globalSession.id)
out, err := os.Create(session.localConfigFileName) out, err := os.Create(globalSession.localConfigFileName)
if err != nil { if err != nil {
log.Fatal("Failed to create local file", err) log.Fatal("Failed to create local file", err)
} }
@ -150,7 +160,7 @@ func handleInitCommand(args []string, w *bufio.Writer) {
} }
// log config file // log config file
content, err := ioutil.ReadFile(session.localConfigFileName) content, err := ioutil.ReadFile(globalSession.localConfigFileName)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -176,19 +186,19 @@ func handlePingCommand(w *bufio.Writer) {
func handleLogCommand(w *bufio.Writer) { func handleLogCommand(w *bufio.Writer) {
log.Println("Log command") log.Println("Log command")
files, err := ioutil.ReadDir(session.logFolder) files, err := ioutil.ReadDir(globalSession.logFolder)
if err != nil { if err != nil {
logAndReply(w, fmt.Sprintf("Failed to create access log folder. Error: %s", err.Error())) logAndReply(w, fmt.Sprintf("Failed to create log folder. Error: %s", err.Error()))
return return
} }
filePaths := make([]string, len(files)) filePaths := make([]string, len(files))
for i, f := range files { for i, f := range files {
filePaths[i] = fmt.Sprintf("%s/%s", session.logFolder, f.Name()) filePaths[i] = fmt.Sprintf("%s/%s", globalSession.logFolder, f.Name())
} }
req, err := newUploadFileRequest( req, err := newUploadFileRequest(
fmt.Sprintf("http://%s:%s/upload", session.commanderIP, session.commanderPort), fmt.Sprintf("http://%s:%s/upload", globalSession.commanderIP, globalSession.commanderPort),
"file", "file",
filePaths, filePaths,
nil) nil)
@ -242,6 +252,29 @@ func logAndReply(w *bufio.Writer, message string) {
w.Flush() w.Flush()
} }
func handleLog2Command(w *bufio.Writer) {
log.Println("Log command")
files, err := ioutil.ReadDir(globalSession.logFolder)
if err != nil {
logAndReply(w, fmt.Sprintf("Failed to create log folder. Error: %s", err.Error()))
return
}
filePaths := make([]string, len(files))
for i, f := range files {
filePaths[i] = fmt.Sprintf("%s/%s", globalSession.logFolder, f.Name())
}
// TODO: currently only upload the first file.
_, err = s3.UploadFile(bucketName, filePaths[0], strings.Replace(filePaths[0], logFolderPrefix, "", 1))
if err != nil {
logAndReply(w, fmt.Sprintf("Failed to create upload request. Error: %s", err.Error()))
return
}
logAndReply(w, "Upload log done!")
}
func runCmd(name string, args ...string) error { func runCmd(name string, args ...string) error {
err := exec.Command(name, args...).Start() err := exec.Command(name, args...).Start()
log.Println("Command running", name, args) log.Println("Command running", name, args)
@ -249,11 +282,11 @@ func runCmd(name string, args ...string) error {
} }
func runInstance() { func runInstance() {
config := readConfigFile(session.localConfigFileName) config := readConfigFile(globalSession.localConfigFileName)
myConfig := getMyConfig(setting.ip, setting.port, &config) myConfig := getMyConfig(setting.ip, setting.port, &config)
os.MkdirAll(session.logFolder, os.ModePerm) os.MkdirAll(globalSession.logFolder, os.ModePerm)
if myConfig[2] == "client" { if myConfig[2] == "client" {
runClient() runClient()
@ -264,12 +297,12 @@ func runInstance() {
func runNode() error { func runNode() error {
log.Println("running instance") log.Println("running instance")
return runCmd("./benchmark", "-ip", setting.ip, "-port", setting.port, "-config_file", session.localConfigFileName, "-log_folder", session.logFolder) return runCmd("./benchmark", "-ip", setting.ip, "-port", setting.port, "-config_file", globalSession.localConfigFileName, "-log_folder", globalSession.logFolder)
} }
func runClient() error { func runClient() error {
log.Println("running client") log.Println("running client")
return runCmd("./txgen", "-config_file", session.localConfigFileName, "-log_folder", session.logFolder) return runCmd("./txgen", "-config_file", globalSession.localConfigFileName, "-log_folder", globalSession.logFolder)
} }
func readConfigFile(configFile string) [][]string { func readConfigFile(configFile string) [][]string {

@ -0,0 +1,93 @@
package s3
import (
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
func createSession() *session.Session {
return session.Must(session.NewSession(&aws.Config{
Region: aws.String(endpoints.UsWest2RegionID),
MaxRetries: aws.Int(3),
}))
}
func CreateBucket(bucketName string, region string) {
sess := createSession()
svc := s3.New(sess)
input := &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(region),
},
}
result, err := svc.CreateBucket(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
fmt.Println(s3.ErrCodeBucketAlreadyExists, aerr.Error())
case s3.ErrCodeBucketAlreadyOwnedByYou:
fmt.Println(s3.ErrCodeBucketAlreadyOwnedByYou, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
func UploadFile(bucketName string, fileName string, key string) (result *s3manager.UploadOutput, err error) {
sess := createSession()
uploader := s3manager.NewUploader(sess)
f, err := os.Open(fileName)
if err != nil {
log.Println("Failed to open file", err)
return nil, err
}
// Upload the file to S3.
result, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
Body: f,
})
if err != nil {
log.Println("failed to upload file", err)
return nil, err
}
fmt.Printf("file uploaded to, %s\n", result.Location)
return result, nil
}
func DownloadFile(bucketName string, fileName string, key string) (n int64, err error) {
sess := createSession()
downloader := s3manager.NewDownloader(sess)
f, err := os.Create(fileName)
if err != nil {
return
}
n, err = downloader.Download(f, &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(key),
})
return
}
Loading…
Cancel
Save