Update runhcs-shim to use go-bindings

Signed-off-by: Justin Terry (VM) <juterry@microsoft.com>
This commit is contained in:
Justin Terry (VM)
2018-09-19 14:35:09 -07:00
parent 16b42fce94
commit 7768ab1b5e
21 changed files with 384 additions and 186 deletions

View File

@@ -0,0 +1,10 @@
package runhcs
import (
"context"
)
// CreateScratch creates a scratch vhdx at 'destpath' that is ext4 formatted.
func (r *Runhcs) CreateScratch(context context.Context, destpath string) error {
return r.runOrError(r.command(context, "create-scratch", "--destpath", destpath))
}

View File

@@ -0,0 +1,28 @@
package runhcs
import (
"context"
"encoding/json"
irunhcs "github.com/Microsoft/hcsshim/internal/runhcs"
)
// ContainerState is the representation of the containers state at the moment of
// query.
type ContainerState = irunhcs.ContainerState
// List containers started by runhcs.
//
// Note: This is specific to the Runhcs.Root namespace provided in the global
// settings.
func (r *Runhcs) List(context context.Context) ([]*ContainerState, error) {
data, err := cmdOutput(r.command(context, "list", "--format=json"), false)
if err != nil {
return nil, err
}
var out []*ContainerState
if err := json.Unmarshal(data, &out); err != nil {
return nil, err
}
return out, nil
}

View File

@@ -0,0 +1,10 @@
package runhcs
import (
"context"
)
// Pause suspends all processes inside the container.
func (r *Runhcs) Pause(context context.Context, id string) error {
return r.runOrError(r.command(context, "pause", id))
}

View File

@@ -0,0 +1,20 @@
package runhcs
import (
"context"
"encoding/json"
"fmt"
)
// Ps displays the processes running inside a container.
func (r *Runhcs) Ps(context context.Context, id string) ([]int, error) {
data, err := cmdOutput(r.command(context, "ps", "--format=json", id), true)
if err != nil {
return nil, fmt.Errorf("%s: %s", err, data)
}
var out []int
if err := json.Unmarshal(data, &out); err != nil {
return nil, err
}
return out, nil
}

View File

@@ -0,0 +1,33 @@
package runhcs
import (
"context"
"strconv"
)
// ResizeTTYOpts is set of options that can be used with the ResizeTTY command.
type ResizeTTYOpts struct {
// Pid is the process pid (defaults to init pid).
Pid *int
}
func (opt *ResizeTTYOpts) args() ([]string, error) {
var out []string
if opt.Pid != nil {
out = append(out, "--pid", strconv.Itoa(*opt.Pid))
}
return out, nil
}
// ResizeTTY updates the terminal size for a container process.
func (r *Runhcs) ResizeTTY(context context.Context, id string, width, height uint16, opts *ResizeTTYOpts) error {
args := []string{"resize-tty"}
if opts != nil {
oargs, err := opts.args()
if err != nil {
return err
}
args = append(args, oargs...)
}
return r.runOrError(r.command(context, append(args, id, strconv.FormatUint(uint64(width), 10), strconv.FormatUint(uint64(height), 10))...))
}

View File

@@ -0,0 +1,10 @@
package runhcs
import (
"context"
)
// Resume resumes all processes that have been previously paused.
func (r *Runhcs) Resume(context context.Context, id string) error {
return r.runOrError(r.command(context, "resume", id))
}

View File

@@ -0,0 +1,20 @@
package runhcs
import (
"context"
"encoding/json"
"fmt"
)
// State outputs the state of a container.
func (r *Runhcs) State(context context.Context, id string) (*ContainerState, error) {
data, err := cmdOutput(r.command(context, "state", id), true)
if err != nil {
return nil, fmt.Errorf("%s: %s", err, data)
}
var out ContainerState
if err := json.Unmarshal(data, &out); err != nil {
return nil, err
}
return &out, nil
}