2017-02-11 00:48:15 +08:00
|
|
|
package buildah
|
|
|
|
|
|
|
|
import (
|
2018-04-12 22:20:36 +08:00
|
|
|
"context"
|
2017-02-11 00:48:15 +08:00
|
|
|
"encoding/json"
|
2018-03-29 02:14:57 +08:00
|
|
|
"fmt"
|
2017-05-09 23:56:44 +08:00
|
|
|
"io"
|
2017-02-24 06:56:48 +08:00
|
|
|
"io/ioutil"
|
2017-03-16 04:44:52 +08:00
|
|
|
"os"
|
2017-02-11 00:48:15 +08:00
|
|
|
"path/filepath"
|
|
|
|
|
2017-07-21 08:02:11 +08:00
|
|
|
"github.com/containers/image/types"
|
2017-05-17 23:53:28 +08:00
|
|
|
"github.com/containers/storage"
|
2017-02-24 06:56:48 +08:00
|
|
|
"github.com/containers/storage/pkg/ioutils"
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
"github.com/opencontainers/image-spec/specs-go/v1"
|
2017-06-03 00:17:27 +08:00
|
|
|
"github.com/pkg/errors"
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
"github.com/projectatomic/buildah/docker"
|
2018-06-20 03:51:19 +08:00
|
|
|
"github.com/projectatomic/buildah/util"
|
2017-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-02-11 03:45:06 +08:00
|
|
|
// Package is the name of this package, used in help output and to
|
|
|
|
// identify working containers.
|
2017-04-19 03:52:53 +08:00
|
|
|
Package = "buildah"
|
2018-02-14 04:29:38 +08:00
|
|
|
// Version for the Package. Bump version in contrib/rpm/buildah.spec
|
|
|
|
// too.
|
2018-08-04 16:00:35 +08:00
|
|
|
Version = "1.4-dev"
|
2017-06-15 01:31:43 +08:00
|
|
|
// The value we use to identify what type of information, currently a
|
|
|
|
// serialized Builder structure, we are using as per-container state.
|
|
|
|
// This should only be changed when we make incompatible changes to
|
|
|
|
// that data structure, as it's used to distinguish containers which
|
|
|
|
// are "ours" from ones that aren't.
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
containerType = Package + " 0.0.1"
|
2017-06-15 01:31:43 +08:00
|
|
|
// The file in the per-container directory which we use to store our
|
|
|
|
// per-container state. If it isn't there, then the container isn't
|
|
|
|
// one of our build containers.
|
|
|
|
stateFile = Package + ".json"
|
2017-02-11 00:48:15 +08:00
|
|
|
)
|
|
|
|
|
2018-03-29 02:14:57 +08:00
|
|
|
// PullPolicy takes the value PullIfMissing, PullAlways, or PullNever.
|
|
|
|
type PullPolicy int
|
|
|
|
|
2017-04-11 02:15:30 +08:00
|
|
|
const (
|
|
|
|
// PullIfMissing is one of the values that BuilderOptions.PullPolicy
|
|
|
|
// can take, signalling that the source image should be pulled from a
|
|
|
|
// registry if a local copy of it is not already present.
|
2018-03-29 02:14:57 +08:00
|
|
|
PullIfMissing PullPolicy = iota
|
2017-04-11 02:15:30 +08:00
|
|
|
// PullAlways is one of the values that BuilderOptions.PullPolicy can
|
|
|
|
// take, signalling that a fresh, possibly updated, copy of the image
|
|
|
|
// should be pulled from a registry before the build proceeds.
|
|
|
|
PullAlways
|
|
|
|
// PullNever is one of the values that BuilderOptions.PullPolicy can
|
|
|
|
// take, signalling that the source image should not be pulled from a
|
|
|
|
// registry if a local copy of it is not already present.
|
|
|
|
PullNever
|
|
|
|
)
|
|
|
|
|
2018-03-29 02:14:57 +08:00
|
|
|
// String converts a PullPolicy into a string.
|
|
|
|
func (p PullPolicy) String() string {
|
|
|
|
switch p {
|
|
|
|
case PullIfMissing:
|
|
|
|
return "PullIfMissing"
|
|
|
|
case PullAlways:
|
|
|
|
return "PullAlways"
|
|
|
|
case PullNever:
|
|
|
|
return "PullNever"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unrecognized policy %d", p)
|
|
|
|
}
|
|
|
|
|
2018-04-14 06:20:25 +08:00
|
|
|
// NetworkConfigurationPolicy takes the value NetworkDefault, NetworkDisabled,
|
|
|
|
// or NetworkEnabled.
|
|
|
|
type NetworkConfigurationPolicy int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NetworkDefault is one of the values that BuilderOptions.ConfigureNetwork
|
|
|
|
// can take, signalling that the default behavior should be used.
|
|
|
|
NetworkDefault NetworkConfigurationPolicy = iota
|
|
|
|
// NetworkDisabled is one of the values that BuilderOptions.ConfigureNetwork
|
|
|
|
// can take, signalling that network interfaces should NOT be configured for
|
|
|
|
// newly-created network namespaces.
|
|
|
|
NetworkDisabled
|
|
|
|
// NetworkEnabled is one of the values that BuilderOptions.ConfigureNetwork
|
|
|
|
// can take, signalling that network interfaces should be configured for
|
|
|
|
// newly-created network namespaces.
|
|
|
|
NetworkEnabled
|
|
|
|
)
|
|
|
|
|
|
|
|
// String formats a NetworkConfigurationPolicy as a string.
|
|
|
|
func (p NetworkConfigurationPolicy) String() string {
|
|
|
|
switch p {
|
|
|
|
case NetworkDefault:
|
|
|
|
return "NetworkDefault"
|
|
|
|
case NetworkDisabled:
|
|
|
|
return "NetworkDisabled"
|
|
|
|
case NetworkEnabled:
|
|
|
|
return "NetworkEnabled"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("unknown NetworkConfigurationPolicy %d", p)
|
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// Builder objects are used to represent containers which are being used to
|
|
|
|
// build images. They also carry potential updates which will be applied to
|
|
|
|
// the image's configuration when the container's contents are used to build an
|
|
|
|
// image.
|
2017-02-11 00:48:15 +08:00
|
|
|
type Builder struct {
|
|
|
|
store storage.Store
|
|
|
|
|
2018-06-25 20:53:47 +08:00
|
|
|
// Args define variables that users can pass at build-time to the builder
|
|
|
|
Args map[string]string
|
2017-02-11 03:45:06 +08:00
|
|
|
// Type is used to help identify a build container's metadata. It
|
|
|
|
// should not be modified.
|
|
|
|
Type string `json:"type"`
|
|
|
|
// FromImage is the name of the source image which was used to create
|
|
|
|
// the container, if one was used. It should not be modified.
|
|
|
|
FromImage string `json:"image,omitempty"`
|
2017-03-16 05:19:29 +08:00
|
|
|
// FromImageID is the ID of the source image which was used to create
|
|
|
|
// the container, if one was used. It should not be modified.
|
|
|
|
FromImageID string `json:"image-id"`
|
2017-02-11 03:45:06 +08:00
|
|
|
// Config is the source image's configuration. It should not be
|
|
|
|
// modified.
|
|
|
|
Config []byte `json:"config,omitempty"`
|
|
|
|
// Manifest is the source image's manifest. It should not be modified.
|
|
|
|
Manifest []byte `json:"manifest,omitempty"`
|
|
|
|
|
|
|
|
// Container is the name of the build container. It should not be modified.
|
|
|
|
Container string `json:"container-name,omitempty"`
|
|
|
|
// ContainerID is the ID of the build container. It should not be modified.
|
|
|
|
ContainerID string `json:"container-id,omitempty"`
|
|
|
|
// MountPoint is the last location where the container's root
|
|
|
|
// filesystem was mounted. It should not be modified.
|
|
|
|
MountPoint string `json:"mountpoint,omitempty"`
|
2017-10-20 05:47:15 +08:00
|
|
|
// ProcessLabel is the SELinux process label associated with the container
|
|
|
|
ProcessLabel string `json:"process-label,omitempty"`
|
|
|
|
// MountLabel is the SELinux mount label associated with the container
|
|
|
|
MountLabel string `json:"mount-label,omitempty"`
|
2017-02-11 00:48:15 +08:00
|
|
|
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
// ImageAnnotations is a set of key-value pairs which is stored in the
|
2017-02-11 03:45:06 +08:00
|
|
|
// image's manifest.
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
ImageAnnotations map[string]string `json:"annotations,omitempty"`
|
|
|
|
// ImageCreatedBy is a description of how this container was built.
|
|
|
|
ImageCreatedBy string `json:"created-by,omitempty"`
|
2018-04-27 22:59:03 +08:00
|
|
|
// ImageHistoryComment is a description of how our added layers were built.
|
|
|
|
ImageHistoryComment string `json:"history-comment,omitempty"`
|
2017-02-11 00:48:15 +08:00
|
|
|
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
// Image metadata and runtime settings, in multiple formats.
|
2017-05-17 05:07:31 +08:00
|
|
|
OCIv1 v1.Image `json:"ociv1,omitempty"`
|
|
|
|
Docker docker.V2Image `json:"docker,omitempty"`
|
2018-03-07 07:13:24 +08:00
|
|
|
// DefaultMountsFilePath is the file path holding the mounts to be mounted in "host-path:container-path" format.
|
2017-11-08 06:44:24 +08:00
|
|
|
DefaultMountsFilePath string `json:"defaultMountsFilePath,omitempty"`
|
2018-03-07 07:13:24 +08:00
|
|
|
|
2018-05-12 01:00:14 +08:00
|
|
|
// Isolation controls how we handle "RUN" statements and the Run() method.
|
|
|
|
Isolation Isolation
|
2018-03-08 07:11:43 +08:00
|
|
|
// NamespaceOptions controls how we set up the namespaces for processes that we run in the container.
|
|
|
|
NamespaceOptions NamespaceOptions
|
2018-04-14 06:20:25 +08:00
|
|
|
// ConfigureNetwork controls whether or not network interfaces and
|
|
|
|
// routing are configured for a new network namespace (i.e., when not
|
|
|
|
// joining another's namespace and not just using the host's
|
|
|
|
// namespace), effectively deciding whether or not the process has a
|
|
|
|
// usable network.
|
|
|
|
ConfigureNetwork NetworkConfigurationPolicy
|
|
|
|
// CNIPluginPath is the location of CNI plugin helpers, if they should be
|
|
|
|
// run from a location other than the default location.
|
|
|
|
CNIPluginPath string
|
|
|
|
// CNIConfigDir is the location of CNI configuration files, if the files in
|
|
|
|
// the default configuration directory shouldn't be used.
|
|
|
|
CNIConfigDir string
|
2018-03-08 07:11:43 +08:00
|
|
|
// ID mapping options to use when running processes in the container with non-host user namespaces.
|
|
|
|
IDMappingOptions IDMappingOptions
|
2018-06-05 05:36:26 +08:00
|
|
|
// AddCapabilities is a list of capabilities to add to the default set when running
|
|
|
|
// commands in the container.
|
|
|
|
AddCapabilities []string
|
|
|
|
// DropCapabilities is a list of capabilities to remove from the default set,
|
|
|
|
// after processing the AddCapabilities set, when running commands in the container.
|
|
|
|
// If a capability appears in both lists, it will be dropped.
|
|
|
|
DropCapabilities []string
|
2018-03-08 07:11:43 +08:00
|
|
|
|
2018-03-07 07:13:24 +08:00
|
|
|
CommonBuildOpts *CommonBuildOptions
|
2018-06-09 00:55:46 +08:00
|
|
|
// TopLayer is the top layer of the image
|
|
|
|
TopLayer string
|
2018-08-24 00:55:16 +08:00
|
|
|
// Format for the build Image
|
|
|
|
Format string
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:12:16 +08:00
|
|
|
// BuilderInfo are used as objects to display container information
|
|
|
|
type BuilderInfo struct {
|
|
|
|
Type string
|
|
|
|
FromImage string
|
|
|
|
FromImageID string
|
|
|
|
Config string
|
|
|
|
Manifest string
|
|
|
|
Container string
|
|
|
|
ContainerID string
|
|
|
|
MountPoint string
|
|
|
|
ProcessLabel string
|
|
|
|
MountLabel string
|
|
|
|
ImageAnnotations map[string]string
|
|
|
|
ImageCreatedBy string
|
|
|
|
OCIv1 v1.Image
|
|
|
|
Docker docker.V2Image
|
|
|
|
DefaultMountsFilePath string
|
2018-05-12 01:00:14 +08:00
|
|
|
Isolation string
|
2018-03-08 07:11:43 +08:00
|
|
|
NamespaceOptions NamespaceOptions
|
2018-04-14 06:20:25 +08:00
|
|
|
ConfigureNetwork string
|
|
|
|
CNIPluginPath string
|
|
|
|
CNIConfigDir string
|
2018-03-08 07:11:43 +08:00
|
|
|
IDMappingOptions IDMappingOptions
|
2018-06-20 03:51:19 +08:00
|
|
|
DefaultCapabilities []string
|
|
|
|
AddCapabilities []string
|
|
|
|
DropCapabilities []string
|
2017-12-18 22:12:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBuildInfo gets a pointer to a Builder object and returns a BuilderInfo object from it.
|
|
|
|
// This is used in the inspect command to display Manifest and Config as string and not []byte.
|
|
|
|
func GetBuildInfo(b *Builder) BuilderInfo {
|
|
|
|
return BuilderInfo{
|
|
|
|
Type: b.Type,
|
|
|
|
FromImage: b.FromImage,
|
|
|
|
FromImageID: b.FromImageID,
|
|
|
|
Config: string(b.Config),
|
|
|
|
Manifest: string(b.Manifest),
|
|
|
|
Container: b.Container,
|
|
|
|
ContainerID: b.ContainerID,
|
|
|
|
MountPoint: b.MountPoint,
|
|
|
|
ProcessLabel: b.ProcessLabel,
|
|
|
|
ImageAnnotations: b.ImageAnnotations,
|
|
|
|
ImageCreatedBy: b.ImageCreatedBy,
|
|
|
|
OCIv1: b.OCIv1,
|
|
|
|
Docker: b.Docker,
|
|
|
|
DefaultMountsFilePath: b.DefaultMountsFilePath,
|
2018-05-12 01:00:14 +08:00
|
|
|
Isolation: b.Isolation.String(),
|
2018-03-08 07:11:43 +08:00
|
|
|
NamespaceOptions: b.NamespaceOptions,
|
2018-04-14 06:20:25 +08:00
|
|
|
ConfigureNetwork: fmt.Sprintf("%v", b.ConfigureNetwork),
|
|
|
|
CNIPluginPath: b.CNIPluginPath,
|
|
|
|
CNIConfigDir: b.CNIConfigDir,
|
2018-03-08 07:11:43 +08:00
|
|
|
IDMappingOptions: b.IDMappingOptions,
|
2018-06-20 03:51:19 +08:00
|
|
|
DefaultCapabilities: append([]string{}, util.DefaultCapabilities...),
|
|
|
|
AddCapabilities: append([]string{}, b.AddCapabilities...),
|
|
|
|
DropCapabilities: append([]string{}, b.DropCapabilities...),
|
2017-12-18 22:12:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-07 07:13:24 +08:00
|
|
|
// CommonBuildOptions are resources that can be defined by flags for both buildah from and build-using-dockerfile
|
2018-02-14 03:58:56 +08:00
|
|
|
type CommonBuildOptions struct {
|
2018-06-16 00:28:03 +08:00
|
|
|
// AddHost is the list of hostnames to add to the build container's /etc/hosts.
|
2018-02-14 03:58:56 +08:00
|
|
|
AddHost []string
|
2018-03-07 07:13:24 +08:00
|
|
|
// CgroupParent is the path to cgroups under which the cgroup for the container will be created.
|
2018-02-14 03:58:56 +08:00
|
|
|
CgroupParent string
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUPeriod limits the CPU CFS (Completely Fair Scheduler) period
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUPeriod uint64
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUQuota limits the CPU CFS (Completely Fair Scheduler) quota
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUQuota int64
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUShares (relative weight
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUShares uint64
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUSetCPUs in which to allow execution (0-3, 0,1)
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUSetCPUs string
|
2018-03-07 07:13:24 +08:00
|
|
|
// CPUSetMems memory nodes (MEMs) in which to allow execution (0-3, 0,1). Only effective on NUMA systems.
|
2018-02-14 03:58:56 +08:00
|
|
|
CPUSetMems string
|
2018-03-07 07:13:24 +08:00
|
|
|
// Memory is the upper limit (in bytes) on how much memory running containers can use.
|
2018-02-14 03:58:56 +08:00
|
|
|
Memory int64
|
2018-03-07 07:13:24 +08:00
|
|
|
// MemorySwap limits the amount of memory and swap together.
|
2018-02-14 03:58:56 +08:00
|
|
|
MemorySwap int64
|
2018-03-07 07:13:24 +08:00
|
|
|
// LabelOpts is the a slice of fields of an SELinux context, given in "field:pair" format, or "disable".
|
|
|
|
// Recognized field names are "role", "type", and "level".
|
|
|
|
LabelOpts []string
|
|
|
|
// SeccompProfilePath is the pathname of a seccomp profile.
|
2018-02-14 03:58:56 +08:00
|
|
|
SeccompProfilePath string
|
2018-03-07 07:13:24 +08:00
|
|
|
// ApparmorProfile is the name of an apparmor profile.
|
|
|
|
ApparmorProfile string
|
|
|
|
// ShmSize is the "size" value to use when mounting an shmfs on the container's /dev/shm directory.
|
2018-02-23 01:41:22 +08:00
|
|
|
ShmSize string
|
2018-03-07 07:13:24 +08:00
|
|
|
// Ulimit specifies resource limit options, in the form type:softlimit[:hardlimit].
|
|
|
|
// These types are recognized:
|
|
|
|
// "core": maximimum core dump size (ulimit -c)
|
|
|
|
// "cpu": maximum CPU time (ulimit -t)
|
|
|
|
// "data": maximum size of a process's data segment (ulimit -d)
|
|
|
|
// "fsize": maximum size of new files (ulimit -f)
|
|
|
|
// "locks": maximum number of file locks (ulimit -x)
|
|
|
|
// "memlock": maximum amount of locked memory (ulimit -l)
|
|
|
|
// "msgqueue": maximum amount of data in message queues (ulimit -q)
|
|
|
|
// "nice": niceness adjustment (nice -n, ulimit -e)
|
|
|
|
// "nofile": maximum number of open files (ulimit -n)
|
|
|
|
// "nproc": maximum number of processes (ulimit -u)
|
|
|
|
// "rss": maximum size of a process's (ulimit -m)
|
|
|
|
// "rtprio": maximum real-time scheduling priority (ulimit -r)
|
|
|
|
// "rttime": maximum amount of real-time execution between blocking syscalls
|
|
|
|
// "sigpending": maximum number of pending signals (ulimit -i)
|
|
|
|
// "stack": maximum stack size (ulimit -s)
|
2018-02-14 03:58:56 +08:00
|
|
|
Ulimit []string
|
2018-03-07 07:13:24 +08:00
|
|
|
// Volumes to bind mount into the container
|
2018-02-23 01:41:22 +08:00
|
|
|
Volumes []string
|
2018-02-14 03:58:56 +08:00
|
|
|
}
|
|
|
|
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
// BuilderOptions are used to initialize a new Builder.
|
2017-02-11 00:48:15 +08:00
|
|
|
type BuilderOptions struct {
|
2018-06-25 20:53:47 +08:00
|
|
|
// Args define variables that users can pass at build-time to the builder
|
|
|
|
Args map[string]string
|
2017-02-11 03:45:06 +08:00
|
|
|
// FromImage is the name of the image which should be used as the
|
|
|
|
// starting point for the container. It can be set to an empty value
|
|
|
|
// or "scratch" to indicate that the container should not be based on
|
|
|
|
// an image.
|
|
|
|
FromImage string
|
|
|
|
// Container is a desired name for the build container.
|
|
|
|
Container string
|
2017-04-11 02:15:30 +08:00
|
|
|
// PullPolicy decides whether or not we should pull the image that
|
|
|
|
// we're using as a base image. It should be PullIfMissing,
|
|
|
|
// PullAlways, or PullNever.
|
2018-03-29 02:14:57 +08:00
|
|
|
PullPolicy PullPolicy
|
2017-02-11 03:45:06 +08:00
|
|
|
// Registry is a value which is prepended to the image's name, if it
|
|
|
|
// needs to be pulled and the image name alone can not be resolved to a
|
2017-07-31 22:44:21 +08:00
|
|
|
// reference to a source image. No separator is implicitly added.
|
2017-02-11 03:45:06 +08:00
|
|
|
Registry string
|
2017-07-31 22:44:21 +08:00
|
|
|
// Transport is a value which is prepended to the image's name, if it
|
|
|
|
// needs to be pulled and the image name alone, or the image name and
|
|
|
|
// the registry together, can not be resolved to a reference to a
|
|
|
|
// source image. No separator is implicitly added.
|
|
|
|
Transport string
|
2017-02-11 03:45:06 +08:00
|
|
|
// Mount signals to NewBuilder() that the container should be mounted
|
|
|
|
// immediately.
|
|
|
|
Mount bool
|
|
|
|
// SignaturePolicyPath specifies an override location for the signature
|
|
|
|
// policy which should be used for verifying the new image as it is
|
|
|
|
// being written. Except in specific circumstances, no value should be
|
|
|
|
// specified, indicating that the shared, system-wide default policy
|
|
|
|
// should be used.
|
2017-02-11 00:48:15 +08:00
|
|
|
SignaturePolicyPath string
|
2017-05-09 23:56:44 +08:00
|
|
|
// ReportWriter is an io.Writer which will be used to log the reading
|
|
|
|
// of the source image from a registry, if we end up pulling the image.
|
|
|
|
ReportWriter io.Writer
|
2017-07-21 08:02:11 +08:00
|
|
|
// github.com/containers/image/types SystemContext to hold credentials
|
|
|
|
// and other authentication/authorization information.
|
|
|
|
SystemContext *types.SystemContext
|
2018-03-07 07:13:24 +08:00
|
|
|
// DefaultMountsFilePath is the file path holding the mounts to be
|
|
|
|
// mounted in "host-path:container-path" format
|
2017-11-08 06:44:24 +08:00
|
|
|
DefaultMountsFilePath string
|
2018-05-12 01:00:14 +08:00
|
|
|
// Isolation controls how we handle "RUN" statements and the Run()
|
|
|
|
// method.
|
|
|
|
Isolation Isolation
|
2018-03-08 07:11:43 +08:00
|
|
|
// NamespaceOptions controls how we set up namespaces for processes that
|
|
|
|
// we might need to run using the container's root filesystem.
|
|
|
|
NamespaceOptions NamespaceOptions
|
2018-04-14 06:20:25 +08:00
|
|
|
// ConfigureNetwork controls whether or not network interfaces and
|
|
|
|
// routing are configured for a new network namespace (i.e., when not
|
|
|
|
// joining another's namespace and not just using the host's
|
|
|
|
// namespace), effectively deciding whether or not the process has a
|
|
|
|
// usable network.
|
|
|
|
ConfigureNetwork NetworkConfigurationPolicy
|
|
|
|
// CNIPluginPath is the location of CNI plugin helpers, if they should be
|
|
|
|
// run from a location other than the default location.
|
|
|
|
CNIPluginPath string
|
|
|
|
// CNIConfigDir is the location of CNI configuration files, if the files in
|
|
|
|
// the default configuration directory shouldn't be used.
|
|
|
|
CNIConfigDir string
|
2018-03-08 07:11:43 +08:00
|
|
|
// ID mapping options to use if we're setting up our own user namespace.
|
|
|
|
IDMappingOptions *IDMappingOptions
|
2018-06-05 05:36:26 +08:00
|
|
|
// AddCapabilities is a list of capabilities to add to the default set when
|
|
|
|
// running commands in the container.
|
|
|
|
AddCapabilities []string
|
|
|
|
// DropCapabilities is a list of capabilities to remove from the default set,
|
|
|
|
// after processing the AddCapabilities set, when running commands in the
|
|
|
|
// container. If a capability appears in both lists, it will be dropped.
|
|
|
|
DropCapabilities []string
|
2018-08-09 04:38:53 +08:00
|
|
|
|
2018-03-08 07:11:43 +08:00
|
|
|
CommonBuildOpts *CommonBuildOptions
|
2018-08-24 00:55:16 +08:00
|
|
|
// Format for the container image
|
|
|
|
Format string
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
// ImportOptions are used to initialize a Builder from an existing container
|
|
|
|
// which was created elsewhere.
|
2017-02-25 04:18:35 +08:00
|
|
|
type ImportOptions struct {
|
|
|
|
// Container is the name of the build container.
|
|
|
|
Container string
|
|
|
|
// SignaturePolicyPath specifies an override location for the signature
|
|
|
|
// policy which should be used for verifying the new image as it is
|
|
|
|
// being written. Except in specific circumstances, no value should be
|
|
|
|
// specified, indicating that the shared, system-wide default policy
|
|
|
|
// should be used.
|
|
|
|
SignaturePolicyPath string
|
|
|
|
}
|
|
|
|
|
2017-05-19 05:38:41 +08:00
|
|
|
// ImportFromImageOptions are used to initialize a Builder from an image.
|
|
|
|
type ImportFromImageOptions struct {
|
|
|
|
// Image is the name or ID of the image we'd like to examine.
|
|
|
|
Image string
|
|
|
|
// SignaturePolicyPath specifies an override location for the signature
|
|
|
|
// policy which should be used for verifying the new image as it is
|
|
|
|
// being written. Except in specific circumstances, no value should be
|
|
|
|
// specified, indicating that the shared, system-wide default policy
|
|
|
|
// should be used.
|
|
|
|
SignaturePolicyPath string
|
2017-06-29 05:07:58 +08:00
|
|
|
// github.com/containers/image/types SystemContext to hold information
|
|
|
|
// about which registries we should check for completing image names
|
|
|
|
// that don't include a domain portion.
|
|
|
|
SystemContext *types.SystemContext
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// NewBuilder creates a new build container.
|
2018-04-12 22:20:36 +08:00
|
|
|
func NewBuilder(ctx context.Context, store storage.Store, options BuilderOptions) (*Builder, error) {
|
|
|
|
return newBuilder(ctx, store, options)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
|
2017-02-25 04:18:35 +08:00
|
|
|
// ImportBuilder creates a new build configuration using an already-present
|
|
|
|
// container.
|
2018-04-12 22:20:36 +08:00
|
|
|
func ImportBuilder(ctx context.Context, store storage.Store, options ImportOptions) (*Builder, error) {
|
|
|
|
return importBuilder(ctx, store, options)
|
2017-02-25 04:18:35 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 05:38:41 +08:00
|
|
|
// ImportBuilderFromImage creates a new builder configuration using an image.
|
|
|
|
// The returned object can be modified and examined, but it can not be saved
|
|
|
|
// or committed because it is not associated with a working container.
|
2018-04-12 22:20:36 +08:00
|
|
|
func ImportBuilderFromImage(ctx context.Context, store storage.Store, options ImportFromImageOptions) (*Builder, error) {
|
|
|
|
return importBuilderFromImage(ctx, store, options)
|
2017-05-19 05:38:41 +08:00
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// OpenBuilder loads information about a build container given its name or ID.
|
2017-02-11 00:48:15 +08:00
|
|
|
func OpenBuilder(store storage.Store, container string) (*Builder, error) {
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := store.ContainerDirectory(container)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-24 06:56:48 +08:00
|
|
|
buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile))
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b := &Builder{}
|
2017-04-04 05:44:23 +08:00
|
|
|
err = json.Unmarshal(buildstate, &b)
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-11 03:45:06 +08:00
|
|
|
if b.Type != containerType {
|
2017-06-03 00:17:27 +08:00
|
|
|
return nil, errors.Errorf("container is not a %s container", Package)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
b.store = store
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
b.fixupConfig()
|
2017-02-11 00:48:15 +08:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2017-03-24 01:32:16 +08:00
|
|
|
// OpenBuilderByPath loads information about a build container given a
|
|
|
|
// path to the container's root filesystem
|
2017-02-11 00:48:15 +08:00
|
|
|
func OpenBuilderByPath(store storage.Store, path string) (*Builder, error) {
|
|
|
|
containers, err := store.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
abs, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
builderMatchesPath := func(b *Builder, path string) bool {
|
2017-05-28 18:52:18 +08:00
|
|
|
return (b.MountPoint == path)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|
|
|
|
for _, container := range containers {
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := store.ContainerDirectory(container.ID)
|
2017-03-16 04:42:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile))
|
2017-02-11 00:48:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b := &Builder{}
|
2017-04-04 05:44:23 +08:00
|
|
|
err = json.Unmarshal(buildstate, &b)
|
2017-02-11 03:45:06 +08:00
|
|
|
if err == nil && b.Type == containerType && builderMatchesPath(b, abs) {
|
2017-02-11 00:48:15 +08:00
|
|
|
b.store = store
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
b.fixupConfig()
|
2017-02-11 00:48:15 +08:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, storage.ErrContainerUnknown
|
|
|
|
}
|
|
|
|
|
2017-03-16 04:44:52 +08:00
|
|
|
// OpenAllBuilders loads all containers which have a state file that we use in
|
|
|
|
// their data directory, typically so that they can be listed.
|
|
|
|
func OpenAllBuilders(store storage.Store) (builders []*Builder, err error) {
|
|
|
|
containers, err := store.Containers()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := store.ContainerDirectory(container.ID)
|
2017-03-16 04:44:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buildstate, err := ioutil.ReadFile(filepath.Join(cdir, stateFile))
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
b := &Builder{}
|
2017-04-04 05:44:23 +08:00
|
|
|
err = json.Unmarshal(buildstate, &b)
|
2017-03-16 04:44:52 +08:00
|
|
|
if err == nil && b.Type == containerType {
|
|
|
|
b.store = store
|
Maintain multiple working container configs
Maintain the container configuration in multiple formats in the Buildah
object, initializing one based on the other, depending on which format
the source image used for its configuration.
Replace directly manipulated fields in the Buildah object (Annotations,
CreatedBy, OS, Architecture, Maintainer, User, Workdir, Env, Cmd,
Entrypoint, Expose, Labels, and Volumes) with accessor functions which
update both configurations and which read from whichever one we consider
to be authoritative. Drop Args because we weren't using them.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
Closes: #102
Approved by: rhatdan
2017-05-16 23:08:52 +08:00
|
|
|
b.fixupConfig()
|
2017-03-16 04:44:52 +08:00
|
|
|
builders = append(builders, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builders, nil
|
|
|
|
}
|
|
|
|
|
2017-02-11 03:45:06 +08:00
|
|
|
// Save saves the builder's current state to the build container's metadata.
|
|
|
|
// This should not need to be called directly, as other methods of the Builder
|
|
|
|
// object take care of saving their state.
|
2017-02-11 00:48:15 +08:00
|
|
|
func (b *Builder) Save() error {
|
|
|
|
buildstate, err := json.Marshal(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-17 23:53:28 +08:00
|
|
|
cdir, err := b.store.ContainerDirectory(b.ContainerID)
|
2017-02-24 06:56:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutils.AtomicWriteFile(filepath.Join(cdir, stateFile), buildstate, 0600)
|
2017-02-11 00:48:15 +08:00
|
|
|
}
|