Skip to content

Commit

Permalink
providers/system: support .d directory for base config fragments
Browse files Browse the repository at this point in the history
Fixes #1101
This change was inspired by coreos/fedora-coreos-config#626, which allows
the two distros to carry different base.ign files with common elements. Also,
this will support an additional .d directory for each platform ID.
  • Loading branch information
sohankunkerkar committed Oct 9, 2020
1 parent 1315642 commit 9812246
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
10 changes: 5 additions & 5 deletions dracut/30ignition/ignition-setup-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ copy_file_if_exists() {
src="${1}"; dst="${2}"
if [ -f "${src}" ]; then
echo "Copying ${src} to ${dst}"
cp "${src}" "${dst}"
cp -r "${src}" "${dst}"
else
echo "File ${src} does not exist.. Skipping copy"
fi
}

destination=/usr/lib/ignition
destination=/usr/lib/ignition/base.${PLATFORM_ID}.d
mkdir -p $destination

# We will support grabbing a platform specific base.ign config
# from the initrd at /usr/lib/ignition/platform/${PLATFORM_ID}/base.ign
copy_file_if_exists "/usr/lib/ignition/platform/${PLATFORM_ID}/base.ign" "${destination}/base.ign"
# We will support grabbing platform-specific config fragments
# from the initrd at /usr/lib/ignition/platform/${PLATFORM_ID}/
copy_file_if_exists "/usr/lib/ignition/platform/${PLATFORM_ID}/" "${destination}/"
42 changes: 40 additions & 2 deletions internal/providers/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package system

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

latest "github.com/coreos/ignition/v2/config/v3_2_experimental"
"github.com/coreos/ignition/v2/config/v3_2_experimental/types"
"github.com/coreos/ignition/v2/internal/distro"
"github.com/coreos/ignition/v2/internal/log"
Expand All @@ -30,12 +33,26 @@ import (
)

const (
baseFilename = "base.ign"
userFilename = "user.ign"
)

func FetchBaseConfig(logger *log.Logger) (types.Config, report.Report, error) {
return fetchConfig(logger, baseFilename)
baseDirectory := "base.d"
// Assuming we get only single directory which is either
// a `base.d` or a `base.${PLATFORM_ID}.d`.
contents, err := ioutil.ReadDir(distro.SystemConfigDir())
if err != nil {
return types.Config{}, report.Report{}, err
}
for _, content := range contents {
if content.IsDir() {
if strings.Contains(content.Name(), "base") && len(strings.Split(content.Name(), ".")) == 3 {
baseDirectory = content.Name()
}
}
}
path := filepath.Join(distro.SystemConfigDir(), baseDirectory)
return fetchBaseConfig(logger, path)
}

func FetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) {
Expand All @@ -56,3 +73,24 @@ func fetchConfig(logger *log.Logger, filename string) (types.Config, report.Repo
}
return util.ParseConfig(logger, rawConfig)
}

func fetchBaseConfig(logger *log.Logger, path string) (types.Config, report.Report, error) {
var baseConfig, fullSystemBaseConfig types.Config
configs, err := ioutil.ReadDir(path)
if os.IsNotExist(err) {
logger.Info("no config dir at %q", path)
return types.Config{}, report.Report{}, err
} else if err != nil {
logger.Err("couldn't read config dir %q: %v", path, err)
return types.Config{}, report.Report{}, err
}
for _, config := range configs {
file := fmt.Sprintf("%s/%s", path, config.Name())
systemBaseConfig, _, err := fetchConfig(logger, file)
if err != nil {
return types.Config{}, report.Report{}, err
}
fullSystemBaseConfig = latest.Merge(baseConfig, systemBaseConfig)
}
return fullSystemBaseConfig, report.Report{}, nil
}

0 comments on commit 9812246

Please sign in to comment.