Writes and verify image
This commit is contained in:
42
clitools/pkg/controller/osimage/apply.go
Normal file
42
clitools/pkg/controller/osimage/apply.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package osimage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ApplyImageStreamed(ctx context.Context, opts ApplyOptions) (*ApplyResult, error) {
|
||||
if err := ValidateApplyOptions(opts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := CheckTargetSafe(opts.TargetPath, opts.ExpectedRawSize); err != nil {
|
||||
return nil, fmt.Errorf("unsafe target %q: %w", opts.TargetPath, err)
|
||||
}
|
||||
|
||||
src, closeFn, err := OpenDecompressedHTTPStream(ctx, opts.URL, opts.HTTPTimeout)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open source stream: %w", err)
|
||||
}
|
||||
defer closeFn()
|
||||
|
||||
written, err := WriteStreamToTarget(ctx, src, opts.TargetPath, opts.ExpectedRawSize, opts.BufferSize, opts.Progress)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("write target: %w", err)
|
||||
}
|
||||
|
||||
sum, err := VerifyTargetSHA256(ctx, opts.TargetPath, opts.ExpectedRawSize, opts.BufferSize, opts.Progress)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("verify target: %w", err)
|
||||
}
|
||||
|
||||
if err := VerifySHA256(sum, opts.ExpectedRawSHA256); err != nil {
|
||||
return nil, fmt.Errorf("final disk checksum mismatch: %w", err)
|
||||
}
|
||||
|
||||
return &ApplyResult{
|
||||
BytesWritten: written,
|
||||
VerifiedSHA256: sum,
|
||||
VerificationOK: true,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user