Пример #1
0
func (s *daemon) doVolumeMount(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	var err error

	request := &api.VolumeMountRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}

	volumeName := request.VolumeName
	if err := util.CheckName(volumeName); err != nil {
		return err
	}
	volume := s.getVolume(volumeName)
	if volume == nil {
		return fmt.Errorf("volume %v doesn't exist", volumeName)
	}

	mountPoint, err := s.processVolumeMount(volume, request)
	if err != nil {
		return err
	}

	if request.Verbose {
		return writeResponseOutput(w, api.VolumeResponse{
			Name:       volumeName,
			MountPoint: mountPoint,
		})
	}
	return writeStringResponse(w, mountPoint)
}
Пример #2
0
func (s *daemon) doSnapshotDelete(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	request := &api.SnapshotDeleteRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}
	snapshotName := request.SnapshotName
	if err := util.CheckName(snapshotName); err != nil {
		return err
	}
	volumeName := s.SnapshotVolumeIndex.Get(snapshotName)
	if volumeName == "" {
		return fmt.Errorf("cannot find volume for snapshot %v", snapshotName)
	}

	volume := s.getVolume(volumeName)
	if !s.snapshotExists(volumeName, snapshotName) {
		return fmt.Errorf("snapshot %v of volume %v doesn't exist", snapshotName, volumeName)
	}

	snapOps, err := s.getSnapshotOpsForVolume(volume)
	if err != nil {
		return err
	}

	req := Request{
		Name: snapshotName,
		Options: map[string]string{
			OPT_VOLUME_NAME: volumeName,
		},
	}
	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:   LOG_REASON_PREPARE,
		LOG_FIELD_EVENT:    LOG_EVENT_DELETE,
		LOG_FIELD_OBJECT:   LOG_OBJECT_SNAPSHOT,
		LOG_FIELD_SNAPSHOT: snapshotName,
		LOG_FIELD_VOLUME:   volumeName,
	}).Debug()
	if err := snapOps.DeleteSnapshot(req); err != nil {
		return err
	}
	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:   LOG_REASON_COMPLETE,
		LOG_FIELD_EVENT:    LOG_EVENT_DELETE,
		LOG_FIELD_OBJECT:   LOG_OBJECT_SNAPSHOT,
		LOG_FIELD_SNAPSHOT: snapshotName,
		LOG_FIELD_VOLUME:   volumeName,
	}).Debug()

	//TODO: error handling
	if err := s.SnapshotVolumeIndex.Delete(snapshotName); err != nil {
		return err
	}
	if err := s.NameUUIDIndex.Delete(snapshotName); err != nil {
		return err
	}
	return nil
}
Пример #3
0
func (s *daemon) doVolumeDelete(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	request := &api.VolumeDeleteRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}

	if err := util.CheckName(request.VolumeName); err != nil {
		return err
	}

	return s.processVolumeDelete(request)
}
Пример #4
0
func (s *daemon) doSnapshotInspect(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	request := &api.SnapshotInspectRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}
	snapshotName := request.SnapshotName
	if err := util.CheckName(snapshotName); err != nil {
		return err
	}
	volumeName := s.SnapshotVolumeIndex.Get(snapshotName)
	if volumeName == "" {
		return fmt.Errorf("cannot find volume for snapshot %v", snapshotName)
	}

	volume := s.getVolume(volumeName)
	if volume == nil {
		return fmt.Errorf("cannot find volume %v", volumeName)
	}

	volumeDriverInfo, err := s.getVolumeDriverInfo(volume)
	if err != nil {
		return err
	}

	snapshot, err := s.getSnapshotDriverInfo(snapshotName, volume)
	if err != nil {
		return fmt.Errorf("cannot find snapshot %v of volume %v", snapshotName, volumeName)
	}

	driverInfo, err := s.getSnapshotDriverInfo(snapshotName, volume)
	if err != nil {
		return err
	}

	resp := api.SnapshotResponse{
		Name:            snapshotName,
		VolumeName:      volumeName,
		VolumeCreatedAt: volumeDriverInfo[OPT_VOLUME_CREATED_TIME],
		CreatedTime:     snapshot[OPT_SNAPSHOT_CREATED_TIME],
		DriverInfo:      driverInfo,
	}
	data, err := api.ResponseOutput(resp)
	if err != nil {
		return err
	}
	_, err = w.Write(data)
	return err
}
Пример #5
0
func (s *daemon) doVolumeUmount(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	request := &api.VolumeUmountRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}

	volumeName := request.VolumeName
	if err := util.CheckName(volumeName); err != nil {
		return err
	}
	volume := s.getVolume(volumeName)
	if volume == nil {
		return fmt.Errorf("volume %v doesn't exist", volumeName)
	}

	return s.processVolumeUmount(volume)
}
Пример #6
0
func (s *daemon) doVolumeInspect(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	request := &api.VolumeInspectRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}

	name := request.VolumeName
	if err := util.CheckName(name); err != nil {
		return err
	}

	data, err := s.inspectVolume(name)
	if err != nil {
		return err
	}
	_, err = w.Write(data)
	return err
}
Пример #7
0
func getName(c *cli.Context, key string, required bool) (string, error) {
	var err error
	var name string
	if key == "" {
		name = c.Args().First()
	} else {
		name, err = util.GetFlag(c, key, required, err)
		if err != nil {
			return "", err
		}
	}
	if name == "" && !required {
		return "", nil
	}

	if err := util.CheckName(name); err != nil {
		return "", err
	}
	return name, nil
}
Пример #8
0
func (s *daemon) doSnapshotCreate(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	s.GlobalLock.Lock()
	defer s.GlobalLock.Unlock()

	request := &api.SnapshotCreateRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}
	volumeUUID := request.VolumeUUID
	if err := util.CheckUUID(volumeUUID); err != nil {
		return err
	}
	snapshotName := request.Name
	if snapshotName != "" {
		if err := util.CheckName(snapshotName); err != nil {
			return err
		}
		existUUID := s.NameUUIDIndex.Get(snapshotName)
		if existUUID != "" {
			return fmt.Errorf("Snapshot name %v already associated with %v", snapshotName, existUUID)
		}
	}

	volume := s.loadVolume(volumeUUID)
	if volume == nil {
		return fmt.Errorf("volume %v doesn't exist", volumeUUID)
	}

	snapOps, err := s.getSnapshotOpsForVolume(volume)
	if err != nil {
		return err
	}

	uuid := uuid.New()

	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:   LOG_REASON_PREPARE,
		LOG_FIELD_EVENT:    LOG_EVENT_CREATE,
		LOG_FIELD_OBJECT:   LOG_OBJECT_SNAPSHOT,
		LOG_FIELD_SNAPSHOT: uuid,
		LOG_FIELD_VOLUME:   volumeUUID,
	}).Debug()
	if err := snapOps.CreateSnapshot(uuid, volumeUUID); err != nil {
		return err
	}
	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:   LOG_REASON_COMPLETE,
		LOG_FIELD_EVENT:    LOG_EVENT_CREATE,
		LOG_FIELD_OBJECT:   LOG_OBJECT_SNAPSHOT,
		LOG_FIELD_SNAPSHOT: uuid,
		LOG_FIELD_VOLUME:   volumeUUID,
	}).Debug()

	snapshot := Snapshot{
		UUID:        uuid,
		VolumeUUID:  volumeUUID,
		Name:        snapshotName,
		CreatedTime: util.Now(),
	}
	//TODO: error handling
	volume.Snapshots[uuid] = snapshot
	if err := s.UUIDIndex.Add(snapshot.UUID); err != nil {
		return err
	}
	if err := s.SnapshotVolumeIndex.Add(snapshot.UUID, volume.UUID); err != nil {
		return err
	}
	if snapshot.Name != "" {
		if err := s.NameUUIDIndex.Add(snapshot.Name, snapshot.UUID); err != nil {
			return err
		}
	}
	if err := s.saveVolume(volume); err != nil {
		return err
	}
	driverInfo, err := s.getSnapshotDriverInfo(snapshot.UUID, volume)
	if err != nil {
		return err
	}
	if request.Verbose {
		return writeResponseOutput(w, api.SnapshotResponse{
			UUID:        snapshot.UUID,
			VolumeUUID:  snapshot.VolumeUUID,
			Name:        snapshot.Name,
			CreatedTime: snapshot.CreatedTime,
			DriverInfo:  driverInfo,
		})
	}
	return writeStringResponse(w, snapshot.UUID)
}
Пример #9
0
func (s *daemon) doSnapshotCreate(version string, w http.ResponseWriter, r *http.Request, objs map[string]string) error {
	request := &api.SnapshotCreateRequest{}
	if err := decodeRequest(r, request); err != nil {
		return err
	}
	volumeName := request.VolumeName
	if err := util.CheckName(volumeName); err != nil {
		return err
	}
	volume := s.getVolume(volumeName)
	if volume == nil {
		return fmt.Errorf("volume %v doesn't exist", volumeName)
	}

	snapshotName := request.Name
	if snapshotName != "" {
		if err := util.CheckName(snapshotName); err != nil {
			return err
		}
		existName := s.NameUUIDIndex.Get(snapshotName)
		if existName != "" {
			return fmt.Errorf("Snapshot name %v already exists", snapshotName)
		}
	} else {
		snapshotName = util.GenerateName("snapshot")
		for s.NameUUIDIndex.Get(snapshotName) != "" {
			snapshotName = util.GenerateName("snapshot")
		}
	}

	snapOps, err := s.getSnapshotOpsForVolume(volume)
	if err != nil {
		return err
	}

	req := Request{
		Name: snapshotName,
		Options: map[string]string{
			OPT_VOLUME_NAME: volumeName,
		},
	}

	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:   LOG_REASON_PREPARE,
		LOG_FIELD_EVENT:    LOG_EVENT_CREATE,
		LOG_FIELD_OBJECT:   LOG_OBJECT_SNAPSHOT,
		LOG_FIELD_SNAPSHOT: snapshotName,
		LOG_FIELD_VOLUME:   volumeName,
	}).Debug()
	if err := snapOps.CreateSnapshot(req); err != nil {
		return err
	}
	log.WithFields(logrus.Fields{
		LOG_FIELD_REASON:   LOG_REASON_COMPLETE,
		LOG_FIELD_EVENT:    LOG_EVENT_CREATE,
		LOG_FIELD_OBJECT:   LOG_OBJECT_SNAPSHOT,
		LOG_FIELD_SNAPSHOT: snapshotName,
		LOG_FIELD_VOLUME:   volumeName,
	}).Debug()

	//TODO: error handling
	if err := s.SnapshotVolumeIndex.Add(snapshotName, volume.Name); err != nil {
		return err
	}
	if err := s.NameUUIDIndex.Add(snapshotName, "exists"); err != nil {
		return err
	}
	driverInfo, err := s.getSnapshotDriverInfo(snapshotName, volume)
	if err != nil {
		return err
	}
	if request.Verbose {
		return writeResponseOutput(w, api.SnapshotResponse{
			Name:        snapshotName,
			VolumeName:  volume.Name,
			CreatedTime: driverInfo[OPT_SNAPSHOT_CREATED_TIME],
			DriverInfo:  driverInfo,
		})
	}
	return writeStringResponse(w, snapshotName)
}