mirror of
https://github.com/clearlinux/rkt.git
synced 2026-11-26 06:01:12 +00:00
Merge pull request #332 from sgotti/treestore
cas: add a tree store (#223)
This commit is contained in:
+64
-3
@@ -43,6 +43,7 @@ const (
|
||||
imageManifestType
|
||||
|
||||
defaultPathPerm os.FileMode = 0777
|
||||
defaultFilePerm os.FileMode = 0660
|
||||
|
||||
// To ameliorate excessively long paths, keys for the (blob)store use
|
||||
// only the first half of a sha512 rather than the entire sum
|
||||
@@ -60,9 +61,10 @@ var diskvStores = [...]string{
|
||||
|
||||
// Store encapsulates a content-addressable-storage for storing ACIs on disk.
|
||||
type Store struct {
|
||||
base string
|
||||
stores []*diskv.Diskv
|
||||
db *DB
|
||||
base string
|
||||
stores []*diskv.Diskv
|
||||
db *DB
|
||||
treestore *TreeStore
|
||||
}
|
||||
|
||||
func NewStore(base string) (*Store, error) {
|
||||
@@ -116,6 +118,8 @@ func NewStore(base string) (*Store, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ds.treestore = &TreeStore{path: filepath.Join(base, "cas", "tree")}
|
||||
|
||||
return ds, nil
|
||||
}
|
||||
|
||||
@@ -247,9 +251,62 @@ func (ds Store) WriteACI(r io.Reader, latest bool) (string, error) {
|
||||
}); err != nil {
|
||||
return "", fmt.Errorf("error writing ACI Info: %v", err)
|
||||
}
|
||||
|
||||
// The treestore for this ACI is not written here as ACIs downloaded as
|
||||
// dependencies of another ACI will be exploded also if never directly used.
|
||||
// Users of treestore should call ds.RenderTreeStore before using it.
|
||||
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// RenderTreeStore renders a treestore for the given image key if it's not
|
||||
// already fully rendered.
|
||||
// Users of treestore should call ds.RenderTreeStore before using it to ensure
|
||||
// that the treestore is completely rendered.
|
||||
func (ds Store) RenderTreeStore(key string, rebuild bool) error {
|
||||
if !rebuild {
|
||||
rendered, err := ds.treestore.IsRendered(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot determine if tree is already rendered: %v", err)
|
||||
}
|
||||
if rendered {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
// Firstly remove a possible partial treestore if existing.
|
||||
// This is needed as a previous ACI removal operation could have failed
|
||||
// cleaning the tree store leaving some stale files.
|
||||
err := ds.treestore.Remove(key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ds.treestore.Write(key, &ds)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckTreeStore verifies the treestore consistency for the specified key.
|
||||
func (ds Store) CheckTreeStore(key string) error {
|
||||
return ds.treestore.Check(key)
|
||||
}
|
||||
|
||||
// GetTreeStorePath returns the absolute path of the treestore for the specified key.
|
||||
// It doesn't ensure that the path exists and is fully rendered. This should
|
||||
// be done calling IsRendered()
|
||||
func (ds Store) GetTreeStorePath(key string) string {
|
||||
return ds.treestore.GetPath(key)
|
||||
}
|
||||
|
||||
// GetTreeStoreRootFS returns the absolute path of the rootfs in the treestore
|
||||
// for specified key.
|
||||
// It doesn't ensure that the rootfs exists and is fully rendered. This should
|
||||
// be done calling IsRendered()
|
||||
func (ds Store) GetTreeStoreRootFS(key string) string {
|
||||
return ds.treestore.GetRootFS(key)
|
||||
}
|
||||
|
||||
// GetRemote tries to retrieve a remote with the given ACIURL. found will be
|
||||
// false if remote doesn't exist.
|
||||
func (ds Store) GetRemote(aciURL string) (*Remote, bool, error) {
|
||||
@@ -380,6 +437,10 @@ func (ds Store) Dump(hex bool) {
|
||||
// calculates its sum, and returns a string which should be used as the key to
|
||||
// store the data matching the hash.
|
||||
func (ds Store) HashToKey(h hash.Hash) string {
|
||||
return hashToKey(h)
|
||||
}
|
||||
|
||||
func hashToKey(h hash.Hash) string {
|
||||
s := h.Sum(nil)
|
||||
return keyToString(s)
|
||||
}
|
||||
|
||||
+120
@@ -20,6 +20,7 @@ import (
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -330,3 +331,122 @@ func TestGetAci(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeStore(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", tstprefix)
|
||||
if err != nil {
|
||||
t.Fatalf("error creating tempdir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
ds, err := NewStore(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
imj := `
|
||||
{
|
||||
"acKind": "ImageManifest",
|
||||
"acVersion": "0.4.1",
|
||||
"name": "example.com/test01"
|
||||
}
|
||||
`
|
||||
|
||||
entries := []*aci.ACIEntry{
|
||||
// An empty dir
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/a",
|
||||
Typeflag: tar.TypeDir,
|
||||
},
|
||||
},
|
||||
{
|
||||
Contents: "hello",
|
||||
Header: &tar.Header{
|
||||
Name: "hello.txt",
|
||||
Size: 5,
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/link.txt",
|
||||
Linkname: "rootfs/hello.txt",
|
||||
Typeflag: tar.TypeSymlink,
|
||||
},
|
||||
},
|
||||
// dangling symlink
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/link2.txt",
|
||||
Linkname: "rootfs/missingfile.txt",
|
||||
Typeflag: tar.TypeSymlink,
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/fifo",
|
||||
Typeflag: tar.TypeFifo,
|
||||
},
|
||||
},
|
||||
}
|
||||
aci, err := aci.NewACI(dir, imj, entries)
|
||||
if err != nil {
|
||||
t.Fatalf("error creating test tar: %v", err)
|
||||
}
|
||||
defer aci.Close()
|
||||
|
||||
// Rewind the ACI
|
||||
if _, err := aci.Seek(0, 0); err != nil {
|
||||
t.Fatalf("unexpected error %v", err)
|
||||
}
|
||||
|
||||
// Import the new ACI
|
||||
key, err := ds.WriteACI(aci, false)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Ask the store to render the treestore
|
||||
err = ds.RenderTreeStore(key, false)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify image Hash. Should be the same.
|
||||
err = ds.CheckTreeStore(key)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Change a file permission
|
||||
rootfs := ds.GetTreeStoreRootFS(key)
|
||||
err = os.Chmod(filepath.Join(rootfs, "a"), 0600)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify image Hash. Should be different
|
||||
err = ds.CheckTreeStore(key)
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// rebuild the tree
|
||||
err = ds.RenderTreeStore(key, true)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Add a file
|
||||
rootfs = ds.GetTreeStoreRootFS(key)
|
||||
err = ioutil.WriteFile(filepath.Join(rootfs, "newfile"), []byte("newfile"), 0644)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify image Hash. Should be different
|
||||
err = ds.CheckTreeStore(key)
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+354
@@ -0,0 +1,354 @@
|
||||
package cas
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"crypto/sha512"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
specaci "github.com/coreos/rocket/Godeps/_workspace/src/github.com/appc/spec/aci"
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/appc/spec/pkg/tarheader"
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/appc/spec/schema/types"
|
||||
"github.com/coreos/rocket/pkg/aci"
|
||||
)
|
||||
|
||||
const (
|
||||
hashfilename = "hash"
|
||||
renderedfilename = "rendered"
|
||||
)
|
||||
|
||||
// TreeStore represents a store of rendered ACIs
|
||||
// The image's key becomes the name of the directory containing the rendered aci.
|
||||
type TreeStore struct {
|
||||
path string
|
||||
}
|
||||
|
||||
// Write renders the ACI with the provided key in the treestore
|
||||
// Write, to avoid having a rendered ACI with old stale files, requires that
|
||||
// the destination directory doesn't exist (usually Remove should be called
|
||||
// before Write)
|
||||
func (ts *TreeStore) Write(key string, ds *Store) error {
|
||||
treepath := filepath.Join(ts.path, key)
|
||||
fi, _ := os.Stat(treepath)
|
||||
if fi != nil {
|
||||
return fmt.Errorf("treestore: path %s already exists", treepath)
|
||||
}
|
||||
imageID, err := types.NewHash(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: cannot convert key to imageID: %v", err)
|
||||
}
|
||||
err = aci.RenderACIWithImageID(*imageID, treepath, ds)
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: cannot render aci: %v", err)
|
||||
}
|
||||
hash, err := ts.Hash(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: cannot calculate tree hash: %v", err)
|
||||
}
|
||||
err = ioutil.WriteFile(filepath.Join(treepath, hashfilename), []byte(hash), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: cannot write hash file: %v", err)
|
||||
}
|
||||
// before creating the "rendered" flag file we need to ensure that all data is fsynced
|
||||
err = filepath.Walk(treepath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.Mode().IsDir() && !info.Mode().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
err = f.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: failed to sync data: %v", err)
|
||||
}
|
||||
// Create rendered file
|
||||
f, err := os.Create(filepath.Join(treepath, renderedfilename))
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: failed to write rendered file: %v", err)
|
||||
}
|
||||
f.Close()
|
||||
|
||||
df, err := os.Open(treepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer df.Close()
|
||||
err = df.Sync()
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: failed to sync tree store directory: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Remove cleans the directory for the specified key
|
||||
func (ts *TreeStore) Remove(key string) error {
|
||||
treepath := filepath.Join(ts.path, key)
|
||||
// If tree path doesn't exist we're done
|
||||
_, err := os.Stat(treepath)
|
||||
if err != nil && os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: failed to open tree store directory: %v", err)
|
||||
}
|
||||
|
||||
renderedFilePath := filepath.Join(treepath, renderedfilename)
|
||||
// The "rendered" flag file should be the firstly removed file. So if
|
||||
// the removal ends with some error leaving some stale files IsRendered()
|
||||
// will return false.
|
||||
_, err = os.Stat(renderedFilePath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
err := os.Remove(renderedFilePath)
|
||||
// Ensure that the treepath directory is fsynced after removing the
|
||||
// "rendered" flag file
|
||||
f, err := os.Open(treepath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: failed to open tree store directory: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
err = f.Sync()
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: failed to sync tree store directory: %v", err)
|
||||
}
|
||||
}
|
||||
return os.RemoveAll(treepath)
|
||||
}
|
||||
|
||||
// IsRendered checks if the tree store is fully rendered
|
||||
func (ts *TreeStore) IsRendered(key string) (bool, error) {
|
||||
// if the "rendered" flag file exists, assume that the store is already
|
||||
// fully rendered.
|
||||
treepath := filepath.Join(ts.path, key)
|
||||
_, err := os.Stat(filepath.Join(treepath, renderedfilename))
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetPath returns the absolute path of the treestore for the specified key.
|
||||
// It doesn't ensure that the path exists and is fully rendered. This should
|
||||
// be done calling IsRendered()
|
||||
func (ts *TreeStore) GetPath(key string) string {
|
||||
return filepath.Join(ts.path, key)
|
||||
}
|
||||
|
||||
// GetRootFS returns the absolute path of the rootfs for the specified key.
|
||||
// It doesn't ensure that the rootfs exists and is fully rendered. This should
|
||||
// be done calling IsRendered()
|
||||
func (ts *TreeStore) GetRootFS(key string) string {
|
||||
return filepath.Join(ts.GetPath(key), "rootfs")
|
||||
}
|
||||
|
||||
// TreeStore calculates an hash of the rendered ACI. It uses the same functions
|
||||
// used to create a tar but instead of writing the full archive is just
|
||||
// computes the sha512 sum of the file infos and contents.
|
||||
func (ts *TreeStore) Hash(key string) (string, error) {
|
||||
treepath := filepath.Join(ts.path, key)
|
||||
|
||||
hash := sha512.New()
|
||||
iw := NewHashWriter(hash)
|
||||
err := filepath.Walk(treepath, buildWalker(treepath, iw))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("treestore: error walking rootfs: %v", err)
|
||||
}
|
||||
|
||||
hashstring := hashToKey(hash)
|
||||
|
||||
return hashstring, nil
|
||||
}
|
||||
|
||||
// Check calculates the actual rendered ACI's hash and verifies that it matches
|
||||
// the saved value.
|
||||
func (ts *TreeStore) Check(key string) error {
|
||||
treepath := filepath.Join(ts.path, key)
|
||||
hash, err := ioutil.ReadFile(filepath.Join(treepath, hashfilename))
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: cannot read hash file: %v", err)
|
||||
}
|
||||
curhash, err := ts.Hash(key)
|
||||
if err != nil {
|
||||
return fmt.Errorf("treestore: cannot calculate tree hash: %v", err)
|
||||
}
|
||||
if curhash != string(hash) {
|
||||
return fmt.Errorf("treestore: wrong tree hash: %s, expected: %s", curhash, hash)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type xattr struct {
|
||||
Name string
|
||||
Value string
|
||||
}
|
||||
|
||||
// Like tar Header but, to keep json output reproducible:
|
||||
// * Xattrs as a slice
|
||||
// * Skip Uname and Gname
|
||||
// TODO. Should ModTime/AccessTime/ChangeTime be saved? For validation its
|
||||
// probably enough to hash the file contents and the other infos and avoid
|
||||
// problems due to them changing.
|
||||
// TODO(sgotti) Is it possible that json output will change between go
|
||||
// versions? Use another or our own Marshaller?
|
||||
type fileInfo struct {
|
||||
Name string // name of header file entry
|
||||
Mode int64 // permission and mode bits
|
||||
Uid int // user id of owner
|
||||
Gid int // group id of owner
|
||||
Size int64 // length in bytes
|
||||
Typeflag byte // type of header entry
|
||||
Linkname string // target name of link
|
||||
Devmajor int64 // major number of character or block device
|
||||
Devminor int64 // minor number of character or block device
|
||||
Xattrs []xattr
|
||||
}
|
||||
|
||||
func FileInfoFromHeader(hdr *tar.Header) *fileInfo {
|
||||
fi := &fileInfo{
|
||||
Name: hdr.Name,
|
||||
Mode: hdr.Mode,
|
||||
Uid: hdr.Uid,
|
||||
Gid: hdr.Gid,
|
||||
Size: hdr.Size,
|
||||
Typeflag: hdr.Typeflag,
|
||||
Linkname: hdr.Linkname,
|
||||
Devmajor: hdr.Devmajor,
|
||||
Devminor: hdr.Devminor,
|
||||
}
|
||||
keys := make([]string, len(hdr.Xattrs))
|
||||
for k := range hdr.Xattrs {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
xattrs := make([]xattr, 0)
|
||||
for _, k := range keys {
|
||||
xattrs = append(xattrs, xattr{Name: k, Value: hdr.Xattrs[k]})
|
||||
}
|
||||
fi.Xattrs = xattrs
|
||||
return fi
|
||||
}
|
||||
|
||||
// TODO(sgotti) this func is copied from appcs/spec/aci/build.go but also
|
||||
// removes the hashfile and the renderedfile. Find a way to reuse it.
|
||||
func buildWalker(root string, aw specaci.ArchiveWriter) filepath.WalkFunc {
|
||||
// cache of inode -> filepath, used to leverage hard links in the archive
|
||||
inos := map[uint64]string{}
|
||||
return func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
relpath, err := filepath.Rel(root, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if relpath == "." {
|
||||
return nil
|
||||
}
|
||||
if relpath == specaci.ManifestFile || relpath == hashfilename || relpath == renderedfilename {
|
||||
// ignore; this will be written by the archive writer
|
||||
// TODO(jonboulle): does this make sense? maybe just remove from archivewriter?
|
||||
return nil
|
||||
}
|
||||
|
||||
link := ""
|
||||
var r io.Reader
|
||||
switch info.Mode() & os.ModeType {
|
||||
case os.ModeSocket:
|
||||
return nil
|
||||
case os.ModeNamedPipe:
|
||||
case os.ModeCharDevice:
|
||||
case os.ModeDevice:
|
||||
case os.ModeDir:
|
||||
case os.ModeSymlink:
|
||||
target, err := os.Readlink(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
link = target
|
||||
default:
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
r = file
|
||||
}
|
||||
|
||||
hdr, err := tar.FileInfoHeader(info, link)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Because os.FileInfo's Name method returns only the base
|
||||
// name of the file it describes, it may be necessary to
|
||||
// modify the Name field of the returned header to provide the
|
||||
// full path name of the file.
|
||||
hdr.Name = relpath
|
||||
tarheader.Populate(hdr, info, inos)
|
||||
// If the file is a hard link to a file we've already seen, we
|
||||
// don't need the contents
|
||||
if hdr.Typeflag == tar.TypeLink {
|
||||
hdr.Size = 0
|
||||
r = nil
|
||||
}
|
||||
|
||||
if err := aw.AddFile(hdr, r); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type imageHashWriter struct {
|
||||
io.Writer
|
||||
}
|
||||
|
||||
func NewHashWriter(w io.Writer) specaci.ArchiveWriter {
|
||||
return &imageHashWriter{w}
|
||||
}
|
||||
|
||||
func (aw *imageHashWriter) AddFile(hdr *tar.Header, r io.Reader) error {
|
||||
// Write the json encoding of the FileInfo struct
|
||||
hdrj, err := json.Marshal(FileInfoFromHeader(hdr))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = aw.Writer.Write(hdrj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r != nil {
|
||||
// Write the file data
|
||||
_, err := io.Copy(aw.Writer, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (aw *imageHashWriter) Close() error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package cas
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/coreos/rocket/pkg/aci"
|
||||
)
|
||||
|
||||
func treeStoreDSWriteACI(dir string, ds *Store) (string, error) {
|
||||
imj := `
|
||||
{
|
||||
"acKind": "ImageManifest",
|
||||
"acVersion": "0.4.1",
|
||||
"name": "example.com/test01"
|
||||
}
|
||||
`
|
||||
|
||||
entries := []*aci.ACIEntry{
|
||||
// An empty dir
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/a",
|
||||
Typeflag: tar.TypeDir,
|
||||
},
|
||||
},
|
||||
{
|
||||
Contents: "hello",
|
||||
Header: &tar.Header{
|
||||
Name: "hello.txt",
|
||||
Size: 5,
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/link.txt",
|
||||
Linkname: "rootfs/hello.txt",
|
||||
Typeflag: tar.TypeSymlink,
|
||||
},
|
||||
},
|
||||
// dangling symlink
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/link2.txt",
|
||||
Linkname: "rootfs/missingfile.txt",
|
||||
Typeflag: tar.TypeSymlink,
|
||||
},
|
||||
},
|
||||
{
|
||||
Header: &tar.Header{
|
||||
Name: "rootfs/fifo",
|
||||
Typeflag: tar.TypeFifo,
|
||||
},
|
||||
},
|
||||
}
|
||||
aci, err := aci.NewACI(dir, imj, entries)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer aci.Close()
|
||||
|
||||
// Rewind the ACI
|
||||
if _, err := aci.Seek(0, 0); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Import the new ACI
|
||||
key, err := ds.WriteACI(aci, false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func TestTreeStoreWrite(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", tstprefix)
|
||||
if err != nil {
|
||||
t.Fatalf("error creating tempdir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
ds, err := NewStore(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
key, err := treeStoreDSWriteACI(dir, ds)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Ask the store to render the treestore
|
||||
err = ds.treestore.Write(key, ds)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify image Hash. Should be the same.
|
||||
err = ds.treestore.Check(key)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTreeStoreRemove(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", tstprefix)
|
||||
if err != nil {
|
||||
t.Fatalf("error creating tempdir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
ds, err := NewStore(dir)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
key, err := treeStoreDSWriteACI(dir, ds)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Test non existent dir
|
||||
err = ds.treestore.Remove(key)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Test rendered tree
|
||||
err = ds.treestore.Write(key, ds)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
err = ds.treestore.Remove(key)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
+73
-2
@@ -18,12 +18,14 @@ package aci
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/appc/spec/aci"
|
||||
"github.com/coreos/rocket/Godeps/_workspace/src/github.com/appc/spec/schema"
|
||||
@@ -35,10 +37,76 @@ type ACIEntry struct {
|
||||
Contents string
|
||||
}
|
||||
|
||||
type imageArchiveWriter struct {
|
||||
*tar.Writer
|
||||
am *schema.ImageManifest
|
||||
}
|
||||
|
||||
// NewImageWriter creates a new ArchiveWriter which will generate an App
|
||||
// Container Image based on the given manifest and write it to the given
|
||||
// tar.Writer
|
||||
// TODO(sgotti) this is a copy of appc/spec/aci.imageArchiveWriter with
|
||||
// addFileNow changed to create the file with the current user. needed for
|
||||
// testing as non root user.
|
||||
func NewImageWriter(am schema.ImageManifest, w *tar.Writer) aci.ArchiveWriter {
|
||||
aw := &imageArchiveWriter{
|
||||
w,
|
||||
&am,
|
||||
}
|
||||
return aw
|
||||
}
|
||||
|
||||
func (aw *imageArchiveWriter) AddFile(hdr *tar.Header, r io.Reader) error {
|
||||
err := aw.Writer.WriteHeader(hdr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r != nil {
|
||||
_, err := io.Copy(aw.Writer, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (aw *imageArchiveWriter) addFileNow(path string, contents []byte) error {
|
||||
buf := bytes.NewBuffer(contents)
|
||||
now := time.Now()
|
||||
hdr := tar.Header{
|
||||
Name: path,
|
||||
Mode: 0644,
|
||||
Uid: os.Getuid(),
|
||||
Gid: os.Getgid(),
|
||||
Size: int64(buf.Len()),
|
||||
ModTime: now,
|
||||
Typeflag: tar.TypeReg,
|
||||
ChangeTime: now,
|
||||
}
|
||||
return aw.AddFile(&hdr, buf)
|
||||
}
|
||||
|
||||
func (aw *imageArchiveWriter) addManifest(name string, m json.Marshaler) error {
|
||||
out, err := m.MarshalJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return aw.addFileNow(name, out)
|
||||
}
|
||||
|
||||
func (aw *imageArchiveWriter) Close() error {
|
||||
if err := aw.addManifest(aci.ManifestFile, aw.am); err != nil {
|
||||
return err
|
||||
}
|
||||
return aw.Writer.Close()
|
||||
}
|
||||
|
||||
// NewBasicACI creates a new ACI in the given directory with the given name.
|
||||
// Used for testing.
|
||||
func NewBasicACI(dir string, name string) (*os.File, error) {
|
||||
manifest := fmt.Sprintf(`{"acKind":"ImageManifest","acVersion":"0.1.1","name":"%s"}`, name)
|
||||
manifest := fmt.Sprintf(`{"acKind":"ImageManifest","acVersion":"0.4.1","name":"%s"}`, name)
|
||||
return NewACI(dir, manifest, nil)
|
||||
}
|
||||
|
||||
@@ -58,7 +126,7 @@ func NewACI(dir string, manifest string, entries []*ACIEntry) (*os.File, error)
|
||||
defer os.Remove(tf.Name())
|
||||
|
||||
tw := tar.NewWriter(tf)
|
||||
aw := aci.NewImageWriter(im, tw)
|
||||
aw := NewImageWriter(im, tw)
|
||||
|
||||
for _, entry := range entries {
|
||||
// Add default mode
|
||||
@@ -69,6 +137,9 @@ func NewACI(dir string, manifest string, entries []*ACIEntry) (*os.File, error)
|
||||
entry.Header.Mode = 0644
|
||||
}
|
||||
}
|
||||
// Add calling user uid and gid or tests will fail
|
||||
entry.Header.Uid = os.Getuid()
|
||||
entry.Header.Gid = os.Getgid()
|
||||
sr := strings.NewReader(entry.Contents)
|
||||
if err := aw.AddFile(entry.Header, sr); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user