-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from kairos-io/2226-detect-boot-state
2226 detect boot state
- Loading branch information
Showing
8 changed files
with
246 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package uki | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
v1 "github.com/kairos-io/kairos-agent/v2/pkg/types/v1" | ||
fsutils "github.com/kairos-io/kairos-agent/v2/pkg/utils/fs" | ||
sdkutils "github.com/kairos-io/kairos-sdk/utils" | ||
"github.com/sanity-io/litter" | ||
) | ||
|
||
const UnassignedArtifactRole = "norole" | ||
|
||
// overwriteArtifactSetRole first deletes all artifacts prefixed with newRole | ||
// (because they are going to be replaced, e.g. old "passive") and then installs | ||
// the artifacts prefixed with oldRole as newRole. | ||
// E.g. removes "passive" and moved "active" to "passive" | ||
// This is a step that should happen before a new passive is installed on upgrades. | ||
func overwriteArtifactSetRole(fs v1.FS, dir, oldRole, newRole string, logger v1.Logger) error { | ||
if err := removeArtifactSetWithRole(fs, dir, newRole); err != nil { | ||
return fmt.Errorf("deleting role %s: %w", newRole, err) | ||
} | ||
|
||
if err := copyArtifactSetRole(fs, dir, oldRole, newRole, logger); err != nil { | ||
return fmt.Errorf("copying artifact set role from %s to %s: %w", oldRole, newRole, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// copy the source file but rename the base name to as | ||
func copyArtifact(source, oldRole, newRole string) (string, error) { | ||
newName := strings.ReplaceAll(source, oldRole, newRole) | ||
return newName, copyFile(source, newName) | ||
} | ||
|
||
func removeArtifactSetWithRole(fs v1.FS, artifactDir, role string) error { | ||
return fsutils.WalkDirFs(fs, artifactDir, func(path string, info os.DirEntry, err error) error { | ||
if !info.IsDir() && strings.HasPrefix(info.Name(), role) { | ||
return os.Remove(path) | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func copyArtifactSetRole(fs v1.FS, artifactDir, oldRole, newRole string, logger v1.Logger) error { | ||
return fsutils.WalkDirFs(fs, artifactDir, func(path string, info os.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !strings.HasPrefix(info.Name(), oldRole) { | ||
return nil | ||
} | ||
|
||
newPath, err := copyArtifact(path, oldRole, newRole) | ||
if err != nil { | ||
return fmt.Errorf("copying artifact from %s to %s: %w", path, newPath, err) | ||
} | ||
if strings.HasSuffix(path, ".conf") { | ||
if err := replaceRoleInKey(newPath, "efi", oldRole, newRole, logger); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
} | ||
|
||
func replaceRoleInKey(path, key, oldRole, newRole string, logger v1.Logger) (err error) { | ||
// Extract the values | ||
conf, err := sdkutils.SystemdBootConfReader(path) | ||
if err != nil { | ||
logger.Errorf("Error reading conf file %s: %s", path, err) | ||
return err | ||
} | ||
logger.Debugf("Conf file %s has values %v", path, litter.Sdump(conf)) | ||
|
||
_, hasKey := conf[key] | ||
if !hasKey { | ||
return fmt.Errorf("no %s entry in .conf file", key) | ||
} | ||
|
||
conf[key] = strings.ReplaceAll(conf[key], oldRole, newRole) | ||
newContents := "" | ||
for k, v := range conf { | ||
newContents = fmt.Sprintf("%s%s %s\n", newContents, k, v) | ||
} | ||
logger.Debugf("Conf file %s new values %v", path, litter.Sdump(conf)) | ||
|
||
return os.WriteFile(path, []byte(newContents), os.ModePerm) | ||
} | ||
|
||
func copyFile(src, dst string) error { | ||
sourceFile, err := os.Open(src) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer sourceFile.Close() | ||
|
||
destinationFile, err := os.Create(dst) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer destinationFile.Close() | ||
|
||
if _, err = io.Copy(destinationFile, sourceFile); err != nil { | ||
return err | ||
} | ||
|
||
// Flushes any buffered data to the destination file | ||
if err = destinationFile.Sync(); err != nil { | ||
return err | ||
} | ||
|
||
if err = sourceFile.Close(); err != nil { | ||
return err | ||
} | ||
|
||
return destinationFile.Close() | ||
} |
Oops, something went wrong.