Merge pull request #1806 from giuseppe/rootless-create-default-files

rootless: create user conf files when they don't exist
This commit is contained in:
OpenShift Merge Robot 2018-11-19 08:44:57 -08:00 committed by GitHub
commit 64a29e383b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 3 deletions

View File

@ -264,6 +264,7 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
configPath := ConfigPath
foundConfig := true
rootlessConfigPath := ""
if rootless.IsRootless() {
home := os.Getenv("HOME")
if runtime.config.SignaturePolicyPath == "" {
@ -272,7 +273,10 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
runtime.config.SignaturePolicyPath = newPath
}
}
configPath = 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
}
@ -317,6 +321,22 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
if err := makeRuntime(runtime); err != nil {
return nil, err
}
if !foundConfig && rootlessConfigPath != "" {
os.MkdirAll(filepath.Dir(rootlessConfigPath), 0755)
file, err := os.OpenFile(rootlessConfigPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil && !os.IsExist(err) {
return nil, errors.Wrapf(err, "cannot open file %s", rootlessConfigPath)
}
if err == nil {
defer file.Close()
enc := toml.NewEncoder(file)
if err := enc.Encode(runtime.config); err != nil {
os.Remove(rootlessConfigPath)
}
}
}
return runtime, nil
}

View File

@ -38,8 +38,10 @@ func GetRegistries() ([]string, error) {
func GetInsecureRegistries() ([]string, error) {
registryConfigPath := ""
if _, err := os.Stat(userRegistriesFile); err == nil {
registryConfigPath = userRegistriesFile
if rootless.IsRootless() {
if _, err := os.Stat(userRegistriesFile); err == nil {
registryConfigPath = userRegistriesFile
}
}
envOverride := os.Getenv("REGISTRIES_CONFIG_PATH")

View File

@ -149,6 +149,15 @@ func SecretMountsWithUIDGID(mountLabel, containerWorkingDir, mountFile, mountPre
mountFiles = append(mountFiles, []string{OverrideMountsFile, DefaultMountsFile}...)
if rootless.IsRootless() {
mountFiles = append([]string{UserOverrideMountsFile}, mountFiles...)
_, err := os.Stat(UserOverrideMountsFile)
if err != nil && os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(UserOverrideMountsFile), 0755)
if f, err := os.Create(UserOverrideMountsFile); err != nil {
logrus.Warnf("could not create file %s: %v", UserOverrideMountsFile, err)
} else {
f.Close()
}
}
}
} else {
mountFiles = append(mountFiles, mountFile)

View File

@ -9,6 +9,7 @@ import (
"strings"
"syscall"
"github.com/BurntSushi/toml"
"github.com/containers/image/types"
"github.com/containers/libpod/pkg/rootless"
"github.com/containers/storage"
@ -296,6 +297,18 @@ func GetDefaultStoreOptions() (storage.StoreOptions, error) {
storageConf := filepath.Join(os.Getenv("HOME"), ".config/containers/storage.conf")
if _, err := os.Stat(storageConf); err == nil {
storage.ReloadConfigurationFile(storageConf, &storageOpts)
} else if os.IsNotExist(err) {
os.MkdirAll(filepath.Dir(storageConf), 0755)
file, err := os.OpenFile(storageConf, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
return storageOpts, errors.Wrapf(err, "cannot open %s", storageConf)
}
defer file.Close()
enc := toml.NewEncoder(file)
if err := enc.Encode(storageOpts); err != nil {
os.Remove(storageConf)
}
}
}
return storageOpts, nil