This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
molecule.go
84 lines (69 loc) · 2.39 KB
/
molecule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package atomfs
import (
"context"
"github.com/anuvu/atomfs/types"
stackeroci "github.com/anuvu/stacker/oci"
"github.com/opencontainers/umoci/oci/casext"
)
func (atomfs *Instance) CreateMolecule(name string, atoms []types.Atom) (types.Molecule, error) {
return atomfs.db.CreateMolecule(name, atoms)
}
// CopyMolecule simply duplicates a molecule's configuration under a new name.
// This is equivalent to a "snapshot" operation under other filesystems.
func (atomfs *Instance) CopyMolecule(dest string, src string) (types.Molecule, error) {
mol, err := atomfs.db.GetMolecule(src)
if err != nil {
return types.Molecule{}, err
}
return atomfs.db.CreateMolecule(dest, mol.Atoms)
}
func (atomfs *Instance) DeleteMolecule(name string) error {
mol, err := atomfs.db.GetMolecule(name)
if err != nil {
return err
}
return atomfs.db.DeleteThing(mol.ID, "molecule")
}
func (atomfs *Instance) RenameMolecule(old, new_ string) error {
mol, err := atomfs.db.GetMolecule(old)
if err != nil {
return err
}
return atomfs.db.RenameThing(mol.ID, "molecule", new_)
}
func (atomfs *Instance) CreateMoleculeFromOCITag(oci casext.Engine, name string) (types.Molecule, error) {
man, err := stackeroci.LookupManifest(oci, name)
if err != nil {
return types.Molecule{}, err
}
atoms := []types.Atom{}
for _, l := range man.Layers {
layer, err := oci.FromDescriptor(context.Background(), l)
if err != nil {
return types.Molecule{}, err
}
defer layer.Close()
atom, err := atomfs.CreateAtomFromOCIBlob(layer)
if err != nil {
return types.Molecule{}, err
}
atoms = append(atoms, atom)
}
// The OCI spec says that the first layer should be the bottom most
// layer. In overlay it's the top most layer. Since the atomfs codebase
// is mostly a wrapper around overlayfs, let's keep things in our db in
// the same order that overlay expects them, i.e. the first layer is
// the top most. That means we need to reverse the order in which the
// atoms were inserted, because they were backwards.
//
// It's also terrible that golang doesn't have a reverse function, but
// that's a discussion for a different block comment.
for i := len(atoms)/2 - 1; i >= 0; i-- {
opp := len(atoms) - 1 - i
atoms[i], atoms[opp] = atoms[opp], atoms[i]
}
return atomfs.db.CreateMolecule(name, atoms)
}
func (atomfs *Instance) GetMolecule(name string) (types.Molecule, error) {
return atomfs.db.GetMolecule(name)
}