NewBundle returns a new bundle on disk
(ctx context.Context, root, state, id string, spec typeurl.Any)
| 45 | |
| 46 | // NewBundle returns a new bundle on disk |
| 47 | func NewBundle(ctx context.Context, root, state, id string, spec typeurl.Any) (b *Bundle, err error) { |
| 48 | if err := identifiers.Validate(id); err != nil { |
| 49 | return nil, fmt.Errorf("invalid task id %s: %w", id, err) |
| 50 | } |
| 51 | |
| 52 | ns, err := namespaces.NamespaceRequired(ctx) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | work := filepath.Join(root, ns, id) |
| 57 | b = &Bundle{ |
| 58 | ID: id, |
| 59 | Path: filepath.Join(state, ns, id), |
| 60 | Namespace: ns, |
| 61 | } |
| 62 | var paths []string |
| 63 | defer func() { |
| 64 | if err != nil { |
| 65 | for _, d := range paths { |
| 66 | os.RemoveAll(d) |
| 67 | } |
| 68 | } |
| 69 | }() |
| 70 | // create state directory for the bundle |
| 71 | if err := os.MkdirAll(filepath.Dir(b.Path), 0711); err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | if err := os.Mkdir(b.Path, 0700); err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | if typeurl.Is(spec, &specs.Spec{}) { |
| 78 | if err := prepareBundleDirectoryPermissions(b.Path, spec.GetValue()); err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | } |
| 82 | paths = append(paths, b.Path) |
| 83 | // create working directory for the bundle |
| 84 | if err := os.MkdirAll(filepath.Dir(work), 0711); err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | rootfs := filepath.Join(b.Path, "rootfs") |
| 88 | if err := os.MkdirAll(rootfs, 0711); err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | paths = append(paths, rootfs) |
| 92 | if err := os.Mkdir(work, 0711); err != nil { |
| 93 | if !os.IsExist(err) { |
| 94 | return nil, err |
| 95 | } |
| 96 | os.RemoveAll(work) |
| 97 | if err := os.Mkdir(work, 0711); err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | } |
| 101 | paths = append(paths, work) |
| 102 | // symlink workdir |
| 103 | if err := os.Symlink(work, filepath.Join(b.Path, "work")); err != nil { |
| 104 | return nil, err |
searching dependent graphs…