import.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package computestorage
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/Microsoft/hcsshim/internal/oc"
  6. "github.com/pkg/errors"
  7. "go.opencensus.io/trace"
  8. )
  9. // ImportLayer imports a container layer.
  10. //
  11. // `layerPath` is a path to a directory to import the layer to. If the directory
  12. // does not exist it will be automatically created.
  13. //
  14. // `sourceFolderpath` is a pre-existing folder that contains the layer to
  15. // import.
  16. //
  17. // `layerData` is the parent layer data.
  18. func ImportLayer(ctx context.Context, layerPath, sourceFolderPath string, layerData LayerData) (err error) {
  19. title := "hcsshim.ImportLayer"
  20. ctx, span := trace.StartSpan(ctx, title) //nolint:ineffassign,staticcheck
  21. defer span.End()
  22. defer func() { oc.SetSpanStatus(span, err) }()
  23. span.AddAttributes(
  24. trace.StringAttribute("layerPath", layerPath),
  25. trace.StringAttribute("sourceFolderPath", sourceFolderPath),
  26. )
  27. bytes, err := json.Marshal(layerData)
  28. if err != nil {
  29. return err
  30. }
  31. err = hcsImportLayer(layerPath, sourceFolderPath, string(bytes))
  32. if err != nil {
  33. return errors.Wrap(err, "failed to import layer")
  34. }
  35. return nil
  36. }