From 81d9d9c9b6a0c2309e080d42c5e2271840feb93d Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Tue, 12 Sep 2017 17:09:43 -0300 Subject: [PATCH] Add missing function to windows provisioner --- provisioner/windows.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/provisioner/windows.go b/provisioner/windows.go index 12b096d..cd70ef3 100644 --- a/provisioner/windows.go +++ b/provisioner/windows.go @@ -1,6 +1,8 @@ package provisioner import ( + "bytes" + "encoding/json" "fmt" "io" "log" @@ -119,6 +121,39 @@ func (d *windows) InstanceDelete(session *types.Session, instance *types.Instanc return d.releaseInstance(instance.WindowsId) } +type execRes struct { + ExitCode int `json:"exit_code"` + Error string `json:"error"` + Stdout string `json:"stdout"` + Stderr string `json:"stderr"` +} + +func (d *windows) InstanceExec(instance *types.Instance, cmd []string) (int, error) { + execBody := struct { + Cmd []string `json:"cmd"` + }{Cmd: cmd} + + b, err := json.Marshal(execBody) + if err != nil { + return -1, err + } + resp, err := http.Post(fmt.Sprintf("http://%s:222/exec", instance.IP), "application/json", bytes.NewReader(b)) + if err != nil { + log.Println(err) + return -1, err + } + if resp.StatusCode != 200 { + log.Printf("Error exec on instance %s. Got %d\n", instance.Name, resp.StatusCode) + return -1, fmt.Errorf("Error exec on instance %s. Got %d\n", instance.Name, resp.StatusCode) + } + var ex execRes + err = json.NewDecoder(resp.Body).Decode(&ex) + if err != nil { + return -1, err + } + return ex.ExitCode, nil +} + func (d *windows) releaseInstance(instanceId string) error { return d.storage.WindowsInstanceDelete(instanceId) }