Esempio n. 1
0
func addImagesub(imageSClient *srpc.Client,
	objectClient *objectclient.ObjectClient,
	name, subName, filterFilename, triggersFilename string) error {
	imageExists, err := client.CheckImage(imageSClient, name)
	if err != nil {
		return errors.New("error checking for image existance: " + err.Error())
	}
	if imageExists {
		return errors.New("image exists")
	}
	newImage := new(image.Image)
	if err := loadImageFiles(newImage, objectClient, filterFilename,
		triggersFilename); err != nil {
		return err
	}
	fs, err := pollImage(subName)
	if err != nil {
		return err
	}
	if fs, err = applyDeleteFilter(fs); err != nil {
		return err
	}
	fs = fs.Filter(newImage.Filter)
	if err := spliceComputedFiles(fs); err != nil {
		return err
	}
	if err := copyMissingObjects(fs, imageSClient, objectClient,
		subName); err != nil {
		return err
	}
	newImage.FileSystem = fs
	return addImage(imageSClient, name, newImage)
}
Esempio n. 2
0
func (imdb *ImageDataBase) loadFile(filename string, logger *log.Logger) error {
	pathname := path.Join(imdb.baseDir, filename)
	file, err := os.Open(pathname)
	if err != nil {
		return err
	}
	defer file.Close()
	reader := fsutil.NewChecksumReader(file)
	decoder := gob.NewDecoder(reader)
	var image image.Image
	if err := decoder.Decode(&image); err != nil {
		return err
	}
	if err := reader.VerifyChecksum(); err != nil {
		if err == fsutil.ErrorChecksumMismatch {
			logger.Printf("Checksum mismatch for image: %s\n", filename)
			return nil
		}
		if err != io.EOF {
			return err
		}
	}
	image.FileSystem.RebuildInodePointers()
	if err := image.Verify(); err != nil {
		return err
	}
	imdb.Lock()
	defer imdb.Unlock()
	if imdb.scheduleExpiration(&image, filename) {
		imdb.logger.Printf("Deleting already expired image: %s\n", filename)
		return os.Remove(pathname)
	}
	imdb.imageMap[filename] = &image
	return nil
}
Esempio n. 3
0
func loadTriggers(image *image.Image, triggersFilename string) error {
	triggersFile, err := os.Open(triggersFilename)
	if err != nil {
		return err
	}
	defer triggersFile.Close()
	decoder := json.NewDecoder(triggersFile)
	var trig triggers.Triggers
	if err = decoder.Decode(&trig.Triggers); err != nil {
		return errors.New("error decoding triggers " + err.Error())
	}
	image.Triggers = &trig
	return nil
}
Esempio n. 4
0
func (imdb *ImageDataBase) addImage(image *image.Image, name string,
	username *string) error {
	if err := image.Verify(); err != nil {
		return err
	}
	imdb.Lock()
	defer imdb.Unlock()
	if imdb.scheduleExpiration(image, name) {
		imdb.logger.Printf("Ignoring already expired image: %s\n", name)
		return nil
	}
	if _, ok := imdb.imageMap[name]; ok {
		return errors.New("image: " + name + " already exists")
	} else {
		err := imdb.checkDirectoryPermissions(path.Dir(name), username)
		if err != nil {
			return err
		}
		filename := path.Join(imdb.baseDir, name)
		file, err := os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_RDWR,
			filePerms)
		if err != nil {
			if os.IsExist(err) {
				return errors.New("cannot add a previously deleted image")
			}
			return err
		}
		defer file.Close()
		w := bufio.NewWriter(file)
		defer w.Flush()
		writer := fsutil.NewChecksumWriter(w)
		defer writer.WriteChecksum()
		encoder := gob.NewEncoder(writer)
		encoder.Encode(image)
		imdb.imageMap[name] = image
		imdb.addNotifiers.sendPlain(name, "add", imdb.logger)
		return nil
	}
}
Esempio n. 5
0
func addImage(imageSClient *srpc.Client, name string, img *image.Image) error {
	if *expiresIn > 0 {
		img.ExpiresAt = time.Now().Add(*expiresIn)
	} else {
		img.ExpiresAt = time.Time{}
	}
	if err := img.Verify(); err != nil {
		return err
	}
	if err := img.VerifyRequiredPaths(requiredPaths); err != nil {
		return err
	}
	if err := client.AddImage(imageSClient, name, img); err != nil {
		return errors.New("remote error: " + err.Error())
	}
	return nil
}