github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/worker/gclient/container.go (about)

     1  package gclient
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/garden"
     9  	"github.com/pf-qiu/concourse/v6/atc/worker/gclient/connection"
    10  )
    11  
    12  //go:generate counterfeiter . Container
    13  type Container interface {
    14  	Handle() string
    15  
    16  	// Stop stops a container.
    17  	//
    18  	// If kill is false, garden stops a container by sending the processes running inside it the SIGTERM signal.
    19  	// It then waits for the processes to terminate before returning a response.
    20  	// If one or more processes do not terminate within 10 seconds,
    21  	// garden sends these processes the SIGKILL signal, killing them ungracefully.
    22  	//
    23  	// If kill is true, garden stops a container by sending the processing running inside it a SIGKILL signal.
    24  	//
    25  	// It is possible to copy files in to and out of a stopped container.
    26  	// It is only when a container is destroyed that its filesystem is cleaned up.
    27  	//
    28  	// Errors:
    29  	// * None.
    30  	Stop(kill bool) error
    31  
    32  	// Returns information about a container.
    33  	Info() (garden.ContainerInfo, error)
    34  
    35  	// StreamIn streams data into a file in a container.
    36  	//
    37  	// Errors:
    38  	// *  TODO.
    39  	StreamIn(spec garden.StreamInSpec) error
    40  
    41  	// StreamOut streams a file out of a container.
    42  	//
    43  	// Errors:
    44  	// * TODO.
    45  	StreamOut(spec garden.StreamOutSpec) (io.ReadCloser, error)
    46  
    47  	// Returns the current bandwidth limits set for the container.
    48  	CurrentBandwidthLimits() (garden.BandwidthLimits, error)
    49  
    50  	// Returns the current CPU limts set for the container.
    51  	CurrentCPULimits() (garden.CPULimits, error)
    52  
    53  	// Returns the current disk limts set for the container.
    54  	CurrentDiskLimits() (garden.DiskLimits, error)
    55  
    56  	// Returns the current memory limts set for the container.
    57  	CurrentMemoryLimits() (garden.MemoryLimits, error)
    58  
    59  	// Map a port on the host to a port in the container so that traffic to the
    60  	// host port is forwarded to the container port. This is deprecated in
    61  	// favour of passing NetIn configuration in the ContainerSpec at creation
    62  	// time.
    63  	//
    64  	// If a host port is not given, a port will be acquired from the server's port
    65  	// pool.
    66  	//
    67  	// If a container port is not given, the port will be the same as the
    68  	// host port.
    69  	//
    70  	// The resulting host and container ports are returned in that order.
    71  	//
    72  	// Errors:
    73  	// * When no port can be acquired from the server's port pool.
    74  	NetIn(hostPort, containerPort uint32) (uint32, uint32, error)
    75  
    76  	// Whitelist outbound network traffic. This is deprecated in favour of passing
    77  	// NetOut configuration in the ContainerSpec at creation time.
    78  	//
    79  	// If the configuration directive deny_networks is not used,
    80  	// all networks are already whitelisted and this command is effectively a no-op.
    81  	//
    82  	// Later NetOut calls take precedence over earlier calls, which is
    83  	// significant only in relation to logging.
    84  	//
    85  	// Errors:
    86  	// * An error is returned if the NetOut call fails.
    87  	NetOut(netOutRule garden.NetOutRule) error
    88  
    89  	// A Bulk call for NetOut. This is deprecated in favour of passing
    90  	// NetOut configuration in the ContainerSpec at creation time.
    91  	//
    92  	// Errors:
    93  	// * An error is returned if any of the NetOut calls fail.
    94  	BulkNetOut(netOutRules []garden.NetOutRule) error
    95  
    96  	// Run a script inside a container.
    97  	//
    98  	// The root user will be mapped to a non-root UID in the host unless the container (not this process) was created with 'privileged' true.
    99  	//
   100  	// Errors:
   101  	// * TODO.
   102  	Run(context.Context, garden.ProcessSpec, garden.ProcessIO) (garden.Process, error)
   103  
   104  	// Attach starts streaming the output back to the client from a specified process.
   105  	//
   106  	// Errors:
   107  	// * processID does not refer to a running process.
   108  	Attach(ctx context.Context, processID string, io garden.ProcessIO) (garden.Process, error)
   109  
   110  	// Metrics returns the current set of metrics for a container
   111  	Metrics() (garden.Metrics, error)
   112  
   113  	// Sets the grace time.
   114  	SetGraceTime(graceTime time.Duration) error
   115  
   116  	// Properties returns the current set of properties
   117  	Properties() (garden.Properties, error)
   118  
   119  	// Property returns the value of the property with the specified name.
   120  	//
   121  	// Errors:
   122  	// * When the property does not exist on the container.
   123  	Property(name string) (string, error)
   124  
   125  	// Set a named property on a container to a specified value.
   126  	//
   127  	// Errors:
   128  	// * None.
   129  	SetProperty(name string, value string) error
   130  
   131  	// Remove a property with the specified name from a container.
   132  	//
   133  	// Errors:
   134  	// * None.
   135  	RemoveProperty(name string) error
   136  }
   137  
   138  type container struct {
   139  	handle string
   140  
   141  	connection connection.Connection
   142  }
   143  
   144  func newContainer(handle string, connection connection.Connection) Container {
   145  	return &container{
   146  		handle:     handle,
   147  		connection: connection,
   148  	}
   149  }
   150  
   151  func (container *container) Handle() string {
   152  	return container.handle
   153  }
   154  
   155  func (container *container) Stop(kill bool) error {
   156  	return container.connection.Stop(container.handle, kill)
   157  }
   158  
   159  func (container *container) Info() (garden.ContainerInfo, error) {
   160  	return container.connection.Info(container.handle)
   161  }
   162  
   163  func (container *container) StreamIn(spec garden.StreamInSpec) error {
   164  	return container.connection.StreamIn(container.handle, spec)
   165  }
   166  
   167  func (container *container) StreamOut(spec garden.StreamOutSpec) (io.ReadCloser, error) {
   168  	return container.connection.StreamOut(container.handle, spec)
   169  }
   170  
   171  func (container *container) CurrentBandwidthLimits() (garden.BandwidthLimits, error) {
   172  	return container.connection.CurrentBandwidthLimits(container.handle)
   173  }
   174  
   175  func (container *container) CurrentCPULimits() (garden.CPULimits, error) {
   176  	return container.connection.CurrentCPULimits(container.handle)
   177  }
   178  
   179  func (container *container) CurrentDiskLimits() (garden.DiskLimits, error) {
   180  	return container.connection.CurrentDiskLimits(container.handle)
   181  }
   182  
   183  func (container *container) CurrentMemoryLimits() (garden.MemoryLimits, error) {
   184  	return container.connection.CurrentMemoryLimits(container.handle)
   185  }
   186  
   187  func (container *container) Run(ctx context.Context, spec garden.ProcessSpec, io garden.ProcessIO) (garden.Process, error) {
   188  	return container.connection.Run(ctx, container.handle, spec, io)
   189  }
   190  
   191  func (container *container) Attach(ctx context.Context, processID string, io garden.ProcessIO) (garden.Process, error) {
   192  	return container.connection.Attach(ctx, container.handle, processID, io)
   193  }
   194  
   195  func (container *container) NetIn(hostPort, containerPort uint32) (uint32, uint32, error) {
   196  	return container.connection.NetIn(container.handle, hostPort, containerPort)
   197  }
   198  
   199  func (container *container) NetOut(netOutRule garden.NetOutRule) error {
   200  	return container.connection.NetOut(container.handle, netOutRule)
   201  }
   202  
   203  func (container *container) BulkNetOut(netOutRules []garden.NetOutRule) error {
   204  	return container.connection.BulkNetOut(container.handle, netOutRules)
   205  }
   206  
   207  func (container *container) Metrics() (garden.Metrics, error) {
   208  	return container.connection.Metrics(container.handle)
   209  }
   210  
   211  func (container *container) SetGraceTime(graceTime time.Duration) error {
   212  	return container.connection.SetGraceTime(container.handle, graceTime)
   213  }
   214  
   215  func (container *container) Properties() (garden.Properties, error) {
   216  	return container.connection.Properties(container.handle)
   217  }
   218  
   219  func (container *container) Property(name string) (string, error) {
   220  	return container.connection.Property(container.handle, name)
   221  }
   222  
   223  func (container *container) SetProperty(name string, value string) error {
   224  	return container.connection.SetProperty(container.handle, name, value)
   225  }
   226  
   227  func (container *container) RemoveProperty(name string) error {
   228  	return container.connection.RemoveProperty(container.handle, name)
   229  }