Andrey Smirnov 354baca8a0
feat: implement boot asset cache
Fixes #4

This caches the result of the boot asset build (a call to Talos
`imager`) so that we don't rebuild the asset twice.

OCI Registry is used as a cache. An internal registry can be used for
caching, we don't need to expose it to the world.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
2023-10-30 14:05:23 +04:00

56 lines
1.2 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package asset
import (
"fmt"
"io"
"os"
"runtime"
)
// tmpDir holds a generates boot asset in a temporary directory.
type tmpDir struct {
directoryPath string
assetPath string
size int64
}
// Check interface.
var _ BootAsset = (*tmpDir)(nil)
// newTmpDir creates a new temporary directory to hold the boot asset.
func newTmpDir() (*tmpDir, error) {
dir, err := os.MkdirTemp("", "talos-asset")
if err != nil {
return nil, fmt.Errorf("failed to create temporary directory: %w", err)
}
t := &tmpDir{
directoryPath: dir,
}
runtime.SetFinalizer(t, func(t *tmpDir) {
t.cleanup() //nolint:errcheck
})
return t, nil
}
// Size returns the size of the boot asset.
func (t *tmpDir) Size() int64 {
return t.size
}
// Reader returns a reader for the boot asset.
func (t *tmpDir) Reader() (io.ReadCloser, error) {
return os.Open(t.assetPath)
}
// cleanup releases the boot asset.
func (t *tmpDir) cleanup() error {
return os.RemoveAll(t.directoryPath)
}