Beispiel #1
0
func (self *Pool) Activate(uuid string) (*Manifest, errors.Error) {
	// Find manifest with the given uuid
	manifest, ok := self.findManifest(uuid)
	if !ok {
		return nil, errors.ResourceNotFound(nil)
	}

	// Make sure that an image file has been uploaded
	if len(manifest.Files) == 0 {
		return nil, errors.NoActivationNoFile(nil)
	}

	// Make sure it has not been activated before
	if manifest.State != StateUnactivated {
		return nil, errors.ImageAlreadyActivated(nil)
	}

	self.lock()
	defer self.unlock()

	// Activate image
	manifest.State = StateActive
	manifest.Disabled = false
	manifest.PublishedAt = time.Now().Format(time.RFC3339)

	// Save manifests to disk
	if err := saveManifests(self.manifests); err != nil {
		return nil, errors.InternalError(err)
	}

	return manifest, nil
}
Beispiel #2
0
func (self *Pool) AddFile(uuid, compression string, reader io.Reader) (*Manifest, errors.Error) {
	// Find manifest with matching uuid
	manifest, ok := self.findManifest(uuid)
	if !ok {
		return nil, errors.ResourceNotFound(nil)
	}

	// Make sure the manifest has the correct state
	if manifest.State != StateUnactivated {
		return nil, errors.ImageAlreadyActivated(nil)
	}

	// Resolve file extension for the given compression type
	ext, ok := FileExtensions[compression]
	if !ok {
		return nil, errors.InvalidParameter(nil)
	}

	// Find absolute path for image and md5file
	imageFpath := filepath.Join(self.imageDir, fmt.Sprintf("%s.%s", uuid, ext))
	md5Fpath := filepath.Join(self.imageDir, uuid+".md5")

	// Create destination directory if it does not exist
	err := os.MkdirAll(self.imageDir, 0775)
	if err != nil {
		return nil, errors.InternalError(err)
	}

	// Open image file
	f, err := os.Create(imageFpath)
	if err != nil {
		return nil, errors.InternalError(err)
	}

	// Remember to close files
	defer f.Close()

	// Calcluate sha1 and md5 sum while writing image to disk
	// Sha1 is a required field in the manifest
	shaHash := sha1.New()
	sha1Reader := io.TeeReader(reader, shaHash)

	// Md5 is needed by imgadm client which expects the
	// content-md5 header to be present
	md5Hash := md5.New()
	md5Reader := io.TeeReader(sha1Reader, md5Hash)

	// Write image to disk
	nBytes, err := io.Copy(f, md5Reader)
	if err != nil {
		return nil, errors.Upload(err)
	}

	// Write md5sum to file
	md5sum := md5Hash.Sum(nil)
	if err := ioutil.WriteFile(md5Fpath, md5sum, 0660); err != nil {
		return nil, errors.InternalError(err)
	}

	// Add image file to manifest
	imageFile := &ImageFile{
		Sha1:        fmt.Sprintf("%x", shaHash.Sum(nil)),
		Compression: compression,
		Size:        nBytes,
	}

	// Update manifest
	self.lock()
	defer self.unlock()
	manifest.Files = []*ImageFile{imageFile}

	// Save manifests to disk
	if err := saveManifests(self.manifests); err != nil {
		return nil, errors.InternalError(err)
	}

	return manifest, nil
}