From 6099299712e25aadaefcf31e304e808bced6d6e7 Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Thu, 12 Jun 2014 22:26:39 +0930 Subject: [PATCH 1/6] Implemented logrus as the new logging solution. --- Godeps/Godeps.json | 11 ++++++++--- agent.go | 7 ++----- artifact.go | 15 ++++++--------- buildbox/agent.go | 19 +++++++++---------- buildbox/artifact.go | 7 +++---- buildbox/client.go | 17 +---------------- buildbox/job.go | 11 +++++------ buildbox/logger.go | 17 +++++++++++++++++ buildbox/script.go | 5 ++--- data.go | 11 ++++------- 10 files changed, 57 insertions(+), 63 deletions(-) create mode 100644 buildbox/logger.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 4bda018083..251a3911ff 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,7 +1,12 @@ { "ImportPath": "github.com/buildboxhq/buildbox-agent", - "GoVersion": "go1.2", + "GoVersion": "go1.2.2", "Deps": [ + { + "ImportPath": "github.com/Sirupsen/logrus", + "Comment": "v0.2.0-9-g97978e5", + "Rev": "97978e5e5483a3f926c2bb6e19f9e2ff344e81f0" + }, { "ImportPath": "github.com/codegangsta/cli", "Comment": "1.0.0-31-gc606efc", @@ -17,8 +22,8 @@ }, { "ImportPath": "github.com/kr/pty", - "Comment": "release.r56-18-g88e4fdb", - "Rev": "88e4fdb5b466ac228f35d9f75b315726bf09b2d1" + "Comment": "release.r56-19-g67e2db2", + "Rev": "67e2db24c831afa6c64fc17b4a143390674365ef" } ] } diff --git a/agent.go b/agent.go index 201b94006c..8937c846c0 100644 --- a/agent.go +++ b/agent.go @@ -3,7 +3,6 @@ package main import ( "os" "fmt" - "log" "github.com/codegangsta/cli" "github.com/buildboxhq/buildbox-agent/buildbox" ) @@ -160,15 +159,13 @@ func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { agent.Client.Debug = agent.Debug // Tell the user that debug mode has been enabled - if agent.Debug { - log.Printf("Debug mode enabled") - } + buildbox.Logger.Debug("Debug mode enabled") // Setup the agent agent.Setup() // A nice welcome message - log.Printf("Started buildbox-agent `%s` (version %s)\n", agent.Name, buildbox.Version) + buildbox.Logger.Infof("Started buildbox-agent `%s` (version %s)", agent.Name, buildbox.Version) return &agent } diff --git a/artifact.go b/artifact.go index dedf59a9e7..d908db819c 100644 --- a/artifact.go +++ b/artifact.go @@ -3,7 +3,6 @@ package main import ( "os" "fmt" - "log" "github.com/codegangsta/cli" "github.com/buildboxhq/buildbox-agent/buildbox" ) @@ -122,9 +121,7 @@ func main() { agent.Client.Debug = agent.Debug // Tell the user that debug mode has been enabled - if agent.Debug { - log.Printf("Debug mode enabled") - } + // TODO: Enable debug // Setup the agent agent.Setup() @@ -132,23 +129,23 @@ func main() { // Find the actual job now job, err := agent.Client.JobFind(jobId) if err != nil { - log.Fatalf("Could not find job: %s", jobId) + buildbox.Logger.Fatal("Could not find job: %s", jobId) } // Create artifact structs for all the files we need to upload artifacts, err := buildbox.CollectArtifacts(job, paths) if err != nil { - log.Fatalf("Failed to collect artifacts: %s", err) + buildbox.Logger.Fatal("Failed to collect artifacts: %s", err) } if len(artifacts) == 0 { - log.Printf("No files matched paths: %s", paths) + buildbox.Logger.Info("No files matched paths: %s", paths) } else { - log.Printf("Uploading %d files that match \"%s\"", len(artifacts), paths) + buildbox.Logger.Info("Uploading %d files that match \"%s\"", len(artifacts), paths) err := buildbox.UploadArtifacts(agent.Client, job, artifacts, destination) if err != nil { - log.Fatalf("Failed to upload artifacts: %s", err) + buildbox.Logger.Fatal("Failed to upload artifacts: %s", err) } } }, diff --git a/buildbox/agent.go b/buildbox/agent.go index 9fdd497b40..a4dd4faea3 100644 --- a/buildbox/agent.go +++ b/buildbox/agent.go @@ -1,7 +1,6 @@ package buildbox import ( - "log" "fmt" "time" "strings" @@ -51,7 +50,7 @@ func (a *Agent) Setup() { // Figure out the hostname of the current machine hostname, err := exec.Command("hostname").Output() if err != nil { - log.Fatal(err) + Logger.Fatal(err) } // Set the hostname @@ -61,7 +60,7 @@ func (a *Agent) Setup() { // current agent struct with data. err = a.Client.AgentUpdate(a) if err != nil { - log.Fatal(err) + Logger.Fatal(err) } } @@ -82,11 +81,11 @@ func (a *Agent) MonitorSignals() { // This will block until a signal is sent sig := <-signals - log.Printf("Received signal `%s`", sig.String()) + Logger.Debugf("Received signal `%s`", sig.String()) // Only monitor certain signals if sig != syscall.SIGINT && sig != syscall.SIGUSR2 { - log.Printf("Ignoring signal `%s`", sig.String()) + Logger.Debugf("Ignoring signal `%s`", sig.String()) // Start monitoring signals again a.MonitorSignals() @@ -96,7 +95,7 @@ func (a *Agent) MonitorSignals() { // If the agent isn't running a job, exit right away if a.Job == nil { - log.Printf("No jobs running. Exiting...") + Logger.Info("No jobs running. Exiting...") os.Exit(1) } @@ -112,7 +111,7 @@ func (a *Agent) MonitorSignals() { // Die time. os.Exit(1) } else { - log.Print("Exiting... Waiting for job to finish before stopping. Send signal again to exit immediately.") + Logger.Info("Exiting... Waiting for job to finish before stopping. Send signal again to exit immediately.") a.stopping = true } @@ -133,7 +132,7 @@ func (a *Agent) Start() { for { job, err := a.Client.JobNext() if err != nil { - log.Printf("Failed to get job (%s)", err) + Logger.Errorf("Failed to get job (%s)", err) break } @@ -162,11 +161,11 @@ func (a *Agent) Run(id string) { job, err := a.Client.JobFindAndAssign(id) if err != nil { - log.Fatal(err) + Logger.Fatal(err) } if job.State != "scheduled" { - log.Fatalf("The agent can only run scheduled jobs. Current state is `%s`", job.State) + Logger.Fatalf("The agent can only run scheduled jobs. Current state is `%s`", job.State) } // Run the paticular job diff --git a/buildbox/artifact.go b/buildbox/artifact.go index d1e1efdde4..f051460b62 100644 --- a/buildbox/artifact.go +++ b/buildbox/artifact.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "strings" - "log" "errors" "path/filepath" "mime" @@ -208,14 +207,14 @@ func UploadArtifacts(client Client, job *Job, artifacts []*Artifact, destination func uploadRoutine(quit chan string, client Client, job *Job, artifact Artifact, uploader Uploader) { // Show a nice message that we're starting to upload the file - log.Printf("Uploading %s\n", artifact.Path) + Logger.Info("Uploading %s", artifact.Path) // Upload the artifact and then set the state depending on whether or not // it passed. err := uploader.Upload(&artifact) if err != nil { artifact.State = "error" - log.Printf("Error uploading artifact %s (%s)", artifact.Path, err) + Logger.Errorf("Error uploading artifact %s (%s)", artifact.Path, err) } else { artifact.State = "finished" } @@ -223,7 +222,7 @@ func uploadRoutine(quit chan string, client Client, job *Job, artifact Artifact, // Update the state of the artifact on Buildbox _, err = client.ArtifactUpdate(job, artifact) if err != nil { - log.Printf("Error marking artifact %s as uploaded (%s)", artifact.Path, err) + Logger.Errorf("Error marking artifact %s as uploaded (%s)", artifact.Path, err) } // We can notify the channel that this routine has finished now diff --git a/buildbox/client.go b/buildbox/client.go index 9e40796351..4d3b79193d 100644 --- a/buildbox/client.go +++ b/buildbox/client.go @@ -2,15 +2,12 @@ package buildbox import ( "net/http" - "net/http/httputil" _ "crypto/sha512" // import sha512 to make sha512 ssl certs work "encoding/json" "runtime" "strings" "io" "errors" - "log" - "os" "bytes" ) @@ -112,9 +109,7 @@ func (c *Client) NewRequest(method string, path string, body interface{}) (*http // Submits an HTTP request, checks its response, and deserializes // the response into v. func (c *Client) DoReq(req *http.Request, v interface{}) error { - if c.Debug { - log.Printf("%s %s\n", req.Method, req.URL) - } + Logger.Debugf("%s %s", req.Method, req.URL) res, err := http.DefaultClient.Do(req) if err != nil { @@ -125,16 +120,6 @@ func (c *Client) DoReq(req *http.Request, v interface{}) error { // this function defer res.Body.Close() - if c.Debug { - dump, err := httputil.DumpResponse(res, true) - if err != nil { - log.Println(err) - } else { - os.Stderr.Write(dump) - os.Stderr.Write([]byte{'\n'}) - } - } - // Check the response of the response if err = checkResp(res); err != nil { return err diff --git a/buildbox/job.go b/buildbox/job.go index 87a967473f..bc5660ff0a 100644 --- a/buildbox/job.go +++ b/buildbox/job.go @@ -2,7 +2,6 @@ package buildbox import ( "fmt" - "log" "time" "path" ) @@ -74,7 +73,7 @@ func (c *Client) JobUpdate(job *Job) (*Job, error) { func (j *Job) Kill() error { if j.process != nil { - log.Printf("Cancelling job %s", j.ID) + Logger.Infof("Cancelling job %s", j.ID) j.process.Kill() } @@ -82,7 +81,7 @@ func (j *Job) Kill() error { } func (j *Job) Run(agent *Agent) error { - log.Printf("Starting job %s", j.ID) + Logger.Infof("Starting job %s", j.ID) // Create the environment that the script will use env := []string{} @@ -111,7 +110,7 @@ func (j *Job) Run(agent *Agent) error { // We don't really care if the job couldn't update at this point. // This is just a partial update. We'll just let the job run // and hopefully the host will fix itself before we finish. - log.Printf("Problem with updating job %s (%s)", j.ID, err) + Logger.Errorf("Problem with updating job %s (%s)", j.ID, err) } else if updatedJob.State == "canceled" { j.Kill() } @@ -146,7 +145,7 @@ func (j *Job) Run(agent *Agent) error { for { _, err = agent.Client.JobUpdate(j) if err != nil { - log.Printf("Problem with updating final job information %s (%s)", j.ID, err) + Logger.Errorf("Problem with updating final job information %s (%s)", j.ID, err) // How long should we wait until we try again? idleSeconds := 5 @@ -159,7 +158,7 @@ func (j *Job) Run(agent *Agent) error { } } - log.Printf("Finished job %s", j.ID) + Logger.Infof("Finished job %s", j.ID) return nil } diff --git a/buildbox/logger.go b/buildbox/logger.go new file mode 100644 index 0000000000..e149f426a6 --- /dev/null +++ b/buildbox/logger.go @@ -0,0 +1,17 @@ +package buildbox + +import ( + "github.com/Sirupsen/logrus" +) + + +func initLogger() *logrus.Logger { + var log = logrus.New() + + log.Level = logrus.Debug + log.Formatter = new(logrus.TextFormatter) + + return log +} + +var Logger = initLogger() diff --git a/buildbox/script.go b/buildbox/script.go index c6d2243cff..6272a38747 100644 --- a/buildbox/script.go +++ b/buildbox/script.go @@ -8,7 +8,6 @@ import ( "io" "os" "bytes" - "log" "path" "path/filepath" "errors" @@ -40,7 +39,7 @@ func RunScript(dir string, script string, env []string, callback func(Process)) absoluteDir, _ := filepath.Abs(dir) pathToScript := path.Join(absoluteDir, script) - log.Printf("Running script `%s` from inside %s\n", script, absoluteDir) + Logger.Infof("Running script `%s` from inside %s\n", script, absoluteDir) process.command = exec.Command(pathToScript) process.command.Dir = absoluteDir @@ -72,7 +71,7 @@ func RunScript(dir string, script string, env []string, callback func(Process)) // or something breaks. _, err = io.Copy(&buffer, pty) if err != nil { - log.Printf("io.Copy failed with error: %s\n", err) + Logger.Errorf("io.Copy failed with error: %s\n", err) } }() diff --git a/data.go b/data.go index 6828c5ec0c..fef3dfa44d 100644 --- a/data.go +++ b/data.go @@ -3,7 +3,6 @@ package main import ( "os" "fmt" - "log" "github.com/codegangsta/cli" "github.com/buildboxhq/buildbox-agent/buildbox" ) @@ -89,7 +88,7 @@ func main() { // Set the data through the API _, err := agent.Client.DataSet(job, key, value) if err != nil { - log.Fatalf("Failed to set data: %s", err) + buildbox.Logger.Fatalf("Failed to set data: %s", err) } }, }, @@ -122,7 +121,7 @@ func main() { // Get the data through the API data, err := agent.Client.DataGet(job, key) if err != nil { - log.Fatalf("Failed to get data: %s", err) + buildbox.Logger.Fatalf("Failed to get data: %s", err) } // Output it @@ -163,9 +162,7 @@ func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { agent.Client.Debug = agent.Debug // Tell the user that debug mode has been enabled - if agent.Debug { - log.Printf("Debug mode enabled") - } + // TODO: Enable debugging return &agent } @@ -188,7 +185,7 @@ func findJobFromCli(agent *buildbox.Agent, c *cli.Context, command string) (*bui // Find the actual job now job, err := agent.Client.JobFind(jobId) if err != nil { - log.Fatalf("Could not find job: %s", jobId) + buildbox.Logger.Fatalf("Could not find job: %s", jobId) } return job From 360a3c0955d28bd18c632b3f1ceea4bd76740bde Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Thu, 12 Jun 2014 22:36:06 +0930 Subject: [PATCH 2/6] Improved debug handling. --- agent.go | 10 +++++----- artifact.go | 10 +++++----- buildbox/agent.go | 3 --- buildbox/client.go | 3 --- buildbox/logger.go | 8 ++++++-- data.go | 10 +++++----- 6 files changed, 21 insertions(+), 23 deletions(-) diff --git a/agent.go b/agent.go index 8937c846c0..e0ae35ad23 100644 --- a/agent.go +++ b/agent.go @@ -123,6 +123,11 @@ func main() { } func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { + // Init debugging + if c.Bool("debug") { + buildbox.LoggerInitDebug() + } + agentAccessToken := c.String("access-token") // Should we look to the environment for the agent access token? @@ -150,16 +155,11 @@ func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { // Set the agent options var agent buildbox.Agent; - agent.Debug = c.Bool("debug") agent.BootstrapScript = bootstrapScript // Client specific options agent.Client.AgentAccessToken = agentAccessToken agent.Client.URL = c.String("url") - agent.Client.Debug = agent.Debug - - // Tell the user that debug mode has been enabled - buildbox.Logger.Debug("Debug mode enabled") // Setup the agent agent.Setup() diff --git a/artifact.go b/artifact.go index d908db819c..25d12a64d8 100644 --- a/artifact.go +++ b/artifact.go @@ -74,6 +74,11 @@ func main() { cli.BoolFlag{"debug", "Enable debug mode"}, }, Action: func(c *cli.Context) { + // Init debugging + if c.Bool("debug") { + buildbox.LoggerInitDebug() + } + agentAccessToken := c.String("agent-access-token") // Should we look to the environment for the agent access token? @@ -113,15 +118,10 @@ func main() { // Set the agent options var agent buildbox.Agent; - agent.Debug = c.Bool("debug") // Client specific options agent.Client.AgentAccessToken = agentAccessToken agent.Client.URL = c.String("url") - agent.Client.Debug = agent.Debug - - // Tell the user that debug mode has been enabled - // TODO: Enable debug // Setup the agent agent.Setup() diff --git a/buildbox/agent.go b/buildbox/agent.go index a4dd4faea3..8f3d7202bd 100644 --- a/buildbox/agent.go +++ b/buildbox/agent.go @@ -21,9 +21,6 @@ type Agent struct { // The hostname of the agent Hostname string `json:"hostname,omitempty"` - // Whether to run the agent in Debug mode - Debug bool - // The boostrap script to run BootstrapScript string diff --git a/buildbox/client.go b/buildbox/client.go index 4d3b79193d..0ed5d8edda 100644 --- a/buildbox/client.go +++ b/buildbox/client.go @@ -24,9 +24,6 @@ type Client struct { // The access token of the agent being used to make API requests AgentAccessToken string - // Debug mode can be used to dump the full request and response to stdout. - Debug bool - // UserAgent to be provided in API requests. Set to DefaultUserAgent if not // specified. UserAgent string diff --git a/buildbox/logger.go b/buildbox/logger.go index e149f426a6..fcbb969870 100644 --- a/buildbox/logger.go +++ b/buildbox/logger.go @@ -4,14 +4,18 @@ import ( "github.com/Sirupsen/logrus" ) +var Logger = initLogger() func initLogger() *logrus.Logger { var log = logrus.New() - log.Level = logrus.Debug log.Formatter = new(logrus.TextFormatter) return log } -var Logger = initLogger() +func LoggerInitDebug() { + Logger.Level = logrus.Debug + + Logger.Debug("Debugging enabled") +} diff --git a/data.go b/data.go index fef3dfa44d..88e808c05d 100644 --- a/data.go +++ b/data.go @@ -140,6 +140,11 @@ func main() { } func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { + // Init debugging + if c.Bool("debug") { + buildbox.LoggerInitDebug() + } + agentAccessToken := c.String("agent-access-token") // Should we look to the environment for the agent access token? @@ -154,15 +159,10 @@ func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { // Set the agent options var agent buildbox.Agent; - agent.Debug = c.Bool("debug") // Client specific options agent.Client.AgentAccessToken = agentAccessToken agent.Client.URL = c.String("url") - agent.Client.Debug = agent.Debug - - // Tell the user that debug mode has been enabled - // TODO: Enable debugging return &agent } From b27cb2558031059bf8eadf0b737d4e5613e3015c Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Thu, 12 Jun 2014 23:39:53 +0930 Subject: [PATCH 3/6] Added more debugging to artifact uploading. --- artifact.go | 10 +++++----- buildbox/artifact.go | 9 +++++++-- buildbox/form_uploader.go | 27 +++++++++++---------------- buildbox/s3_uploader.go | 10 ++++++---- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/artifact.go b/artifact.go index 25d12a64d8..5046804a56 100644 --- a/artifact.go +++ b/artifact.go @@ -129,23 +129,23 @@ func main() { // Find the actual job now job, err := agent.Client.JobFind(jobId) if err != nil { - buildbox.Logger.Fatal("Could not find job: %s", jobId) + buildbox.Logger.Fatalf("Could not find job: %s", jobId) } // Create artifact structs for all the files we need to upload artifacts, err := buildbox.CollectArtifacts(job, paths) if err != nil { - buildbox.Logger.Fatal("Failed to collect artifacts: %s", err) + buildbox.Logger.Fatalf("Failed to collect artifacts: %s", err) } if len(artifacts) == 0 { - buildbox.Logger.Info("No files matched paths: %s", paths) + buildbox.Logger.Infof("No files matched paths: %s", paths) } else { - buildbox.Logger.Info("Uploading %d files that match \"%s\"", len(artifacts), paths) + buildbox.Logger.Infof("Found %d files that match \"%s\"", len(artifacts), paths) err := buildbox.UploadArtifacts(agent.Client, job, artifacts, destination) if err != nil { - buildbox.Logger.Fatal("Failed to upload artifacts: %s", err) + buildbox.Logger.Fatalf("Failed to upload artifacts: %s", err) } } }, diff --git a/buildbox/artifact.go b/buildbox/artifact.go index f051460b62..47082cb8d5 100644 --- a/buildbox/artifact.go +++ b/buildbox/artifact.go @@ -96,6 +96,8 @@ func CollectArtifacts(job *Job, artifactPaths string) (artifacts []*Artifact, er glob = strings.TrimSpace(glob) if glob != "" { + Logger.Debugf("Globbing %s for %s", workingDirectory, glob) + files, err := Glob(workingDirectory, glob) if err != nil { return nil, err @@ -180,6 +182,8 @@ func UploadArtifacts(client Client, job *Job, artifacts []*Artifact, destination var routines []chan string var concurrency int = 10 + Logger.Debugf("Spinning up %d concurrent threads for uploads", concurrency) + count := 0 for _, artifact := range createdArtifacts { // Create a channel and apend it to the routines array. Once we've hit our @@ -191,7 +195,8 @@ func UploadArtifacts(client Client, job *Job, artifacts []*Artifact, destination routines = append(routines, wait) if count >= concurrency { - // fmt.Printf("Maxiumum concurrent threads running. Waiting.\n") + Logger.Debug("Maxiumum concurrent threads running. Waiting.") + // Wait for all the routines to finish, then reset waitForRoutines(routines) count = 0 @@ -207,7 +212,7 @@ func UploadArtifacts(client Client, job *Job, artifacts []*Artifact, destination func uploadRoutine(quit chan string, client Client, job *Job, artifact Artifact, uploader Uploader) { // Show a nice message that we're starting to upload the file - Logger.Info("Uploading %s", artifact.Path) + Logger.Infof("Uploading %s (%d bytes)", artifact.Path, artifact.FileSize) // Upload the artifact and then set the state depending on whether or not // it passed. diff --git a/buildbox/form_uploader.go b/buildbox/form_uploader.go index dbf7ab2a0e..40c59363c5 100644 --- a/buildbox/form_uploader.go +++ b/buildbox/form_uploader.go @@ -34,16 +34,11 @@ func (u *FormUploader) Upload(artifact *Artifact) (error) { return err } - // dump, err := httputil.DumpRequest(request, true) - // if err != nil { - // fmt.Println(err) - // } else { - // os.Stderr.Write(dump) - // os.Stderr.Write([]byte{'\n'}) - // } + // Create the client + client := &http.Client{} // Perform the request - client := &http.Client{} + Logger.Debugf("%s %s", request.Method, request.URL) response, err := client.Do(request) // Check for errors @@ -54,14 +49,6 @@ func (u *FormUploader) Upload(artifact *Artifact) (error) { // this function defer response.Body.Close() - // dump, err := httputil.DumpResponse(response, true) - // if err != nil { - // fmt.Println(err) - // } else { - // os.Stderr.Write(dump) - // os.Stderr.Write([]byte{'\n'}) - // } - if response.StatusCode/100 != 2 { body := &bytes.Buffer{} _, err := body.ReadFrom(response.Body) @@ -104,7 +91,11 @@ func createUploadRequest(artifact *Artifact) (*http.Request, error) { if err != nil { return nil, err } + _, err = io.Copy(part, file) + if err != nil { + return nil, err + } err = writer.Close() if err != nil { @@ -113,6 +104,10 @@ func createUploadRequest(artifact *Artifact) (*http.Request, error) { // Create the URL that we'll send data to uri, err := url.Parse(artifact.Uploader.Action.URL) + if err != nil { + return nil, err + } + uri.Path = artifact.Uploader.Action.Path // Create the request diff --git a/buildbox/s3_uploader.go b/buildbox/s3_uploader.go index 62961aee2b..92d79e2718 100644 --- a/buildbox/s3_uploader.go +++ b/buildbox/s3_uploader.go @@ -5,7 +5,6 @@ import ( "github.com/crowdmob/goamz/s3" "github.com/crowdmob/goamz/aws" "os" - "fmt" "strings" "errors" ) @@ -26,8 +25,7 @@ func (u *S3Uploader) Setup(destination string) (error) { // Setup the AWS authentication auth, err := aws.EnvAuth() if err != nil { - fmt.Printf("Error loading AWS credentials: %s", err) - os.Exit(1) + return errors.New("Error loading AWS credentials: " + err.Error()) } // Decide what region to use @@ -65,13 +63,17 @@ func (u *S3Uploader) URL(artifact *Artifact) (string) { } func (u *S3Uploader) Upload(artifact *Artifact) (error) { - Perms := s3.ACL("public-read") + // Define the permission to use. Hard coded for now. + permission := "public-read" + Perms := s3.ACL(permission) + Logger.Debugf("Reading file %s", artifact.AbsolutePath) data, err := ioutil.ReadFile(artifact.AbsolutePath) if err != nil { return errors.New("Failed to read file " + artifact.AbsolutePath + " (" + err.Error() + ")") } + Logger.Debugf("Putting to %s with permission %s", u.artifactPath(artifact), permission) err = u.Bucket.Put(u.artifactPath(artifact), data, artifact.MimeType(), Perms, s3.Options{}) if err != nil { return errors.New("Failed to PUT file " + u.artifactPath(artifact) + " (" + err.Error() + ")") From 2164a827fe1fc426f282a7d5be8e9de5760d258f Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Fri, 13 Jun 2014 17:57:32 +0930 Subject: [PATCH 4/6] Show PID in the logs when booting up. --- agent.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agent.go b/agent.go index e0ae35ad23..6141f2d69f 100644 --- a/agent.go +++ b/agent.go @@ -165,7 +165,7 @@ func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { agent.Setup() // A nice welcome message - buildbox.Logger.Infof("Started buildbox-agent `%s` (version %s)", agent.Name, buildbox.Version) + buildbox.Logger.Infof("Started buildbox-agent `%s` (Version: %s, PID: %d)", agent.Name, buildbox.Version, os.Getpid()) return &agent } From b752558e7fce2e1c21fb5f5e52e4265a04158336 Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Fri, 13 Jun 2014 19:57:21 +0930 Subject: [PATCH 5/6] Implemented my own logger. --- agent.go | 6 +++- buildbox/logger.go | 76 ++++++++++++++++++++++++++++++++++++++++++++-- buildbox/script.go | 4 +-- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/agent.go b/agent.go index 6141f2d69f..0d6ff1452e 100644 --- a/agent.go +++ b/agent.go @@ -3,6 +3,7 @@ package main import ( "os" "fmt" + "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" "github.com/buildboxhq/buildbox-agent/buildbox" ) @@ -165,7 +166,10 @@ func setupAgentFromCli(c *cli.Context, command string) *buildbox.Agent { agent.Setup() // A nice welcome message - buildbox.Logger.Infof("Started buildbox-agent `%s` (Version: %s, PID: %d)", agent.Name, buildbox.Version, os.Getpid()) + buildbox.Logger.WithFields(logrus.Fields{ + "pid": os.Getpid(), + "version": buildbox.Version, + }).Infof("Started buildbox-agent `%s`", agent.Name) return &agent } diff --git a/buildbox/logger.go b/buildbox/logger.go index fcbb969870..fbb5859b14 100644 --- a/buildbox/logger.go +++ b/buildbox/logger.go @@ -2,20 +2,92 @@ package buildbox import ( "github.com/Sirupsen/logrus" + "bytes" + "strings" + "fmt" + "time" +) + +const ( + nocolor = 0 + red = 31 + green = 32 + yellow = 33 + blue = 34 ) var Logger = initLogger() +type LogFormatter struct { + +} + +type LoggerFields logrus.Fields; + +func (f *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) { + b := &bytes.Buffer{} + + // Start by printing the time + now := time.Now() + fmt.Fprintf(b, "%s ", now.Format("2006-01-02 15:04:05")) + + // Upper case the log level + levelText := strings.ToUpper(entry.Data["level"].(string)) + + // Print the log level, but toggle the color + if logrus.IsTerminal() { + levelColor := blue + + if entry.Data["level"] == "debug" { + levelColor = green + } else if entry.Data["level"] == "warning" { + levelColor = yellow + } else if entry.Data["level"] == "error" || entry.Data["level"] == "fatal" || entry.Data["level"] == "panic" { + levelColor = red + } + + fmt.Fprintf(b, "\x1b[%dm[%-5s]\x1b[0m ", levelColor, levelText) + } else { + fmt.Fprintf(b, "[%-5s] ", levelText) + } + + // Now print the message + fmt.Fprintf(b, "%s ", entry.Data["msg"]) + + // Print any extra data. By default, the data map has 3 + // elements. + if len(entry.Data) > 3 { + keys := make([]string, 0) + for key, value := range entry.Data { + if key != "time" && key != "level" && key != "msg" { + if _, ok := value.(string); ok { + keys = append(keys, fmt.Sprintf("%v: %s", key, value)) + } else { + keys = append(keys, fmt.Sprintf("%v: %v", key, value)) + } + } + } + + fmt.Fprintf(b, "(%s)", strings.Join(keys, " ")) + } + + b.WriteByte('\n') + + return b.Bytes(), nil +} + func initLogger() *logrus.Logger { + // Create a new instance of the logrus logging library var log = logrus.New() - log.Formatter = new(logrus.TextFormatter) + // Use our custom log formatter + log.Formatter = new(LogFormatter) return log } func LoggerInitDebug() { + // Enable debugging Logger.Level = logrus.Debug - Logger.Debug("Debugging enabled") } diff --git a/buildbox/script.go b/buildbox/script.go index 33b56f9766..825b680bfc 100644 --- a/buildbox/script.go +++ b/buildbox/script.go @@ -40,7 +40,7 @@ func RunScript(dir string, script string, env []string, callback func(Process)) absoluteDir, _ := filepath.Abs(dir) pathToScript := path.Join(absoluteDir, script) - Logger.Infof("Running script `%s` from inside %s\n", script, absoluteDir) + Logger.Infof("Running script `%s` from inside %s", script, absoluteDir) process.command = exec.Command(pathToScript) process.command.Dir = absoluteDir @@ -74,7 +74,7 @@ func RunScript(dir string, script string, env []string, callback func(Process)) // or something breaks. _, err = io.Copy(&buffer, pty) if err != nil { - Logger.Errorf("io.Copy failed with error: %s\n", err) + Logger.Errorf("io.Copy failed with error: %s", err) } w.Done() }() From 47acca0a2612905d0bfdcd787c7ee0ddb3c35980 Mon Sep 17 00:00:00 2001 From: Keith Pitt Date: Fri, 13 Jun 2014 20:00:05 +0930 Subject: [PATCH 6/6] Tweaked the logging colors. --- buildbox/logger.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buildbox/logger.go b/buildbox/logger.go index fbb5859b14..d1d0e64eca 100644 --- a/buildbox/logger.go +++ b/buildbox/logger.go @@ -36,10 +36,10 @@ func (f *LogFormatter) Format(entry *logrus.Entry) ([]byte, error) { // Print the log level, but toggle the color if logrus.IsTerminal() { - levelColor := blue + levelColor := green if entry.Data["level"] == "debug" { - levelColor = green + levelColor = blue } else if entry.Data["level"] == "warning" { levelColor = yellow } else if entry.Data["level"] == "error" || entry.Data["level"] == "fatal" || entry.Data["level"] == "panic" {