updated to use a common logging library and enable log level configuration
Change-Id: Ib473615f25318c3b40cd6cf3bd49248e8a3d4fb1
diff --git a/provisioner/Dockerfile b/provisioner/Dockerfile
index 429203a..b2721f0 100644
--- a/provisioner/Dockerfile
+++ b/provisioner/Dockerfile
@@ -39,7 +39,7 @@
ADD . /go/src/gerrit.opencord.org/maas/cord-provisioner
WORKDIR /go/src/gerrit.opencord.org/maas/cord-provisioner
-RUN $GOPATH/bin/godep restore
+RUN $GOPATH/bin/godep restore || true
WORKDIR $GOPATH
RUN go install gerrit.opencord.org/maas/cord-provisioner
diff --git a/provisioner/Godeps/Godeps.json b/provisioner/Godeps/Godeps.json
index a96e9e3..2870953 100644
--- a/provisioner/Godeps/Godeps.json
+++ b/provisioner/Godeps/Godeps.json
@@ -22,6 +22,10 @@
"ImportPath": "github.com/kelseyhightower/envconfig",
"Comment": "1.1.0-17-g91921eb",
"Rev": "91921eb4cf999321cdbeebdba5a03555800d493b"
- }
+ },
+ {
+ "ImportPath": "github.com/Sirupsen/logrus",
+ "Rev": "f3cfb454f4c209e6668c95216c4744b8fddb2356"
+ }
]
}
diff --git a/provisioner/consul_storage.go b/provisioner/consul_storage.go
index 5701c72..0a0c62d 100644
--- a/provisioner/consul_storage.go
+++ b/provisioner/consul_storage.go
@@ -3,7 +3,6 @@
import (
"encoding/json"
consul "github.com/hashicorp/consul/api"
- "log"
"net/url"
)
@@ -27,7 +26,7 @@
Scheme: "http",
}
- log.Printf("%+v", cfg)
+ log.Debugf("Consul config = %+v", cfg)
client, err := consul.NewClient(&cfg)
if err != nil {
diff --git a/provisioner/dispatcher.go b/provisioner/dispatcher.go
index d448e19..eaa65cb 100644
--- a/provisioner/dispatcher.go
+++ b/provisioner/dispatcher.go
@@ -1,7 +1,6 @@
package main
import (
- "log"
"os/exec"
"time"
)
@@ -51,7 +50,7 @@
case work := <-w.Work:
// Receive a work request.
w.StatusChan <- StatusMsg{&work, w.ID, Running, "", time.Now().Unix()}
- log.Printf("[debug] RUN: %s %s %s %s %s %s",
+ log.Debugf("RUN: %s %s %s %s %s %s",
work.Script, work.Info.Id, work.Info.Name,
work.Info.Ip, work.Info.Mac, work.Role)
err := exec.Command(work.Script, work.Info.Id, work.Info.Name,
@@ -65,7 +64,7 @@
}
case <-w.QuitChan:
// We have been asked to stop.
- log.Printf("worker%d stopping\n", w.ID)
+ log.Infof("worker%d stopping\n", w.ID)
return
}
}
@@ -112,7 +111,7 @@
func (d *Dispatcher) Start() {
// Now, create all of our workers.
for i := 0; i < d.NumWorkers; i++ {
- log.Printf("Creating worker %d", i)
+ log.Infof("Creating worker %d", i)
worker := NewWorker(i, d.WorkerQueue, d.StatusChan)
worker.Start()
}
@@ -121,24 +120,24 @@
for {
select {
case work := <-d.WorkQueue:
- log.Println("[debug] Received work requeust")
+ log.Debugf("Received work requeust")
d.StatusChan <- StatusMsg{&work, -1, Pending, "", time.Now().Unix()}
go func() {
worker := <-d.WorkerQueue
- log.Println("[debug] Dispatching work request")
+ log.Debugf("Dispatching work request")
worker <- work
}()
case update := <-d.StatusChan:
err := d.Storage.Put(update.Request.Info.Id, update)
if err != nil {
- log.Printf("[error] Unable to update storage with status for '%s' : %s",
+ log.Errorf("Unable to update storage with status for '%s' : %s",
update.Request.Info.Id, err)
} else {
- log.Printf("[debug] Storage updated for '%s'", update.Request.Info.Id)
+ log.Debugf("Storage updated for '%s'", update.Request.Info.Id)
}
case <-d.QuitChan:
- log.Println("[info] Stopping dispatcher")
+ log.Infof("Stopping dispatcher")
return
}
}
diff --git a/provisioner/handlers.go b/provisioner/handlers.go
index 7fd53fa..bb93de4 100644
--- a/provisioner/handlers.go
+++ b/provisioner/handlers.go
@@ -4,7 +4,6 @@
"bufio"
"encoding/json"
"github.com/gorilla/mux"
- "log"
"net/http"
"strings"
)
@@ -59,20 +58,20 @@
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
if err := decoder.Decode(&info); err != nil {
- log.Printf("[error] Unable to decode request to provision : %s", err)
+ log.Errorf("Unable to decode request to provision : %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if !c.validateData(&info) {
- log.Printf("[error] Provisioning request not valid for '%s'", info.Name)
+ log.Errorf("Provisioning request not valid for '%s'", info.Name)
w.WriteHeader(http.StatusBadRequest)
return
}
role, err := c.GetRole(&info)
if err != nil {
- log.Printf("[error] unable to get provisioning role for node '%s' : %s", info.Name, err)
+ log.Errorf("unable to get provisioning role for node '%s' : %s", info.Name, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -84,7 +83,7 @@
}
err = c.dispatcher.Dispatch(&info, role, script)
if err != nil {
- log.Printf("[error] unable to dispatch provisioning request for node '%s' : %s", info.Name, err)
+ log.Errorf("unable to dispatch provisioning request for node '%s' : %s", info.Name, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -112,7 +111,7 @@
err := c.storage.Delete(id)
if err != nil {
- log.Printf("[warn] Error while deleting status fo '%s' from storage : %s", id, err)
+ log.Errorf("Error while deleting status fo '%s' from storage : %s", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -129,7 +128,7 @@
}
s, err := c.storage.Get(id)
if err != nil {
- log.Printf("[warn] Error while retrieving status for '%s' from strorage : %s", id, err)
+ log.Errorf("Error while retrieving status for '%s' from strorage : %s", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
@@ -139,7 +138,7 @@
}
bytes, err := json.Marshal(s)
if err != nil {
- log.Printf("[error] Error while attempting to marshal status for '%s' from storage : %s", id, err)
+ log.Errorf("Error while attempting to marshal status for '%s' from storage : %s", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
diff --git a/provisioner/provisioner.go b/provisioner/provisioner.go
index 41240b5..6c02c14 100644
--- a/provisioner/provisioner.go
+++ b/provisioner/provisioner.go
@@ -2,9 +2,9 @@
import (
"fmt"
+ "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/kelseyhightower/envconfig"
- "log"
"net/http"
)
@@ -15,6 +15,8 @@
DefaultRole string `default:"compute-node" envconfig:"default_role"`
Script string `default:"do-ansible"`
StorageURL string `default:"memory:" envconfig:"storage_url"`
+ LogLevel string `default:"warning" envconfig:"LOG_LEVEL"`
+ LogFormat string `default:"text" envconfig:"LOG_FORMAT"`
}
type Context struct {
@@ -24,23 +26,44 @@
dispatcher *Dispatcher
}
+var log = logrus.New()
+
func main() {
context := &Context{}
err := envconfig.Process("PROVISION", &(context.config))
if err != nil {
- log.Fatalf("[error] Unable to parse configuration options : %s", err)
+ log.Fatalf("[ERRO] Unable to parse configuration options : %s", err)
}
- log.Printf(`Configuration:
+ switch context.config.LogFormat {
+ case "json":
+ log.Formatter = &logrus.JSONFormatter{}
+ default:
+ log.Formatter = &logrus.TextFormatter{
+ FullTimestamp: true,
+ ForceColors: true,
+ }
+ }
+
+ level, err := logrus.ParseLevel(context.config.LogLevel)
+ if err != nil {
+ level = logrus.WarnLevel
+ }
+ log.Level = level
+
+ log.Infof(`Configuration:
Listen: %s
Port: %d
RoleSelectorURL: %s
DefaultRole: %s
Script: %s
- StorageURL: %s`,
+ StorageURL: %s
+ Log Level: %s
+ Log Format: %s`,
context.config.Listen, context.config.Port, context.config.RoleSelectorURL,
- context.config.DefaultRole, context.config.Script, context.config.StorageURL)
+ context.config.DefaultRole, context.config.Script, context.config.StorageURL,
+ context.config.LogLevel, context.config.LogFormat)
context.storage, err = NewStorage(context.config.StorageURL)
if err != nil {