Make SessionSetup faster and support for command execution

This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-09-11 18:09:27 -03:00
parent 86044788b9
commit fe299fed90
9 changed files with 162 additions and 124 deletions

View File

@@ -116,6 +116,14 @@ func (d *DinD) InstanceDelete(session *types.Session, instance *types.Instance)
return nil
}
func (d *DinD) InstanceExec(instance *types.Instance, cmd []string) (int, error) {
dockerClient, err := d.factory.GetForSession(instance.SessionId)
if err != nil {
return -1, err
}
return dockerClient.Exec(instance.Name, cmd)
}
func (d *DinD) InstanceResizeTerminal(instance *types.Instance, rows, cols uint) error {
dockerClient, err := d.factory.GetForSession(instance.SessionId)
if err != nil {

View File

@@ -17,6 +17,7 @@ func OutOfCapacity(e error) bool {
type InstanceProvisionerApi interface {
InstanceNew(session *types.Session, conf types.InstanceConfig) (*types.Instance, error)
InstanceDelete(session *types.Session, instance *types.Instance) error
InstanceExec(instance *types.Instance, cmd []string) (int, error)
InstanceResizeTerminal(instance *types.Instance, cols, rows uint) error
InstanceGetTerminal(instance *types.Instance) (net.Conn, error)

View File

@@ -1,6 +1,8 @@
package provisioner
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
@@ -121,6 +123,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)
}