update switchq to calling provisioner, fixed a few bugs found while testing at on.labs

Change-Id: I2367669aa54f680b98ff0cbbc8d41a49fb7e7a79
diff --git a/provisioner/Dockerfile b/provisioner/Dockerfile
index cf4a86b..429203a 100644
--- a/provisioner/Dockerfile
+++ b/provisioner/Dockerfile
@@ -24,7 +24,7 @@
 RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
 
 # CORD Provisioner Dockerfile
-WORKDIR $GOPATH
+WORKDIR /go
 
 RUN apt-get update && \
 	apt-get install -y  software-properties-common && \
@@ -36,12 +36,12 @@
 COPY ssh-config /root/.ssh/config
 
 RUN go get github.com/tools/godep
-ADD . $GOPATH/src/github.com/ciena/cord-provisioner
+ADD . /go/src/gerrit.opencord.org/maas/cord-provisioner
 
-WORKDIR $GOPATH/src/github.com/ciena/cord-provisioner
+WORKDIR /go/src/gerrit.opencord.org/maas/cord-provisioner
 RUN $GOPATH/bin/godep restore
 
 WORKDIR $GOPATH
-RUN go install github.com/ciena/cord-provisioner
+RUN go install gerrit.opencord.org/maas/cord-provisioner
 
 ENTRYPOINT ["/go/bin/cord-provisioner"]
diff --git a/provisioner/Godeps/Godeps.json b/provisioner/Godeps/Godeps.json
index a68b4ef..0b4660b 100644
--- a/provisioner/Godeps/Godeps.json
+++ b/provisioner/Godeps/Godeps.json
@@ -1,5 +1,5 @@
 {
-	"ImportPath": "github.com/ciena/provisioner",
+	"ImportPath": "gerrit.opencord.org/maas/cord-provisioner",
 	"GoVersion": "go1.6",
 	"GodepVersion": "v72",
 	"Deps": [
@@ -10,8 +10,8 @@
 		},
 		{
 			"ImportPath": "github.com/gorilla/mux",
-			"Comment": "v1.1-9-gbd09be0",
-			"Rev": "bd09be08ed4377796d312df0a45314e11b8f5dc1"
+			"Comment": "v1.1-13-g9fa818a",
+			"Rev": "9fa818a44c2bf1396a17f9d5a3c0f6dd39d2ff8e"
 		},
 		{
 			"ImportPath": "github.com/kelseyhightower/envconfig",
diff --git a/provisioner/dispatcher.go b/provisioner/dispatcher.go
index fb16458..bf6ebbf 100644
--- a/provisioner/dispatcher.go
+++ b/provisioner/dispatcher.go
@@ -20,10 +20,10 @@
 }
 
 type StatusMsg struct {
-	Request *WorkRequest
-	Worker  int
-	Status  TaskStatus
-	Message string
+	Request *WorkRequest `json:"request"`
+	Worker  int          `json:"worker"`
+	Status  TaskStatus   `json:"status"`
+	Message string       `json:"message"`
 }
 
 func NewWorker(id int, workerQueue chan chan WorkRequest, statusChan chan StatusMsg) Worker {
@@ -49,7 +49,7 @@
 			case work := <-w.Work:
 				// Receive a work request.
 				w.StatusChan <- StatusMsg{&work, w.ID, Running, ""}
-				log.Printf("RUN: %s %s %s %s %s %s",
+				log.Printf("[debug] 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,
@@ -117,12 +117,12 @@
 		for {
 			select {
 			case work := <-d.WorkQueue:
-				log.Println("Received work requeust")
+				log.Println("[debug] Received work requeust")
 				go func() {
 					d.StatusChan <- StatusMsg{&work, -1, Pending, ""}
 					worker := <-d.WorkerQueue
 
-					log.Println("Dispatching work request")
+					log.Println("[debug] Dispatching work request")
 					worker <- work
 				}()
 			case update := <-d.StatusChan:
diff --git a/provisioner/handlers.go b/provisioner/handlers.go
index 42946da..c2e78ba 100644
--- a/provisioner/handlers.go
+++ b/provisioner/handlers.go
@@ -4,22 +4,33 @@
 	"bufio"
 	"encoding/json"
 	"github.com/gorilla/mux"
+	"log"
 	"net/http"
 	"strings"
 )
 
 type RequestInfo struct {
-	Id   string
-	Name string
-	Ip   string
-	Mac  string
+	Id           string `json:"id"`
+	Name         string `json:"name"`
+	Ip           string `json:"ip"`
+	Mac          string `json:"mac"`
+	RoleSelector string `json:"role_selector"`
+	Role         string `json:"role"`
+	Script       string `json:"script"`
 }
 
 func (c *Context) GetRole(info *RequestInfo) (string, error) {
-	if c.config.RoleSelectorURL == "" {
+	if info.Role != "" {
+		return info.Role, nil
+	} else if c.config.RoleSelectorURL == "" && info.RoleSelector == "" {
 		return c.config.DefaultRole, nil
 	}
-	r, err := http.Get(c.config.RoleSelectorURL)
+	selector := c.config.RoleSelectorURL
+	if info.RoleSelector != "" {
+		selector = info.RoleSelector
+	}
+
+	r, err := http.Get(selector)
 	if err != nil {
 		return "", err
 	}
@@ -51,6 +62,7 @@
 		http.Error(w, err.Error(), http.StatusBadRequest)
 		return
 	}
+
 	if !c.validateData(&info) {
 		w.WriteHeader(http.StatusBadRequest)
 		return
@@ -89,6 +101,7 @@
 	}
 	s, err := c.storage.Get(id)
 	if err != nil {
+		log.Printf("[warn] Error while retrieving status for '%s' from strorage : %s", id, err)
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		return
 	}
@@ -98,6 +111,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)
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		return
 	}
@@ -105,7 +119,7 @@
 	switch s.Status {
 	case Pending, Running:
 		w.WriteHeader(http.StatusAccepted)
-	case Complete:
+	case Failed, Complete:
 		w.WriteHeader(http.StatusOK)
 	default:
 		w.WriteHeader(http.StatusInternalServerError)
diff --git a/provisioner/storage.go b/provisioner/storage.go
index db64df2..92c5339 100644
--- a/provisioner/storage.go
+++ b/provisioner/storage.go
@@ -1,9 +1,5 @@
 package main
 
-import (
-	"log"
-)
-
 type Storage interface {
 	Put(id string, update StatusMsg) error
 	Get(id string) (*StatusMsg, error)
@@ -22,7 +18,6 @@
 
 func (s *MemoryStorage) Put(id string, update StatusMsg) error {
 	s.Data[id] = update
-	log.Printf("%s : %s", id, update.Status.String())
 	return nil
 }
 
@@ -39,6 +34,7 @@
 	i := 0
 	for _, v := range s.Data {
 		r[i] = v
+		i += 1
 	}
 	return r, nil
 }