예제 #1
0
파일: volume.go 프로젝트: pb-it/rexray
func (d *driver) createGetSnapshot(
	volumeOpts core.VolumeOpts) (*core.Snapshot, error) {

	var optSnapshotName string
	var optSnapshotID string

	optSnapshotName, _ = volumeOpts["snapshotname"]
	optSnapshotID, _ = volumeOpts["snapshotid"]

	if optSnapshotName == "" && optSnapshotID == "" {
		return nil, nil
	}

	var err error
	var snapshots []*core.Snapshot

	if snapshots, err = d.r.Storage.GetSnapshot(
		"", optSnapshotID, optSnapshotName); err != nil {
		return nil, err
	}

	switch {
	case len(snapshots) == 0:
		return nil, errors.WithField(
			"optSnapshotName", optSnapshotName, "No snapshots returned")
	case len(snapshots) > 1:
		return nil, errors.WithField(
			"optSnapshotName", optSnapshotName, "Too many snapshots returned")
	}

	return snapshots[0], nil
}
예제 #2
0
파일: volume.go 프로젝트: pb-it/rexray
func (d *driver) createInitVolume(
	volumeName string,
	volumeOpts core.VolumeOpts) (*core.Volume, error) {

	var optVolumeName string
	var optVolumeID string

	optVolumeName, _ = volumeOpts["volumename"]
	optVolumeID, _ = volumeOpts["volumeid"]

	if optVolumeName == "" && optVolumeID == "" {
		return nil, nil
	}

	var err error
	var volumes []*core.Volume
	if volumes, err = d.r.Storage.GetVolume(optVolumeID, optVolumeName); err != nil {
		return nil, err
	}

	switch {
	case len(volumes) == 0:
		return nil, errors.WithField(
			"optVolumeName", optVolumeName, "No volumes returned")
	case len(volumes) > 1:
		return nil, errors.WithField(
			"optVolumeName", optVolumeName, "Too many volumes returned")
	}

	return volumes[0], nil
}
예제 #3
0
// ParseAddress parses a standard golang network address and returns the
// protocol and path.
func ParseAddress(addr string) (proto string, path string, err error) {
	m := netAddrRx.FindStringSubmatch(addr)
	if m == nil {
		return "", "", errors.WithField("address", addr, "invalid address")
	}
	return m[1], m[2], nil
}
예제 #4
0
// GetModuleInstance gets the module instance with the provided instance ID.
func GetModuleInstance(modInstID int32) (*Instance, error) {
	modInstancesRwl.RLock()
	defer modInstancesRwl.RUnlock()

	mod, modExists := modInstances[modInstID]

	if !modExists {
		return nil,
			errors.WithField("id", modInstID, "unknown module instance")
	}

	return mod, nil
}
예제 #5
0
파일: volume.go 프로젝트: pb-it/rexray
func (d *driver) createGetVolumes(
	volumeName string,
	volumeOpts core.VolumeOpts) ([]*core.Volume, bool, error) {
	var err error
	var volumes []*core.Volume

	if volumes, err = d.r.Storage.GetVolume("", volumeName); err != nil {
		return nil, false, err
	}

	overwriteFs, _ := strconv.ParseBool(volumeOpts["overwritefs"])

	switch {
	case len(volumes) == 1 && !overwriteFs:
		return volumes, overwriteFs, nil
	case len(volumes) > 1:
		return nil, overwriteFs, errors.WithField(
			"volumeName", volumeName, "Too many volumes returned")
	}

	return volumes, overwriteFs, nil
}
예제 #6
0
func (m *mod) Start() error {

	proto, addr, parseAddrErr := util.ParseAddress(m.Address())
	if parseAddrErr != nil {
		return parseAddrErr
	}

	const validProtoPatt = "(?i)^unix|tcp$"
	isProtoValid, matchProtoErr := regexp.MatchString(validProtoPatt, proto)
	if matchProtoErr != nil {
		return errors.WithFieldsE(errors.Fields{
			"protocol":       proto,
			"validProtoPatt": validProtoPatt,
		}, "error matching protocol", matchProtoErr)
	}
	if !isProtoValid {
		return errors.WithField("protocol", proto, "invalid protocol")
	}

	if err := m.r.InitDrivers(); err != nil {
		return errors.WithFieldsE(errors.Fields{
			"m":   m,
			"m.r": m.r,
		}, "error initializing drivers", err)
	}

	if err := os.MkdirAll("/etc/docker/plugins", 0755); err != nil {
		return err
	}

	var specPath string
	var startFunc func() error

	mux := m.buildMux()

	if proto == "unix" {
		sockFile := addr
		sockFileDir := filepath.Dir(sockFile)
		mkSockFileDirErr := os.MkdirAll(sockFileDir, 0755)
		if mkSockFileDirErr != nil {
			return mkSockFileDirErr
		}

		_ = os.RemoveAll(sockFile)

		specPath = m.Address()
		startFunc = func() error {
			l, lErr := net.Listen("unix", sockFile)
			if lErr != nil {
				return lErr
			}
			defer l.Close()
			defer os.Remove(sockFile)

			return http.Serve(l, mux)
		}
	} else {
		specPath = addr
		startFunc = func() error {
			s := &http.Server{
				Addr:           addr,
				Handler:        mux,
				ReadTimeout:    10 * time.Second,
				WriteTimeout:   10 * time.Second,
				MaxHeaderBytes: 1 << 20,
			}
			return s.ListenAndServe()
		}
	}

	go func() {
		sErr := startFunc()
		if sErr != nil {
			panic(sErr)
		}
	}()

	writeSpecErr := ioutil.WriteFile(
		"/etc/docker/plugins/rexray.spec", []byte(specPath), 0644)
	if writeSpecErr != nil {
		return writeSpecErr
	}

	return nil
}