func (d *Driver) ListVolume(id, snapshotID string) ([]byte, error) {
	volumes := api.DeviceMapperVolumes{
		Volumes: make(map[string]api.DeviceMapperVolume),
	}
	if id != "" {
		volume := d.loadVolume(id)
		if volume == nil {
			return nil, generateError(logrus.Fields{
				LOG_FIELD_VOLUME: id,
			}, "volume doesn't exists")
		}
		if snapshotID != "" {
			volumes.Volumes[id] = *getVolumeSnapshotInfo(id, volume, snapshotID)
		} else {
			volumes.Volumes[id] = *getVolumeInfo(id, volume)
		}

	} else {
		volumeIDs := d.listVolumeIDs()
		for _, uuid := range volumeIDs {
			volume := d.loadVolume(uuid)
			if volume == nil {
				return nil, generateError(logrus.Fields{
					LOG_FIELD_VOLUME: uuid,
				}, "Volume list changed for volume")
			}
			volumes.Volumes[uuid] = *getVolumeInfo(uuid, volume)
		}
	}
	return api.ResponseOutput(volumes)
}
func listVolume(volumeID, snapshotID string, driver ObjectStoreDriver) ([]byte, error) {
	log.WithFields(logrus.Fields{
		LOG_FIELD_VOLUME:   volumeID,
		LOG_FIELD_SNAPSHOT: snapshotID,
	}).Debug("Listing objectstore for volume and snapshot")
	resp := api.VolumesResponse{
		Volumes: make(map[string]api.VolumeResponse),
	}

	v, err := loadVolumeConfig(volumeID, driver)
	if err != nil {
		// Volume doesn't exist
		return api.ResponseOutput(resp)
	}

	snapshots, err := getSnapshots(volumeID, driver)
	if err != nil {
		return nil, err
	}

	volumeResp := api.VolumeResponse{
		UUID:      volumeID,
		Name:      v.Name,
		Base:      v.Base,
		Size:      v.Size,
		Snapshots: make(map[string]api.SnapshotResponse),
	}

	if snapshotID != "" {
		if _, exists := snapshots[snapshotID]; exists {
			volumeResp.Snapshots[snapshotID] = api.SnapshotResponse{
				UUID:       snapshotID,
				VolumeUUID: volumeID,
			}
		}
	} else {
		for s := range snapshots {
			volumeResp.Snapshots[s] = api.SnapshotResponse{
				UUID:       s,
				VolumeUUID: volumeID,
			}
		}
	}
	resp.Volumes[volumeID] = volumeResp
	return api.ResponseOutput(resp)
}
Example #3
0
func writeResponseOutput(w http.ResponseWriter, v interface{}) error {
	output, err := api.ResponseOutput(v)
	if err != nil {
		return err
	}
	log.Debugln("Response: ", string(output))
	_, err = w.Write(output)
	return err
}
func (d *Driver) Info() ([]byte, error) {
	// from sector count to byte
	blockSize := d.ThinpoolBlockSize * 512

	dmInfo := api.DeviceMapperInfo{
		Driver:            d.Name(),
		Root:              d.Root,
		DataDevice:        d.DataDevice,
		MetadataDevice:    d.MetadataDevice,
		ThinpoolDevice:    d.ThinpoolDevice,
		ThinpoolSize:      d.ThinpoolSize,
		ThinpoolBlockSize: blockSize,
	}

	data, err := api.ResponseOutput(dmInfo)
	if err != nil {
		return nil, err
	}

	return data, nil
}
func (s *Server) ListVolume(volumeUUID, snapshotUUID string) ([]byte, error) {
	resp := api.VolumesResponse{
		Volumes: make(map[string]api.VolumeResponse),
	}

	var volumeUUIDs []string

	if volumeUUID != "" {
		volumeUUIDs = append(volumeUUIDs, volumeUUID)
	} else {
		volumeUUIDs = util.ListConfigIDs(s.Root, VOLUME_CFG_PREFIX, CFG_POSTFIX)
	}

	for _, uuid := range volumeUUIDs {
		volume := s.loadVolume(uuid)
		if volume == nil {
			return nil, fmt.Errorf("Volume list changed for volume %v", uuid)
		}
		resp.Volumes[uuid] = *getVolumeInfo(volume, snapshotUUID)
	}

	return api.ResponseOutput(resp)
}
func (s *Server) doInfo(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	s.GlobalLock.RLock()
	defer s.GlobalLock.RUnlock()

	var err error
	_, err = w.Write([]byte(fmt.Sprint("{\n\"General\" : ")))
	if err != nil {
		return err
	}

	data, err := api.ResponseOutput(&s.Config)
	if err != nil {
		return err
	}
	_, err = w.Write(data)
	if err != nil {
		return err
	}
	if _, err := w.Write([]byte(fmt.Sprint(",\n\"Driver\" : "))); err != nil {
		return err
	}

	driver := s.StorageDriver
	data, err = driver.Info()
	if err != nil {
		return err
	}
	_, err = w.Write(data)
	if err != nil {
		return err
	}
	if _, err := w.Write([]byte(fmt.Sprint("\n}"))); err != nil {
		return err
	}

	return nil
}
func AddImage(root, imageDir, imageUUID, imageName, imageFilePath, objectstoreUUID string) ([]byte, error) {
	imageStat, err := os.Stat(imageFilePath)
	if os.IsNotExist(err) || imageStat.IsDir() {
		return nil, fmt.Errorf("Invalid image file")
	}
	imageLocalStorePath := GetImageLocalStorePath(imageDir, imageUUID)
	if _, err := os.Stat(imageLocalStorePath); err == nil {
		return nil, generateError(logrus.Fields{
			LOG_FIELD_IMAGE: imageUUID,
		}, "UUID is already used by another image")
	}

	_, bsDriver, err := getObjectStoreCfgAndDriver(root, objectstoreUUID)
	if err != nil {
		return nil, err
	}

	imageObjectStorePath := getImageObjectStorePath(imageUUID)
	imageCfgObjectStorePath := getImageCfgObjectStorePath(imageUUID)

	imageExists := bsDriver.FileExists(imageObjectStorePath)
	imageCfgExists := bsDriver.FileExists(imageCfgObjectStorePath)
	if imageExists && imageCfgExists {
		return nil, generateError(logrus.Fields{
			LOG_FIELD_IMAGE: imageUUID,
		}, "The image already existed in objectstore")
	} else if imageExists != imageCfgExists {
		return nil, generateError(logrus.Fields{
			LOG_FIELD_IMAGE: imageUUID,
		}, "The image state is inconsistent in objectstore")
	}

	if imageStat.Size()%DEFAULT_BLOCK_SIZE != 0 {
		return nil, fmt.Errorf("The image size must be multiplier of %v", DEFAULT_BLOCK_SIZE)
	}

	image := &Image{}
	image.UUID = imageUUID
	image.Name = imageName
	image.Size = imageStat.Size()

	log.Debugf("Copying image %v to local store %v", imageFilePath, imageLocalStorePath)
	if err := util.Copy(imageFilePath, imageLocalStorePath); err != nil {
		log.Debugf("Copying image failed")
		return nil, err
	}
	log.Debug("Copied image to local store")

	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:      LOG_REASON_START,
		LOG_FIELD_EVENT:       LOG_EVENT_UPLOAD,
		LOG_FIELD_OBJECT:      LOG_OBJECT_IMAGE,
		LOG_FIELD_IMAGE:       imageUUID,
		LOG_FIELD_IMAGE_FILE:  imageLocalStorePath,
		LOG_FIELD_OBJECTSTORE: objectstoreUUID,
	}).Debug("Uploading image to objectstore")
	if err := uploadImage(imageLocalStorePath, bsDriver, image); err != nil {
		log.Debugf("Uploading image failed")
		return nil, err
	}

	if err := saveImageConfig(imageUUID, bsDriver, image); err != nil {
		return nil, err
	}

	imageResp := api.ImageResponse{
		UUID:        image.UUID,
		Name:        image.Name,
		Size:        image.Size,
		Checksum:    image.Checksum,
		RawChecksum: image.RawChecksum,
	}
	return api.ResponseOutput(imageResp)
}