2018-03-19 18:57:30 -04:00
|
|
|
package hcsshim
|
|
|
|
|
|
|
|
import (
|
2020-01-06 12:15:00 -05:00
|
|
|
"context"
|
2018-03-19 18:57:30 -04:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-01-06 12:15:00 -05:00
|
|
|
"sync"
|
2018-03-19 18:57:30 -04:00
|
|
|
"time"
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcs"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/mergemaps"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/schema1"
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ContainerProperties holds the properties for a container and the processes running in that container
|
2018-09-05 14:54:38 -04:00
|
|
|
type ContainerProperties = schema1.ContainerProperties
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// MemoryStats holds the memory statistics for a container
|
2018-09-05 14:54:38 -04:00
|
|
|
type MemoryStats = schema1.MemoryStats
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ProcessorStats holds the processor statistics for a container
|
2018-09-05 14:54:38 -04:00
|
|
|
type ProcessorStats = schema1.ProcessorStats
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// StorageStats holds the storage statistics for a container
|
2018-09-05 14:54:38 -04:00
|
|
|
type StorageStats = schema1.StorageStats
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// NetworkStats holds the network statistics for a container
|
2018-09-05 14:54:38 -04:00
|
|
|
type NetworkStats = schema1.NetworkStats
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// Statistics is the structure returned by a statistics call on a container
|
2018-09-05 14:54:38 -04:00
|
|
|
type Statistics = schema1.Statistics
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// ProcessList is the structure of an item returned by a ProcessList call on a container
|
2018-09-05 14:54:38 -04:00
|
|
|
type ProcessListItem = schema1.ProcessListItem
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// MappedVirtualDiskController is the structure of an item returned by a MappedVirtualDiskList call on a container
|
2018-09-05 14:54:38 -04:00
|
|
|
type MappedVirtualDiskController = schema1.MappedVirtualDiskController
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// Type of Request Support in ModifySystem
|
2018-09-05 14:54:38 -04:00
|
|
|
type RequestType = schema1.RequestType
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// Type of Resource Support in ModifySystem
|
2018-09-05 14:54:38 -04:00
|
|
|
type ResourceType = schema1.ResourceType
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// RequestType const
|
|
|
|
const (
|
2018-09-05 14:54:38 -04:00
|
|
|
Add = schema1.Add
|
|
|
|
Remove = schema1.Remove
|
|
|
|
Network = schema1.Network
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceModificationRequestResponse is the structure used to send request to the container to modify the system
|
|
|
|
// Supported resource types are Network and Request Types are Add/Remove
|
2018-09-05 14:54:38 -04:00
|
|
|
type ResourceModificationRequestResponse = schema1.ResourceModificationRequestResponse
|
|
|
|
|
|
|
|
type container struct {
|
2020-01-06 12:15:00 -05:00
|
|
|
system *hcs.System
|
|
|
|
waitOnce sync.Once
|
|
|
|
waitErr error
|
|
|
|
waitCh chan struct{}
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// createComputeSystemAdditionalJSON is read from the environment at initialisation
|
2018-03-19 18:57:30 -04:00
|
|
|
// time. It allows an environment variable to define additional JSON which
|
2018-09-05 14:54:38 -04:00
|
|
|
// is merged in the CreateComputeSystem call to HCS.
|
|
|
|
var createContainerAdditionalJSON []byte
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
func init() {
|
2018-09-05 14:54:38 -04:00
|
|
|
createContainerAdditionalJSON = ([]byte)(os.Getenv("HCSSHIM_CREATECONTAINER_ADDITIONALJSON"))
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateContainer creates a new container with the given configuration but does not start it.
|
|
|
|
func CreateContainer(id string, c *ContainerConfig) (Container, error) {
|
2018-09-05 14:54:38 -04:00
|
|
|
fullConfig, err := mergemaps.MergeJSON(c, createContainerAdditionalJSON)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, fmt.Errorf("failed to merge additional JSON '%s': %s", createContainerAdditionalJSON, err)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2020-01-06 12:15:00 -05:00
|
|
|
system, err := hcs.CreateComputeSystem(context.Background(), id, fullConfig)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
return &container{system: system}, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenContainer opens an existing container by ID.
|
|
|
|
func OpenContainer(id string) (Container, error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
system, err := hcs.OpenComputeSystem(context.Background(), id)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
return &container{system: system}, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetContainers gets a list of the containers on the system that match the query
|
|
|
|
func GetContainers(q ComputeSystemQuery) ([]ContainerProperties, error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
return hcs.GetComputeSystems(context.Background(), q)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start synchronously starts the container.
|
|
|
|
func (container *container) Start() error {
|
2020-01-06 12:15:00 -05:00
|
|
|
return convertSystemError(container.system.Start(context.Background()), container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Shutdown requests a container shutdown, but it may not actually be shutdown until Wait() succeeds.
|
2018-03-19 18:57:30 -04:00
|
|
|
func (container *container) Shutdown() error {
|
2020-01-06 12:15:00 -05:00
|
|
|
err := container.system.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return convertSystemError(err, container)
|
|
|
|
}
|
|
|
|
return &ContainerError{Container: container, Err: ErrVmcomputeOperationPending, Operation: "hcsshim::ComputeSystem::Shutdown"}
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Terminate requests a container terminate, but it may not actually be terminated until Wait() succeeds.
|
2018-03-19 18:57:30 -04:00
|
|
|
func (container *container) Terminate() error {
|
2020-01-06 12:15:00 -05:00
|
|
|
err := container.system.Terminate(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return convertSystemError(err, container)
|
|
|
|
}
|
|
|
|
return &ContainerError{Container: container, Err: ErrVmcomputeOperationPending, Operation: "hcsshim::ComputeSystem::Terminate"}
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Waits synchronously waits for the container to shutdown or terminate.
|
2018-03-19 18:57:30 -04:00
|
|
|
func (container *container) Wait() error {
|
2020-01-06 12:15:00 -05:00
|
|
|
err := container.system.Wait()
|
|
|
|
if err == nil {
|
|
|
|
err = container.system.ExitError()
|
|
|
|
}
|
|
|
|
return convertSystemError(err, container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// WaitTimeout synchronously waits for the container to terminate or the duration to elapse. It
|
|
|
|
// returns false if timeout occurs.
|
2020-01-06 12:15:00 -05:00
|
|
|
func (container *container) WaitTimeout(timeout time.Duration) error {
|
|
|
|
container.waitOnce.Do(func() {
|
|
|
|
container.waitCh = make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
container.waitErr = container.Wait()
|
|
|
|
close(container.waitCh)
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
t := time.NewTimer(timeout)
|
|
|
|
defer t.Stop()
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
return &ContainerError{Container: container, Err: ErrTimeout, Operation: "hcsshim::ComputeSystem::Wait"}
|
|
|
|
case <-container.waitCh:
|
|
|
|
return container.waitErr
|
|
|
|
}
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Pause pauses the execution of a container.
|
|
|
|
func (container *container) Pause() error {
|
2020-01-06 12:15:00 -05:00
|
|
|
return convertSystemError(container.system.Pause(context.Background()), container)
|
2018-09-05 14:54:38 -04:00
|
|
|
}
|
2018-03-19 18:57:30 -04:00
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Resume resumes the execution of a container.
|
|
|
|
func (container *container) Resume() error {
|
2020-01-06 12:15:00 -05:00
|
|
|
return convertSystemError(container.system.Resume(context.Background()), container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasPendingUpdates returns true if the container has updates pending to install
|
|
|
|
func (container *container) HasPendingUpdates() (bool, error) {
|
2018-09-05 14:54:38 -04:00
|
|
|
return false, nil
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Statistics returns statistics for the container. This is a legacy v1 call
|
2018-03-19 18:57:30 -04:00
|
|
|
func (container *container) Statistics() (Statistics, error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
properties, err := container.system.Properties(context.Background(), schema1.PropertyTypeStatistics)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return Statistics{}, convertSystemError(err, container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return properties.Statistics, nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// ProcessList returns an array of ProcessListItems for the container. This is a legacy v1 call
|
2018-03-19 18:57:30 -04:00
|
|
|
func (container *container) ProcessList() ([]ProcessListItem, error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
properties, err := container.system.Properties(context.Background(), schema1.PropertyTypeProcessList)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, convertSystemError(err, container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return properties.ProcessList, nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// This is a legacy v1 call
|
2018-03-19 18:57:30 -04:00
|
|
|
func (container *container) MappedVirtualDisks() (map[int]MappedVirtualDiskController, error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
properties, err := container.system.Properties(context.Background(), schema1.PropertyTypeMappedVirtualDisk)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, convertSystemError(err, container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return properties.MappedVirtualDiskControllers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateProcess launches a new process within the container.
|
|
|
|
func (container *container) CreateProcess(c *ProcessConfig) (Process, error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
p, err := container.system.CreateProcess(context.Background(), c)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, convertSystemError(err, container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
return &process{p: p.(*hcs.Process)}, nil
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenProcess gets an interface to an existing process within the container.
|
|
|
|
func (container *container) OpenProcess(pid int) (Process, error) {
|
2020-01-06 12:15:00 -05:00
|
|
|
p, err := container.system.OpenProcess(context.Background(), pid)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, convertSystemError(err, container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2020-01-06 12:15:00 -05:00
|
|
|
return &process{p: p}, nil
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleans up any state associated with the container but does not terminate or wait for it.
|
|
|
|
func (container *container) Close() error {
|
2018-09-05 14:54:38 -04:00
|
|
|
return convertSystemError(container.system.Close(), container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
// Modify the System
|
2018-03-19 18:57:30 -04:00
|
|
|
func (container *container) Modify(config *ResourceModificationRequestResponse) error {
|
2020-01-06 12:15:00 -05:00
|
|
|
return convertSystemError(container.system.Modify(context.Background(), config), container)
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|