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 allows the two distrosi (i.e. FCOS and RHCOS) 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 022f0f3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 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
2 changes: 1 addition & 1 deletion internal/exec/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (e Engine) Run(stageName string) error {
}
baseConfig := emptyConfig

systemBaseConfig, r, err := system.FetchBaseConfig(e.Logger)
systemBaseConfig, r, err := system.FetchBaseConfig(e.Logger, e.PlatformConfig.Name())
e.logReport(r)
if err != nil && err != providers.ErrNoProvider {
e.Logger.Crit("failed to acquire system base config: %v", err)
Expand Down
48 changes: 45 additions & 3 deletions internal/providers/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package system

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

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 @@ -30,12 +32,24 @@ import (
)

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

func FetchBaseConfig(logger *log.Logger) (types.Config, report.Report, error) {
return fetchConfig(logger, baseFilename)
func FetchBaseConfig(logger *log.Logger, platformName string) (types.Config, report.Report, error) {
var fullBaseConfig types.Config
baseDConfig, baseDReport, err := fetchBaseDirectoryConfig(logger, "base.d")
if err != nil {
return types.Config{}, baseDReport, err
}

platformDir := fmt.Sprintf("base.%s.d", platformName)
basePlatformDConfig, basePlatformDReport, err := fetchBaseDirectoryConfig(logger, platformDir)
if err != nil {
logger.Info("no config at %q: %v", platformDir, err)
}
fullBaseConfig = latest.Merge(baseDConfig, basePlatformDConfig)
baseDReport.Merge(basePlatformDReport)
return fullBaseConfig, baseDReport, nil
}

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

// fetchBaseDirectoryConfig is a helper function to merge all the base config fragments inside of a particular directory.
func fetchBaseDirectoryConfig(logger *log.Logger, dir string) (types.Config, report.Report, error) {
var baseConfig types.Config
var report report.Report
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, nil
} else if err != nil {
logger.Err("couldn't read config dir %q: %v", path, err)
return types.Config{}, report, err
}
if len(configs) == 0 {
logger.Info("no configs at %q", path)
return types.Config{}, report, nil
}
for _, config := range configs {
intermediateConfig, intermediateReport, err := fetchConfig(logger, filepath.Join(dir, config.Name()))
if err != nil {
return types.Config{}, intermediateReport, err
}
baseConfig = latest.Merge(baseConfig, intermediateConfig)
report.Merge(intermediateReport)
}
return baseConfig, report, nil
}

0 comments on commit 022f0f3

Please sign in to comment.