Skip to content

Commit

Permalink
Directory handling in custom/files (suse-edge#527)
Browse files Browse the repository at this point in the history
* added handling for directories in custom/files

* feedback changes
  • Loading branch information
dbw7 authored Aug 26, 2024
1 parent 6a0341b commit 28eb542
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

## General

* The "custom files" functionality may now include directories, which will be maintained when copied to the image
* Improved Kubernetes definition validation
* Allow RKE2 deployments with Calico, Cilium and Multus on aarch64 platforms
* Helm chart installation backOffLimit changed from 1000(default) to 20
Expand Down
4 changes: 3 additions & 1 deletion docs/building-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,8 @@ not begin with a number.
└── custom
├── files
│ ├── custom-binary
│ ├── sub-directory
│ │ └── sub-directory-file.txt
│ └── custom-script.sh
└── scripts
└── 70-manual-configuration.sh
Expand All @@ -527,4 +529,4 @@ not begin with a number.
* `custom` - May be included to inject files into the built image. Files are organized by subdirectory as follows:
* `scripts` - If present, all the files in this directory will be included in the built image and automatically
executed during the combustion phase.
* `files` - If present, all the files in this directory will be available at combustion time on the booted node.
* `files` - If present, all the files, directories, and subdirectories in this directory will be available at combustion time on the booted node.
47 changes: 29 additions & 18 deletions pkg/combustion/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,52 @@ func configureCustomFiles(ctx *image.Context) ([]string, error) {

func handleCustomFiles(ctx *image.Context) error {
fullFilesDir := generateComponentPath(ctx, filepath.Join(customDir, customFilesDir))
_, err := copyCustomFiles(fullFilesDir, ctx.CombustionDir, nil)
err := copyCustomFiles(fullFilesDir, ctx.CombustionDir)
return err
}

func handleCustomScripts(ctx *image.Context) ([]string, error) {
fullScriptsDir := generateComponentPath(ctx, filepath.Join(customDir, customScriptsDir))
executablePerms := fileio.ExecutablePerms
scripts, err := copyCustomFiles(fullScriptsDir, ctx.CombustionDir, &executablePerms)
scripts, err := copyCustomScripts(fullScriptsDir, ctx.CombustionDir, &executablePerms)
return scripts, err
}

func copyCustomFiles(fromDir, toDir string, filePermissions *os.FileMode) ([]string, error) {
func copyCustomFiles(fromDir, toDir string) error {
if _, err := os.Stat(fromDir); os.IsNotExist(err) {
return nil
}

dirEntries, err := os.ReadDir(fromDir)
if err != nil {
return fmt.Errorf("reading the custom files directory at %s: %w", fromDir, err)
}

// If the directory exists but there's nothing in it, consider it an error case
if len(dirEntries) == 0 {
return fmt.Errorf("no files found in directory %s", fromDir)
}

if err = fileio.CopyFiles(fromDir, toDir, "", true); err != nil {
return fmt.Errorf("copying custom files and directories: %w", err)
}

return nil
}

func copyCustomScripts(fromDir, toDir string, filePermissions *os.FileMode) ([]string, error) {
if _, err := os.Stat(fromDir); os.IsNotExist(err) {
return nil, nil
}

dirEntries, err := os.ReadDir(fromDir)
if err != nil {
return nil, fmt.Errorf("reading the custom directory at %s: %w", fromDir, err)
return nil, fmt.Errorf("reading the custom scripts directory at %s: %w", fromDir, err)
}

// If the directory exists but there's nothing in it, consider it an error case
if len(dirEntries) == 0 {
return nil, fmt.Errorf("no files found in directory %s", fromDir)
return nil, fmt.Errorf("no scripts found in directory %s", fromDir)
}

var copiedFiles []string
Expand All @@ -73,19 +95,8 @@ func copyCustomFiles(fromDir, toDir string, filePermissions *os.FileMode) ([]str
copyMe := filepath.Join(fromDir, entry.Name())
copyTo := filepath.Join(toDir, entry.Name())

var mode os.FileMode
if filePermissions == nil {
info, infoErr := entry.Info()
if infoErr != nil {
return nil, fmt.Errorf("reading file info: %w", infoErr)
}
mode = info.Mode()
} else {
mode = *filePermissions
}

if err = fileio.CopyFile(copyMe, copyTo, mode); err != nil {
return nil, fmt.Errorf("copying file to %s: %w", copyTo, err)
if err = fileio.CopyFile(copyMe, copyTo, *filePermissions); err != nil {
return nil, fmt.Errorf("copying script to %s: %w", copyTo, err)
}

copiedFiles = append(copiedFiles, entry.Name())
Expand Down
7 changes: 3 additions & 4 deletions pkg/combustion/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,9 @@ func TestCopyCustomFiles_MissingFromDir(t *testing.T) {
defer teardown()

// Test
files, err := copyCustomFiles("missing", ctx.CombustionDir, nil)
err := copyCustomFiles("missing", ctx.CombustionDir)

// Verify
assert.Nil(t, files)
assert.Nil(t, err)
}

Expand All @@ -126,10 +125,10 @@ func TestCopyCustomFiles_EmptyFromDir(t *testing.T) {
require.NoError(t, err)

// Test
scripts, err := copyCustomFiles(fullScriptsDir, ctx.CombustionDir, nil)
scripts, err := copyCustomScripts(fullScriptsDir, ctx.CombustionDir, nil)

// Verify
require.Error(t, err)
assert.ErrorContains(t, err, "no files found in directory")
assert.ErrorContains(t, err, "no scripts found in directory")
assert.Nil(t, scripts)
}
6 changes: 5 additions & 1 deletion pkg/fileio/file_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ func CopyFiles(src, dest, ext string, copySubDir bool) error {
zap.S().Debugf("Skipping %s as it is not a '%s' file", file.Name(), ext)
continue
}
info, infoErr := file.Info()
if infoErr != nil {
return fmt.Errorf("reading file info %w", infoErr)
}

err := CopyFile(sourcePath, destPath, NonExecutablePerms)
err := CopyFile(sourcePath, destPath, info.Mode())
if err != nil {
return fmt.Errorf("copying file %s: %w", sourcePath, err)
}
Expand Down

0 comments on commit 28eb542

Please sign in to comment.