Ejemplo n.º 1
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
}
Ejemplo n.º 2
0
func (c *Config) marshalJSON(s JsonMarshalStrategy) ([]byte, error) {
	switch s {
	case JsonMarshalSecure:
		return json.MarshalIndent(c.secureConfig, "", "  ")
	case JsonMarshalPlainText:
		return json.MarshalIndent(c.plainTextConfig, "", "  ")
	}

	return nil, errors.WithField(
		"strategy", s, "unknown json marshalling strategy")
}
Ejemplo n.º 3
0
func GetModuleInstance(modInstId int32) (*ModuleInstance, error) {
	modInstancesRwl.RLock()
	defer modInstancesRwl.RUnlock()

	mod, modExists := modInstances[modInstId]

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

	return mod, nil
}
Ejemplo n.º 4
0
func ReadFileToString(path string) (string, error) {

	f, err := os.Open(path)
	if err != nil {
		return "", err
	}
	defer f.Close()

	scanner := bufio.NewScanner(f)
	if !scanner.Scan() {
		return "", errors.WithField("path", path, "error reading file")
	}

	return scanner.Text(), nil
}
Ejemplo n.º 5
0
// Create will create a remote volume
func (driver *Driver) Create(volumeName string, volumeOpts volume.VolumeOpts) error {
	log.WithFields(log.Fields{
		"volumeName": volumeName,
		"volumeOpts": volumeOpts,
		"driverName": driver.Name()}).Info("creating volume")

	if volumeName == "" {
		return errors.New("Missing volume name")
	}

	instances, err := driver.sdm.GetInstance()
	if err != nil {
		return err
	}

	switch {
	case len(instances) == 0:
		return errors.New("No instances")
	case len(instances) > 1:
		return errors.New("Too many instances returned, limit the storagedrivers")
	}

	volumes, err := driver.sdm.GetVolume("", volumeName)
	if err != nil {
		return err
	}

	for k, v := range volumeOpts {
		volumeOpts[strings.ToLower(k)] = v
	}

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

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

	var (
		ok               bool
		volumeType       string
		IOPSi            int
		sizei            int
		availabilityZone string
		optVolumeID      string
		optSnapshotID    string
	)

	if volumeType, ok = volumeOpts["volumetype"]; !ok {
		volumeType = os.Getenv("REXRAY_DOCKER_VOLUMETYPE")
	}

	if IOPSs, ok := volumeOpts["iops"]; ok {
		IOPSi, _ = strconv.Atoi(IOPSs)
	} else {
		IOPSi, _ = strconv.Atoi(os.Getenv("REXRAY_DOCKER_IOPS"))
	}
	IOPS := int64(IOPSi)

	if sizes, ok := volumeOpts["size"]; ok {
		sizei, _ = strconv.Atoi(sizes)
	} else {
		sizei, _ = strconv.Atoi(os.Getenv("REXRAY_DOCKER_SIZE"))
	}
	size := int64(sizei)
	if size == 0 {
		size = DefaultVolumeSize
	}

	if availabilityZone, ok = volumeOpts["availabilityzone"]; !ok {
		availabilityZone = os.Getenv("REXRAY_DOCKER_AVAILABILITYZONE")
	}

	if optSnapshotName, ok := volumeOpts["snapshotname"]; !ok {
		optSnapshotID = volumeOpts["snapshotid"]
	} else {
		snapshots, err := driver.sdm.GetSnapshot("", "", optSnapshotName)
		if err != nil {
			return err
		}

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

		optSnapshotID = snapshots[0].SnapshotID
	}

	if optVolumeName, ok := volumeOpts["volumename"]; !ok {
		optVolumeID = volumeOpts["volumeid"]
	} else {
		volumes, err := driver.sdm.GetVolume("", optVolumeName)
		if err != nil {
			return err
		}

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

		optVolumeID = volumes[0].VolumeID
	}

	if len(volumes) == 0 {
		_, err := driver.sdm.CreateVolume(
			false, volumeName, optVolumeID, optSnapshotID, volumeType, IOPS, size, availabilityZone)
		if err != nil {
			return err
		}
	}

	if newFsType != "" || overwriteFs {
		_, err = driver.Mount(volumeName, "", overwriteFs, newFsType)
		if err != nil {
			log.WithFields(log.Fields{
				"volumeName":  volumeName,
				"overwriteFs": overwriteFs,
				"newFsType":   newFsType,
				"driverName":  driver.Name()}).Error("Failed to create or mount file system")
		}
		err = driver.Unmount(volumeName, "")
		if err != nil {
			return err
		}
	}

	return nil
}