Example #1
0
// Get returns the image with the given id, or an error if the image doesn't exist.
func (graph *Graph) Get(name string) (*image.Image, error) {
	id, err := graph.idIndex.Get(name)
	if err != nil {
		return nil, fmt.Errorf("could not find image: %v", err)
	}
	img, err := image.LoadImage(graph.ImageRoot(id))
	if err != nil {
		return nil, err
	}
	if img.ID != id {
		return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.ID)
	}
	img.SetGraph(graph)

	if img.Size < 0 {
		size, err := graph.driver.DiffSize(img.ID, img.Parent)
		if err != nil {
			return nil, fmt.Errorf("unable to calculate size of image id %q: %s", img.ID, err)
		}

		img.Size = size
		if err := img.SaveSize(graph.ImageRoot(id)); err != nil {
			return nil, err
		}
	}
	return img, nil
}
Example #2
0
//krgo commit -r rootfs
//commit current changes in a new properly formated branch ready for pushing
func commitChanges(rootfs, message string) error {
	if !isGitRepo(rootfs) {
		return fmt.Errorf("%v not a git repository", rootfs)
	}
	gitRepo, _ := newGitRepo(rootfs)

	layerData, err := gitRepo.exportUncommitedChangeSet()
	if err != nil {
		return err
	}
	defer layerData.Close()

	//Load image data
	image, err := image.LoadImage(gitRepo.Path) //reading json file in rootfs
	if err != nil {
		return err
	}

	//fill new infos
	image.Parent = image.ID
	image.ID = utils.GenerateRandomID()
	image.Created = time.Now()
	image.Comment = message

	layer, err := archive.NewTempArchive(layerData, "")
	if err != nil {
		return err
	}
	image.Size = layer.Size
	os.RemoveAll(layer.Name())

	if err := image.SaveSize(rootfs); err != nil {
		return err
	}

	jsonRaw, err := json.Marshal(image)
	if err != nil {
		return err
	}

	err = ioutil.WriteFile(path.Join(rootfs, "json"), jsonRaw, 0600)
	if err != nil {
		return err
	}

	//commit the changes in a new branch
	n, _ := gitRepo.countBranch()
	br := newBranch(n, image.ID)
	if _, err = gitRepo.checkoutB(br); err != nil {
		return err
	}
	if _, err := gitRepo.addAllAndCommit(message); err != nil {
		return err
	}

	fmt.Printf("Changes commited in %v\n", br)
	fmt.Printf("Image ID: %v\nParent: %v\nChecksum: %v\nLayer size: %v\n", image.ID, image.Parent, image.Size)

	return nil
}
Example #3
0
// Get returns the image with the given id, or an error if the image doesn't exist.
func (graph *Graph) Get(name string) (*image.Image, error) {
	id, err := graph.idIndex.Get(name)
	if err != nil {
		return nil, err
	}
	// FIXME: return nil when the image doesn't exist, instead of an error
	img, err := image.LoadImage(graph.ImageRoot(id))
	if err != nil {
		return nil, err
	}
	if img.ID != id {
		return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.ID)
	}
	img.SetGraph(graph)

	if img.Size < 0 {
		rootfs, err := graph.driver.Get(img.ID, "")
		if err != nil {
			return nil, fmt.Errorf("Driver %s failed to get image rootfs %s: %s", graph.driver, img.ID, err)
		}
		defer graph.driver.Put(img.ID)

		var size int64
		if img.Parent == "" {
			if size, err = utils.TreeSize(rootfs); err != nil {
				return nil, err
			}
		} else {
			parentFs, err := graph.driver.Get(img.Parent, "")
			if err != nil {
				return nil, err
			}
			changes, err := archive.ChangesDirs(rootfs, parentFs)
			if err != nil {
				return nil, err
			}
			size = archive.ChangesSize(rootfs, changes)
		}

		img.Size = size
		if err := img.SaveSize(graph.ImageRoot(id)); err != nil {
			return nil, err
		}
	}
	return img, nil
}