Skip to content

Commit

Permalink
userdata: Ensure the parent dir exists
Browse files Browse the repository at this point in the history
Occassionally the writing of config files may fail due to
non-existing parent dir.

Signed-off-by: Pradipta Banerjee <pradipta.banerjee@gmail.com>
  • Loading branch information
bpradipt committed Mar 27, 2024
1 parent 93ae704 commit a554e6d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cloud-api-adaptor/pkg/userdata/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"time"

"github.com/avast/retry-go/v4"
Expand Down Expand Up @@ -144,7 +145,13 @@ func findConfigEntry(path string, cc *CloudConfig) []byte {
}

func writeFile(path string, bytes []byte) error {
err := os.WriteFile(path, bytes, 0644)
// Ensure the parent directory exists
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return fmt.Errorf("failed to create directory: %w", err)
}

err = os.WriteFile(path, bytes, 0644)
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
Expand Down

0 comments on commit a554e6d

Please sign in to comment.