示例#1
0
func (d *DaemonConfig) handleForceRemoveLock(req *config.VolumeRequest, vc *config.Volume, locks []config.UseLocker) error {
	exists, err := control.ExistsVolume(vc, d.Global.Timeout)
	if err != nil && err != errors.NoActionTaken {
		return errors.RemoveVolume.Combine(errored.New(vc.String())).Combine(err)
	}

	if err == errors.NoActionTaken {
		if err := d.completeRemove(req, vc); err != nil {
			return err
		}

		d.removeVolumeUse(locks[0], vc)
	}

	if err != nil {
		return errors.RemoveVolume.Combine(errored.New(vc.String())).Combine(err)
	}

	if !exists {
		d.removeVolume(req, vc)
		return errors.RemoveVolume.Combine(errored.New(vc.String())).Combine(errors.NotExists)
	}

	err = d.completeRemove(req, vc)
	if err != nil {
		return errors.RemoveVolume.Combine(errored.New(vc.String())).Combine(errors.NotExists)
	}

	d.removeVolumeUse(locks[0], vc)
	return nil
}
示例#2
0
func (d *DaemonConfig) handleRemove(w http.ResponseWriter, r *http.Request) {
	// set a default timeout if none is specified
	timeout := d.Global.Timeout
	req, err := unmarshalRequest(r)
	if err != nil {
		api.RESTHTTPError(w, errors.UnmarshalRequest.Combine(err))
		return
	}

	if req.Options["timeout"] != "" {
		var t time.Duration
		if t, err = time.ParseDuration(req.Options["timeout"]); err != nil {
			api.RESTHTTPError(w, errors.RemoveVolume.Combine(err))
			return
		}
		timeout = t
	}

	vc, err := d.Config.GetVolume(req.Policy, req.Name)
	if err != nil {
		api.RESTHTTPError(w, errors.GetVolume.Combine(err))
		return
	}

	locks, err := d.createRemoveLocks(vc)
	if err != nil {
		api.RESTHTTPError(w, err)
		return
	}

	if req.Options["force"] == "true" {
		if err := d.handleForceRemoveLock(req, vc, locks); err != nil {
			api.RESTHTTPError(w, err)
			return
		}
	}

	err = lock.NewDriver(d.Config).ExecuteWithMultiUseLock(locks, timeout, func(ld *lock.Driver, ucs []config.UseLocker) error {
		exists, err := control.ExistsVolume(vc, timeout)
		if err != nil && err != errors.NoActionTaken {
			return err
		}

		if err == errors.NoActionTaken {
			return d.completeRemove(req, vc)
		}

		if !exists {
			d.removeVolume(req, vc)
			return errors.NotExists
		}

		return d.completeRemove(req, vc)
	})

	if err == errors.NotExists {
		w.WriteHeader(404)
		return
	}

	if err != nil {
		api.RESTHTTPError(w, errors.RemoveVolume.Combine(errored.New(vc.String())).Combine(err))
		return
	}
}