mirror of
https://github.com/kairos-io/kairos-agent.git
synced 2025-06-03 01:44:53 +00:00
Add sort-key during install based on the entry name (#609)
* Add sort-key during install based on the entry name Signed-off-by: Itxaka <itxaka@kairos.io> * Fix logger output Signed-off-by: Itxaka <itxaka@kairos.io> --------- Signed-off-by: Itxaka <itxaka@kairos.io>
This commit is contained in:
parent
7be897c1d5
commit
07480abde2
@ -166,3 +166,47 @@ func copyFile(src, dst string) error {
|
||||
|
||||
return destinationFile.Close()
|
||||
}
|
||||
|
||||
func AddSystemdConfSortKey(fs v1.FS, artifactDir string, log sdkTypes.KairosLogger) error {
|
||||
return fsutils.WalkDirFs(fs, artifactDir, func(path string, info os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Only do files that are conf files but dont match the loader.conf
|
||||
if !info.IsDir() && filepath.Ext(path) == ".conf" && !strings.Contains(info.Name(), "loader.conf") {
|
||||
log.Logger.Debug().Str("path", path).Msg("Adding sort key to file")
|
||||
conf, err := sdkutils.SystemdBootConfReader(path)
|
||||
if err != nil {
|
||||
log.Errorf("Error reading conf file to extract values %s: %s", conf, path)
|
||||
}
|
||||
// Now check and put the proper sort key
|
||||
var sortKey string
|
||||
// If we have 2 different files that start with active, like with the extra-cmdline, how do we set this?
|
||||
// Ideally if they both have the same sort key, they will be sorted by name so the single one will be first
|
||||
// and the extra-cmdline will be second. This is the best we can do currently without making this a mess
|
||||
// Maybe we need the bootentry command to also set the sort key somehow?
|
||||
switch {
|
||||
case strings.Contains(info.Name(), "active"):
|
||||
sortKey = "0001"
|
||||
case strings.Contains(info.Name(), "passive"):
|
||||
sortKey = "0002"
|
||||
case strings.Contains(info.Name(), "recovery"):
|
||||
sortKey = "0003"
|
||||
case strings.Contains(info.Name(), "statereset"):
|
||||
sortKey = "0004"
|
||||
default: // Anything that dont matches, goes to the bottom
|
||||
sortKey = "0010"
|
||||
}
|
||||
conf["sort-key"] = sortKey
|
||||
newContents := ""
|
||||
for k, v := range conf {
|
||||
newContents = fmt.Sprintf("%s%s %s\n", newContents, k, v)
|
||||
}
|
||||
log.Logger.Trace().Str("contents", litter.Sdump(conf)).Str("path", path).Msg("Final values for conf file")
|
||||
|
||||
return os.WriteFile(path, []byte(newContents), 0600)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -181,6 +181,12 @@ func (i *InstallAction) Run() (err error) {
|
||||
return fmt.Errorf("removing artifact set with role %s: %w", UnassignedArtifactRole, err)
|
||||
}
|
||||
|
||||
// add sort key to all files
|
||||
err = AddSystemdConfSortKey(i.cfg.Fs, i.spec.Partitions.EFI.MountPoint, i.cfg.Logger)
|
||||
if err != nil {
|
||||
i.cfg.Logger.Warnf("adding sort key: %s", err.Error())
|
||||
}
|
||||
|
||||
// Add boot assessment to files by appending +3 to the name
|
||||
err = utils.AddBootAssessment(i.cfg.Fs, i.spec.Partitions.EFI.MountPoint, i.cfg.Logger)
|
||||
if err != nil {
|
||||
|
@ -81,6 +81,12 @@ func (r *ResetAction) Run() (err error) {
|
||||
return fmt.Errorf("copying recovery to active: %w", err)
|
||||
}
|
||||
|
||||
// add sort key to all files
|
||||
err = AddSystemdConfSortKey(r.cfg.Fs, r.spec.Partitions.EFI.MountPoint, r.cfg.Logger)
|
||||
if err != nil {
|
||||
r.cfg.Logger.Warnf("adding sort key: %s", err.Error())
|
||||
}
|
||||
|
||||
// Add boot assessment to files by appending +3 to the name
|
||||
err = elementalUtils.AddBootAssessment(r.cfg.Fs, r.spec.Partitions.EFI.MountPoint, r.cfg.Logger)
|
||||
if err != nil {
|
||||
|
@ -109,6 +109,13 @@ func (i *UpgradeAction) Run() (err error) {
|
||||
i.cfg.Logger.Errorf("removing artifact set: %s", err.Error())
|
||||
return fmt.Errorf("removing artifact set: %w", err)
|
||||
}
|
||||
|
||||
// add sort key to all files
|
||||
err = AddSystemdConfSortKey(i.cfg.Fs, i.spec.EfiPartition.MountPoint, i.cfg.Logger)
|
||||
if err != nil {
|
||||
i.cfg.Logger.Warnf("adding sort key: %s", err.Error())
|
||||
}
|
||||
|
||||
// Add boot assessment to files by appending +3 to the name
|
||||
err = elementalUtils.AddBootAssessment(i.cfg.Fs, i.spec.EfiPartition.MountPoint, i.cfg.Logger)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user