func (d *Driver) CreateSnapshot(req Request) error { d.mutex.Lock() defer d.mutex.Unlock() id := req.Name volumeID, err := util.GetFieldFromOpts(OPT_VOLUME_NAME, req.Options) if err != nil { return err } volume := d.blankVolume(volumeID) if err := util.ObjectLoad(volume); err != nil { return err } if _, exists := volume.Snapshots[id]; exists { return fmt.Errorf("Snapshot %v already exists for volume %v", id, volumeID) } snapFile := d.getSnapshotFilePath(id, volumeID) if err := util.MkdirIfNotExists(filepath.Dir(snapFile)); err != nil { return err } if err := util.CompressDir(volume.Path, snapFile); err != nil { return err } volume.Snapshots[id] = Snapshot{ Name: id, CreatedTime: util.Now(), VolumeUUID: volumeID, FilePath: snapFile, } return util.ObjectSave(volume) }
func (d *Driver) ListSnapshot(opts map[string]string) (map[string]map[string]string, error) { var ( volumeIDs []string err error ) d.mutex.RLock() defer d.mutex.RUnlock() snapshots := make(map[string]map[string]string) specifiedVolumeID, _ := util.GetFieldFromOpts(OPT_VOLUME_NAME, opts) if specifiedVolumeID != "" { volumeIDs = []string{ specifiedVolumeID, } } else { volumeIDs, err = d.listVolumeNames() if err != nil { return nil, err } } for _, volumeID := range volumeIDs { volume := d.blankVolume(volumeID) if err := util.ObjectLoad(volume); err != nil { return nil, err } for snapshotID := range volume.Snapshots { snapshots[snapshotID], err = d.getSnapshotInfo(snapshotID, volumeID) if err != nil { return nil, err } } } return snapshots, nil }
func (d *Driver) CreateSnapshot(req Request) error { var err error d.mutex.Lock() defer d.mutex.Unlock() id := req.Name volumeID, err := util.GetFieldFromOpts(OPT_VOLUME_NAME, req.Options) if err != nil { return err } volume := d.blankVolume(volumeID) if err := util.ObjectLoad(volume); err != nil { return err } devID, err := d.allocateDevID() if err != nil { return err } snapshot, exists := volume.Snapshots[id] if exists { return generateError(logrus.Fields{ LOG_FIELD_VOLUME: volumeID, LOG_FIELD_SNAPSHOT: id, }, "Already has snapshot with name") } log.WithFields(logrus.Fields{ LOG_FIELD_REASON: LOG_REASON_START, LOG_FIELD_EVENT: LOG_EVENT_CREATE, LOG_FIELD_OBJECT: LOG_OBJECT_SNAPSHOT, LOG_FIELD_SNAPSHOT: id, LOG_FIELD_VOLUME: volumeID, DM_LOG_FIELD_VOLUME_DEVID: volume.DevID, DM_LOG_FIELD_SNAPSHOT_DEVID: devID, }).Debugf("Creating snapshot") err = devicemapper.CreateSnapDevice(d.ThinpoolDevice, devID, volumeID, volume.DevID) if err != nil { return err } log.Debugf("Created snapshot device") snapshot = Snapshot{ Name: id, CreatedTime: util.Now(), DevID: devID, Activated: false, } volume.Snapshots[id] = snapshot if err := util.ObjectSave(volume); err != nil { return err } return nil }
func (d *Driver) DeleteSnapshot(req Request) error { d.mutex.Lock() defer d.mutex.Unlock() volumeID, err := util.GetFieldFromOpts(OPT_VOLUME_NAME, req.Options) if err != nil { return err } return d.deleteSnapshot(req.Name, volumeID) }
func (d *Driver) GetSnapshotInfo(req Request) (map[string]string, error) { d.mutex.RLock() defer d.mutex.RUnlock() id := req.Name volumeID, err := util.GetFieldFromOpts(OPT_VOLUME_NAME, req.Options) if err != nil { return nil, err } return d.getSnapshotInfo(id, volumeID) }
func (d *Driver) CreateSnapshot(req Request) error { d.mutex.Lock() defer d.mutex.Unlock() id := req.Name volumeID, err := util.GetFieldFromOpts(OPT_VOLUME_NAME, req.Options) if err != nil { return err } volume := d.blankVolume(volumeID) if err := util.ObjectLoad(volume); err != nil { return err } snapshot, exists := volume.Snapshots[id] if exists { return generateError(logrus.Fields{ LOG_FIELD_VOLUME: volumeID, LOG_FIELD_SNAPSHOT: id, }, "Already has snapshot with uuid") } tags := map[string]string{ "ConvoyVolumeName": volumeID, "ConvoySnapshotName": id, } request := &CreateSnapshotRequest{ VolumeID: volume.EBSID, Description: fmt.Sprintf("Convoy snapshot"), Tags: tags, } ebsSnapshotID, err := d.ebsService.CreateSnapshot(request) if err != nil { return err } log.Debugf("Creating snapshot %v(%v) of volume %v(%v)", id, ebsSnapshotID, volumeID, volume.EBSID) snapshot = Snapshot{ Name: id, VolumeName: volumeID, EBSID: ebsSnapshotID, } volume.Snapshots[id] = snapshot return util.ObjectSave(volume) }
func (d *Driver) DeleteSnapshot(req Request) error { d.mutex.Lock() defer d.mutex.Unlock() id := req.Name volumeID, err := util.GetFieldFromOpts(OPT_VOLUME_NAME, req.Options) if err != nil { return err } snapshot, volume, err := d.getSnapshotAndVolume(id, volumeID) if err != nil { return err } log.Debugf("Removing reference of snapshot %v(%v) of volume %v(%v)", id, snapshot.EBSID, volumeID, volume.EBSID) delete(volume.Snapshots, id) return util.ObjectSave(volume) }