Beispiel #1
0
func getInstanceID(c *config.Config) (string, error) {

	cmd := newCmd(c, "/usr/bin/xenstore-read", "name")
	cmdOut, err := cmd.Output()

	if err != nil {
		return "",
			errors.WithFields(eff(errors.Fields{
				"cmd.Path": cmd.Path,
				"cmd.Args": cmd.Args,
				"cmd.Out":  cmdOut,
			}), "error getting instance id")
	}

	instanceID := strings.Replace(string(cmdOut), "\n", "", -1)

	validInstanceID := regexp.MustCompile(`^instance-`)
	valid := validInstanceID.MatchString(instanceID)
	if !valid {
		return "", errors.WithFields(eff(errors.Fields{
			"instanceId": instanceID}), "error matching instance id")
	}

	instanceID = strings.Replace(instanceID, "instance-", "", 1)
	return instanceID, nil
}
Beispiel #2
0
func (d *driver) AttachVolume(
	runAsync bool,
	volumeID, instanceID string) ([]*core.VolumeAttachment, error) {

	fields := eff(map[string]interface{}{
		"runAsync":   runAsync,
		"volumeId":   volumeID,
		"instanceId": instanceID,
	})

	if volumeID == "" {
		return nil, errors.WithFields(fields, "volumeId is required")
	}

	mapVolumeSdcParam := &types.MapVolumeSdcParam{
		SdcID: d.sdc.Sdc.ID,
		AllowMultipleMappings: "false",
		AllSdcs:               "",
	}

	volumes, err := d.getVolume(volumeID, "")
	if err != nil {
		return nil, errors.WithFieldsE(fields, "error getting volume", err)
	}

	if len(volumes) == 0 {
		return nil, errors.WithFields(fields, "no volumes returned")
	}

	targetVolume := goscaleio.NewVolume(d.client)
	targetVolume.Volume = volumes[0]

	err = targetVolume.MapVolumeSdc(mapVolumeSdcParam)
	if err != nil {
		return nil, errors.WithFieldsE(fields, "error mapping volume sdc", err)
	}

	_, err = waitMount(volumes[0].ID)
	if err != nil {
		fields["volumeId"] = volumes[0].ID
		return nil, errors.WithFieldsE(
			fields, "error waiting on volume to mount", err)
	}

	volumeAttachment, err := d.GetVolumeAttach(volumeID, instanceID)
	if err != nil {
		return nil, errors.WithFieldsE(
			fields, "error getting volume attachments", err)
	}

	log.WithFields(log.Fields{
		"provider":   providerName,
		"volumeId":   volumeID,
		"instanceId": instanceID,
	}).Debug("attached volume to instance")
	return volumeAttachment, nil
}
Beispiel #3
0
func (d *driver) createVolumeHandleSnapshotID(
	size *int64, snapshotID string, fields map[string]interface{}) error {
	if snapshotID == "" {
		return nil
	}
	snapshots, err := d.GetSnapshot("", snapshotID, "")
	if err != nil {
		return errors.WithFieldsE(fields, "error getting snapshot", err)
	}

	if len(snapshots) == 0 {
		return errors.WithFields(fields, "snapshot array is empty")
	}

	volSize := snapshots[0].VolumeSize
	sizeInt, err := strconv.Atoi(volSize)
	if err != nil {
		f := errors.Fields{
			"volumeSize": volSize,
		}
		for k, v := range fields {
			f[k] = v
		}
		return errors.WithFieldsE(f, "error casting volume size", err)
	}
	*size = int64(sizeInt)
	return nil
}
Beispiel #4
0
func (d *driver) Init(r *core.RexRay) error {
	d.r = r

	var err error
	d.instanceDocument, err = getInstanceIdentityDocument()
	if err != nil {
		return errors.WithFields(ef(), "error getting instance id doc")
	}

	auth := aws.Auth{
		AccessKey: d.r.Config.AwsAccessKey,
		SecretKey: d.r.Config.AwsSecretKey,
	}
	region := d.r.Config.AwsRegion
	if region == "" {
		region = d.instanceDocument.Region
	}
	d.ec2Instance = ec2.New(
		auth,
		aws.Regions[region],
	)

	log.WithField("provider", providerName).Info("storage driver initialized")

	return nil
}
Beispiel #5
0
func (d *driver) RemoveVolume(volumeID string) error {

	fields := eff(map[string]interface{}{
		"volumeId": volumeID,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}

	var err error
	var volumes []*types.Volume

	if volumes, err = d.getVolume(volumeID, ""); err != nil {
		return errors.WithFieldsE(fields, "error getting volume", err)
	}

	targetVolume := goscaleio.NewVolume(d.client)
	targetVolume.Volume = volumes[0]

	if err = targetVolume.RemoveVolume("ONLY_ME"); err != nil {
		return errors.WithFieldsE(fields, "error removing volume", err)
	}

	log.WithFields(fields).Debug("removed volume")
	return nil
}
Beispiel #6
0
func (d *driver) GetVolumeAttach(
	volumeID, instanceID string) ([]*core.VolumeAttachment, error) {

	fields := eff(map[string]interface{}{
		"volumeId":   volumeID,
		"instanceId": instanceID,
	})

	if volumeID == "" {
		return []*core.VolumeAttachment{},
			errors.WithFields(fields, "volumeId is required")
	}
	volume, err := d.GetVolume(volumeID, "")
	if err != nil {
		return []*core.VolumeAttachment{},
			errors.WithFieldsE(fields, "error getting volume attach", err)
	}

	if instanceID != "" {
		var attached bool
		for _, volumeAttachment := range volume[0].Attachments {
			if volumeAttachment.InstanceID == instanceID {
				return volume[0].Attachments, nil
			}
		}
		if !attached {
			return []*core.VolumeAttachment{}, nil
		}
	}
	return volume[0].Attachments, nil
}
Beispiel #7
0
func waitMount(volumeID string) (*goscaleio.SdcMappedVolume, error) {

	timeout := make(chan bool, 1)
	go func() {
		time.Sleep(10 * time.Second)
		timeout <- true
	}()

	successCh := make(chan *goscaleio.SdcMappedVolume, 1)
	errorCh := make(chan error, 1)
	go func(volumeID string) {
		log.WithField("provider", providerName).Debug("waiting for volume mount")
		for {
			sdcMappedVolumes, err := goscaleio.GetLocalVolumeMap()
			if err != nil {
				errorCh <- errors.WithFieldE(
					"provider", providerName,
					"problem getting local volume mappings", err)
				return
			}

			sdcMappedVolume := &goscaleio.SdcMappedVolume{}
			var foundVolume bool
			for _, sdcMappedVolume = range sdcMappedVolumes {
				if sdcMappedVolume.VolumeID ==
					volumeID && sdcMappedVolume.SdcDevice != "" {
					foundVolume = true
					break
				}
			}

			if foundVolume {
				successCh <- sdcMappedVolume
				return
			}
			time.Sleep(100 * time.Millisecond)
		}

	}(volumeID)

	select {
	case sdcMappedVolume := <-successCh:
		log.WithFields(log.Fields{
			"provider": providerName,
			"volumeId": sdcMappedVolume.VolumeID,
			"volume":   sdcMappedVolume.SdcDevice,
		}).Debug("got sdcMappedVolume")
		return sdcMappedVolume, nil
	case err := <-errorCh:
		return &goscaleio.SdcMappedVolume{}, err
	case <-timeout:
		return &goscaleio.SdcMappedVolume{}, errors.WithFields(
			ef(), "timed out waiting for mount")
	}

}
Beispiel #8
0
func (d *driver) DetachVolume(
	runAsync bool, volumeID string, blank string) error {

	fields := eff(map[string]interface{}{
		"runAsync": runAsync,
		"volumeId": volumeID,
		"blank":    blank,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}

	volumes, err := d.getVolume(volumeID, "")
	if err != nil {
		return errors.WithFieldsE(fields, "error getting volume", err)
	}

	if len(volumes) == 0 {
		return errors.WithFields(fields, "no volumes returned")
	}

	targetVolume := goscaleio.NewVolume(d.client)
	targetVolume.Volume = volumes[0]

	unmapVolumeSdcParam := &types.UnmapVolumeSdcParam{
		SdcID:                d.sdc.Sdc.ID,
		IgnoreScsiInitiators: "true",
		AllSdcs:              "",
	}

	// need to detect if unmounted first
	err = targetVolume.UnmapVolumeSdc(unmapVolumeSdcParam)
	if err != nil {
		return errors.WithFieldsE(fields, "error unmapping volume sdc", err)
	}

	log.WithFields(log.Fields{
		"provider": providerName,
		"volumeId": volumeID}).Debug("detached volume")
	return nil
}
Beispiel #9
0
func (d *driver) createVolumeHandleVolumeID(
	availabilityZone, snapshotID, volumeID *string,
	size *int64,
	fields map[string]interface{}) ([]*core.Volume, error) {

	if *volumeID == "" {
		return nil, nil
	}

	var err error
	var volume []*core.Volume

	if volume, err = d.GetVolume(*volumeID, ""); err != nil {
		return nil, errors.WithFieldsE(fields, "error getting volumes", err)
	}

	if len(volume) == 0 {
		return nil, errors.WithFieldsE(fields, "", errors.ErrNoVolumesReturned)
	}

	volSize := volume[0].Size
	sizeInt, err := strconv.Atoi(volSize)
	if err != nil {
		f := errors.Fields{
			"volumeSize": volSize,
		}
		for k, v := range fields {
			f[k] = v
		}
		return nil,
			errors.WithFieldsE(f, "error casting volume size", err)
	}
	*size = int64(sizeInt)

	*volumeID = volume[0].VolumeID
	snapshot, err := d.CreateSnapshot(
		false, fmt.Sprintf("temp-%s", *volumeID), *volumeID, "")
	if err != nil {
		return nil,
			errors.WithFields(fields, "error creating snapshot")
	}

	*snapshotID = snapshot[0].SnapshotID

	if *availabilityZone == "" {
		*availabilityZone = volume[0].AvailabilityZone
	}

	return volume, nil
}
Beispiel #10
0
func (d *xtremIODriver) Init(r *core.RexRay) error {

	d.r = r
	d.volumesByNaa = map[string]xtio.Volume{}

	fields := eff(map[string]interface{}{
		"endpoint":         r.Config.XtremIOEndpoint,
		"userName":         r.Config.XtremIOUserName,
		"deviceMapper":     r.Config.XtremIODeviceMapper,
		"multipath":        r.Config.XtremIOMultipath,
		"remoteManagement": r.Config.XtremIORemoteManagement,
		"insecure":         r.Config.XtremIOInsecure,
	})

	if r.Config.XtremIoPassword == "" {
		fields["password"] = ""
	} else {
		fields["password"] = "******"
	}

	if !isXtremIOAttached() && !d.r.Config.XtremIORemoteManagement {
		return errors.WithFields(fields, "device not detected")
	}

	var err error

	if d.client, err = xtio.NewClientWithArgs(
		r.Config.XtremIOEndpoint,
		r.Config.XtremIOInsecure,
		r.Config.XtremIOUserName,
		r.Config.XtremIoPassword); err != nil {
		return errors.WithFieldsE(fields,
			"error creating xtremio client", err)
	}

	if !d.r.Config.XtremIORemoteManagement {
		var iqn string
		if iqn, err = getIQN(); err != nil {
			return err
		}
		if d.initiator, err = d.client.GetInitiator("", iqn); err != nil {
			return err
		}
	}

	log.WithField("provider", providerName).Debug("storage driver initialized")

	return nil
}
Beispiel #11
0
func (d *driver) Init(r *core.RexRay) error {

	d.r = r
	d.volumesByNaa = map[string]xtio.Volume{}

	fields := eff(map[string]interface{}{
		"endpoint":         d.endpoint(),
		"userName":         d.userName(),
		"deviceMapper":     d.deviceMapper(),
		"multipath":        d.multipath(),
		"remoteManagement": d.remoteManagement(),
		"insecure":         d.insecure(),
	})

	if d.password() == "" {
		fields["password"] = ""
	} else {
		fields["password"] = "******"
	}

	if !isXtremIOAttached() && !d.remoteManagement() {
		return errors.WithFields(fields, "device not detected")
	}

	var err error

	if d.client, err = xtio.NewClientWithArgs(
		d.endpoint(),
		d.insecure(),
		d.userName(),
		d.password()); err != nil {
		return errors.WithFieldsE(fields,
			"error creating xtremio client", err)
	}

	if !d.remoteManagement() {
		var iqn string
		if iqn, err = getIQN(); err != nil {
			return err
		}
		if d.initiator, err = d.client.GetInitiator("", iqn); err != nil {
			return err
		}
	}

	log.WithField("provider", providerName).Info("storage driver initialized")

	return nil
}
Beispiel #12
0
func (d *driver) RemoveVolume(volumeID string) error {
	fields := eff(map[string]interface{}{
		"volumeId": volumeID,
	})
	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}
	res := volumes.Delete(d.clientBlockStorage, volumeID)
	if res.Err != nil {
		return errors.WithFieldsE(fields, "error removing volume", res.Err)
	}

	log.WithFields(fields).Debug("removed volume")
	return nil
}
Beispiel #13
0
func getInstanceID(c *config.Config) (string, error) {
	cmd := newCmd(c, "/usr/sbin/dmidecode")
	cmdOut, err := cmd.Output()

	if err != nil {
		return "",
			errors.WithFields(eff(errors.Fields{
				"cmd.Path": cmd.Path,
				"cmd.Args": cmd.Args,
				"cmd.Out":  cmdOut,
			}), "error getting instance id")
	}

	rp := regexp.MustCompile("UUID:(.*)")
	uuid := strings.Replace(rp.FindString(string(cmdOut)), "UUID: ", "", -1)

	return strings.ToLower(uuid), nil
}
Beispiel #14
0
func getInstanceRegion(cfg *config.Config) (string, error) {
	cmd := newCmd(
		cfg, "/usr/bin/xenstore-read",
		"vm-data/provider_data/region")

	cmdOut, err := cmd.Output()
	if err != nil {
		return "",
			errors.WithFields(eff(errors.Fields{
				"cmd.Path": cmd.Path,
				"cmd.Args": cmd.Args,
				"cmd.Out":  cmdOut,
			}), "error getting instance region")
	}

	region := strings.Replace(string(cmdOut), "\n", "", -1)
	return region, nil
}
Beispiel #15
0
// InitializeModule initializes a module.
func InitializeModule(
	modTypeID int32,
	modConfig *Config) (*Instance, error) {

	modInstancesRwl.Lock()
	defer modInstancesRwl.Unlock()

	lf := log.Fields{
		"typeId":  modTypeID,
		"address": modConfig.Address,
	}

	mt, modTypeExists := modTypes[modTypeID]
	if !modTypeExists {
		return nil, errors.WithFields(lf, "unknown module type")
	}

	lf["typeName"] = mt.Name
	lf["ignoreFailOnInit"] = mt.IgnoreFailOnInit

	modInstID := atomic.AddInt32(&nextModInstanceID, 1)
	mod, initErr := mt.InitFunc(modInstID, modConfig)
	if initErr != nil {
		atomic.AddInt32(&nextModInstanceID, -1)
		return nil, initErr
	}

	modInst := &Instance{
		ID:          modInstID,
		Type:        mt,
		TypeID:      mt.ID,
		Inst:        mod,
		Name:        mod.Name(),
		Config:      modConfig,
		Description: mod.Description(),
	}
	modInstances[modInstID] = modInst

	lf["id"] = modInstID
	log.WithFields(lf).Info("initialized module instance")

	return modInst, nil
}
Beispiel #16
0
func (d *driver) waitVolumeAttach(volumeID string) error {

	fields := eff(map[string]interface{}{
		"volumeId": volumeID,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}
	for {
		volume, err := d.GetVolume(volumeID, "")
		if err != nil {
			return errors.WithFieldsE(fields, "error getting volume", err)
		}
		if volume[0].Status == "in-use" {
			break
		}
		time.Sleep(1 * time.Second)
	}

	return nil
}
Beispiel #17
0
func (d *driver) DetachVolume(
	runAsync bool, volumeID, instanceID string) error {

	fields := eff(map[string]interface{}{
		"runAsync":   runAsync,
		"volumeId":   volumeID,
		"instanceId": instanceID,
	})

	if volumeID == "" {
		return errors.WithFields(fields, "volumeId is required")
	}
	volume, err := d.GetVolume(volumeID, "")
	if err != nil {
		return errors.WithFieldsE(fields, "error getting volume", err)
	}

	fields["instanceId"] = volume[0].Attachments[0].InstanceID
	resp := volumeattach.Delete(
		d.client, volume[0].Attachments[0].InstanceID, volumeID)
	if resp.Err != nil {
		return errors.WithFieldsE(fields, "error deleting volume", err)
	}

	if !runAsync {
		log.WithFields(fields).Debug("waiting for volume to detach")
		err = d.waitVolumeDetach(volumeID)
		if err != nil {
			return errors.WithFieldsE(
				fields, "error waiting for volume to detach", err)
		}
	}

	log.WithFields(fields).Debug("volume detached")
	return nil
}
Beispiel #18
0
// StartModule starts the module with the provided instance ID.
func StartModule(modInstID int32) error {

	modInstancesRwl.RLock()
	defer modInstancesRwl.RUnlock()

	lf := map[string]interface{}{"id": modInstID}

	mod, modExists := modInstances[modInstID]

	if !modExists {
		return errors.WithFields(lf, "unknown module instance")
	}

	lf["id"] = mod.ID
	lf["typeId"] = mod.Type.ID
	lf["typeName"] = mod.Type.Name
	lf["address"] = mod.Config.Address

	started := make(chan bool)
	timeout := make(chan bool)
	startError := make(chan error)

	go func() {

		defer func() {
			r := recover()
			m := "error starting module"

			errMsg := fmt.Sprintf(
				"Error starting module type %d, %d-%s at %s",
				mod.TypeID, mod.ID, mod.Name, mod.Config.Address)

			if r == nil {
				startError <- errors.New(errMsg)
				return
			}

			switch x := r.(type) {
			case string:
				lf["inner"] = x
				startError <- errors.WithFields(lf, m)
			case error:
				startError <- errors.WithFieldsE(lf, m, x)
			default:
				startError <- errors.WithFields(lf, m)
			}
		}()

		sErr := mod.Inst.Start()
		if sErr != nil {
			startError <- sErr
		} else {
			started <- true
		}
	}()

	go func() {
		time.Sleep(3 * time.Second)
		timeout <- true
	}()

	select {
	case <-started:
		mod.IsStarted = true
		log.WithFields(lf).Info("started module")
	case <-timeout:
		log.WithFields(lf).Debug("timed out while monitoring module start")
	case sErr := <-startError:
		return sErr
	}

	return nil
}
Beispiel #19
0
func (d *driver) CreateVolume(
	runAsync bool,
	volumeName string,
	volumeID string,
	snapshotID string,
	volumeType string,
	IOPS int64,
	size int64,
	availabilityZone string) (*core.Volume, error) {

	fields := map[string]interface{}{
		"provider":         providerName,
		"runAsync":         runAsync,
		"volumeName":       volumeName,
		"volumeId":         volumeID,
		"snapshotId":       snapshotID,
		"volumeType":       volumeType,
		"iops":             IOPS,
		"size":             size,
		"availabilityZone": availabilityZone,
	}

	if volumeID != "" && runAsync {
		return nil, errors.ErrRunAsyncFromVolume
	}

	d.createVolumeEnsureAvailabilityZone(&availabilityZone)

	var err error

	if err = d.createVolumeHandleSnapshotID(
		&size, snapshotID, fields); err != nil {
		return nil, err
	}

	var volume []*core.Volume
	if volume, err = d.createVolumeHandleVolumeID(
		&availabilityZone, &snapshotID, &volumeID, &size, fields); err != nil {
		return nil, err
	}

	createVolumeEnsureSize(&size)

	options := &volumes.CreateOpts{
		Name:         volumeName,
		Size:         int(size),
		SnapshotID:   snapshotID,
		VolumeType:   volumeType,
		Availability: availabilityZone,
	}
	resp, err := volumes.Create(d.clientBlockStorage, options).Extract()
	if err != nil {
		return nil,
			errors.WithFields(fields, "error creating volume")
	}

	if !runAsync {
		log.Debug("waiting for volume creation to complete")
		err = volumes.WaitForStatus(d.clientBlockStorage, resp.ID, "available", 120)
		if err != nil {
			return nil,
				errors.WithFields(fields,
					"error waiting for volume creation to complete")
		}

		if volumeID != "" {
			err := d.RemoveSnapshot(snapshotID)
			if err != nil {
				return nil,
					errors.WithFields(fields,
						"error removing snapshot")
			}
		}
	}

	fields["volumeId"] = resp.ID
	fields["volumeName"] = ""

	volume, err = d.GetVolume(resp.ID, "")
	if err != nil {
		return nil, errors.WithFields(fields,
			"error removing snapshot")
	}

	log.WithFields(fields).Debug("created volume")
	return volume[0], nil
}