Skip to content

Commit

Permalink
daemon: make error message explicit
Browse files Browse the repository at this point in the history
When we are skipping the mcd binary write when it already
exists. Today this is the case for RHEL worker node during upgrade.
Explicitly make sure that we are skipping because file doesn't exist
  • Loading branch information
sinnykumari committed Jul 31, 2023
1 parent 25e033a commit edc68be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func ReexecuteForTargetRoot(target string) error {
// Otherwise, we assume that there's no suffixing needed. Hopefully
// by RHEL10 the MCD will have fundamentally changed and we won't be doing the
// chroot() thing anymore.
klog.Info("not chrooting for source=rhel-%s target=rhel-%s", sourceMajor, targetMajor)
klog.Infof("not chrooting for source=rhel-%s target=rhel-%s", sourceMajor, targetMajor)
}
} else {
klog.Info("assuming we can use container binary chroot() to host")
Expand All @@ -476,7 +476,11 @@ func ReexecuteForTargetRoot(target string) error {
targetBin := filepath.Join(target, targetBinBase)

// Be idempotent
if _, err := os.Stat(targetBin); err != nil {
targetBinExist, err := fileExists(targetBin)
if err != nil {
return err
}
if !targetBinExist {
sourceBinary := "/usr/bin/machine-config-daemon" + sourceBinarySuffix
src, err := os.Open(sourceBinary)
if err != nil {
Expand All @@ -485,7 +489,12 @@ func ReexecuteForTargetRoot(target string) error {
defer src.Close()

targetBinDir := filepath.Dir(targetBin)
if _, err := os.Stat(targetBinDir); err != nil {
// Before creating targetBinDir, ensure that it doesn't exist
targetBinDirExist, err := directoryExists(targetBinDir)
if err != nil {
return err
}
if !targetBinDirExist {
if err := os.Mkdir(targetBinDir, 0o755); err != nil {
return fmt.Errorf("mkdir %s: %w", targetBinDir, err)
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/daemon/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -1648,6 +1648,27 @@ func fileExists(path string) (bool, error) {
return false, fmt.Errorf("cannot stat file: %w", err)
}

// Determines if a directory exists by checking the returned error when we stat the file.
// Also, check that it is a directory.
func directoryExists(path string) (bool, error) {
info, err := os.Stat(path)
// If there is no error, check if it is a directory
if err == nil {
if info.IsDir() {
return true, nil
}
return false, fmt.Errorf("%s exists but it is not a directory", path)
}

// If the error matches fs.ErrNotExist, file definitely does not exist.
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}

// An unexpected error occurred.
return false, fmt.Errorf("cannot stat file: %w", err)
}

// Removes the old SSH key path (/home/core/.ssh/authorized_keys), if found.
func cleanSSHKeyPaths() error {
oldKeyExists, err := fileExists(constants.RHCOS8SSHKeyPath)
Expand Down

0 comments on commit edc68be

Please sign in to comment.