mirror of
https://github.com/containers/podman.git
synced 2025-06-26 04:46:57 +08:00
Merge pull request #2617 from giuseppe/fix-with-config
runtime: fill the runtime config with sane defaults
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
package libpod
|
package libpod
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/containers/libpod/libpod/events"
|
"github.com/containers/libpod/libpod/events"
|
||||||
"github.com/hpcloud/tail"
|
"github.com/hpcloud/tail"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -85,7 +87,7 @@ func (r *Runtime) Events(fromStart, stream bool, options []events.EventFilter, e
|
|||||||
|
|
||||||
func (r *Runtime) getTail(fromStart, stream bool) (*tail.Tail, error) {
|
func (r *Runtime) getTail(fromStart, stream bool) (*tail.Tail, error) {
|
||||||
reopen := true
|
reopen := true
|
||||||
seek := tail.SeekInfo{Offset: 0, Whence: 2}
|
seek := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
|
||||||
if fromStart || !stream {
|
if fromStart || !stream {
|
||||||
seek.Whence = 0
|
seek.Whence = 0
|
||||||
reopen = false
|
reopen = false
|
||||||
|
@ -241,6 +241,12 @@ type runtimeConfiguredFrom struct {
|
|||||||
libpodStaticDirSet bool
|
libpodStaticDirSet bool
|
||||||
libpodTmpDirSet bool
|
libpodTmpDirSet bool
|
||||||
volPathSet bool
|
volPathSet bool
|
||||||
|
conmonPath bool
|
||||||
|
conmonEnvVars bool
|
||||||
|
ociRuntimes bool
|
||||||
|
runtimePath bool
|
||||||
|
cniPluginDir bool
|
||||||
|
noPivotRoot bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -324,6 +330,22 @@ func SetXdgRuntimeDir(val string) error {
|
|||||||
// NewRuntime creates a new container runtime
|
// NewRuntime creates a new container runtime
|
||||||
// Options can be passed to override the default configuration for the runtime
|
// Options can be passed to override the default configuration for the runtime
|
||||||
func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
||||||
|
return newRuntimeFromConfig("", options...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRuntimeFromConfig creates a new container runtime using the given
|
||||||
|
// configuration file for its default configuration. Passed RuntimeOption
|
||||||
|
// functions can be used to mutate this configuration further.
|
||||||
|
// An error will be returned if the configuration file at the given path does
|
||||||
|
// not exist or cannot be loaded
|
||||||
|
func NewRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runtime *Runtime, err error) {
|
||||||
|
if userConfigPath == "" {
|
||||||
|
return nil, errors.New("invalid configuration file specified")
|
||||||
|
}
|
||||||
|
return newRuntimeFromConfig(userConfigPath, options...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runtime *Runtime, err error) {
|
||||||
runtime = new(Runtime)
|
runtime = new(Runtime)
|
||||||
runtime.config = new(RuntimeConfig)
|
runtime.config = new(RuntimeConfig)
|
||||||
runtime.configuredFrom = new(runtimeConfiguredFrom)
|
runtime.configuredFrom = new(runtimeConfiguredFrom)
|
||||||
@ -358,11 +380,6 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
|||||||
|
|
||||||
rootlessConfigPath = filepath.Join(home, ".config/containers/libpod.conf")
|
rootlessConfigPath = filepath.Join(home, ".config/containers/libpod.conf")
|
||||||
|
|
||||||
configPath = rootlessConfigPath
|
|
||||||
if _, err := os.Stat(configPath); err != nil {
|
|
||||||
foundConfig = false
|
|
||||||
}
|
|
||||||
|
|
||||||
runtimeDir, err := util.GetRootlessRuntimeDir()
|
runtimeDir, err := util.GetRootlessRuntimeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -374,6 +391,20 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
|||||||
return nil, errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR")
|
return nil, errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if userConfigPath != "" {
|
||||||
|
configPath = userConfigPath
|
||||||
|
if _, err := os.Stat(configPath); err != nil {
|
||||||
|
// If the user specified a config file, we must fail immediately
|
||||||
|
// when it doesn't exist
|
||||||
|
return nil, errors.Wrapf(err, "cannot stat %s", configPath)
|
||||||
|
}
|
||||||
|
} else if rootless.IsRootless() {
|
||||||
|
configPath = rootlessConfigPath
|
||||||
|
if _, err := os.Stat(configPath); err != nil {
|
||||||
|
foundConfig = false
|
||||||
|
}
|
||||||
} else if _, err := os.Stat(OverrideConfigPath); err == nil {
|
} else if _, err := os.Stat(OverrideConfigPath); err == nil {
|
||||||
// Use the override configuration path
|
// Use the override configuration path
|
||||||
configPath = OverrideConfigPath
|
configPath = OverrideConfigPath
|
||||||
@ -409,6 +440,24 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
|||||||
if tmpConfig.VolumePath != "" {
|
if tmpConfig.VolumePath != "" {
|
||||||
runtime.configuredFrom.volPathSet = true
|
runtime.configuredFrom.volPathSet = true
|
||||||
}
|
}
|
||||||
|
if tmpConfig.ConmonPath != nil {
|
||||||
|
runtime.configuredFrom.conmonPath = true
|
||||||
|
}
|
||||||
|
if tmpConfig.ConmonEnvVars != nil {
|
||||||
|
runtime.configuredFrom.conmonEnvVars = true
|
||||||
|
}
|
||||||
|
if tmpConfig.OCIRuntimes != nil {
|
||||||
|
runtime.configuredFrom.ociRuntimes = true
|
||||||
|
}
|
||||||
|
if tmpConfig.RuntimePath != nil {
|
||||||
|
runtime.configuredFrom.runtimePath = true
|
||||||
|
}
|
||||||
|
if tmpConfig.CNIPluginDir != nil {
|
||||||
|
runtime.configuredFrom.cniPluginDir = true
|
||||||
|
}
|
||||||
|
if tmpConfig.NoPivotRoot {
|
||||||
|
runtime.configuredFrom.noPivotRoot = true
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := toml.Decode(string(contents), runtime.config); err != nil {
|
if _, err := toml.Decode(string(contents), runtime.config); err != nil {
|
||||||
return nil, errors.Wrapf(err, "error decoding configuration file %s", configPath)
|
return nil, errors.Wrapf(err, "error decoding configuration file %s", configPath)
|
||||||
@ -428,12 +477,24 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cherry pick the settings we want from the global configuration
|
// Cherry pick the settings we want from the global configuration
|
||||||
|
if !runtime.configuredFrom.conmonPath {
|
||||||
runtime.config.ConmonPath = tmpConfig.ConmonPath
|
runtime.config.ConmonPath = tmpConfig.ConmonPath
|
||||||
|
}
|
||||||
|
if !runtime.configuredFrom.conmonEnvVars {
|
||||||
runtime.config.ConmonEnvVars = tmpConfig.ConmonEnvVars
|
runtime.config.ConmonEnvVars = tmpConfig.ConmonEnvVars
|
||||||
|
}
|
||||||
|
if !runtime.configuredFrom.ociRuntimes {
|
||||||
runtime.config.OCIRuntimes = tmpConfig.OCIRuntimes
|
runtime.config.OCIRuntimes = tmpConfig.OCIRuntimes
|
||||||
|
}
|
||||||
|
if !runtime.configuredFrom.runtimePath {
|
||||||
runtime.config.RuntimePath = tmpConfig.RuntimePath
|
runtime.config.RuntimePath = tmpConfig.RuntimePath
|
||||||
|
}
|
||||||
|
if !runtime.configuredFrom.cniPluginDir {
|
||||||
runtime.config.CNIPluginDir = tmpConfig.CNIPluginDir
|
runtime.config.CNIPluginDir = tmpConfig.CNIPluginDir
|
||||||
|
}
|
||||||
|
if !runtime.configuredFrom.noPivotRoot {
|
||||||
runtime.config.NoPivotRoot = tmpConfig.NoPivotRoot
|
runtime.config.NoPivotRoot = tmpConfig.NoPivotRoot
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -465,80 +526,9 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
|||||||
return runtime, nil
|
return runtime, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRuntimeFromConfig creates a new container runtime using the given
|
|
||||||
// configuration file for its default configuration. Passed RuntimeOption
|
|
||||||
// functions can be used to mutate this configuration further.
|
|
||||||
// An error will be returned if the configuration file at the given path does
|
|
||||||
// not exist or cannot be loaded
|
|
||||||
func NewRuntimeFromConfig(configPath string, options ...RuntimeOption) (runtime *Runtime, err error) {
|
|
||||||
runtime = new(Runtime)
|
|
||||||
runtime.config = new(RuntimeConfig)
|
|
||||||
runtime.configuredFrom = new(runtimeConfiguredFrom)
|
|
||||||
|
|
||||||
// Set three fields not in the TOML config
|
|
||||||
runtime.config.StateType = defaultRuntimeConfig.StateType
|
|
||||||
runtime.config.OCIRuntime = defaultRuntimeConfig.OCIRuntime
|
|
||||||
|
|
||||||
storageConf, err := util.GetDefaultStoreOptions()
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "error retrieving storage config")
|
|
||||||
}
|
|
||||||
runtime.config.StorageConfig = storageConf
|
|
||||||
runtime.config.StaticDir = filepath.Join(storageConf.GraphRoot, "libpod")
|
|
||||||
runtime.config.VolumePath = filepath.Join(storageConf.GraphRoot, "volumes")
|
|
||||||
|
|
||||||
tmpDir, err := getDefaultTmpDir()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
runtime.config.TmpDir = tmpDir
|
|
||||||
if rootless.IsRootless() {
|
|
||||||
runtimeDir, err := util.GetRootlessRuntimeDir()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// containers/image uses XDG_RUNTIME_DIR to locate the auth file.
|
|
||||||
// So make sure the env variable is set.
|
|
||||||
if err := SetXdgRuntimeDir(runtimeDir); err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check to see if the given configuration file exists
|
|
||||||
if _, err := os.Stat(configPath); err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "error checking existence of configuration file %s", configPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read contents of the config file
|
|
||||||
contents, err := ioutil.ReadFile(configPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "error reading configuration file %s", configPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode configuration file
|
|
||||||
if _, err := toml.Decode(string(contents), runtime.config); err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "error decoding configuration from file %s", configPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Overwrite the config with user-given configuration options
|
|
||||||
for _, opt := range options {
|
|
||||||
if err := opt(runtime); err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "error configuring runtime")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := makeRuntime(runtime); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return runtime, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make a new runtime based on the given configuration
|
// Make a new runtime based on the given configuration
|
||||||
// Sets up containers/storage, state store, OCI runtime
|
// Sets up containers/storage, state store, OCI runtime
|
||||||
func makeRuntime(runtime *Runtime) (err error) {
|
func makeRuntime(runtime *Runtime) (err error) {
|
||||||
runtime.config.EventsLogFilePath = filepath.Join(runtime.config.TmpDir, "events", "events.log")
|
|
||||||
|
|
||||||
// Backward compatibility for `runtime_path`
|
// Backward compatibility for `runtime_path`
|
||||||
if runtime.config.RuntimePath != nil {
|
if runtime.config.RuntimePath != nil {
|
||||||
// Don't print twice in rootless mode.
|
// Don't print twice in rootless mode.
|
||||||
@ -697,6 +687,8 @@ func makeRuntime(runtime *Runtime) (err error) {
|
|||||||
runtime.config.VolumePath = dbConfig.VolumePath
|
runtime.config.VolumePath = dbConfig.VolumePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
runtime.config.EventsLogFilePath = filepath.Join(runtime.config.TmpDir, "events", "events.log")
|
||||||
|
|
||||||
logrus.Debugf("Using graph driver %s", runtime.config.StorageConfig.GraphDriverName)
|
logrus.Debugf("Using graph driver %s", runtime.config.StorageConfig.GraphDriverName)
|
||||||
logrus.Debugf("Using graph root %s", runtime.config.StorageConfig.GraphRoot)
|
logrus.Debugf("Using graph root %s", runtime.config.StorageConfig.GraphRoot)
|
||||||
logrus.Debugf("Using run root %s", runtime.config.StorageConfig.RunRoot)
|
logrus.Debugf("Using run root %s", runtime.config.StorageConfig.RunRoot)
|
||||||
|
Reference in New Issue
Block a user