| 
									
										
										
										
											2017-02-14 05:12:02 +08:00
										 |  |  | package buildah | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-02-14 03:58:56 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2018-03-20 19:31:51 +08:00
										 |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2017-02-14 05:12:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	"github.com/containers/buildah/define" | 
					
						
							| 
									
										
										
										
											2021-10-18 13:51:51 +08:00
										 |  |  | 	"github.com/containers/buildah/internal" | 
					
						
							| 
									
										
										
										
											2021-07-26 14:07:23 +08:00
										 |  |  | 	"github.com/containers/buildah/pkg/sshagent" | 
					
						
							| 
									
										
										
										
											2021-10-18 13:51:51 +08:00
										 |  |  | 	"github.com/containers/image/v5/types" | 
					
						
							| 
									
										
										
										
											2022-10-19 08:13:05 +08:00
										 |  |  | 	"github.com/containers/storage/pkg/lockfile" | 
					
						
							| 
									
										
										
										
											2019-04-26 03:39:49 +08:00
										 |  |  | 	"github.com/opencontainers/runtime-spec/specs-go" | 
					
						
							| 
									
										
										
										
											2021-05-08 01:38:44 +08:00
										 |  |  | 	"github.com/sirupsen/logrus" | 
					
						
							| 
									
										
										
										
											2017-02-14 05:12:02 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2018-03-21 23:19:47 +08:00
										 |  |  | 	// runUsingRuntimeCommand is a command we use as a key for reexec
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	runUsingRuntimeCommand = define.Package + "-oci-runtime" | 
					
						
							| 
									
										
										
										
											2017-02-14 05:12:02 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-29 02:14:57 +08:00
										 |  |  | // TerminalPolicy takes the value DefaultTerminal, WithoutTerminal, or WithTerminal.
 | 
					
						
							|  |  |  | type TerminalPolicy int | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-05 06:36:55 +08:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2017-04-04 01:43:34 +08:00
										 |  |  | 	// DefaultTerminal indicates that this Run invocation should be
 | 
					
						
							|  |  |  | 	// connected to a pseudoterminal if we're connected to a terminal.
 | 
					
						
							| 
									
										
										
										
											2018-03-29 02:14:57 +08:00
										 |  |  | 	DefaultTerminal TerminalPolicy = iota | 
					
						
							| 
									
										
										
										
											2017-04-04 01:43:34 +08:00
										 |  |  | 	// WithoutTerminal indicates that this Run invocation should NOT be
 | 
					
						
							|  |  |  | 	// connected to a pseudoterminal.
 | 
					
						
							| 
									
										
										
										
											2017-04-05 06:36:55 +08:00
										 |  |  | 	WithoutTerminal | 
					
						
							| 
									
										
										
										
											2017-04-04 01:43:34 +08:00
										 |  |  | 	// WithTerminal indicates that this Run invocation should be connected
 | 
					
						
							|  |  |  | 	// to a pseudoterminal.
 | 
					
						
							| 
									
										
										
										
											2017-04-05 06:36:55 +08:00
										 |  |  | 	WithTerminal | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 00:19:56 +08:00
										 |  |  | // String converts a TerminalPolicy into a string.
 | 
					
						
							| 
									
										
										
										
											2018-03-29 02:14:57 +08:00
										 |  |  | func (t TerminalPolicy) String() string { | 
					
						
							|  |  |  | 	switch t { | 
					
						
							|  |  |  | 	case DefaultTerminal: | 
					
						
							|  |  |  | 		return "DefaultTerminal" | 
					
						
							|  |  |  | 	case WithoutTerminal: | 
					
						
							|  |  |  | 		return "WithoutTerminal" | 
					
						
							|  |  |  | 	case WithTerminal: | 
					
						
							|  |  |  | 		return "WithTerminal" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return fmt.Sprintf("unrecognized terminal setting %d", t) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 07:11:43 +08:00
										 |  |  | // NamespaceOption controls how we set up a namespace when launching processes.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | type NamespaceOption = define.NamespaceOption | 
					
						
							| 
									
										
										
										
											2018-03-08 07:11:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // NamespaceOptions provides some helper methods for a slice of NamespaceOption
 | 
					
						
							|  |  |  | // structs.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | type NamespaceOptions = define.NamespaceOptions | 
					
						
							| 
									
										
										
										
											2018-03-08 07:11:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // IDMappingOptions controls how we set up UID/GID mapping when we set up a
 | 
					
						
							|  |  |  | // user namespace.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | type IDMappingOptions = define.IDMappingOptions | 
					
						
							| 
									
										
										
										
											2018-03-08 07:11:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 01:00:14 +08:00
										 |  |  | // Isolation provides a way to specify whether we're supposed to use a proper
 | 
					
						
							|  |  |  | // OCI runtime, or some other method for running commands.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | type Isolation = define.Isolation | 
					
						
							| 
									
										
										
										
											2018-05-12 01:00:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	// IsolationDefault is whatever we think will work best.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	IsolationDefault = define.IsolationDefault | 
					
						
							| 
									
										
										
										
											2018-05-12 01:00:14 +08:00
										 |  |  | 	// IsolationOCI is a proper OCI runtime.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	IsolationOCI = define.IsolationOCI | 
					
						
							| 
									
										
										
										
											2018-05-12 01:08:18 +08:00
										 |  |  | 	// IsolationChroot is a more chroot-like environment: less isolation,
 | 
					
						
							|  |  |  | 	// but with fewer requirements.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	IsolationChroot = define.IsolationChroot | 
					
						
							| 
									
										
										
										
											2018-07-19 02:59:20 +08:00
										 |  |  | 	// IsolationOCIRootless is a proper OCI runtime in rootless mode.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	IsolationOCIRootless = define.IsolationOCIRootless | 
					
						
							| 
									
										
										
										
											2018-05-12 01:00:14 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-14 05:12:02 +08:00
										 |  |  | // RunOptions can be used to alter how a command is run in the container.
 | 
					
						
							|  |  |  | type RunOptions struct { | 
					
						
							| 
									
										
										
										
											2021-05-08 01:38:44 +08:00
										 |  |  | 	// Logger is the logrus logger to write log messages with
 | 
					
						
							|  |  |  | 	Logger *logrus.Logger `json:"-"` | 
					
						
							| 
									
										
										
										
											2017-02-14 05:12:02 +08:00
										 |  |  | 	// Hostname is the hostname we set for the running container.
 | 
					
						
							|  |  |  | 	Hostname string | 
					
						
							| 
									
										
										
										
											2018-07-19 02:59:20 +08:00
										 |  |  | 	// Isolation is either IsolationDefault, IsolationOCI, IsolationChroot, or IsolationOCIRootless.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	Isolation define.Isolation | 
					
						
							| 
									
										
										
										
											2018-05-12 01:08:18 +08:00
										 |  |  | 	// Runtime is the name of the runtime to run.  It should accept the
 | 
					
						
							|  |  |  | 	// same arguments that runc does, and produce similar output.
 | 
					
						
							| 
									
										
										
										
											2017-02-18 02:54:49 +08:00
										 |  |  | 	Runtime string | 
					
						
							|  |  |  | 	// Args adds global arguments for the runtime.
 | 
					
						
							|  |  |  | 	Args []string | 
					
						
							| 
									
										
										
										
											2022-03-08 04:44:14 +08:00
										 |  |  | 	// NoHosts use the images /etc/hosts file
 | 
					
						
							|  |  |  | 	NoHosts bool | 
					
						
							| 
									
										
										
										
											2018-10-06 05:53:23 +08:00
										 |  |  | 	// NoPivot adds the --no-pivot runtime flag.
 | 
					
						
							|  |  |  | 	NoPivot bool | 
					
						
							| 
									
										
										
										
											2017-02-18 02:58:34 +08:00
										 |  |  | 	// Mounts are additional mount points which we want to provide.
 | 
					
						
							|  |  |  | 	Mounts []specs.Mount | 
					
						
							| 
									
										
										
										
											2017-03-28 02:46:35 +08:00
										 |  |  | 	// Env is additional environment variables to set.
 | 
					
						
							|  |  |  | 	Env []string | 
					
						
							|  |  |  | 	// User is the user as whom to run the command.
 | 
					
						
							|  |  |  | 	User string | 
					
						
							|  |  |  | 	// WorkingDir is an override for the working directory.
 | 
					
						
							|  |  |  | 	WorkingDir string | 
					
						
							| 
									
										
										
										
											2021-09-28 19:20:36 +08:00
										 |  |  | 	// ContextDir is used as the root directory for the source location for mounts that are of type "bind".
 | 
					
						
							|  |  |  | 	ContextDir string | 
					
						
							| 
									
										
										
										
											2018-03-16 19:57:36 +08:00
										 |  |  | 	// Shell is default shell to run in a container.
 | 
					
						
							|  |  |  | 	Shell string | 
					
						
							| 
									
										
										
										
											2017-03-28 02:46:35 +08:00
										 |  |  | 	// Cmd is an override for the configured default command.
 | 
					
						
							|  |  |  | 	Cmd []string | 
					
						
							|  |  |  | 	// Entrypoint is an override for the configured entry point.
 | 
					
						
							|  |  |  | 	Entrypoint []string | 
					
						
							| 
									
										
										
										
											2018-03-08 07:11:43 +08:00
										 |  |  | 	// NamespaceOptions controls how we set up the namespaces for the process.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	NamespaceOptions define.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.
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	ConfigureNetwork define.NetworkConfigurationPolicy | 
					
						
							| 
									
										
										
										
											2018-04-14 06:20:25 +08:00
										 |  |  | 	// 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 | 
					
						
							| 
									
										
										
										
											2017-04-05 06:36:55 +08:00
										 |  |  | 	// Terminal provides a way to specify whether or not the command should
 | 
					
						
							|  |  |  | 	// be run with a pseudoterminal.  By default (DefaultTerminal), a
 | 
					
						
							|  |  |  | 	// terminal is used if os.Stdout is connected to a terminal, but that
 | 
					
						
							|  |  |  | 	// decision can be overridden by specifying either WithTerminal or
 | 
					
						
							|  |  |  | 	// WithoutTerminal.
 | 
					
						
							| 
									
										
										
										
											2018-03-29 02:14:57 +08:00
										 |  |  | 	Terminal TerminalPolicy | 
					
						
							| 
									
										
										
										
											2018-06-12 02:15:00 +08:00
										 |  |  | 	// TerminalSize provides a way to set the number of rows and columns in
 | 
					
						
							|  |  |  | 	// a pseudo-terminal, if we create one, and Stdin/Stdout/Stderr aren't
 | 
					
						
							|  |  |  | 	// connected to a terminal.
 | 
					
						
							|  |  |  | 	TerminalSize *specs.Box | 
					
						
							| 
									
										
										
										
											2018-06-06 01:53:39 +08:00
										 |  |  | 	// The stdin/stdout/stderr descriptors to use.  If set to nil, the
 | 
					
						
							|  |  |  | 	// corresponding files in the "os" package are used as defaults.
 | 
					
						
							| 
									
										
										
										
											2018-06-08 02:41:54 +08:00
										 |  |  | 	Stdin  io.Reader `json:"-"` | 
					
						
							|  |  |  | 	Stdout io.Writer `json:"-"` | 
					
						
							|  |  |  | 	Stderr io.Writer `json:"-"` | 
					
						
							| 
									
										
										
										
											2018-01-24 00:17:31 +08:00
										 |  |  | 	// Quiet tells the run to turn off output to stdout.
 | 
					
						
							|  |  |  | 	Quiet bool | 
					
						
							| 
									
										
										
										
											2018-06-05 05:36:26 +08:00
										 |  |  | 	// AddCapabilities is a list of capabilities to add to the default set.
 | 
					
						
							|  |  |  | 	AddCapabilities []string | 
					
						
							|  |  |  | 	// DropCapabilities is a list of capabilities to remove from the default set,
 | 
					
						
							|  |  |  | 	// after processing the AddCapabilities set.  If a capability appears in both
 | 
					
						
							|  |  |  | 	// lists, it will be dropped.
 | 
					
						
							|  |  |  | 	DropCapabilities []string | 
					
						
							| 
									
										
										
										
											2019-09-07 03:07:18 +08:00
										 |  |  | 	// Devices are the additional devices to add to the containers
 | 
					
						
							| 
									
										
										
										
											2021-02-07 06:49:40 +08:00
										 |  |  | 	Devices define.ContainerDevices | 
					
						
							| 
									
										
										
										
											2021-04-17 06:21:31 +08:00
										 |  |  | 	// Secrets are the available secrets to use in a RUN
 | 
					
						
							| 
									
										
										
										
											2021-10-22 04:14:05 +08:00
										 |  |  | 	Secrets map[string]define.Secret | 
					
						
							| 
									
										
										
										
											2021-07-26 14:07:23 +08:00
										 |  |  | 	// SSHSources is the available ssh agents to use in a RUN
 | 
					
						
							|  |  |  | 	SSHSources map[string]*sshagent.Source `json:"-"` | 
					
						
							| 
									
										
										
										
											2021-04-17 06:21:31 +08:00
										 |  |  | 	// RunMounts are mounts for this run. RunMounts for this run
 | 
					
						
							|  |  |  | 	// will not show up in subsequent runs.
 | 
					
						
							|  |  |  | 	RunMounts []string | 
					
						
							| 
									
										
										
										
											2021-10-18 13:51:51 +08:00
										 |  |  | 	// Map of stages and container mountpoint if any from stage executor
 | 
					
						
							|  |  |  | 	StageMountPoints map[string]internal.StageMountDetails | 
					
						
							|  |  |  | 	// External Image mounts to be cleaned up.
 | 
					
						
							|  |  |  | 	// Buildah run --mount could mount image before RUN calls, RUN could cleanup
 | 
					
						
							|  |  |  | 	// them up as well
 | 
					
						
							|  |  |  | 	ExternalImageMounts []string | 
					
						
							|  |  |  | 	// System context of current build
 | 
					
						
							|  |  |  | 	SystemContext *types.SystemContext | 
					
						
							| 
									
										
										
										
											2022-02-14 21:38:10 +08:00
										 |  |  | 	// CgroupManager to use for running OCI containers
 | 
					
						
							|  |  |  | 	CgroupManager string | 
					
						
							| 
									
										
										
										
											2018-03-08 07:11:43 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-26 14:07:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // RunMountArtifacts are the artifacts created when using a run mount.
 | 
					
						
							|  |  |  | type runMountArtifacts struct { | 
					
						
							|  |  |  | 	// RunMountTargets are the run mount targets inside the container
 | 
					
						
							|  |  |  | 	RunMountTargets []string | 
					
						
							| 
									
										
										
										
											2021-10-22 04:14:05 +08:00
										 |  |  | 	// TmpFiles are artifacts that need to be removed outside the container
 | 
					
						
							|  |  |  | 	TmpFiles []string | 
					
						
							| 
									
										
										
										
											2021-10-18 13:51:51 +08:00
										 |  |  | 	// Any external images which were mounted inside container
 | 
					
						
							|  |  |  | 	MountedImages []string | 
					
						
							| 
									
										
										
										
											2021-07-26 14:07:23 +08:00
										 |  |  | 	// Agents are the ssh agents started
 | 
					
						
							|  |  |  | 	Agents []*sshagent.AgentServer | 
					
						
							|  |  |  | 	// SSHAuthSock is the path to the ssh auth sock inside the container
 | 
					
						
							|  |  |  | 	SSHAuthSock string | 
					
						
							| 
									
										
										
										
											2022-10-19 08:13:05 +08:00
										 |  |  | 	// TargetLocks to be unlocked if there are any.
 | 
					
						
							| 
									
										
										
										
											2022-11-22 06:22:17 +08:00
										 |  |  | 	TargetLocks []*lockfile.LockFile | 
					
						
							| 
									
										
										
										
											2021-07-26 14:07:23 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-05-27 10:47:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // RunMountInfo are the available run mounts for this run
 | 
					
						
							|  |  |  | type runMountInfo struct { | 
					
						
							| 
									
										
										
										
											2022-12-07 20:06:12 +08:00
										 |  |  | 	// WorkDir is the current working directory inside the container.
 | 
					
						
							|  |  |  | 	WorkDir string | 
					
						
							| 
									
										
										
										
											2022-05-27 10:47:30 +08:00
										 |  |  | 	// ContextDir is the root directory for the source location for bind mounts.
 | 
					
						
							|  |  |  | 	ContextDir string | 
					
						
							|  |  |  | 	// Secrets are the available secrets to use in a RUN
 | 
					
						
							|  |  |  | 	Secrets map[string]define.Secret | 
					
						
							|  |  |  | 	// SSHSources is the available ssh agents to use in a RUN
 | 
					
						
							|  |  |  | 	SSHSources map[string]*sshagent.Source `json:"-"` | 
					
						
							|  |  |  | 	// Map of stages and container mountpoint if any from stage executor
 | 
					
						
							|  |  |  | 	StageMountPoints map[string]internal.StageMountDetails | 
					
						
							|  |  |  | 	// System context of current build
 | 
					
						
							|  |  |  | 	SystemContext *types.SystemContext | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // IDMaps are the UIDs, GID, and maps for the run
 | 
					
						
							|  |  |  | type IDMaps struct { | 
					
						
							| 
									
										
										
										
											2023-06-26 16:40:39 +08:00
										 |  |  | 	uidmap     []specs.LinuxIDMapping | 
					
						
							|  |  |  | 	gidmap     []specs.LinuxIDMapping | 
					
						
							| 
									
										
										
										
											2022-05-27 10:47:30 +08:00
										 |  |  | 	rootUID    int | 
					
						
							|  |  |  | 	rootGID    int | 
					
						
							|  |  |  | 	processUID int | 
					
						
							|  |  |  | 	processGID int | 
					
						
							|  |  |  | } |