-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added ability to copy files into the built image's filesystem (#474)
* added ability to copy files into the built image's filesystem * changed os-files implementation to use the existing CopyFiles function * incorporated PR feedback
- Loading branch information
Showing
6 changed files
with
178 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package combustion | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/suse-edge/edge-image-builder/pkg/fileio" | ||
"github.com/suse-edge/edge-image-builder/pkg/image" | ||
"github.com/suse-edge/edge-image-builder/pkg/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
const ( | ||
osFilesComponentName = "os files" | ||
osFilesConfigDir = "os-files" | ||
osFilesScriptName = "19-copy-os-files.sh" | ||
osFilesLogFile = "copy-os-files.log" | ||
) | ||
|
||
var ( | ||
//go:embed templates/19-copy-os-files.sh | ||
osFilesScript string | ||
) | ||
|
||
func configureOSFiles(ctx *image.Context) ([]string, error) { | ||
if !isComponentConfigured(ctx, osFilesConfigDir) { | ||
log.AuditComponentSkipped(osFilesComponentName) | ||
zap.S().Info("skipping os files component, no files provided") | ||
return nil, nil | ||
} | ||
|
||
if err := copyOSFiles(ctx); err != nil { | ||
log.AuditComponentFailed(osFilesComponentName) | ||
return nil, err | ||
} | ||
|
||
if err := writeOSFilesScript(ctx); err != nil { | ||
log.AuditComponentFailed(osFilesComponentName) | ||
return nil, err | ||
} | ||
|
||
log.AuditComponentSuccessful(osFilesComponentName) | ||
return []string{osFilesScriptName}, nil | ||
} | ||
|
||
func copyOSFiles(ctx *image.Context) error { | ||
srcDirectory := filepath.Join(ctx.ImageConfigDir, osFilesConfigDir) | ||
destDirectory := filepath.Join(ctx.CombustionDir, osFilesConfigDir) | ||
|
||
dirEntries, err := os.ReadDir(srcDirectory) | ||
if err != nil { | ||
return fmt.Errorf("reading the os files directory at %s: %w", srcDirectory, 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", srcDirectory) | ||
} | ||
|
||
if err := fileio.CopyFiles(srcDirectory, destDirectory, "", true); err != nil { | ||
return fmt.Errorf("copying os-files: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func writeOSFilesScript(ctx *image.Context) error { | ||
osFilesScriptFilename := filepath.Join(ctx.CombustionDir, osFilesScriptName) | ||
|
||
if err := os.WriteFile(osFilesScriptFilename, []byte(osFilesScript), fileio.ExecutablePerms); err != nil { | ||
return fmt.Errorf("writing os files script %s: %w", osFilesScriptFilename, err) | ||
} | ||
|
||
return nil | ||
} |
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,71 @@ | ||
package combustion | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/suse-edge/edge-image-builder/pkg/image" | ||
) | ||
|
||
func setupOsFilesConfigDir(t *testing.T, empty bool) (ctx *image.Context, teardown func()) { | ||
ctx, teardown = setupContext(t) | ||
|
||
testOsFilesDir := filepath.Join(ctx.ImageConfigDir, osFilesConfigDir) | ||
err := os.Mkdir(testOsFilesDir, 0o755) | ||
require.NoError(t, err) | ||
|
||
if !empty { | ||
nestedOsFilesDir := filepath.Join(testOsFilesDir, "etc", "ssh") | ||
err = os.MkdirAll(nestedOsFilesDir, 0o755) | ||
require.NoError(t, err) | ||
|
||
testFile := filepath.Join(nestedOsFilesDir, "test-config-file") | ||
_, err = os.Create(testFile) | ||
require.NoError(t, err) | ||
} | ||
|
||
return | ||
} | ||
|
||
func TestConfigureOSFiles(t *testing.T) { | ||
// Setup | ||
ctx, teardown := setupOsFilesConfigDir(t, false) | ||
defer teardown() | ||
|
||
// Test | ||
scriptNames, err := configureOSFiles(ctx) | ||
|
||
// Verify | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, []string{osFilesScriptName}, scriptNames) | ||
|
||
// -- Combustion Script | ||
expectedCombustionScript := filepath.Join(ctx.CombustionDir, osFilesScriptName) | ||
contents, err := os.ReadFile(expectedCombustionScript) | ||
require.NoError(t, err) | ||
assert.Contains(t, string(contents), "cp -R") | ||
|
||
// -- Files | ||
expectedFile := filepath.Join(ctx.CombustionDir, osFilesConfigDir, "etc", "ssh", "test-config-file") | ||
assert.FileExists(t, expectedFile) | ||
} | ||
|
||
func TestConfigureOSFiles_EmptyDirectory(t *testing.T) { | ||
// Setup | ||
ctx, teardown := setupOsFilesConfigDir(t, true) | ||
defer teardown() | ||
|
||
// Test | ||
scriptName, err := configureOSFiles(ctx) | ||
|
||
// Verify | ||
assert.Nil(t, scriptName) | ||
|
||
srcDirectory := filepath.Join(ctx.ImageConfigDir, osFilesConfigDir) | ||
assert.EqualError(t, err, fmt.Sprintf("no files found in directory %s", srcDirectory)) | ||
} |
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,4 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
cp -R ./os-files/* / |