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 coreos#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 23, 2020
1 parent 5be43fd commit d3d274f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
18 changes: 0 additions & 18 deletions dracut/30ignition/ignition-setup-base.service

This file was deleted.

19 changes: 0 additions & 19 deletions dracut/30ignition/ignition-setup-base.sh

This file was deleted.

3 changes: 0 additions & 3 deletions dracut/30ignition/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ install() {
# Required on system using SELinux
inst_multiple -o setfiles

inst_script "$moddir/ignition-setup-base.sh" \
"/usr/sbin/ignition-setup-base"
inst_script "$moddir/ignition-setup-user.sh" \
"/usr/sbin/ignition-setup-user"

Expand All @@ -82,7 +80,6 @@ install() {
"$systemdsystemunitdir/ignition-$x.target"
done

install_ignition_unit ignition-setup-base.service
install_ignition_unit ignition-setup-user.service
install_ignition_unit ignition-fetch.service
install_ignition_unit ignition-fetch-offline.service
Expand Down
59 changes: 58 additions & 1 deletion 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_3_experimental"
"github.com/coreos/ignition/v2/config/v3_3_experimental/types"
"github.com/coreos/ignition/v2/internal/distro"
"github.com/coreos/ignition/v2/internal/log"
Expand All @@ -35,7 +38,35 @@ const (
)

func FetchBaseConfig(logger *log.Logger) (types.Config, report.Report, error) {
return fetchConfig(logger, baseFilename)
var fullBaseConfig types.Config
baseConfig, _, err := fetchConfig(logger, baseFilename)
if err != nil {
logger.Err("couldn't fetch base config: %v", err)
return types.Config{}, report.Report{}, err
}
fullBaseConfig = latest.Merge(fullBaseConfig, baseConfig)

// For merging base.d config fragments
baseDConfig, _, err := fetchBaseConfig(logger, "base.d", fullBaseConfig)
if err != nil {
return types.Config{}, report.Report{}, err
}

contents, _ := ioutil.ReadDir(distro.SystemConfigDir())
for _, content := range contents {
if content.IsDir() {
// To check if a `base.$PLATFORM_ID.d` directory exists and
// accordingly proceed with the config merging operation.
if strings.Contains(content.Name(), "base") && len(strings.Split(content.Name(), ".")) == 3 && content.Size() > 0 {
basePlatformDConfig, _, err := fetchBaseConfig(logger, content.Name(), fullBaseConfig)
if err != nil {
return types.Config{}, report.Report{}, err
}
baseDConfig = latest.Merge(baseDConfig, basePlatformDConfig)
}
}
}
return baseDConfig, report.Report{}, nil
}

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

// fetchBaseConfig is a helper function to merge all the base config fragments inside of a particular directory.
func fetchBaseConfig(logger *log.Logger, dir string, fullBaseConfig types.Config) (types.Config, report.Report, error) {
path := filepath.Join(distro.SystemConfigDir(), dir)
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
}
if len(configs) == 0 {
logger.Info("no configs at %q", path)
return types.Config{}, report.Report{}, nil
}
for _, config := range configs {
file := fmt.Sprintf("%s/%s", path, config.Name())
baseConfig, _, err := fetchConfig(logger, file)
if err != nil {
return types.Config{}, report.Report{}, err
}
fullBaseConfig = latest.Merge(fullBaseConfig, baseConfig)
}
return fullBaseConfig, report.Report{}, nil
}

0 comments on commit d3d274f

Please sign in to comment.