Skip to content

Commit

Permalink
implement exitCode
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceralon committed Sep 27, 2021
1 parent 912e9f1 commit c8c8fdb
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 49 deletions.
6 changes: 3 additions & 3 deletions cluster/calcium/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo
Detach: false,
}

execID, stdout, stderr, inStream, err := workload.Engine.Execute(ctx, opts.WorkloadID, execConfig)
result, stdout, stderr, inStream, err := workload.Engine.Execute(ctx, opts.WorkloadID, execConfig)
if err != nil {
logger.Errorf(ctx, "[ExecuteWorkload] Failed to attach execID: %+v", err)
return
Expand All @@ -52,7 +52,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo
splitFunc, split := bufio.ScanLines, byte('\n')
if opts.OpenStdin {
processVirtualizationInStream(ctx, inStream, inCh, func(height, width uint) error {
return workload.Engine.ExecResize(ctx, execID, height, width)
return workload.Engine.ExecResize(ctx, opts.WorkloadID, result, height, width)
})
splitFunc, split = bufio.ScanBytes, byte(0)
}
Expand All @@ -61,7 +61,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo
ch <- &types.AttachWorkloadMessage{WorkloadID: opts.WorkloadID, Data: m.Data, StdStreamType: m.StdStreamType}
}

execCode, err := workload.Engine.ExecExitCode(ctx, execID)
execCode, err := workload.Engine.ExecExitCode(ctx, opts.WorkloadID, result)
if err != nil {
logger.Errorf(ctx, "[ExecuteWorkload] Failed to get exitcode: %+v", err)
return
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func execuateInside(ctx context.Context, client engine.API, ID, cmd, user string
AttachStdout: true,
}
b := []byte{}
execID, stdout, stderr, _, err := client.Execute(ctx, ID, execConfig)
result, stdout, stderr, _, err := client.Execute(ctx, ID, execConfig)
if err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -46,7 +46,7 @@ func execuateInside(ctx context.Context, client engine.API, ID, cmd, user string
b = append(b, m.Data...)
}

exitCode, err := client.ExecExitCode(ctx, execID)
exitCode, err := client.ExecExitCode(ctx, ID, result)
if err != nil {
return b, errors.WithStack(err)
}
Expand Down
8 changes: 4 additions & 4 deletions engine/docker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func (e *Engine) execAttach(ctx context.Context, execID string, tty bool) (io.Re
}

// Execute executes a workload
func (e *Engine) Execute(ctx context.Context, target string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
if execID, err = e.execCreate(ctx, target, config); err != nil {
func (e *Engine) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
if execID, err = e.execCreate(ctx, ID, config); err != nil {
return
}

Expand Down Expand Up @@ -79,7 +79,7 @@ func (e *Engine) demultiplexStdStream(ctx context.Context, stdStream io.Reader)
}

// ExecExitCode get exec return code
func (e *Engine) ExecExitCode(ctx context.Context, execID string) (int, error) {
func (e *Engine) ExecExitCode(ctx context.Context, ID, execID string) (int, error) {
r, err := e.client.ContainerExecInspect(ctx, execID)
if err != nil {
return -1, err
Expand All @@ -88,7 +88,7 @@ func (e *Engine) ExecExitCode(ctx context.Context, execID string) (int, error) {
}

// ExecResize resize exec tty
func (e *Engine) ExecResize(ctx context.Context, execID string, height, width uint) (err error) {
func (e *Engine) ExecResize(ctx context.Context, ID, execID string, height, width uint) error {
opts := dockertypes.ResizeOptions{
Height: height,
Width: width,
Expand Down
6 changes: 3 additions & 3 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
type API interface {
Info(ctx context.Context) (*enginetypes.Info, error)

Execute(ctx context.Context, target string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, _ error)
ExecResize(ctx context.Context, execID string, height, width uint) (err error)
ExecExitCode(ctx context.Context, execID string) (int, error)
Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (result string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error)
ExecResize(ctx context.Context, ID, result string, height, width uint) (err error)
ExecExitCode(ctx context.Context, ID, result string) (int, error)

NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error)
NetworkDisconnect(ctx context.Context, network, target string, force bool) error
Expand Down
40 changes: 20 additions & 20 deletions engine/mocks/API.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions engine/systemd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func (e *Engine) Execute(ctx context.Context, target string, config *enginetypes
}

// ExecResize resize the terminal size
func (e *Engine) ExecResize(ctx context.Context, execID string, height, width uint) (err error) {
func (e *Engine) ExecResize(ctx context.Context, ID, result string, height, width uint) (err error) {
err = types.ErrEngineNotImplemented
return
}

// ExecExitCode fetches exceuction exit code
func (e *Engine) ExecExitCode(ctx context.Context, execID string) (execCode int, err error) {
func (e *Engine) ExecExitCode(ctx context.Context, ID, pid string) (execCode int, err error) {
err = types.ErrEngineNotImplemented
return
}
31 changes: 19 additions & 12 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -73,30 +74,36 @@ func (v *Virt) Info(ctx context.Context) (*enginetypes.Info, error) {
}

// Execute executes a command in vm
func (v *Virt) Execute(ctx context.Context, target string, config *enginetypes.ExecConfig) (execID string, stdout io.ReadCloser, stderr io.ReadCloser, inputStream io.WriteCloser, err error) {
func (v *Virt) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (pid string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
if config.Tty {
flags := virttypes.AttachGuestFlags{Safe: true, Force: true}
stream, err := v.client.AttachGuest(ctx, target, config.Cmd, flags)
stream, err := v.client.AttachGuest(ctx, ID, config.Cmd, flags)
if err != nil {
return "", nil, nil, nil, err
}
return target, ioutil.NopCloser(stream), nil, stream, nil
return "", ioutil.NopCloser(stream), nil, stream, nil

}

msg, err := v.client.ExecuteGuest(ctx, target, config.Cmd)
return target, ioutil.NopCloser(bytes.NewReader(msg.Data)), nil, nil, err

msg, err := v.client.ExecuteGuest(ctx, ID, config.Cmd)
return strconv.Itoa(msg.Pid), ioutil.NopCloser(bytes.NewReader(msg.Data)), nil, nil, err
}

// ExecExitCode gets return code of a specific execution.
func (v *Virt) ExecExitCode(ctx context.Context, execID string) (code int, err error) {
return 0, nil
// ExecExitCode get return code of a specific execution.
func (v *Virt) ExecExitCode(ctx context.Context, ID, pid string) (code int, err error) {
iPid, err := strconv.Atoi(pid)
if err != nil {
return -1, err
}
code, err = v.client.ExecExitCode(ctx, ID, iPid)
if err != nil {
return -1, err
}
return
}

// ExecResize resize exec tty
func (v *Virt) ExecResize(ctx context.Context, execID string, height, width uint) (err error) {
return v.client.ResizeConsoleWindow(ctx, execID, height, width)
func (v *Virt) ExecResize(ctx context.Context, ID, pid string, height, width uint) (err error) {
return v.client.ResizeConsoleWindow(ctx, ID, height, width)
}

// NetworkConnect connects to a network.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/opencontainers/runc v1.0.0-rc95 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/projecteru2/libyavirt v0.0.0-20210920022816-abc7aa0cf6ae
github.com/projecteru2/libyavirt v0.0.0-20210927061827-61332cb449e6
github.com/prometheus/client_golang v1.11.0
github.com/sanity-io/litter v1.5.1
github.com/sirupsen/logrus v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/projecteru2/libyavirt v0.0.0-20210920022816-abc7aa0cf6ae h1:Pohd/mJQaWHnwa88GIzGl63DkvGrt5AzHEQu6ymJw+w=
github.com/projecteru2/libyavirt v0.0.0-20210920022816-abc7aa0cf6ae/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE=
github.com/projecteru2/libyavirt v0.0.0-20210927061827-61332cb449e6 h1:URFUsdkCfuOdI30j+r6/rOUl7OKkAwL0AdjDrlfKhWs=
github.com/projecteru2/libyavirt v0.0.0-20210927061827-61332cb449e6/go.mod h1:FOc+hWBMLsMrmx5p3/moizKeSomedZPNwB6LhS+kEnE=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
Expand Down

0 comments on commit c8c8fdb

Please sign in to comment.