2018-09-05 14:54:38 -04:00
|
|
|
package hcs
|
|
|
|
|
|
|
|
import (
|
2020-01-06 12:15:00 -05:00
|
|
|
"context"
|
2018-09-05 14:54:38 -04:00
|
|
|
"encoding/json"
|
2020-01-06 12:15:00 -05:00
|
|
|
"errors"
|
2018-09-05 14:54:38 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2020-01-06 12:15:00 -05:00
|
|
|
"strings"
|
2018-09-05 14:54:38 -04:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
"github.com/Microsoft/hcsshim/internal/cow"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/log"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/oc"
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/schema1"
|
2020-01-06 12:15:00 -05:00
|
|
|
hcsschema "github.com/Microsoft/hcsshim/internal/schema2"
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/timeout"
|
2020-01-06 12:15:00 -05:00
|
|
|
"github.com/Microsoft/hcsshim/internal/vmcompute"
|
|
|
|
"go.opencensus.io/trace"
|
2018-09-05 14:54:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// currentContainerStarts is used to limit the number of concurrent container
|
|
|
|
// starts.
|
|
|
|
var currentContainerStarts containerStarts
|
|
|
|
|
|
|
|
type containerStarts struct {
|
|
|
|
maxParallel int
|
|
|
|
inProgress int
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
mpsS := os.Getenv("HCSSHIM_MAX_PARALLEL_START")
|
|
|
|
if len(mpsS) > 0 {
|
|
|
|
mpsI, err := strconv.Atoi(mpsS)
|
|
|
|
if err != nil || mpsI < 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentContainerStarts.maxParallel = mpsI
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type System struct {
|
|
|
|
handleLock sync.RWMutex
|
2020-01-06 12:15:00 -05:00
|
|
|
handle vmcompute.HcsSystem
|
2018-09-05 14:54:38 -04:00
|
|
|
id string
|
|
|
|
callbackNumber uintptr
|
|
|
|
|
2019-04-03 15:06:12 -04:00
|
|
|
closedWaitOnce sync.Once
|
|
|
|
waitBlock chan struct{}
|
|
|
|
waitError error
|
2020-01-06 12:15:00 -05:00
|
|
|
exitError error
|
|
|
|
|
|
|
|
os, typ string
|
2019-02-28 13:16:30 -05:00
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
|
2019-02-28 13:16:30 -05:00
|
|
|
func newSystem(id string) *System {
|
|
|
|
return &System{
|
2020-01-06 12:15:00 -05:00
|
|
|
id: id,
|
2019-04-03 15:06:12 -04:00
|
|
|
waitBlock: make(chan struct{}),
|
2019-02-28 13:16:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateComputeSystem creates a new compute system with the given configuration but does not start it.
|
2020-01-06 12:15:00 -05:00
|
|
|
func CreateComputeSystem(ctx context.Context, id string, hcsDocumentInterface interface{}) (_ *System, err error) {
|
2019-02-28 13:16:30 -05:00
|
|
|
operation := "hcsshim::CreateComputeSystem"
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// hcsCreateComputeSystemContext is an async operation. Start the outer span
|
|
|
|
// here to measure the full create time.
|
|
|
|
ctx, span := trace.StartSpan(ctx, operation)
|
|
|
|
defer span.End()
|
|
|
|
defer func() { oc.SetSpanStatus(span, err) }()
|
|
|
|
span.AddAttributes(trace.StringAttribute("cid", id))
|
|
|
|
|
2019-02-28 13:16:30 -05:00
|
|
|
computeSystem := newSystem(id)
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
hcsDocumentB, err := json.Marshal(hcsDocumentInterface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hcsDocument := string(hcsDocumentB)
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
var (
|
2019-02-28 13:16:30 -05:00
|
|
|
identity syscall.Handle
|
2020-01-06 12:15:00 -05:00
|
|
|
resultJSON string
|
2019-02-28 13:16:30 -05:00
|
|
|
createError error
|
2018-09-05 14:54:38 -04:00
|
|
|
)
|
2020-01-06 12:15:00 -05:00
|
|
|
computeSystem.handle, resultJSON, createError = vmcompute.HcsCreateComputeSystem(ctx, id, hcsDocument, identity)
|
2018-09-05 14:54:38 -04:00
|
|
|
if createError == nil || IsPending(createError) {
|
2020-01-06 12:15:00 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
computeSystem.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err = computeSystem.registerCallback(ctx); err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
// Terminate the compute system if it still exists. We're okay to
|
|
|
|
// ignore a failure here.
|
2020-01-06 12:15:00 -05:00
|
|
|
computeSystem.Terminate(ctx)
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
events, err := processAsyncHcsResult(ctx, createError, resultJSON, computeSystem.callbackNumber, hcsNotificationSystemCreateCompleted, &timeout.SystemCreate)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrTimeout {
|
|
|
|
// Terminate the compute system if it still exists. We're okay to
|
|
|
|
// ignore a failure here.
|
2020-01-06 12:15:00 -05:00
|
|
|
computeSystem.Terminate(ctx)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
return nil, makeSystemError(computeSystem, operation, hcsDocument, err, events)
|
|
|
|
}
|
2019-04-03 15:06:12 -04:00
|
|
|
go computeSystem.waitBackground()
|
2020-01-06 12:15:00 -05:00
|
|
|
if err = computeSystem.getCachedProperties(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
return computeSystem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenComputeSystem opens an existing compute system by ID.
|
2020-01-06 12:15:00 -05:00
|
|
|
func OpenComputeSystem(ctx context.Context, id string) (*System, error) {
|
2019-02-28 13:16:30 -05:00
|
|
|
operation := "hcsshim::OpenComputeSystem"
|
|
|
|
|
|
|
|
computeSystem := newSystem(id)
|
2020-01-06 12:15:00 -05:00
|
|
|
handle, resultJSON, err := vmcompute.HcsOpenComputeSystem(ctx, id)
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, events)
|
|
|
|
}
|
|
|
|
computeSystem.handle = handle
|
2020-01-06 12:15:00 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
computeSystem.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err = computeSystem.registerCallback(ctx); err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
|
|
|
}
|
2019-04-03 15:06:12 -04:00
|
|
|
go computeSystem.waitBackground()
|
2020-01-06 12:15:00 -05:00
|
|
|
if err = computeSystem.getCachedProperties(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
return computeSystem, nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) getCachedProperties(ctx context.Context) error {
|
|
|
|
props, err := computeSystem.Properties(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
computeSystem.typ = strings.ToLower(props.SystemType)
|
|
|
|
computeSystem.os = strings.ToLower(props.RuntimeOSType)
|
|
|
|
if computeSystem.os == "" && computeSystem.typ == "container" {
|
|
|
|
// Pre-RS5 HCS did not return the OS, but it only supported containers
|
|
|
|
// that ran Windows.
|
|
|
|
computeSystem.os = "windows"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// OS returns the operating system of the compute system, "linux" or "windows".
|
|
|
|
func (computeSystem *System) OS() string {
|
|
|
|
return computeSystem.os
|
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// IsOCI returns whether processes in the compute system should be created via
|
|
|
|
// OCI.
|
|
|
|
func (computeSystem *System) IsOCI() bool {
|
|
|
|
return computeSystem.os == "linux" && computeSystem.typ == "container"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetComputeSystems gets a list of the compute systems on the system that match the query
|
|
|
|
func GetComputeSystems(ctx context.Context, q schema1.ComputeSystemQuery) ([]schema1.ContainerProperties, error) {
|
|
|
|
operation := "hcsshim::GetComputeSystems"
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
queryb, err := json.Marshal(q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
computeSystemsJSON, resultJSON, err := vmcompute.HcsEnumerateComputeSystems(ctx, string(queryb))
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, &HcsError{Op: operation, Err: err, Events: events}
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
if computeSystemsJSON == "" {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, ErrUnexpectedValue
|
|
|
|
}
|
|
|
|
computeSystems := []schema1.ContainerProperties{}
|
2020-01-06 12:15:00 -05:00
|
|
|
if err = json.Unmarshal([]byte(computeSystemsJSON), &computeSystems); err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return computeSystems, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start synchronously starts the computeSystem.
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) Start(ctx context.Context) (err error) {
|
|
|
|
operation := "hcsshim::System::Start"
|
|
|
|
|
|
|
|
// hcsStartComputeSystemContext is an async operation. Start the outer span
|
|
|
|
// here to measure the full start time.
|
|
|
|
ctx, span := trace.StartSpan(ctx, operation)
|
|
|
|
defer span.End()
|
|
|
|
defer func() { oc.SetSpanStatus(span, err) }()
|
|
|
|
span.AddAttributes(trace.StringAttribute("cid", computeSystem.id))
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", ErrAlreadyClosed, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a very simple backoff-retry loop to limit the number
|
|
|
|
// of parallel container starts if environment variable
|
|
|
|
// HCSSHIM_MAX_PARALLEL_START is set to a positive integer.
|
|
|
|
// It should generally only be used as a workaround to various
|
|
|
|
// platform issues that exist between RS1 and RS4 as of Aug 2018
|
|
|
|
if currentContainerStarts.maxParallel > 0 {
|
|
|
|
for {
|
|
|
|
currentContainerStarts.Lock()
|
|
|
|
if currentContainerStarts.inProgress < currentContainerStarts.maxParallel {
|
|
|
|
currentContainerStarts.inProgress++
|
|
|
|
currentContainerStarts.Unlock()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if currentContainerStarts.inProgress == currentContainerStarts.maxParallel {
|
|
|
|
currentContainerStarts.Unlock()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure we decrement the count when we are done.
|
|
|
|
defer func() {
|
|
|
|
currentContainerStarts.Lock()
|
|
|
|
currentContainerStarts.inProgress--
|
|
|
|
currentContainerStarts.Unlock()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
resultJSON, err := vmcompute.HcsStartComputeSystem(ctx, computeSystem.handle, "")
|
|
|
|
events, err := processAsyncHcsResult(ctx, err, resultJSON, computeSystem.callbackNumber, hcsNotificationSystemStartCompleted, &timeout.SystemStart)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns the compute system's identifier.
|
|
|
|
func (computeSystem *System) ID() string {
|
|
|
|
return computeSystem.id
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// Shutdown requests a compute system shutdown.
|
|
|
|
func (computeSystem *System) Shutdown(ctx context.Context) error {
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::Shutdown"
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
resultJSON, err := vmcompute.HcsShutdownComputeSystem(ctx, computeSystem.handle, "")
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
|
|
|
switch err {
|
|
|
|
case nil, ErrVmcomputeAlreadyStopped, ErrComputeSystemDoesNotExist, ErrVmcomputeOperationPending:
|
|
|
|
default:
|
|
|
|
return makeSystemError(computeSystem, operation, "", err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// Terminate requests a compute system terminate.
|
|
|
|
func (computeSystem *System) Terminate(ctx context.Context) error {
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::Terminate"
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
resultJSON, err := vmcompute.HcsTerminateComputeSystem(ctx, computeSystem.handle, "")
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
|
|
|
switch err {
|
|
|
|
case nil, ErrVmcomputeAlreadyStopped, ErrComputeSystemDoesNotExist, ErrVmcomputeOperationPending:
|
|
|
|
default:
|
|
|
|
return makeSystemError(computeSystem, operation, "", err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-03 15:06:12 -04:00
|
|
|
// waitBackground waits for the compute system exit notification. Once received
|
2020-01-06 12:15:00 -05:00
|
|
|
// sets `computeSystem.waitError` (if any) and unblocks all `Wait` calls.
|
2019-04-03 15:06:12 -04:00
|
|
|
//
|
2020-01-06 12:15:00 -05:00
|
|
|
// This MUST be called exactly once per `computeSystem.handle` but `Wait` is
|
|
|
|
// safe to call multiple times.
|
2019-04-03 15:06:12 -04:00
|
|
|
func (computeSystem *System) waitBackground() {
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::waitBackground"
|
|
|
|
ctx, span := trace.StartSpan(context.Background(), operation)
|
|
|
|
defer span.End()
|
|
|
|
span.AddAttributes(trace.StringAttribute("cid", computeSystem.id))
|
|
|
|
|
|
|
|
err := waitForNotification(ctx, computeSystem.callbackNumber, hcsNotificationSystemExited, nil)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
log.G(ctx).Debug("system exited")
|
|
|
|
case ErrVmcomputeUnexpectedExit:
|
|
|
|
log.G(ctx).Debug("unexpected system exit")
|
|
|
|
computeSystem.exitError = makeSystemError(computeSystem, operation, "", err, nil)
|
|
|
|
err = nil
|
|
|
|
default:
|
|
|
|
err = makeSystemError(computeSystem, operation, "", err, nil)
|
|
|
|
}
|
2019-04-03 15:06:12 -04:00
|
|
|
computeSystem.closedWaitOnce.Do(func() {
|
2020-01-06 12:15:00 -05:00
|
|
|
computeSystem.waitError = err
|
2019-04-03 15:06:12 -04:00
|
|
|
close(computeSystem.waitBlock)
|
|
|
|
})
|
2020-01-06 12:15:00 -05:00
|
|
|
oc.SetSpanStatus(span, err)
|
2019-04-03 15:06:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait synchronously waits for the compute system to shutdown or terminate. If
|
|
|
|
// the compute system has already exited returns the previous error (if any).
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) Wait() error {
|
2019-04-03 15:06:12 -04:00
|
|
|
<-computeSystem.waitBlock
|
2020-01-06 12:15:00 -05:00
|
|
|
return computeSystem.waitError
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// ExitError returns an error describing the reason the compute system terminated.
|
|
|
|
func (computeSystem *System) ExitError() error {
|
2019-04-03 15:06:12 -04:00
|
|
|
select {
|
|
|
|
case <-computeSystem.waitBlock:
|
|
|
|
if computeSystem.waitError != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return computeSystem.waitError
|
2019-04-03 15:06:12 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
return computeSystem.exitError
|
|
|
|
default:
|
|
|
|
return errors.New("container not exited")
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// Properties returns the requested container properties targeting a V1 schema container.
|
|
|
|
func (computeSystem *System) Properties(ctx context.Context, types ...schema1.PropertyType) (*schema1.ContainerProperties, error) {
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::Properties"
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2019-04-24 14:14:51 -04:00
|
|
|
queryBytes, err := json.Marshal(schema1.PropertyQuery{PropertyTypes: types})
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
propertiesJSON, resultJSON, err := vmcompute.HcsGetComputeSystemProperties(ctx, computeSystem.handle, string(queryBytes))
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
if propertiesJSON == "" {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, ErrUnexpectedValue
|
|
|
|
}
|
|
|
|
properties := &schema1.ContainerProperties{}
|
2020-01-06 12:15:00 -05:00
|
|
|
if err := json.Unmarshal([]byte(propertiesJSON), properties); err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
return properties, nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// PropertiesV2 returns the requested container properties targeting a V2 schema container.
|
|
|
|
func (computeSystem *System) PropertiesV2(ctx context.Context, types ...hcsschema.PropertyType) (*hcsschema.Properties, error) {
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::PropertiesV2"
|
|
|
|
|
|
|
|
queryBytes, err := json.Marshal(hcsschema.PropertyQuery{PropertyTypes: types})
|
|
|
|
if err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
propertiesJSON, resultJSON, err := vmcompute.HcsGetComputeSystemProperties(ctx, computeSystem.handle, string(queryBytes))
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, events)
|
|
|
|
}
|
|
|
|
|
|
|
|
if propertiesJSON == "" {
|
|
|
|
return nil, ErrUnexpectedValue
|
|
|
|
}
|
|
|
|
properties := &hcsschema.Properties{}
|
|
|
|
if err := json.Unmarshal([]byte(propertiesJSON), properties); err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
return properties, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pause pauses the execution of the computeSystem. This feature is not enabled in TP5.
|
|
|
|
func (computeSystem *System) Pause(ctx context.Context) (err error) {
|
|
|
|
operation := "hcsshim::System::Pause"
|
|
|
|
|
|
|
|
// hcsPauseComputeSystemContext is an async peration. Start the outer span
|
|
|
|
// here to measure the full pause time.
|
|
|
|
ctx, span := trace.StartSpan(ctx, operation)
|
|
|
|
defer span.End()
|
|
|
|
defer func() { oc.SetSpanStatus(span, err) }()
|
|
|
|
span.AddAttributes(trace.StringAttribute("cid", computeSystem.id))
|
|
|
|
|
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", ErrAlreadyClosed, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
resultJSON, err := vmcompute.HcsPauseComputeSystem(ctx, computeSystem.handle, "")
|
|
|
|
events, err := processAsyncHcsResult(ctx, err, resultJSON, computeSystem.callbackNumber, hcsNotificationSystemPauseCompleted, &timeout.SystemPause)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume resumes the execution of the computeSystem. This feature is not enabled in TP5.
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) Resume(ctx context.Context) (err error) {
|
|
|
|
operation := "hcsshim::System::Resume"
|
|
|
|
|
|
|
|
// hcsResumeComputeSystemContext is an async operation. Start the outer span
|
|
|
|
// here to measure the full restore time.
|
|
|
|
ctx, span := trace.StartSpan(ctx, operation)
|
|
|
|
defer span.End()
|
|
|
|
defer func() { oc.SetSpanStatus(span, err) }()
|
|
|
|
span.AddAttributes(trace.StringAttribute("cid", computeSystem.id))
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", ErrAlreadyClosed, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
resultJSON, err := vmcompute.HcsResumeComputeSystem(ctx, computeSystem.handle, "")
|
|
|
|
events, err := processAsyncHcsResult(ctx, err, resultJSON, computeSystem.callbackNumber, hcsNotificationSystemResumeCompleted, &timeout.SystemResume)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) createProcess(ctx context.Context, operation string, c interface{}) (*Process, *vmcompute.HcsProcessInformation, error) {
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, nil, makeSystemError(computeSystem, operation, "", ErrAlreadyClosed, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
configurationb, err := json.Marshal(c)
|
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, nil, makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
configuration := string(configurationb)
|
2020-01-06 12:15:00 -05:00
|
|
|
processInfo, processHandle, resultJSON, err := vmcompute.HcsCreateProcess(ctx, computeSystem.handle, configuration)
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, makeSystemError(computeSystem, operation, configuration, err, events)
|
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
log.G(ctx).WithField("pid", processInfo.ProcessId).Debug("created process pid")
|
|
|
|
return newProcess(processHandle, int(processInfo.ProcessId), computeSystem), &processInfo, nil
|
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
// CreateProcess launches a new process within the computeSystem.
|
|
|
|
func (computeSystem *System) CreateProcess(ctx context.Context, c interface{}) (cow.Process, error) {
|
|
|
|
operation := "hcsshim::System::CreateProcess"
|
|
|
|
process, processInfo, err := computeSystem.createProcess(ctx, operation, c)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, err
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
process.Close()
|
|
|
|
}
|
|
|
|
}()
|
2018-09-05 14:54:38 -04:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
pipes, err := makeOpenFiles([]syscall.Handle{processInfo.StdInput, processInfo.StdOutput, processInfo.StdError})
|
|
|
|
if err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
process.stdin = pipes[0]
|
|
|
|
process.stdout = pipes[1]
|
|
|
|
process.stderr = pipes[2]
|
|
|
|
process.hasCachedStdio = true
|
2018-09-05 14:54:38 -04:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
if err = process.registerCallback(ctx); err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
2019-04-03 15:06:12 -04:00
|
|
|
go process.waitBackground()
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
return process, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenProcess gets an interface to an existing process within the computeSystem.
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) OpenProcess(ctx context.Context, pid int) (*Process, error) {
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::OpenProcess"
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, makeSystemError(computeSystem, operation, "", ErrAlreadyClosed, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
processHandle, resultJSON, err := vmcompute.HcsOpenProcess(ctx, computeSystem.handle, uint32(pid))
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2019-02-28 13:16:30 -05:00
|
|
|
process := newProcess(processHandle, pid, computeSystem)
|
2020-01-06 12:15:00 -05:00
|
|
|
if err = process.registerCallback(ctx); err != nil {
|
|
|
|
return nil, makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
2019-04-03 15:06:12 -04:00
|
|
|
go process.waitBackground()
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
return process, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleans up any state associated with the compute system but does not terminate or wait for it.
|
2019-02-28 13:16:30 -05:00
|
|
|
func (computeSystem *System) Close() (err error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::Close"
|
|
|
|
ctx, span := trace.StartSpan(context.Background(), operation)
|
|
|
|
defer span.End()
|
|
|
|
defer func() { oc.SetSpanStatus(span, err) }()
|
|
|
|
span.AddAttributes(trace.StringAttribute("cid", computeSystem.id))
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.Lock()
|
|
|
|
defer computeSystem.handleLock.Unlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Don't double free this
|
|
|
|
if computeSystem.handle == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
if err = computeSystem.unregisterCallback(ctx); err != nil {
|
|
|
|
return makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
err = vmcompute.HcsCloseComputeSystem(ctx, computeSystem.handle)
|
2018-09-26 06:54:23 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", err, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
computeSystem.handle = 0
|
2019-04-03 15:06:12 -04:00
|
|
|
computeSystem.closedWaitOnce.Do(func() {
|
2020-01-06 12:15:00 -05:00
|
|
|
computeSystem.waitError = ErrAlreadyClosed
|
2019-04-03 15:06:12 -04:00
|
|
|
close(computeSystem.waitBlock)
|
|
|
|
})
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) registerCallback(ctx context.Context) error {
|
|
|
|
callbackContext := ¬ifcationWatcherContext{
|
2019-04-24 14:14:51 -04:00
|
|
|
channels: newSystemChannels(),
|
|
|
|
systemID: computeSystem.id,
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
callbackMapLock.Lock()
|
|
|
|
callbackNumber := nextCallback
|
|
|
|
nextCallback++
|
2020-01-06 12:15:00 -05:00
|
|
|
callbackMap[callbackNumber] = callbackContext
|
2018-09-05 14:54:38 -04:00
|
|
|
callbackMapLock.Unlock()
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
callbackHandle, err := vmcompute.HcsRegisterComputeSystemCallback(ctx, computeSystem.handle, notificationWatcherCallback, callbackNumber)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
callbackContext.handle = callbackHandle
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.callbackNumber = callbackNumber
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) unregisterCallback(ctx context.Context) error {
|
2018-09-05 14:54:38 -04:00
|
|
|
callbackNumber := computeSystem.callbackNumber
|
|
|
|
|
|
|
|
callbackMapLock.RLock()
|
2020-01-06 12:15:00 -05:00
|
|
|
callbackContext := callbackMap[callbackNumber]
|
2018-09-05 14:54:38 -04:00
|
|
|
callbackMapLock.RUnlock()
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
if callbackContext == nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
handle := callbackContext.handle
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
if handle == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// hcsUnregisterComputeSystemCallback has its own syncronization
|
|
|
|
// to wait for all callbacks to complete. We must NOT hold the callbackMapLock.
|
2020-01-06 12:15:00 -05:00
|
|
|
err := vmcompute.HcsUnregisterComputeSystemCallback(ctx, handle)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
closeChannels(callbackContext.channels)
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
callbackMapLock.Lock()
|
2019-04-03 15:06:12 -04:00
|
|
|
delete(callbackMap, callbackNumber)
|
2018-09-05 14:54:38 -04:00
|
|
|
callbackMapLock.Unlock()
|
|
|
|
|
|
|
|
handle = 0
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:16:30 -05:00
|
|
|
// Modify the System by sending a request to HCS
|
2020-01-06 12:15:00 -05:00
|
|
|
func (computeSystem *System) Modify(ctx context.Context, config interface{}) error {
|
2018-09-05 14:54:38 -04:00
|
|
|
computeSystem.handleLock.RLock()
|
|
|
|
defer computeSystem.handleLock.RUnlock()
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
operation := "hcsshim::System::Modify"
|
2018-09-05 14:54:38 -04:00
|
|
|
|
|
|
|
if computeSystem.handle == 0 {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, "", ErrAlreadyClosed, nil)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
requestBytes, err := json.Marshal(config)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
requestJSON := string(requestBytes)
|
|
|
|
resultJSON, err := vmcompute.HcsModifyComputeSystem(ctx, computeSystem.handle, requestJSON)
|
|
|
|
events := processHcsResult(ctx, resultJSON)
|
2018-09-05 14:54:38 -04:00
|
|
|
if err != nil {
|
2020-01-06 12:15:00 -05:00
|
|
|
return makeSystemError(computeSystem, operation, requestJSON, err, events)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil
|
|
|
|
}
|